fix(bookdrop): directly copy instead of move between filesystems (#1759)

This commit is contained in:
James Ward
2026-06-17 10:20:25 -04:00
committed by GitHub
parent 2f3e3be866
commit 64ddbe550c
2 changed files with 14 additions and 38 deletions
@@ -412,21 +412,18 @@ public class BookDropService {
}
private BookdropFileResult performFileMove(BookdropFileEntity bookdropFile, Path source, Path target, LibraryEntity library, LibraryPathEntity path, BookMetadata metadata) {
Path tempPath = null;
try {
String suffix = "";
String fileName = bookdropFile.getFileName();
int lastDotIndex = fileName.lastIndexOf('.');
if (lastDotIndex >= 0) {
suffix = fileName.substring(lastDotIndex);
}
tempPath = Files.createTempFile("bookdrop-finalize-", suffix);
Files.copy(source, tempPath, StandardCopyOption.REPLACE_EXISTING);
Files.createDirectories(target.getParent());
Files.move(tempPath, target, StandardCopyOption.REPLACE_EXISTING);
log.info("Moved file id={}, name={} from '{}' to '{}'", bookdropFile.getId(), bookdropFile.getFileName(), source, target);
// We used to use `Files.copy` to copy from `/bookdrop` to tmp, then used `Files.move`
// to get the file to its target destination. However, there seemed to be a bug
// that caused moves between filesystems to end up with a file at the target
// with 0 bytes.
//
// Instead, we are using `Files.copy` directly
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
log.info("Copied file id={}, name={} from '{}' to '{}'", bookdropFile.getId(), bookdropFile.getFileName(), source, target);
BookdropFileResult result;
try {
@@ -453,8 +450,6 @@ public class BookDropService {
log.error("Failed to move file id={}, name={} from '{}' to '{}': {}", bookdropFile.getId(), bookdropFile.getFileName(), source, target, e.getMessage(), e);
cleanupFailedMove(target);
return failureResult(bookdropFile.getFileName(), "Failed to move file: " + e.getMessage());
} finally {
cleanupTempFile(tempPath);
}
}
@@ -532,16 +527,6 @@ public class BookDropService {
}
}
private void cleanupTempFile(Path tempPath) {
if (tempPath != null) {
try {
Files.deleteIfExists(tempPath);
} catch (Exception e) {
log.warn("Failed to cleanup temp file: {}", tempPath, e);
}
}
}
private void cleanupTargetFile(Path target, Long fileId, String reason) {
try {
if (Files.deleteIfExists(target)) {
@@ -18,7 +18,6 @@ import org.booklore.repository.BookdropFileRepository;
import org.booklore.repository.LibraryPathRepository;
import org.booklore.repository.LibraryRepository;
import org.booklore.service.NotificationService;
import org.booklore.service.event.BookAddedEvent;
import org.booklore.service.file.FileMovingHelper;
import org.booklore.service.fileprocessor.BookFileProcessor;
import org.booklore.service.fileprocessor.BookFileProcessorRegistry;
@@ -268,10 +267,8 @@ class BookDropServiceTest {
try (MockedStatic<Files> filesMock = mockStatic(Files.class, withSettings().lenient())) {
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
filesMock.when(() -> Files.createTempFile(anyString(), anyString())).thenReturn(tempDir.resolve("temp-file"));
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any())).thenReturn(1024L);
filesMock.when(() -> Files.createDirectories(any(Path.class))).thenReturn(tempDir);
filesMock.when(() -> Files.move(any(Path.class), any(Path.class), any())).thenReturn(tempDir);
filesMock.when(() -> Files.deleteIfExists(any(Path.class))).thenReturn(true);
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
@@ -435,7 +432,7 @@ class BookDropServiceTest {
try (MockedStatic<Files> filesMock = mockStatic(Files.class, withSettings().lenient())) {
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
filesMock.when(() -> Files.createTempFile(anyString(), anyString()))
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any()))
.thenThrow(new IOException("Disk full"));
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
@@ -490,16 +487,13 @@ class BookDropServiceTest {
when(appProperties.getPathConfig()).thenReturn(tempDir.toString());
Path sourcePath = Path.of(bookdropFileEntity.getFilePath());
Path tempPath = tempDir.resolve("temp-file");
Path targetPath = tempDir.resolve("moved-book.pdf");
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
filesMock.when(() -> Files.createTempFile(anyString(), anyString())).thenReturn(tempPath);
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any())).thenReturn(tempPath);
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any())).thenReturn(targetPath);
filesMock.when(() -> Files.createDirectories(any(Path.class))).thenReturn(tempDir);
filesMock.when(() -> Files.move(any(Path.class), any(Path.class), any())).thenReturn(targetPath);
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
assertNotNull(result);
@@ -536,16 +530,13 @@ class BookDropServiceTest {
when(processor.processFile(any())).thenThrow(new RuntimeException("Processing failed"));
Path sourcePath = Path.of(bookdropFileEntity.getFilePath());
Path tempPath = tempDir.resolve("temp-file");
Path targetPath = tempDir.resolve("moved-book.pdf");
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
filesMock.when(() -> Files.createTempFile(anyString(), anyString())).thenReturn(tempPath);
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any())).thenReturn(tempPath);
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any())).thenReturn(targetPath);
filesMock.when(() -> Files.createDirectories(any(Path.class))).thenReturn(tempDir);
filesMock.when(() -> Files.move(any(Path.class), any(Path.class), any())).thenReturn(targetPath);
filesMock.when(() -> Files.deleteIfExists(targetPath)).thenReturn(true);
BookdropFinalizeResult result = bookDropService.finalizeImport(request);