impl a char limit for pastes

This commit is contained in:
Lee 2024-04-23 21:16:42 +01:00
parent 5490bc446c
commit 7d1f81c3a2
3 changed files with 23 additions and 0 deletions

@ -0,0 +1,9 @@
package cc.fascinated.backend.exception.impl;
import lombok.experimental.StandardException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@StandardException
@ResponseStatus(HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException { }

@ -1,5 +1,6 @@
package cc.fascinated.backend.service; package cc.fascinated.backend.service;
import cc.fascinated.backend.exception.impl.BadRequestException;
import cc.fascinated.backend.model.Paste; import cc.fascinated.backend.model.Paste;
import cc.fascinated.backend.exception.impl.ResourceNotFoundException; import cc.fascinated.backend.exception.impl.ResourceNotFoundException;
import cc.fascinated.backend.repository.PasteRepository; import cc.fascinated.backend.repository.PasteRepository;
@ -24,6 +25,12 @@ public class PasteService {
@Value("${paste.id-length}") @Value("${paste.id-length}")
private int idLength; private int idLength;
/**
* The maximum size of the paste content.
*/
@Value("${paste.upload-size-limit}")
private int uploadSizeLimit;
@Autowired @Autowired
public PasteService(PasteRepository pasteRepository) { public PasteService(PasteRepository pasteRepository) {
this.pasteRepository = pasteRepository; this.pasteRepository = pasteRepository;
@ -36,6 +43,11 @@ public class PasteService {
* @return The id of the paste. * @return The id of the paste.
*/ */
public String createPaste(String content) { public String createPaste(String content) {
int length = content.length();
if (length > uploadSizeLimit) {
throw new BadRequestException("The paste content is too large, the limit is " + uploadSizeLimit + " characters");
}
return pasteRepository.save(new Paste( return pasteRepository.save(new Paste(
RandomStringUtils.randomAlphabetic(idLength), RandomStringUtils.randomAlphabetic(idLength),
System.currentTimeMillis(), System.currentTimeMillis(),

@ -23,6 +23,8 @@ spring:
paste: paste:
# The length of the ID for the paste # The length of the ID for the paste
id-length: 12 id-length: 12
# The limit of the paste size (in characters)
upload-size-limit: 400000
# Set the embedded MongoDB version # Set the embedded MongoDB version
de: de: