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.
This commit is contained in:
@@ -402,7 +402,11 @@ func (h *OPDSHandler) DownloadBook(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if h.conversionService != nil {
|
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 {
|
if err != nil {
|
||||||
return c.JSON(http.StatusInternalServerError, map[string]string{"error": fmt.Sprintf("KEPUB conversion failed: %v", err)})
|
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
|
fileSha256 = converted.SHA256
|
||||||
mimeType = "application/vnd.kobo+xml+zip"
|
mimeType = "application/vnd.kobo+xml+zip"
|
||||||
} else {
|
} 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 {
|
if mediaItem.MimeType.Valid {
|
||||||
mimeType = mediaItem.MimeType.String
|
mimeType = mediaItem.MimeType.String
|
||||||
}
|
}
|
||||||
@@ -435,7 +442,11 @@ func (h *OPDSHandler) DownloadBook(c echo.Context) error {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Default: EPUB
|
// 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 {
|
if mediaItem.MimeType.Valid {
|
||||||
mimeType = mediaItem.MimeType.String
|
mimeType = mediaItem.MimeType.String
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user