artwork-multi.go 988 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package pages
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
  9. "github.com/gofiber/fiber/v2"
  10. )
  11. func ArtworkMultiPage(c *fiber.Ctx) error {
  12. param_ids := c.Params("ids")
  13. ids := strings.Split(param_ids, ",")
  14. artworks := make([]*core.Illust, len(ids))
  15. wg := sync.WaitGroup{}
  16. wg.Add(len(ids))
  17. for i, id := range ids {
  18. if _, err := strconv.Atoi(id); err != nil {
  19. return errors.New("invalid id")
  20. }
  21. go func(i int, id string) {
  22. defer wg.Done()
  23. illust, err := core.GetArtworkByID(c, id, false)
  24. if err != nil {
  25. artworks[i] = &core.Illust{
  26. Title: err.Error(), // this might be flaky
  27. }
  28. return
  29. }
  30. metaDescription := ""
  31. for _, i := range illust.Tags {
  32. metaDescription += "#" + i.Name + ", "
  33. }
  34. artworks[i] = illust
  35. }(i, id)
  36. }
  37. wg.Wait()
  38. return c.Render("pages/artwork-multi", fiber.Map{
  39. "Artworks": artworks,
  40. "Title": fmt.Sprintf("(%d images)", len(artworks)),
  41. })
  42. }