artworkMulti.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package routes
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "sync"
  7. "codeberg.org/vnpower/pixivfe/v2/core"
  8. "net/http"
  9. )
  10. func ArtworkMultiPage(w http.ResponseWriter, r *http.Request) error {
  11. ids_ := GetPathVar(r, "ids")
  12. ids := strings.Split(ids_, ",")
  13. artworks := make([]core.Illust, len(ids))
  14. wg := sync.WaitGroup{}
  15. // // gofiber/fasthttp's API is trash
  16. // // i can't replace r.Context() with this
  17. // // so i guess we will have to wait for network traffic to finish on error
  18. // ctx, cancel := context.WithCancel(r.Context())
  19. // defer cancel()
  20. // r.SetUserContext(ctx)
  21. var err_global error = nil
  22. for i, id := range ids {
  23. if _, err := strconv.Atoi(id); err != nil {
  24. err_global = fmt.Errorf("Invalid ID: %s", id)
  25. break
  26. }
  27. wg.Add(1)
  28. go func(i int, id string) {
  29. defer wg.Done()
  30. illust, err := core.GetArtworkByID(r, id, false)
  31. if err != nil {
  32. artworks[i] = core.Illust{
  33. Title: err.Error(), // this might be flaky
  34. }
  35. return
  36. }
  37. metaDescription := ""
  38. for _, i := range illust.Tags {
  39. metaDescription += "#" + i.Name + ", "
  40. }
  41. artworks[i] = *illust
  42. }(i, id)
  43. }
  44. // if err_global != nil {
  45. // cancel()
  46. // }
  47. wg.Wait()
  48. if err_global != nil {
  49. return err_global
  50. }
  51. for _, illust := range artworks {
  52. for _, img := range illust.Images {
  53. PreloadImage(w, img.Large)
  54. }
  55. }
  56. return Render(w, r, Data_artworkMulti{
  57. Artworks: artworks,
  58. Title: fmt.Sprintf("(%d images)", len(artworks)),
  59. })
  60. }