2023-08-10 15:02:51 +00:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2023-08-10 16:00:42 +00:00
|
|
|
"strconv"
|
|
|
|
|
2023-08-10 15:02:51 +00:00
|
|
|
"codeberg.org/rimgo/rimgo/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func HandleSearch(c *fiber.Ctx) error {
|
|
|
|
utils.SetHeaders(c)
|
|
|
|
c.Set("X-Frame-Options", "DENY")
|
|
|
|
c.Set("Cache-Control", "public,max-age=604800")
|
2023-06-13 13:37:00 +00:00
|
|
|
c.Set("Content-Security-Policy", "default-src 'none'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'; style-src 'unsafe-inline' 'self'; media-src 'self' https://i.imgur.com https://i.stack.imgur.com; img-src 'self' https://i.imgur.com https://i.stack.imgur.com; manifest-src 'self'; block-all-mixed-content")
|
2023-08-10 15:02:51 +00:00
|
|
|
|
|
|
|
query := c.Query("q")
|
|
|
|
|
|
|
|
if utils.ImgurRe.MatchString(query) {
|
|
|
|
return c.Redirect(utils.ImgurRe.ReplaceAllString(query, ""))
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:00:42 +00:00
|
|
|
page := "0"
|
2023-08-10 15:02:51 +00:00
|
|
|
if c.Query("page") != "" {
|
|
|
|
page = c.Query("page")
|
|
|
|
}
|
|
|
|
|
|
|
|
pageNumber, err := strconv.Atoi(c.Query("page"))
|
|
|
|
if err != nil {
|
|
|
|
pageNumber = 0
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:00:42 +00:00
|
|
|
results, err := ApiClient.Search(query, page)
|
2023-08-10 15:02:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render("search", fiber.Map{
|
2023-08-10 16:00:42 +00:00
|
|
|
"query": query,
|
|
|
|
"results": results,
|
2023-08-30 17:24:54 +00:00
|
|
|
"page": pageNumber,
|
2023-08-10 15:02:51 +00:00
|
|
|
"nextPage": pageNumber + 1,
|
2023-08-10 16:00:42 +00:00
|
|
|
"prevPage": pageNumber - 1,
|
2023-08-10 15:02:51 +00:00
|
|
|
})
|
|
|
|
}
|