From 524d963a972f8ff742eab2c65e297fde7ce16fba Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Fri, 27 Feb 2026 21:50:15 -0500 Subject: [PATCH] fix(handlers): update OPDS download to use path resolution service Update the DownloadBook function to properly resolve media file paths using the LibraryService.ResolveMediaPath method instead of directly accessing the FilePath field. This ensures correct file resolution after the migration to relative path storage. The change affects three code paths in the download handler: - KEPUB conversion path - Direct file serve path (non-EPUB with conversion service) - Default EPUB path Error handling added to return 404 when path resolution fails, preventing potential errors when accessing non-existent files. This fixes potential file access issues after the relative path storage implementation. --- internal/handlers/opds.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/handlers/opds.go b/internal/handlers/opds.go index 1827ab6..84b0764 100644 --- a/internal/handlers/opds.go +++ b/internal/handlers/opds.go @@ -402,7 +402,11 @@ func (h *OPDSHandler) DownloadBook(c echo.Context) error { } } else { if h.conversionService != nil { - converted, err := h.conversionService.ConvertEPUBToKEPUB(c.Request().Context(), pgtype.UUID{Bytes: bookUUID, Valid: true}, mediaItem.FilePath) + resolvedPath, err := h.libraryService.ResolveMediaPath(c.Request().Context(), mediaItem.LibraryID, mediaItem.FilePath) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"error": "file not found"}) + } + converted, err := h.conversionService.ConvertEPUBToKEPUB(c.Request().Context(), pgtype.UUID{Bytes: bookUUID, Valid: true}, resolvedPath) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("KEPUB conversion failed: %v", err)}) } @@ -410,7 +414,10 @@ func (h *OPDSHandler) DownloadBook(c echo.Context) error { fileSha256 = converted.SHA256 mimeType = "application/vnd.kobo+xml+zip" } else { - filePath = mediaItem.FilePath + filePath, err = h.libraryService.ResolveMediaPath(c.Request().Context(), mediaItem.LibraryID, mediaItem.FilePath) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"error": "file not found"}) + } if mediaItem.MimeType.Valid { mimeType = mediaItem.MimeType.String } @@ -435,7 +442,11 @@ func (h *OPDSHandler) DownloadBook(c echo.Context) error { } } else { // Default: EPUB - filePath = mediaItem.FilePath + var err error + filePath, err = h.libraryService.ResolveMediaPath(c.Request().Context(), mediaItem.LibraryID, mediaItem.FilePath) + if err != nil { + return c.JSON(http.StatusNotFound, map[string]string{"error": "file not found"}) + } if mediaItem.MimeType.Valid { mimeType = mediaItem.MimeType.String }