feat(bookdrop): add full pagination support with select-all and metadata caching
- Implemented backend pagination for Bookdrop file review - Updated finalizeImport logic to support select-all with excluded IDs - Handled partial metadata submission from frontend - Refactored chunk processing and logging for clarity - Enhanced frontend Bookdrop UI with paginated selection, metadata sync, and virtual scroll compatibility - Improved UX for bulk finalize and discard actions
This commit is contained in:
committed by
Aditya Chandel
parent
6fb458dc33
commit
f69110c164
+13
-36
@@ -1,62 +1,40 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.mapper.BookdropFileMapper;
|
||||
import com.adityachandel.booklore.model.dto.BookdropFile;
|
||||
import com.adityachandel.booklore.model.dto.BookdropFileNotification;
|
||||
import com.adityachandel.booklore.model.dto.request.BookdropFinalizeRequest;
|
||||
import com.adityachandel.booklore.model.dto.request.BookdropSelectionRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.BookdropFinalizeResult;
|
||||
import com.adityachandel.booklore.model.entity.BookdropFileEntity;
|
||||
import com.adityachandel.booklore.repository.BookdropFileRepository;
|
||||
import com.adityachandel.booklore.service.bookdrop.BookDropService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/api/bookdrop")
|
||||
public class BookdropFileController {
|
||||
|
||||
private final BookdropFileRepository repository;
|
||||
private final BookdropFileMapper mapper;
|
||||
private final BookDropService bookDropService;
|
||||
|
||||
@GetMapping("/notification")
|
||||
public BookdropFileNotification getSummary() {
|
||||
long pendingCount = repository.countByStatus(BookdropFileEntity.Status.PENDING_REVIEW);
|
||||
long totalCount = repository.count();
|
||||
|
||||
return new BookdropFileNotification(
|
||||
(int) pendingCount,
|
||||
(int) totalCount,
|
||||
Instant.now().toString()
|
||||
);
|
||||
return bookDropService.getFileNotificationSummary();
|
||||
}
|
||||
|
||||
@GetMapping("/files")
|
||||
public List<BookdropFile> getFilesByStatus(@RequestParam(required = false) String status) {
|
||||
if ("pending".equalsIgnoreCase(status)) {
|
||||
return repository.findAllByStatus(BookdropFileEntity.Status.PENDING_REVIEW)
|
||||
.stream()
|
||||
.map(mapper::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return repository.findAll()
|
||||
.stream()
|
||||
.map(mapper::toDto)
|
||||
.collect(Collectors.toList());
|
||||
public Page<BookdropFile> getFilesByStatus(@RequestParam(required = false) String status, Pageable pageable) {
|
||||
return bookDropService.getFilesByStatus(status, pageable);
|
||||
}
|
||||
|
||||
@DeleteMapping("/files")
|
||||
public ResponseEntity<Void> discardAllFiles() {
|
||||
bookDropService.discardAllFiles();
|
||||
@PostMapping("/files/discard")
|
||||
public ResponseEntity<Void> discardSelectedFiles(@RequestBody BookdropSelectionRequest request) {
|
||||
bookDropService.discardSelectedFiles(request.isSelectAll(), request.getExcludedIds(), request.getSelectedIds());
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@@ -69,12 +47,11 @@ public class BookdropFileController {
|
||||
@GetMapping("/{bookdropId}/cover")
|
||||
public ResponseEntity<Resource> getBookdropCover(@PathVariable long bookdropId) {
|
||||
Resource file = bookDropService.getBookdropCover(bookdropId);
|
||||
if (file == null) {
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
return ResponseEntity.ok()
|
||||
return (file != null)
|
||||
? ResponseEntity.ok()
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=cover.jpg")
|
||||
.contentType(MediaType.IMAGE_JPEG)
|
||||
.body(file);
|
||||
.body(file)
|
||||
: ResponseEntity.noContent().build();
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -8,7 +8,11 @@ import java.util.List;
|
||||
@Data
|
||||
public class BookdropFinalizeRequest {
|
||||
private String uploadPattern;
|
||||
private Boolean selectAll;
|
||||
private List<Long> excludedIds;
|
||||
private List<BookdropFinalizeFile> files;
|
||||
private Long defaultLibraryId;
|
||||
private Long defaultPathId;
|
||||
|
||||
@Data
|
||||
public static class BookdropFinalizeFile {
|
||||
@@ -17,4 +21,4 @@ public class BookdropFinalizeRequest {
|
||||
private Long pathId;
|
||||
private BookMetadata metadata;
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.adityachandel.booklore.model.dto.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class BookdropSelectionRequest {
|
||||
private boolean selectAll;
|
||||
private List<Long> excludedIds;
|
||||
private List<Long> selectedIds;
|
||||
}
|
||||
+6
-1
@@ -4,6 +4,8 @@ package com.adityachandel.booklore.repository;
|
||||
import com.adityachandel.booklore.model.entity.BookdropFileEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookdropFileEntity.Status;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@@ -18,7 +20,7 @@ public interface BookdropFileRepository extends JpaRepository<BookdropFileEntity
|
||||
|
||||
Optional<BookdropFileEntity> findByFilePath(String filePath);
|
||||
|
||||
List<BookdropFileEntity> findAllByStatus(Status status);
|
||||
Page<BookdropFileEntity> findAllByStatus(Status status, Pageable pageable);
|
||||
|
||||
long countByStatus(Status status);
|
||||
|
||||
@@ -26,4 +28,7 @@ public interface BookdropFileRepository extends JpaRepository<BookdropFileEntity
|
||||
@Modifying
|
||||
@Query("DELETE FROM BookdropFileEntity f WHERE f.filePath LIKE CONCAT(:prefix, '%')")
|
||||
int deleteAllByFilePathStartingWith(@Param("prefix") String prefix);
|
||||
|
||||
@Query("SELECT f.id FROM BookdropFileEntity f WHERE f.id NOT IN :excludedIds")
|
||||
List<Long> findAllExcludingIdsFlat(@Param("excludedIds") List<Long> excludedIds);
|
||||
}
|
||||
|
||||
+231
-63
@@ -2,34 +2,55 @@ package com.adityachandel.booklore.service.bookdrop;
|
||||
|
||||
import com.adityachandel.booklore.config.AppProperties;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.mapper.BookdropFileMapper;
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.BookdropFile;
|
||||
import com.adityachandel.booklore.model.dto.BookdropFileNotification;
|
||||
import com.adityachandel.booklore.model.dto.request.BookdropFinalizeRequest;
|
||||
import com.adityachandel.booklore.model.dto.response.BookdropFinalizeResult;
|
||||
import com.adityachandel.booklore.model.dto.response.BookdropFileResult;
|
||||
import com.adityachandel.booklore.model.dto.response.BookdropFinalizeResult;
|
||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
import com.adityachandel.booklore.model.entity.*;
|
||||
import com.adityachandel.booklore.model.enums.*;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookdropFileEntity;
|
||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||
import com.adityachandel.booklore.model.entity.LibraryPathEntity;
|
||||
import com.adityachandel.booklore.model.enums.BookFileExtension;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.model.websocket.Topic;
|
||||
import com.adityachandel.booklore.repository.*;
|
||||
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.fileprocessor.*;
|
||||
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
|
||||
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataRefreshService;
|
||||
import com.adityachandel.booklore.service.monitoring.*;
|
||||
import com.adityachandel.booklore.service.monitoring.MonitoringService;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import com.adityachandel.booklore.util.PathPatternResolver;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.core.io.PathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Instant;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Slf4j
|
||||
@@ -47,6 +68,22 @@ public class BookDropService {
|
||||
private final BookdropNotificationService bookdropNotificationService;
|
||||
private final BookFileProcessorRegistry processorRegistry;
|
||||
private final AppProperties appProperties;
|
||||
private final BookdropFileMapper mapper;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public BookdropFileNotification getFileNotificationSummary() {
|
||||
long pendingCount = bookdropFileRepository.countByStatus(BookdropFileEntity.Status.PENDING_REVIEW);
|
||||
long totalCount = bookdropFileRepository.count();
|
||||
return new BookdropFileNotification((int) pendingCount, (int) totalCount, Instant.now().toString());
|
||||
}
|
||||
|
||||
public Page<BookdropFile> getFilesByStatus(String status, Pageable pageable) {
|
||||
if ("pending".equalsIgnoreCase(status)) {
|
||||
return bookdropFileRepository.findAllByStatus(BookdropFileEntity.Status.PENDING_REVIEW, pageable).map(mapper::toDto);
|
||||
} else {
|
||||
return bookdropFileRepository.findAll(pageable).map(mapper::toDto);
|
||||
}
|
||||
}
|
||||
|
||||
public BookdropFinalizeResult finalizeImport(BookdropFinalizeRequest request) {
|
||||
boolean monitoringWasActive = !monitoringService.isPaused();
|
||||
@@ -54,36 +91,91 @@ public class BookDropService {
|
||||
bookdropMonitoringService.pauseMonitoring();
|
||||
|
||||
BookdropFinalizeResult results = BookdropFinalizeResult.builder().build();
|
||||
Long defaultLibraryId = request.getDefaultLibraryId();
|
||||
Long defaultPathId = request.getDefaultPathId();
|
||||
|
||||
for (BookdropFinalizeRequest.BookdropFinalizeFile fileReq : request.getFiles()) {
|
||||
try {
|
||||
BookdropFileEntity fileEntity = bookdropFileRepository.findById(fileReq.getFileId()).orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException(fileReq.getFileId()));
|
||||
Map<Long, BookdropFinalizeRequest.BookdropFinalizeFile> metadataById = Optional.ofNullable(request.getFiles())
|
||||
.orElse(List.of())
|
||||
.stream()
|
||||
.collect(Collectors.toMap(BookdropFinalizeRequest.BookdropFinalizeFile::getFileId, Function.identity()));
|
||||
|
||||
BookdropFileResult result = moveFile(
|
||||
fileReq.getLibraryId(),
|
||||
fileReq.getPathId(),
|
||||
request.getUploadPattern(),
|
||||
fileReq.getMetadata(),
|
||||
fileEntity
|
||||
);
|
||||
results.getResults().add(result);
|
||||
} catch (Exception e) {
|
||||
String msg = String.format("Failed to finalize file [id=%s]: %s", fileReq.getFileId(), e.getMessage());
|
||||
log.error(msg, e);
|
||||
notificationService.sendMessage(Topic.LOG, msg);
|
||||
final int CHUNK_SIZE = 100;
|
||||
AtomicInteger failedCount = new AtomicInteger();
|
||||
|
||||
log.info("Starting finalizeImport: selectAll={}, provided file count={}, defaultLibraryId={}, defaultPathId={}",
|
||||
request.getSelectAll(), metadataById.size(), defaultLibraryId, defaultPathId);
|
||||
|
||||
if (Boolean.TRUE.equals(request.getSelectAll())) {
|
||||
List<Long> excludedIds = Optional.ofNullable(request.getExcludedIds()).orElse(List.of());
|
||||
|
||||
List<Long> allIds = bookdropFileRepository.findAllExcludingIdsFlat(excludedIds); // You need to write this
|
||||
log.info("SelectAll: Total files to finalize (after exclusions): {}, Excluded IDs: {}", allIds.size(), excludedIds);
|
||||
|
||||
for (int i = 0; i < allIds.size(); i += CHUNK_SIZE) {
|
||||
int end = Math.min(i + CHUNK_SIZE, allIds.size());
|
||||
List<Long> chunk = allIds.subList(i, end);
|
||||
|
||||
log.info("Processing chunk {}/{} ({} files): IDs={}", (i / CHUNK_SIZE + 1), (int) Math.ceil((double) allIds.size() / CHUNK_SIZE), chunk.size(), chunk);
|
||||
|
||||
List<BookdropFileEntity> chunkFiles = bookdropFileRepository.findAllById(chunk);
|
||||
Map<Long, BookdropFileEntity> fileMap = chunkFiles.stream().collect(Collectors.toMap(BookdropFileEntity::getId, Function.identity()));
|
||||
|
||||
for (Long id : chunk) {
|
||||
BookdropFileEntity file = fileMap.get(id);
|
||||
if (file == null) {
|
||||
log.warn("File ID {} missing in DB during finalizeImport chunk processing", id);
|
||||
failedCount.incrementAndGet();
|
||||
continue;
|
||||
}
|
||||
processFile(file, metadataById.get(id), defaultLibraryId, defaultPathId, request.getUploadPattern(), results, failedCount);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
List<Long> ids = Optional.ofNullable(request.getFiles())
|
||||
.orElse(List.of())
|
||||
.stream()
|
||||
.map(BookdropFinalizeRequest.BookdropFinalizeFile::getFileId)
|
||||
.toList();
|
||||
|
||||
log.info("Processing {} manually selected files in chunks of {}. File IDs: {}", ids.size(), CHUNK_SIZE, ids);
|
||||
|
||||
for (int i = 0; i < ids.size(); i += CHUNK_SIZE) {
|
||||
int end = Math.min(i + CHUNK_SIZE, ids.size());
|
||||
List<Long> chunkIds = ids.subList(i, end);
|
||||
List<BookdropFileEntity> chunkFiles = bookdropFileRepository.findAllById(chunkIds);
|
||||
|
||||
log.info("Processing chunk {} of {} ({} files): IDs={}", (i / CHUNK_SIZE + 1), (int) Math.ceil((double) ids.size() / CHUNK_SIZE), chunkFiles.size(), chunkIds);
|
||||
|
||||
Map<Long, BookdropFileEntity> fileMap = chunkFiles.stream()
|
||||
.collect(Collectors.toMap(BookdropFileEntity::getId, Function.identity()));
|
||||
|
||||
for (Long id : chunkIds) {
|
||||
BookdropFileEntity file = fileMap.get(id);
|
||||
if (file == null) {
|
||||
log.error("File ID {} not found in DB during finalizeImport chunk processing", id);
|
||||
failedCount.incrementAndGet();
|
||||
continue;
|
||||
}
|
||||
processFile(file, metadataById.get(id), defaultLibraryId, defaultPathId, request.getUploadPattern(), results, failedCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Finalization complete. Success: {}, Failed: {}, Total processed: {}",
|
||||
results.getResults().stream().filter(BookdropFileResult::isSuccess).count(),
|
||||
failedCount.get(),
|
||||
results.getResults().size());
|
||||
|
||||
if (monitoringWasActive) {
|
||||
Thread.startVirtualThread(() -> {
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
monitoringService.resumeMonitoring();
|
||||
bookdropMonitoringService.resumeMonitoring();
|
||||
log.info("Monitoring resumed after 5s delay");
|
||||
log.info("Monitoring resumed after 5s delay following finalizeImport");
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
log.warn("Interrupted while delaying resume of monitoring");
|
||||
log.warn("Interrupted while delaying resume of monitoring after finalizeImport");
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -91,6 +183,64 @@ public class BookDropService {
|
||||
return results;
|
||||
}
|
||||
|
||||
private void processFile(
|
||||
BookdropFileEntity fileEntity,
|
||||
BookdropFinalizeRequest.BookdropFinalizeFile fileReq,
|
||||
Long defaultLibraryId,
|
||||
Long defaultPathId,
|
||||
String uploadPattern,
|
||||
BookdropFinalizeResult results,
|
||||
AtomicInteger failedCount
|
||||
) {
|
||||
try {
|
||||
Long libraryId;
|
||||
Long pathId;
|
||||
BookMetadata metadata;
|
||||
|
||||
if (fileReq != null) {
|
||||
libraryId = fileReq.getLibraryId() != null ? fileReq.getLibraryId() : defaultLibraryId;
|
||||
pathId = fileReq.getPathId() != null ? fileReq.getPathId() : defaultPathId;
|
||||
metadata = fileReq.getMetadata();
|
||||
log.debug("Processing fileId={}, fileName={} with provided metadata, libraryId={}, pathId={}", fileEntity.getId(), fileEntity.getFileName(), libraryId, pathId);
|
||||
} else {
|
||||
if (defaultLibraryId == null || defaultPathId == null) {
|
||||
log.warn("Missing default metadata for fileId={}", fileEntity.getId());
|
||||
throw ApiError.BAD_REQUEST.createException("Missing metadata and defaults for fileId=" + fileEntity.getId());
|
||||
}
|
||||
|
||||
metadata = fileEntity.getFetchedMetadata() != null
|
||||
? objectMapper.readValue(fileEntity.getFetchedMetadata(), BookMetadata.class)
|
||||
: objectMapper.readValue(fileEntity.getOriginalMetadata(), BookMetadata.class);
|
||||
|
||||
libraryId = defaultLibraryId;
|
||||
pathId = defaultPathId;
|
||||
log.debug("Processing fileId={}, fileName={} with default metadata, libraryId={}, pathId={}", fileEntity.getId(), fileEntity.getFileName(), libraryId, pathId);
|
||||
}
|
||||
|
||||
BookdropFileResult result = moveFile(
|
||||
libraryId,
|
||||
pathId,
|
||||
uploadPattern,
|
||||
metadata,
|
||||
fileEntity
|
||||
);
|
||||
|
||||
results.getResults().add(result);
|
||||
if (!result.isSuccess()) {
|
||||
log.warn("Finalization failed (non-exception) for file id={}, name={}, message={}", fileEntity.getId(), fileEntity.getFileName(), result.getMessage());
|
||||
failedCount.incrementAndGet();
|
||||
} else {
|
||||
log.info("Successfully finalized file id={}, name={}", fileEntity.getId(), fileEntity.getFileName());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
failedCount.incrementAndGet();
|
||||
String msg = String.format("Error finalizing file [id=%s, name=%s]: %s", fileEntity.getId(), fileEntity.getFileName(), e.getMessage());
|
||||
log.error(msg, e);
|
||||
notificationService.sendMessage(Topic.LOG, msg);
|
||||
}
|
||||
}
|
||||
|
||||
private BookdropFileResult moveFile(long libraryId, long pathId, String filePattern, BookMetadata metadata, BookdropFileEntity bookdropFile) throws Exception {
|
||||
LibraryEntity library = libraryRepository.findById(libraryId)
|
||||
.orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId));
|
||||
@@ -109,20 +259,26 @@ public class BookDropService {
|
||||
Path target = Paths.get(path.getPath(), relativePath);
|
||||
File targetFile = target.toFile();
|
||||
|
||||
log.debug("Preparing to move file id={}, name={}, source={}, target={}, library={}, path={}",
|
||||
bookdropFile.getId(), bookdropFile.getFileName(), source, target, library.getName(), path.getPath());
|
||||
|
||||
if (!Files.exists(source)) {
|
||||
bookdropFileRepository.deleteById(bookdropFile.getId());
|
||||
log.warn("Source file [id={}] not found. Deleting entry.", bookdropFile.getId());
|
||||
log.warn("Source file [id={}] not found at '{}'. Deleting entry from DB.", bookdropFile.getId(), source);
|
||||
bookdropNotificationService.sendBookdropFileSummaryNotification();
|
||||
return failureResult(targetFile.getName(), "Source file does not exist in bookdrop folder");
|
||||
}
|
||||
|
||||
if (targetFile.exists()) {
|
||||
log.warn("Target file already exists: id={}, name={}, target={}", bookdropFile.getId(), bookdropFile.getFileName(), target);
|
||||
return failureResult(targetFile.getName(), "File already exists in the library '" + library.getName() + "'");
|
||||
}
|
||||
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.move(source, target);
|
||||
|
||||
log.info("Moved file id={}, name={} from '{}' to '{}'", bookdropFile.getId(), bookdropFile.getFileName(), source, target);
|
||||
|
||||
Book processedBook = processFile(targetFile.getName(), library, path, targetFile,
|
||||
BookFileExtension.fromFileName(bookdropFile.getFileName())
|
||||
.orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension"))
|
||||
@@ -143,6 +299,8 @@ public class BookDropService {
|
||||
log.debug("Deleted cached cover image for bookdropId={}: {}", bookdropFile.getId(), deleted);
|
||||
}
|
||||
|
||||
log.info("File import completed: id={}, name={}, library={}, path={}", bookdropFile.getId(), targetFile.getName(), library.getName(), path.getPath());
|
||||
|
||||
return BookdropFileResult.builder()
|
||||
.fileName(targetFile.getName())
|
||||
.message("File successfully imported into the '" + library.getName() + "' library from the Bookdrop folder")
|
||||
@@ -171,7 +329,7 @@ public class BookDropService {
|
||||
return processor.processFile(libraryFile);
|
||||
}
|
||||
|
||||
public void discardAllFiles() {
|
||||
public void discardSelectedFiles(boolean selectAll, List<Long> excludedIds, List<Long> selectedIds) {
|
||||
bookdropMonitoringService.pauseMonitoring();
|
||||
Path bookdropPath = Path.of(appProperties.getBookdropFolder());
|
||||
|
||||
@@ -185,52 +343,62 @@ public class BookDropService {
|
||||
return;
|
||||
}
|
||||
|
||||
try (Stream<Path> paths = Files.walk(bookdropPath)) {
|
||||
paths.sorted(Comparator.reverseOrder())
|
||||
.filter(p -> !p.equals(bookdropPath))
|
||||
.forEach(path -> {
|
||||
try {
|
||||
if (Files.isRegularFile(path) && Files.deleteIfExists(path)) {
|
||||
deletedFiles.incrementAndGet();
|
||||
} else if (Files.isDirectory(path) && Files.deleteIfExists(path)) {
|
||||
deletedDirs.incrementAndGet();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to delete path: {}", path, e);
|
||||
}
|
||||
});
|
||||
List<BookdropFileEntity> filesToDelete;
|
||||
if (selectAll) {
|
||||
filesToDelete = bookdropFileRepository.findAll().stream()
|
||||
.filter(f -> excludedIds == null || !excludedIds.contains(f.getId()))
|
||||
.toList();
|
||||
log.info("Discarding all files except excluded IDs: {}", excludedIds);
|
||||
} else {
|
||||
filesToDelete = bookdropFileRepository.findAllById(selectedIds == null ? List.of() : selectedIds);
|
||||
log.info("Discarding selected files: {}", selectedIds);
|
||||
}
|
||||
|
||||
long removedDbCount = bookdropFileRepository.count();
|
||||
bookdropFileRepository.deleteAll();
|
||||
|
||||
Path tempCoverDir = Paths.get(appProperties.getPathConfig(), "bookdrop_temp");
|
||||
if (Files.exists(tempCoverDir)) {
|
||||
try (Stream<Path> files = Files.walk(tempCoverDir)) {
|
||||
files
|
||||
.filter(Files::isRegularFile)
|
||||
.filter(p -> p.toString().endsWith(".jpg"))
|
||||
.forEach(p -> {
|
||||
try {
|
||||
Files.delete(p);
|
||||
deletedCovers.incrementAndGet();
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to delete cached cover: {}", p, e);
|
||||
}
|
||||
});
|
||||
for (BookdropFileEntity entity : filesToDelete) {
|
||||
try {
|
||||
Path filePath = Path.of(entity.getFilePath());
|
||||
if (Files.exists(filePath) && Files.isRegularFile(filePath) && Files.deleteIfExists(filePath)) {
|
||||
deletedFiles.incrementAndGet();
|
||||
log.debug("Deleted file from disk: id={}, path={}", entity.getId(), filePath);
|
||||
}
|
||||
Path coverPath = Paths.get(appProperties.getPathConfig(), "bookdrop_temp", entity.getId() + ".jpg");
|
||||
if (Files.exists(coverPath) && Files.deleteIfExists(coverPath)) {
|
||||
deletedCovers.incrementAndGet();
|
||||
log.debug("Deleted cover image: id={}, path={}", entity.getId(), coverPath);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to clean bookdrop_temp folder", e);
|
||||
log.warn("Failed to delete file or cover for bookdropId={}: {}", entity.getId(), e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
bookdropFileRepository.deleteAllById(filesToDelete.stream().map(BookdropFileEntity::getId).toList());
|
||||
log.info("Deleted {} bookdrop DB entries", filesToDelete.size());
|
||||
|
||||
try (Stream<Path> paths = Files.walk(bookdropPath)) {
|
||||
paths.sorted(Comparator.reverseOrder())
|
||||
.filter(p -> !p.equals(bookdropPath) && Files.isDirectory(p))
|
||||
.forEach(p -> {
|
||||
try (Stream<Path> subPaths = Files.list(p)) {
|
||||
if (subPaths.findAny().isEmpty()) {
|
||||
Files.deleteIfExists(p);
|
||||
deletedDirs.incrementAndGet();
|
||||
log.debug("Deleted empty directory: {}", p);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to delete folder: {}", p, e);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
log.warn("Failed to scan bookdrop folder for empty directories", e);
|
||||
}
|
||||
|
||||
bookdropNotificationService.sendBookdropFileSummaryNotification();
|
||||
log.info("Bookdrop cleanup summary: deleted {} files, {} folders, {} DB entries, {} covers",
|
||||
deletedFiles.get(), deletedDirs.get(), filesToDelete.size(), deletedCovers.get());
|
||||
|
||||
log.info("Discarded all files: deleted {} files, {} folders, {} DB entries, and {} cover images", deletedFiles.get(), deletedDirs.get(), removedDbCount, deletedCovers.get());
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to clean bookdrop folder", e);
|
||||
} finally {
|
||||
bookdropMonitoringService.resumeMonitoring();
|
||||
log.info("Bookdrop monitoring resumed after cleanup");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,4 +411,4 @@ public class BookDropService {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -93,7 +93,7 @@ public class BookdropEventHandlerService {
|
||||
}
|
||||
|
||||
if (bookdropFileRepository.findByFilePath(filePath).isPresent()) {
|
||||
log.info("File already processed in bookdrop, ignoring: {}", filePath);
|
||||
log.info("File already exists in Bookdrop and is pending review or acceptance: {}", filePath);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user