ADD directory sharding for permanent clips

This commit is contained in:
2025-07-12 19:12:25 +02:00
parent db9426ba1a
commit 3f5d325efd
3 changed files with 19 additions and 4 deletions

View File

@@ -72,7 +72,6 @@ public class ClipService {
return clipRepository.findByUser(user); return clipRepository.findByUser(user);
} }
private User getUser() { private User getUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth != null && auth.isAuthenticated() && auth.getPrincipal() instanceof CustomOAuth2User oAuth2user) { if (auth != null && auth.isAuthenticated() && auth.getPrincipal() instanceof CustomOAuth2User oAuth2user) {

View File

@@ -43,12 +43,27 @@ public class DirectoryService {
} }
public File getOutputFile(String id, String extension) { public File getOutputFile(String id, String extension) {
String dir = outputDir + File.separator + id + (extension.isEmpty() ? "" : "." + extension); if (id == null || id.length() < 2) {
throw new IllegalArgumentException("ID must be at least 2 characters long");
}
// Create subdirectories from first 2 characters of the ID
String shard1 = id.substring(0, 2);
String shard2 = id.substring(2);
String dir = outputDir +
File.separator +
shard1 +
File.separator +
shard2 +
(extension.isEmpty() ? "" : "." + extension);
return new File(dir); return new File(dir);
} }
public void saveData(File file, MultipartFile multipartFile) { public void saveAtDir(File file, MultipartFile multipartFile) {
try { try {
createDirectory(file.getAbsolutePath());
Path filePath = Paths.get(file.getAbsolutePath()); Path filePath = Paths.get(file.getAbsolutePath());
Files.copy(multipartFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING); Files.copy(multipartFile.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
@@ -61,6 +76,7 @@ public class DirectoryService {
Path destPath = Paths.get(target.getAbsolutePath()); Path destPath = Paths.get(target.getAbsolutePath());
try { try {
Files.createDirectories(destPath.getParent());
Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING); Files.copy(sourcePath, destPath, StandardCopyOption.REPLACE_EXISTING);
logger.info("Copied file from {} to {}", sourcePath, destPath); logger.info("Copied file from {} to {}", sourcePath, destPath);
} catch (IOException e) { } catch (IOException e) {

View File

@@ -38,7 +38,7 @@ public class UploadService {
File inputFile = directoryService.getTempInputFile(uuid, extension); File inputFile = directoryService.getTempInputFile(uuid, extension);
File outputFile = directoryService.getTempOutputFile(uuid, extension); File outputFile = directoryService.getTempOutputFile(uuid, extension);
directoryService.saveData(inputFile, file); directoryService.saveAtDir(inputFile, file);
// add job // add job
logger.info("Uploaded file and creating job with UUID: {}", uuid); logger.info("Uploaded file and creating job with UUID: {}", uuid);