Skip to content

Commit

Permalink
chore: formatting, rm useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
querwurzel committed Dec 15, 2024
1 parent c96703c commit 8aba23c
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Optional;

@Validated
@RestController
Expand All @@ -53,41 +52,41 @@ public PasteController(final PasteViewService pasteViewService) {

@GetMapping("/{pasteId:[a-z0-9]{40}}")
public Mono<DetailView> findPaste(
@PathVariable("pasteId")
final String pasteId,
final ServerHttpRequest request,
final ServerHttpResponse response
@PathVariable("pasteId")
final String pasteId,
final ServerHttpRequest request,
final ServerHttpResponse response
) {
return pasteViewService
.viewPaste(pasteId, remoteAddress(request).orElse(null))
.doOnNext(paste -> {
if (paste.isOneTime()) {
response.getHeaders().setCacheControl(CacheControl.noStore());
return;
}

var now = LocalDateTime.now();
if (paste.isPermanent() || paste.dateOfExpiry().get().plusMinutes(1).isAfter(now)) {
response.getHeaders().setCacheControl(
CacheControl.maxAge(Duration.ofMinutes(1)));
} else {
response.getHeaders().setCacheControl(
CacheControl.maxAge(Duration.between(now, paste.dateOfExpiry().get())).mustRevalidate());
}
})
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)));
.viewPaste(pasteId, remoteAddress(request))
.doOnNext(paste -> {
if (paste.isOneTime()) {
response.getHeaders().setCacheControl(CacheControl.noStore());
return;
}

var now = LocalDateTime.now();
if (paste.isPermanent() || paste.dateOfExpiry().get().plusMinutes(1).isAfter(now)) {
response.getHeaders().setCacheControl(
CacheControl.maxAge(Duration.ofMinutes(1)));
} else {
response.getHeaders().setCacheControl(
CacheControl.maxAge(Duration.between(now, paste.dateOfExpiry().get())).mustRevalidate());
}
})
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)));
}

@PostMapping("/{pasteId:[a-z0-9]{40}}")
public Mono<DetailView> findAndBurnOneTimePaste(
@PathVariable("pasteId")
final String pasteId,
final ServerHttpResponse response
@PathVariable("pasteId")
final String pasteId,
final ServerHttpResponse response
) {
response.getHeaders().setCacheControl(CacheControl.noStore());
return pasteViewService
.viewOneTimePaste(pasteId)
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)));
.viewOneTimePaste(pasteId)
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)));
}

@GetMapping
Expand All @@ -97,11 +96,11 @@ public Mono<ListView> findPastes() {

@GetMapping("/search")
public Mono<SearchView> searchPastes(
@RequestParam("term")
@NotBlank
@Pattern(regexp = "[\\p{L}\\p{N}\\p{P}\\s]{3,50}")
final String term,
final ServerHttpResponse response
@RequestParam("term")
@NotBlank
@Pattern(regexp = "[\\p{L}\\p{N}\\p{P}\\s]{3,50}")
final String term,
final ServerHttpResponse response
) {
var decodedTerm = URLDecoder.decode(term, Charset.defaultCharset());
response.getHeaders().setCacheControl(CacheControl.maxAge(Duration.ofMinutes(1)));
Expand All @@ -111,13 +110,13 @@ public Mono<SearchView> searchPastes(
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Mono<DetailView> createPaste(@Valid @RequestBody final CreateCmd createCmd, final ServerHttpRequest request) {
return pasteViewService.createPaste(createCmd, remoteAddress(request).orElse(null));
return pasteViewService.createPaste(createCmd, remoteAddress(request));
}

@DeleteMapping("/{pasteId:[a-z0-9]{40}}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> deletePaste(@PathVariable("pasteId") final String pasteId, final ServerHttpRequest request) {
return pasteViewService.requestDeletion(pasteId, remoteAddress(request).orElse(null));
return pasteViewService.requestDeletion(pasteId, remoteAddress(request));
}

@ExceptionHandler({ConstraintViolationException.class, WebExchangeBindException.class})
Expand All @@ -126,15 +125,15 @@ private void handleValidationException(final RuntimeException e) {
log.info("Received invalid request [{}]: {}", e.getClass().getSimpleName(), e.getMessage());
}

private static Optional<String> remoteAddress(final ServerHttpRequest request) {
private static String remoteAddress(final ServerHttpRequest request) {
if (request.getHeaders().containsKey("X-Forwarded-For")) {
return Optional.of(request.getHeaders().getFirst("X-Forwarded-For"));
return request.getHeaders().getFirst("X-Forwarded-For");
}

if (request.getRemoteAddress() == null) {
return Optional.empty();
return null;
}

return Optional.ofNullable(request.getRemoteAddress().getAddress().getHostAddress());
return request.getRemoteAddress().getAddress().getHostAddress();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.binpastes.paste.application.model;

import com.github.binpastes.paste.domain.Paste;
import com.github.binpastes.paste.domain.Paste.PasteExposure;
import com.github.binpastes.util.NullOrNotBlank;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand All @@ -19,14 +19,14 @@ public final class CreateCmd {
private final String content;
private final Boolean isEncrypted;
private final ExpirationRange expiry;
private final Paste.PasteExposure exposure;
private final PasteExposure exposure;

private CreateCmd(
final String title,
final String content,
final Boolean isEncrypted,
final ExpirationRange expiry,
final Paste.PasteExposure exposure
final String title,
final String content,
final Boolean isEncrypted,
final ExpirationRange expiry,
final PasteExposure exposure
) {
this.title = title;
this.content = content;
Expand All @@ -37,8 +37,8 @@ private CreateCmd(

public String title() {
return title == null
? null
: title.strip();
? null
: title.strip();
}

public String content() {
Expand All @@ -51,14 +51,14 @@ public boolean isEncrypted() {

public LocalDateTime dateOfExpiry() {
return expiry == null
? ExpirationRange.ONE_DAY.toTimestamp() // default expiry if not set
: expiry.toTimestamp();
? ExpirationRange.ONE_DAY.toTimestamp() // default expiry if not set
: expiry.toTimestamp();
}

public Paste.PasteExposure pasteExposure() {
public PasteExposure pasteExposure() {
return exposure == null
? Paste.PasteExposure.PUBLIC // default exposure if not set
: exposure;
? PasteExposure.PUBLIC // default exposure if not set
: exposure;
}

private enum ExpirationRange {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,53 @@

@JsonInclude(Include.NON_DEFAULT)
public record DetailView(
String id,
Optional<String> title,
String content,
int sizeInBytes,
boolean isPublic,
boolean isErasable,
boolean isEncrypted,
boolean isOneTime,
boolean isPermanent,
LocalDateTime dateCreated,
Optional<LocalDateTime> dateOfExpiry,
Optional<LocalDateTime> lastViewed,
long views
String id,
Optional<String> title,
String content,
int sizeInBytes,
boolean isPublic,
boolean isErasable,
boolean isEncrypted,
boolean isOneTime,
boolean isPermanent,
LocalDateTime dateCreated,
Optional<LocalDateTime> dateOfExpiry,
Optional<LocalDateTime> lastViewed,
long views
) {
public static DetailView of(final Paste reference, final String remoteAddress) {
return new DetailView(
reference.getId(),
reference.getTitle(),
reference.getContent(),
reference.getContent().getBytes().length,
reference.isPublic(),
reference.isErasable(remoteAddress),
reference.isEncrypted(),
reference.isOneTime(),
reference.isPermanent(),
reference.getDateCreated(),
reference.getDateOfExpiry(),
reference.getLastViewed(),
reference.getViews()
reference.getId(),
reference.getTitle(),
reference.getContent(),
reference.getContent().getBytes().length,
reference.isPublic(),
reference.isErasable(remoteAddress),
reference.isEncrypted(),
reference.isOneTime(),
reference.isPermanent(),
reference.getDateCreated(),
reference.getDateOfExpiry(),
reference.getLastViewed(),
reference.getViews()
);
}

public static DetailView ofOneTime(final Paste reference, final String remoteAddress) {
return new DetailView(
reference.getId(),
null,
null,
0,
reference.isPublic(),
reference.isErasable(remoteAddress),
reference.isEncrypted(),
reference.isOneTime(),
reference.isPermanent(),
reference.getDateCreated(),
reference.getDateOfExpiry(),
reference.getLastViewed(),
reference.getViews()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@
import java.util.Optional;

public record ListView(
List<ListItemView> pastes
List<ListItemView> pastes
) {
public static ListView of(final List<ListItemView> pastes) {
return new ListView(pastes);
}

@JsonInclude(Include.NON_DEFAULT)
public record ListItemView(
String id,
Optional<String> title,
int sizeInBytes,
boolean isEncrypted,
boolean isPermanent,
LocalDateTime dateCreated,
Optional<LocalDateTime> dateOfExpiry
String id,
Optional<String> title,
int sizeInBytes,
boolean isEncrypted,
boolean isPermanent,
LocalDateTime dateCreated,
Optional<LocalDateTime> dateOfExpiry
) {
public static ListItemView of(final Paste reference) {
return new ListItemView(
reference.getId(),
reference.getTitle(),
reference.getContent().getBytes().length,
reference.isEncrypted(),
reference.isPermanent(),
reference.getDateCreated(),
reference.getDateOfExpiry()
reference.getId(),
reference.getTitle(),
reference.getContent().getBytes().length,
reference.isEncrypted(),
reference.isPermanent(),
reference.getDateCreated(),
reference.getDateOfExpiry()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,29 @@
import java.util.Optional;

public record SearchView(
List<SearchItemView> pastes
List<SearchItemView> pastes
) {

public static SearchView of(List<SearchItemView> pastes) {
return new SearchView(pastes);
}

@JsonInclude(Include.NON_DEFAULT)
public record SearchItemView(
String id,
Optional<String> title,
String highlight,
int sizeInBytes,
LocalDateTime dateCreated,
Optional<LocalDateTime> dateOfExpiry
String id,
Optional<String> title,
String highlight,
int sizeInBytes,
LocalDateTime dateCreated,
Optional<LocalDateTime> dateOfExpiry
) {

private static final short HIGHLIGHT_RANGE = 30;

public static SearchItemView of(final Paste reference, final String term) {
return new SearchItemView(
reference.getId(),
reference.getTitle(),
highlight(reference.getContent(), term),
reference.getContent().getBytes().length,
reference.getDateCreated(),
reference.getDateOfExpiry()
reference.getId(),
reference.getTitle(),
highlight(reference.getContent(), term),
reference.getContent().getBytes().length,
reference.getDateCreated(),
reference.getDateOfExpiry()
);
}

Expand All @@ -47,7 +43,7 @@ public static String highlight(final String content, final String term) {
}

final int leftRemainder = Math.abs(Math.min(0, idx - HIGHLIGHT_RANGE));
final int rightRemainder = Math.max(0, idx + HIGHLIGHT_RANGE - content.length());
final int rightRemainder = Math.max(0, (idx + HIGHLIGHT_RANGE) - content.length());

return content.substring(
Math.max(0, idx - HIGHLIGHT_RANGE - rightRemainder),
Expand Down
Loading

0 comments on commit 8aba23c

Please sign in to comment.