From cb8f5e7a29906f9fb0193c8b93f287005cb08221 Mon Sep 17 00:00:00 2001 From: Subhashini Ganesh <105427420+subha0319@users.noreply.github.com> Date: Fri, 21 Nov 2025 09:38:39 +0530 Subject: [PATCH] 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 --- .../booklore/util/FileUtils.java | 20 ++++-- .../booklore/util/FileUtilsTest.java | 61 +++++++++++++++++++ 2 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 booklore-api/src/test/java/com/adityachandel/booklore/util/FileUtilsTest.java 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