impl a char limit for pastes
This commit is contained in:
@ -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(),
|
||||
|
Reference in New Issue
Block a user