Fix: Normalize paths for relative sub-path calculation (#1501)
Fixes a bug preventing import of books with duplicate filenames in different sub-folders. The path relativization in FileUtils.getRelativeSubPath would fail in some cases, yielding an empty sub-path. This caused a database unique index violation as only the filename was saved as the filePath. This commit fixes the issue by normalizing both paths before relativization. Adds FileUtilsTest.java to validate the fix. Fixes #1499
This commit is contained in:
@@ -27,11 +27,21 @@ public class FileUtils {
|
||||
}
|
||||
|
||||
public static String getRelativeSubPath(String basePath, Path fullFilePath) {
|
||||
return Optional.ofNullable(Path.of(basePath)
|
||||
.relativize(fullFilePath)
|
||||
.getParent())
|
||||
.map(path -> path.toString().replace("\\", "/"))
|
||||
.orElse("");
|
||||
try {
|
||||
// Add .normalize() to both paths for robust comparison
|
||||
Path normalizedBasePath = Path.of(basePath).normalize();
|
||||
Path normalizedFullPath = fullFilePath.normalize();
|
||||
|
||||
return Optional.ofNullable(normalizedBasePath
|
||||
.relativize(normalizedFullPath)
|
||||
.getParent())
|
||||
.map(path -> path.toString().replace("\\", "/"))
|
||||
.orElse("");
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to get relative sub-path for basePath '{}' and fullFilePath '{}': {}",
|
||||
basePath, fullFilePath, e.getMessage(), e);
|
||||
return ""; // Fallback to empty string on any error
|
||||
}
|
||||
}
|
||||
|
||||
public static Long getFileSizeInKb(BookEntity bookEntity) {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
// package test.java.com.adityachandel.booklore.util;
|
||||
// import com.adityachandel.booklore.util.FileUtils;
|
||||
|
||||
package com.adityachandel.booklore.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.nio.file.Path;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class FileUtilsTest {
|
||||
|
||||
@Test
|
||||
void getRelativeSubPath_shouldHandleStandardCase() {
|
||||
String basePath = "C:\\Library\\Books";
|
||||
Path fullFilePath = Path.of("C:\\Library\\Books\\Series A\\book.epub");
|
||||
String result = FileUtils.getRelativeSubPath(basePath, fullFilePath);
|
||||
assertThat(result).isEqualTo("Series A");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativeSubPath_shouldHandleNestedFolders() {
|
||||
String basePath = "C:\\Library\\Books";
|
||||
Path fullFilePath = Path.of("C:\\Library\\Books\\Series A\\Subfolder 1\\book.epub");
|
||||
String result = FileUtils.getRelativeSubPath(basePath, fullFilePath);
|
||||
assertThat(result).isEqualTo("Series A/Subfolder 1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativeSubPath_shouldHandleFileInRoot() {
|
||||
String basePath = "C:\\Library\\Books";
|
||||
Path fullFilePath = Path.of("C:\\Library\\Books\\book.epub");
|
||||
String result = FileUtils.getRelativeSubPath(basePath, fullFilePath);
|
||||
assertThat(result).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativeSubPath_shouldHandleTrailingSlashOnBase() {
|
||||
// This was a likely cause of the bug
|
||||
String basePath = "C:\\Library\\Books\\";
|
||||
Path fullFilePath = Path.of("C:\\Library\\Books\\Series A\\book.epub");
|
||||
String result = FileUtils.getRelativeSubPath(basePath, fullFilePath);
|
||||
assertThat(result).isEqualTo("Series A");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativeSubPath_shouldHandleLinuxPaths() {
|
||||
String basePath = "/home/user/books";
|
||||
Path fullFilePath = Path.of("/home/user/books/Series A/book.epub");
|
||||
String result = FileUtils.getRelativeSubPath(basePath, fullFilePath);
|
||||
assertThat(result).isEqualTo("Series A");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRelativeSubPath_shouldHandleNonNormalizedPaths() {
|
||||
String basePath = "C:\\Library\\.\\Books";
|
||||
Path fullFilePath = Path.of("C:\\Library\\Books\\Series A\\..\\Series A\\book.epub");
|
||||
String result = FileUtils.getRelativeSubPath(basePath, fullFilePath);
|
||||
// The normalization should fix both paths and find the correct relative path
|
||||
assertThat(result).isEqualTo("Series A");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user