artwork-multi.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package pages
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "sync"
  7. core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
  8. "github.com/gofiber/fiber/v2"
  9. )
  10. func ArtworkMultiPage(c *fiber.Ctx) error {
  11. ids_ := c.Params("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 c.Context() with this
  17. // // so i guess we will have to wait for network traffic to finish on error
  18. // ctx, cancel := context.WithCancel(c.Context())
  19. // defer cancel()
  20. // c.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(c, 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. return c.Render("artwork-multi", fiber.Map{
  52. "Artworks": artworks,
  53. "Title": fmt.Sprintf("(%d images)", len(artworks)),
  54. })
  55. }