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 bde782d
Show file tree
Hide file tree
Showing 10 changed files with 273 additions and 270 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
Expand Up @@ -52,56 +52,57 @@ public Mono<DetailView> viewOneTimePaste(String id) {

public Mono<ListView> viewAllPastes() {
return pasteService.findAll()
.map(ListItemView::of)
.collectList()
.map(ListView::of);
.map(ListItemView::of)
.collectList()
.map(ListView::new);
}

public Mono<SearchView> searchByFullText(String term) {
return pasteService.findByFullText(term)
.map(paste -> SearchItemView.of(paste, term))
.collectList()
.map(SearchView::of)
.doOnSuccess(searchView -> log.info("Found {} pastes searching for: {}", searchView.pastes().size(), term));
.map(paste -> SearchItemView.of(paste, term))
.collectList()
.map(SearchView::new)
.doOnSuccess(searchView -> log.info("Found {} pastes searching for: {}", searchView.pastes().size(), term));
}

public Mono<DetailView> createPaste(CreateCmd cmd, String remoteAddress) {
return pasteService.create(
cmd.title(),
cmd.content(),
cmd.dateOfExpiry(),
cmd.isEncrypted(),
cmd.pasteExposure(),
remoteAddress
)
.map(paste -> {
if (paste.isOneTime()) {
return toOneTimeView(paste, remoteAddress);
} else {
return DetailView.of(paste, remoteAddress);
}
});
cmd.title(),
cmd.content(),
cmd.dateOfExpiry(),
cmd.isEncrypted(),
cmd.pasteExposure(),
remoteAddress
)
.map(paste -> {
if (paste.isOneTime()) {
return DetailView.ofOneTime(paste, remoteAddress);
} else {
return DetailView.of(paste, remoteAddress);
}
});
}

public Mono<Void> requestDeletion(String id, String remoteAddress) {
return pasteService.requestDeletion(id, remoteAddress);
}

@Deprecated
private static DetailView toOneTimeView(Paste reference, 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()
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
@@ -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()
);
}
}
Loading

0 comments on commit bde782d

Please sign in to comment.