artwork.go 982 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package routes
  2. import (
  3. "fmt"
  4. "strconv"
  5. "codeberg.org/vnpower/pixivfe/v2/core"
  6. "net/http"
  7. )
  8. func ArtworkPage(w http.ResponseWriter, r *http.Request) error {
  9. id := GetPathVar(r, "id")
  10. if _, err := strconv.Atoi(id); err != nil {
  11. return fmt.Errorf("Invalid ID: %s", id)
  12. }
  13. illust, err := core.GetArtworkByID(r, id, true)
  14. if err != nil {
  15. return err
  16. }
  17. metaDescription := ""
  18. for _, i := range illust.Tags {
  19. metaDescription += "#" + i.Name + ", "
  20. }
  21. // monkey patching. assuming illust.Images[_].Large is used
  22. for _, img := range illust.Images {
  23. PreloadImage(w, img.Large)
  24. }
  25. return Render(w, r, Data_artwork{
  26. Illust: *illust,
  27. Title: illust.Title,
  28. MetaDescription: metaDescription,
  29. MetaImage: illust.Images[0].Original,
  30. MetaAuthor: illust.UserName,
  31. MetaAuthorID: illust.UserID,
  32. })
  33. }
  34. func PreloadImage(w http.ResponseWriter, url string) {
  35. w.Header().Add("Link", fmt.Sprintf("<%s>; rel=preload; as=image", url))
  36. }