fix(bookdrop): directly copy instead of move between filesystems (#1759)
This commit is contained in:
@@ -412,21 +412,18 @@ public class BookDropService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private BookdropFileResult performFileMove(BookdropFileEntity bookdropFile, Path source, Path target, LibraryEntity library, LibraryPathEntity path, BookMetadata metadata) {
|
private BookdropFileResult performFileMove(BookdropFileEntity bookdropFile, Path source, Path target, LibraryEntity library, LibraryPathEntity path, BookMetadata metadata) {
|
||||||
Path tempPath = null;
|
|
||||||
try {
|
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.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;
|
BookdropFileResult result;
|
||||||
try {
|
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);
|
log.error("Failed to move file id={}, name={} from '{}' to '{}': {}", bookdropFile.getId(), bookdropFile.getFileName(), source, target, e.getMessage(), e);
|
||||||
cleanupFailedMove(target);
|
cleanupFailedMove(target);
|
||||||
return failureResult(bookdropFile.getFileName(), "Failed to move file: " + e.getMessage());
|
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) {
|
private void cleanupTargetFile(Path target, Long fileId, String reason) {
|
||||||
try {
|
try {
|
||||||
if (Files.deleteIfExists(target)) {
|
if (Files.deleteIfExists(target)) {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import org.booklore.repository.BookdropFileRepository;
|
|||||||
import org.booklore.repository.LibraryPathRepository;
|
import org.booklore.repository.LibraryPathRepository;
|
||||||
import org.booklore.repository.LibraryRepository;
|
import org.booklore.repository.LibraryRepository;
|
||||||
import org.booklore.service.NotificationService;
|
import org.booklore.service.NotificationService;
|
||||||
import org.booklore.service.event.BookAddedEvent;
|
|
||||||
import org.booklore.service.file.FileMovingHelper;
|
import org.booklore.service.file.FileMovingHelper;
|
||||||
import org.booklore.service.fileprocessor.BookFileProcessor;
|
import org.booklore.service.fileprocessor.BookFileProcessor;
|
||||||
import org.booklore.service.fileprocessor.BookFileProcessorRegistry;
|
import org.booklore.service.fileprocessor.BookFileProcessorRegistry;
|
||||||
@@ -268,10 +267,8 @@ class BookDropServiceTest {
|
|||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class, withSettings().lenient())) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class, withSettings().lenient())) {
|
||||||
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
|
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.copy(any(Path.class), any(Path.class), any())).thenReturn(1024L);
|
||||||
filesMock.when(() -> Files.createDirectories(any(Path.class))).thenReturn(tempDir);
|
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);
|
filesMock.when(() -> Files.deleteIfExists(any(Path.class))).thenReturn(true);
|
||||||
|
|
||||||
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
|
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
|
||||||
@@ -435,7 +432,7 @@ class BookDropServiceTest {
|
|||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class, withSettings().lenient())) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class, withSettings().lenient())) {
|
||||||
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
|
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"));
|
.thenThrow(new IOException("Disk full"));
|
||||||
|
|
||||||
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
|
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
|
||||||
@@ -490,15 +487,12 @@ class BookDropServiceTest {
|
|||||||
when(appProperties.getPathConfig()).thenReturn(tempDir.toString());
|
when(appProperties.getPathConfig()).thenReturn(tempDir.toString());
|
||||||
|
|
||||||
Path sourcePath = Path.of(bookdropFileEntity.getFilePath());
|
Path sourcePath = Path.of(bookdropFileEntity.getFilePath());
|
||||||
Path tempPath = tempDir.resolve("temp-file");
|
|
||||||
Path targetPath = tempDir.resolve("moved-book.pdf");
|
Path targetPath = tempDir.resolve("moved-book.pdf");
|
||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
||||||
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
|
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(targetPath);
|
||||||
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any())).thenReturn(tempPath);
|
|
||||||
filesMock.when(() -> Files.createDirectories(any(Path.class))).thenReturn(tempDir);
|
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);
|
BookdropFinalizeResult result = bookDropService.finalizeImport(request);
|
||||||
|
|
||||||
@@ -536,15 +530,12 @@ class BookDropServiceTest {
|
|||||||
when(processor.processFile(any())).thenThrow(new RuntimeException("Processing failed"));
|
when(processor.processFile(any())).thenThrow(new RuntimeException("Processing failed"));
|
||||||
|
|
||||||
Path sourcePath = Path.of(bookdropFileEntity.getFilePath());
|
Path sourcePath = Path.of(bookdropFileEntity.getFilePath());
|
||||||
Path tempPath = tempDir.resolve("temp-file");
|
|
||||||
Path targetPath = tempDir.resolve("moved-book.pdf");
|
Path targetPath = tempDir.resolve("moved-book.pdf");
|
||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
||||||
filesMock.when(() -> Files.exists(any(Path.class))).thenReturn(true);
|
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(targetPath);
|
||||||
filesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any())).thenReturn(tempPath);
|
|
||||||
filesMock.when(() -> Files.createDirectories(any(Path.class))).thenReturn(tempDir);
|
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);
|
filesMock.when(() -> Files.deleteIfExists(targetPath)).thenReturn(true);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user