fix(utils): URL-encode media paths to handle special characters in filenames

Cover image URLs with special characters like parentheses, #, ?, or
spaces would break because browsers interpret them as URL delimiters.
Apply url.PathEscape() per path segment in ResolveMediaURL so the
server can correctly resolve files like "Wonder Woman (2016) #001.cbz.cover.jpg".

Also adds a package doc comment and fixes the exported function comment.
This commit is contained in:
2026-04-23 13:57:30 -04:00
parent 133ca1fdaa
commit 3c3c4e8bf5
+9 -2
View File
@@ -1,7 +1,9 @@
// Package utils provides helper functions for URL resolution, ISBN handling, and tag management.
package utils package utils
import ( import (
"fmt" "fmt"
"net/url"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -9,7 +11,7 @@ import (
"github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgtype"
) )
// resolveMediaURL resolves a relative path (cover or file) to a full URL // ResolveMediaURL resolves a relative path (cover or file) to a full URL
func ResolveMediaURL(libraryID pgtype.UUID, relativePath pgtype.Text) string { func ResolveMediaURL(libraryID pgtype.UUID, relativePath pgtype.Text) string {
if !relativePath.Valid || relativePath.String == "" { if !relativePath.Valid || relativePath.String == "" {
return "" return ""
@@ -24,5 +26,10 @@ func ResolveMediaURL(libraryID pgtype.UUID, relativePath pgtype.Text) string {
} }
libraryIDStr := uuid.UUID(libraryID.Bytes).String() libraryIDStr := uuid.UUID(libraryID.Bytes).String()
return fmt.Sprintf("/uploads/library-%s/%s", libraryIDStr, relativePath.String) segments := strings.Split(relativePath.String, "/")
for i, seg := range segments {
segments[i] = url.PathEscape(seg)
}
encodedPath := strings.Join(segments, "/")
return fmt.Sprintf("/uploads/library-%s/%s", libraryIDStr, encodedPath)
} }