From 3ab52de17111aecf9a14858022d4546230a050ce Mon Sep 17 00:00:00 2001 From: "aditya.chandel" <8075870+adityachandelgit@users.noreply.github.com> Date: Thu, 14 Aug 2025 18:22:03 -0600 Subject: [PATCH] Refactor Bookdrop file processing to prevent orphan target files on error --- .../service/bookdrop/BookDropService.java | 65 +++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java index bd22e96a9..dc7e48c3e 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java @@ -274,38 +274,51 @@ public class BookDropService { return failureResult(targetFile.getName(), "File already exists in the library '" + library.getName() + "'"); } - Files.createDirectories(target.getParent()); - Files.move(source, target); + try { + Files.createDirectories(target.getParent()); + Files.move(source, target); - log.info("Moved file id={}, name={} from '{}' to '{}'", bookdropFile.getId(), bookdropFile.getFileName(), 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")) - .getType() - ); + Book processedBook = processFile(targetFile.getName(), library, path, targetFile, + BookFileExtension.fromFileName(bookdropFile.getFileName()) + .orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension")) + .getType()); - BookEntity bookEntity = bookRepository.findById(processedBook.getId()) - .orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("Book ID missing after import")); + BookEntity bookEntity = bookRepository.findById(processedBook.getId()) + .orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("Book ID missing after import")); - notificationService.sendMessage(Topic.BOOK_ADD, processedBook); - metadataRefreshService.updateBookMetadata(bookEntity, metadata, metadata.getThumbnailUrl() != null, false); - bookdropFileRepository.deleteById(bookdropFile.getId()); - bookdropNotificationService.sendBookdropFileSummaryNotification(); + notificationService.sendMessage(Topic.BOOK_ADD, processedBook); + metadataRefreshService.updateBookMetadata(bookEntity, metadata, metadata.getThumbnailUrl() != null, false); + bookdropFileRepository.deleteById(bookdropFile.getId()); + bookdropNotificationService.sendBookdropFileSummaryNotification(); - File cachedCover = Paths.get(appProperties.getPathConfig(), "bookdrop_temp", bookdropFile.getId() + ".jpg").toFile(); - if (cachedCover.exists()) { - boolean deleted = cachedCover.delete(); - log.debug("Deleted cached cover image for bookdropId={}: {}", bookdropFile.getId(), deleted); + File cachedCover = Paths.get(appProperties.getPathConfig(), "bookdrop_temp", bookdropFile.getId() + ".jpg").toFile(); + if (cachedCover.exists()) { + boolean deleted = cachedCover.delete(); + 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") + .success(true) + .build(); + + } catch (Exception e) { + log.error("Failed to move file id={}, name={} from '{}' to '{}': {}", bookdropFile.getId(), bookdropFile.getFileName(), source, target, e.getMessage(), e); + try { + if (Files.exists(target)) { + Files.deleteIfExists(target); + log.info("Cleaned up partially created target file: {}", target); + } + } catch (Exception cleanupException) { + log.warn("Failed to cleanup target file after move error: {}", target, cleanupException); + } + return failureResult(bookdropFile.getFileName(), "Failed to move file: " + e.getMessage()); } - - 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") - .success(true) - .build(); } private BookdropFileResult failureResult(String fileName, String message) {