diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/util/FileUtils.java b/booklore-api/src/main/java/com/adityachandel/booklore/util/FileUtils.java index b5db45f90..eeea91dd7 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/util/FileUtils.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/util/FileUtils.java @@ -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) { diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/util/FileUtilsTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/util/FileUtilsTest.java new file mode 100644 index 000000000..7b3d9771a --- /dev/null +++ b/booklore-api/src/test/java/com/adityachandel/booklore/util/FileUtilsTest.java @@ -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"); + } +} \ No newline at end of file