artwork-multi.go 979 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. wg.Add(len(ids))
  16. for i, id := range ids {
  17. if _, err := strconv.Atoi(id); err != nil {
  18. return fmt.Errorf("Invalid ID: %s", id)
  19. }
  20. go func(i int, id string) {
  21. defer wg.Done()
  22. illust, err := core.GetArtworkByID(c, id, false)
  23. if err != nil {
  24. artworks[i] = &core.Illust{
  25. Title: err.Error(), // this might be flaky
  26. }
  27. return
  28. }
  29. metaDescription := ""
  30. for _, i := range illust.Tags {
  31. metaDescription += "#" + i.Name + ", "
  32. }
  33. artworks[i] = illust
  34. }(i, id)
  35. }
  36. wg.Wait()
  37. return c.Render("pages/artwork-multi", fiber.Map{
  38. "Artworks": artworks,
  39. "Title": fmt.Sprintf("(%d images)", len(artworks)),
  40. })
  41. }