helpers.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package models
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. func ProxyImage(URL string, target string) string {
  7. if strings.Contains(URL, "s.pximg.net") {
  8. // This subdomain didn't get proxied
  9. return URL
  10. }
  11. regex := regexp.MustCompile(`.*?pximg\.net`)
  12. proxy := "https://" + target
  13. return regex.ReplaceAllString(URL, proxy)
  14. }
  15. func ProxyShortArtworkSlice(artworks []IllustShort, proxy string) []IllustShort {
  16. for i := range artworks {
  17. artworks[i].Thumbnail = ProxyImage(artworks[i].Thumbnail, proxy)
  18. artworks[i].ArtistAvatar = ProxyImage(artworks[i].ArtistAvatar, proxy)
  19. }
  20. return artworks
  21. }
  22. func ProxyRecommendedByTagsSlice(artworks []LandingRecommendByTags, proxy string) []LandingRecommendByTags {
  23. for i := range artworks {
  24. artworks[i].Artworks = ProxyShortArtworkSlice(artworks[i].Artworks, proxy)
  25. }
  26. return artworks
  27. }
  28. func ProxyRankedArtworkSlice(artworks []RankedArtwork, proxy string) []RankedArtwork {
  29. for i := range artworks {
  30. artworks[i].Image = ProxyImage(artworks[i].Image, proxy)
  31. artworks[i].ArtistAvatar = ProxyImage(artworks[i].ArtistAvatar, proxy)
  32. }
  33. return artworks
  34. }
  35. func ProxyCommentsSlice(comments []Comment, proxy string) []Comment {
  36. for i := range comments {
  37. comments[i].Avatar = ProxyImage(comments[i].Avatar, proxy)
  38. }
  39. return comments
  40. }
  41. func ProxyPixivisionSlice(articles []Pixivision, proxy string) []Pixivision {
  42. for i := range articles {
  43. articles[i].Thumbnail = ProxyImage(articles[i].Thumbnail, proxy)
  44. }
  45. return articles
  46. }