diff --git a/assets/views/tag.jet.html b/assets/views/tag.jet.html index 75c6eaf..18a03bd 100644 --- a/assets/views/tag.jet.html +++ b/assets/views/tag.jet.html @@ -34,7 +34,12 @@
- {{ .Tag.Name }} + {{ .Tag.Name }}
diff --git a/core/tag.go b/core/tag.go index 26cd1dc..9a35d18 100644 --- a/core/tag.go +++ b/core/tag.go @@ -2,6 +2,8 @@ package core import ( "net/http" + "net/url" + "regexp" "strings" "codeberg.org/vnpower/pixivfe/v2/audit" @@ -13,10 +15,11 @@ type TagDetail struct { Name string `json:"tag"` AlternativeName string `json:"word"` Metadata struct { - Detail string `json:"abstract"` - Image string `json:"image"` - Name string `json:"tag"` - ID json.Number `json:"id"` + Detail string `json:"abstract"` + Image string `json:"image"` // The original image URL as returned by the API + ImageMaster string // The higher resolution version obtained by transformImageURL + Name string `json:"tag"` + ID json.Number `json:"id"` } `json:"pixpedia"` } @@ -78,6 +81,32 @@ func (s SearchPageSettings) ReturnMap() map[string]string { } } +// transformImageURL replaces the "/c/{parameters}/" segment in the image URL with "/". +// +// Regex matching is used instead of simple substring replacement for flexibility. +func transformImageURL(auditor *audit.Auditor, inputURL string) (string, error) { + auditor.SugaredLogger.Debugf("transformImageURL provided with original URL: %s", inputURL) + + // Parse the URL to ensure it's valid and to manipulate its path + parsedURL, err := url.Parse(inputURL) + if err != nil { + auditor.SugaredLogger.Errorf("transformImageURL found invalid URL '%s': %v", inputURL, err) + return "", err + } + + // Define a regex to match the "/c/{parameters}/" segment and replace it with "/" + re := regexp.MustCompile(`/c/[^/]+/`) + updatedPath := re.ReplaceAllString(parsedURL.Path, "/") + + // Update the path in the parsed URL and reconstruct the full URL + parsedURL.Path = updatedPath + updatedURL := parsedURL.String() + + auditor.SugaredLogger.Debugf("transformImageURL returned transformed URL: %s", updatedURL) + + return updatedURL, nil +} + func GetTagData(auditor *audit.Auditor, r *http.Request, name string) (TagDetail, error) { var tag TagDetail @@ -95,6 +124,16 @@ func GetTagData(auditor *audit.Auditor, r *http.Request, name string) (TagDetail return tag, err } + auditor.SugaredLogger.Debugf("testesetsetsetse: %s", tag.Metadata.Image) + + // Call transformImageURL to obtain a higher resolution thumbnail + originalImageURL := tag.Metadata.Image + masterURL, err := transformImageURL(auditor, originalImageURL) + if err != nil { + return tag, err + } + tag.Metadata.ImageMaster = masterURL + return tag, nil } diff --git a/server/routes/tag.go b/server/routes/tag.go index 919a5ed..39869bc 100644 --- a/server/routes/tag.go +++ b/server/routes/tag.go @@ -70,6 +70,9 @@ func TagPage(auditor *audit.Auditor, w http.ResponseWriter, r *http.Request) err ActiveSearchMode: GetQueryParam(r, "smode", ""), } + PreloadImage(w, tag.Metadata.Image) + PreloadImage(w, tag.Metadata.ImageMaster) + return RenderHTML(w, r, data) }