This repository has been archived on 2024-06-10. You can view files and clone it, but cannot push or open issues or pull requests.
Files
Backend/src/main/java/cc/fascinated/backend/service/PasteService.java

85 lines
2.7 KiB
Java
Raw Normal View History

2024-04-23 01:15:24 +01:00
package cc.fascinated.backend.service;
2024-04-23 00:43:05 +01:00
2024-04-23 21:16:42 +01:00
import cc.fascinated.backend.exception.impl.BadRequestException;
2024-04-23 01:15:24 +01:00
import cc.fascinated.backend.exception.impl.ResourceNotFoundException;
2024-04-23 21:18:15 +01:00
import cc.fascinated.backend.model.Paste;
2024-04-23 01:15:24 +01:00
import cc.fascinated.backend.repository.PasteRepository;
2024-04-24 17:17:28 +01:00
import lombok.extern.log4j.Log4j2;
2024-04-23 00:43:05 +01:00
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Optional;
2024-04-24 17:17:28 +01:00
@Service @Log4j2(topic = "Paste Service")
2024-04-23 00:43:05 +01:00
public class PasteService {
/**
* The {@link PasteRepository} instance.
*/
private final PasteRepository pasteRepository;
/**
* The length of the paste id.
*/
@Value("${paste.id-length}")
private int idLength;
2024-04-23 21:16:42 +01:00
/**
* The maximum size of the paste content.
*/
@Value("${paste.upload-size-limit}")
private int uploadSizeLimit;
2024-04-23 00:43:05 +01:00
@Autowired
public PasteService(PasteRepository pasteRepository) {
this.pasteRepository = pasteRepository;
}
/**
* Creates a new paste with the specified content.
*
* @param content The content of the paste.
* @return The id of the paste.
*/
public String createPaste(String content) {
2024-04-23 21:16:42 +01:00
int length = content.length();
2024-04-24 17:17:28 +01:00
log.info("Creating a new paste. (characters: {})", length);
// Check if the content is too large.
if (length > uploadSizeLimit && uploadSizeLimit != -1) {
2024-04-24 17:17:28 +01:00
log.info("Paste didn't meet the size requirements. (characters: {})", length);
2024-04-23 21:16:42 +01:00
throw new BadRequestException("The paste content is too large, the limit is " + uploadSizeLimit + " characters");
}
// Save the paste to the database.
2024-04-24 17:17:28 +01:00
Paste paste = pasteRepository.save(new Paste(
2024-04-23 03:36:14 +01:00
RandomStringUtils.randomAlphabetic(idLength),
System.currentTimeMillis(),
content
2024-04-24 17:17:28 +01:00
));
log.info("Created a new paste with the id '{}'", paste.getId());
return paste.getId();
2024-04-23 00:43:05 +01:00
}
/**
* Gets the content of the paste with the specified id.
*
* @param id The id of the paste.
* @return The content of the paste.
*/
public Paste getPaste(String id) {
2024-04-24 17:17:28 +01:00
log.info("Getting paste with the id '{}'", id);
2024-04-23 00:43:05 +01:00
Optional<Paste> paste = pasteRepository.findById(id);
// The paste does not exist.
if (paste.isEmpty()) {
2024-04-24 17:17:28 +01:00
log.info("Paste with the id '{}' not found", id);
throw new ResourceNotFoundException("Paste '%s' not found".formatted(id));
2024-04-23 00:43:05 +01:00
}
2024-04-24 17:17:28 +01:00
log.info("Got paste with the id '{}'", id);
2024-04-23 00:43:05 +01:00
return paste.get();
}
}