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.
36 lines
918 B
Go
36 lines
918 B
Go
// Package utils provides helper functions for URL resolution, ISBN handling, and tag management.
|
|
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
// ResolveMediaURL resolves a relative path (cover or file) to a full URL
|
|
func ResolveMediaURL(libraryID pgtype.UUID, relativePath pgtype.Text) string {
|
|
if !relativePath.Valid || relativePath.String == "" {
|
|
return ""
|
|
}
|
|
|
|
if strings.HasPrefix(relativePath.String, "/uploads/") {
|
|
return relativePath.String
|
|
}
|
|
|
|
if filepath.IsAbs(relativePath.String) {
|
|
return relativePath.String
|
|
}
|
|
|
|
libraryIDStr := uuid.UUID(libraryID.Bytes).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)
|
|
}
|