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

View File

@ -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 { }

View File

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

View File

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