proxy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package pages
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "image"
  9. _ "image/jpeg" // register JPEG decoder
  10. // xxx: if "panic: image: unknown format", register more formats may help
  11. core_http "codeberg.org/vnpower/pixivfe/v2/core/http"
  12. "codeberg.org/vnpower/pixivfe/v2/core/zip"
  13. "github.com/gofiber/fiber/v2"
  14. "github.com/kettek/apng"
  15. "github.com/tidwall/gjson"
  16. )
  17. func SPximgProxy(c *fiber.Ctx) error {
  18. URL := fmt.Sprintf("https://s.pximg.net/%s", c.Params("*"))
  19. req, err := http.NewRequest("GET", URL, nil)
  20. if err != nil {
  21. return err
  22. }
  23. req = req.WithContext(c.Context())
  24. // Make the request
  25. resp, err := http.DefaultClient.Do(req)
  26. if err != nil {
  27. return err
  28. }
  29. body, err := io.ReadAll(resp.Body)
  30. if err != nil {
  31. return err
  32. }
  33. c.Set("Content-Type", resp.Header.Get("Content-Type"))
  34. return c.Send([]byte(body))
  35. }
  36. func IPximgProxy(c *fiber.Ctx) error {
  37. URL := fmt.Sprintf("https://i.pximg.net/%s", c.Params("*"))
  38. req, err := http.NewRequest("GET", URL, nil)
  39. if err != nil {
  40. return err
  41. }
  42. req = req.WithContext(c.Context())
  43. req.Header.Add("Referer", "https://www.pixiv.net/")
  44. // Make the request
  45. resp, err := http.DefaultClient.Do(req)
  46. if err != nil {
  47. return err
  48. }
  49. body, err := io.ReadAll(resp.Body)
  50. if err != nil {
  51. return err
  52. }
  53. c.Set("Content-Type", resp.Header.Get("Content-Type"))
  54. return c.Send([]byte(body))
  55. }
  56. // relies on ugoira.com
  57. func UgoiraProxy_mp4(c *fiber.Ctx) error {
  58. URL := fmt.Sprintf("https://ugoira.com/api/mp4/%s", c.Params("*"))
  59. req, err := http.NewRequestWithContext(c.Context(), "GET", URL, nil)
  60. if err != nil {
  61. return err
  62. }
  63. req = req.WithContext(c.Context())
  64. // Make the request
  65. resp, err := http.DefaultClient.Do(req)
  66. if err != nil {
  67. return err
  68. }
  69. body, err := io.ReadAll(resp.Body)
  70. if err != nil {
  71. return err
  72. }
  73. c.Set("Content-Type", resp.Header.Get("Content-Type"))
  74. return c.Send([]byte(body))
  75. // todo:
  76. // delay... where do you get the delay?
  77. }
  78. func UgoiraProxy_apng(c *fiber.Ctx) error {
  79. id := c.Params("id")
  80. if id == "" {
  81. return errors.New("No ID provided.")
  82. }
  83. URL_meta := core_http.GetUgoiraMetaURL(id)
  84. ugoira_metadata, err := core_http.UnwrapWebAPIRequest(c.Context(), URL_meta, "")
  85. if err != nil {
  86. return err
  87. }
  88. delays := []uint16{}
  89. for _, o := range gjson.Get(ugoira_metadata, "frames").Array() {
  90. delays = append(delays, uint16(o.Get("delay").Int()))
  91. }
  92. URL_zip := gjson.Get(ugoira_metadata, "originalSrc").Str
  93. req, err := http.NewRequestWithContext(c.Context(), "GET", URL_zip, nil)
  94. if err != nil {
  95. return err
  96. }
  97. req.Header.Set("Referer", "https://www.pixiv.net/")
  98. resp, err := http.DefaultClient.Do(req)
  99. if err != nil {
  100. return err
  101. }
  102. body, err := io.ReadAll(resp.Body)
  103. if err != nil {
  104. return fmt.Errorf("while reading respose body: %w", err)
  105. }
  106. if resp.StatusCode != 200 {
  107. return errors.New(string(body))
  108. }
  109. r := bytes.NewReader(body)
  110. c.Set("Content-Type", "image/apng")
  111. return zip2apng(r, c, delays)
  112. }
  113. // each frame will be delayed $delayNumerator/delayDenominator$ seconds
  114. func zip2apng(reader_zip io.ReadSeeker, writer_apng io.Writer, delays []uint16) error {
  115. img_apng := apng.APNG{}
  116. i := 0
  117. for {
  118. file, err := zip.ReadFile(reader_zip)
  119. if err == zip.ErrFormat {
  120. break
  121. }
  122. if err != nil {
  123. return err
  124. }
  125. fmt.Println("len", len(file.Content))
  126. img, img_format, err := image.Decode(bytes.NewReader(file.Content))
  127. _ = img_format
  128. if err != nil {
  129. return err
  130. }
  131. frame := apng.Frame{
  132. Image: img,
  133. DelayNumerator: delays[i],
  134. DelayDenominator: 1000,
  135. }
  136. i += 1
  137. img_apng.Frames = append(img_apng.Frames, frame)
  138. }
  139. return apng.Encode(writer_apng, img_apng)
  140. }