Add per-library customizable file naming patterns

This commit is contained in:
aditya.chandel
2025-08-10 11:27:28 -06:00
committed by Aditya Chandel
parent 1f83dfa85d
commit 978bd05b1e
20 changed files with 394 additions and 342 deletions
@@ -11,6 +11,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/v1/libraries")
@@ -72,4 +73,13 @@ public class LibraryController {
libraryService.rescanLibrary(libraryId);
return ResponseEntity.noContent().build();
}
@PatchMapping("/{libraryId}/file-naming-pattern")
@CheckLibraryAccess(libraryIdParam = "libraryId")
@PreAuthorize("@securityUtil.canManipulateLibrary() or @securityUtil.isAdmin()")
public ResponseEntity<Library> setFileNamingPattern(@PathVariable long libraryId, @RequestBody Map<String, String> body) {
String pattern = body.get("fileNamingPattern");
Library updated = libraryService.setFileNamingPattern(libraryId, pattern);
return ResponseEntity.ok(updated);
}
}
@@ -14,6 +14,7 @@ public class Library {
private String name;
private Sort sort;
private String icon;
private String fileNamingPattern;
private boolean watch;
private List<LibraryPath> paths;
}
@@ -7,7 +7,6 @@ import java.util.List;
@Data
public class BookdropFinalizeRequest {
private String uploadPattern;
private Boolean selectAll;
private List<Long> excludedIds;
private List<BookdropFinalizeFile> files;
@@ -37,4 +37,7 @@ public class LibraryEntity {
private boolean watch;
private String icon;
@Column(name = "file_naming_pattern")
private String fileNamingPattern;
}
@@ -22,6 +22,7 @@ import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.repository.BookdropFileRepository;
import com.adityachandel.booklore.repository.LibraryRepository;
import com.adityachandel.booklore.service.NotificationService;
import com.adityachandel.booklore.service.appsettings.AppSettingService;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
import com.adityachandel.booklore.service.metadata.MetadataRefreshService;
@@ -70,6 +71,7 @@ public class BookDropService {
private final AppProperties appProperties;
private final BookdropFileMapper mapper;
private final ObjectMapper objectMapper;
AppSettingService appSettingService;
public BookdropFileNotification getFileNotificationSummary() {
long pendingCount = bookdropFileRepository.countByStatus(BookdropFileEntity.Status.PENDING_REVIEW);
@@ -127,7 +129,7 @@ public class BookDropService {
failedCount.incrementAndGet();
continue;
}
processFile(file, metadataById.get(id), defaultLibraryId, defaultPathId, request.getUploadPattern(), results, failedCount);
processFile(file, metadataById.get(id), defaultLibraryId, defaultPathId, results, failedCount);
}
}
} else {
@@ -156,7 +158,7 @@ public class BookDropService {
failedCount.incrementAndGet();
continue;
}
processFile(file, metadataById.get(id), defaultLibraryId, defaultPathId, request.getUploadPattern(), results, failedCount);
processFile(file, metadataById.get(id), defaultLibraryId, defaultPathId, results, failedCount);
}
}
}
@@ -188,7 +190,6 @@ public class BookDropService {
BookdropFinalizeRequest.BookdropFinalizeFile fileReq,
Long defaultLibraryId,
Long defaultPathId,
String uploadPattern,
BookdropFinalizeResult results,
AtomicInteger failedCount
) {
@@ -217,13 +218,7 @@ public class BookDropService {
log.debug("Processing fileId={}, fileName={} with default metadata, libraryId={}, pathId={}", fileEntity.getId(), fileEntity.getFileName(), libraryId, pathId);
}
BookdropFileResult result = moveFile(
libraryId,
pathId,
uploadPattern,
metadata,
fileEntity
);
BookdropFileResult result = moveFile(libraryId, pathId, metadata, fileEntity);
results.getResults().add(result);
if (!result.isSuccess()) {
@@ -241,7 +236,7 @@ public class BookDropService {
}
}
private BookdropFileResult moveFile(long libraryId, long pathId, String filePattern, BookMetadata metadata, BookdropFileEntity bookdropFile) throws Exception {
private BookdropFileResult moveFile(long libraryId, long pathId, BookMetadata metadata, BookdropFileEntity bookdropFile) throws Exception {
LibraryEntity library = libraryRepository.findById(libraryId)
.orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
@@ -250,6 +245,11 @@ public class BookDropService {
.findFirst()
.orElseThrow(() -> ApiError.INVALID_LIBRARY_PATH.createException(libraryId));
String filePattern = library.getFileNamingPattern();
if (filePattern == null || filePattern.isBlank()) {
filePattern = appSettingService.getAppSettings().getUploadPattern();
}
if (filePattern.endsWith("/") || filePattern.endsWith("\\")) {
filePattern += "{currentFilename}";
}
@@ -231,4 +231,10 @@ public class LibraryService {
List<BookEntity> bookEntities = bookRepository.findAllWithMetadataByLibraryId(libraryId);
return bookEntities.stream().map(bookMapper::toBook).toList();
}
public Library setFileNamingPattern(long libraryId, String pattern) {
LibraryEntity library = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
library.setFileNamingPattern(pattern);
return libraryMapper.toLibrary(libraryRepository.save(library));
}
}
@@ -0,0 +1,3 @@
ALTER TABLE library
ADD COLUMN IF NOT EXISTS file_naming_pattern VARCHAR(1000);