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")
|
|
|
|
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'; img-src 'self'; manifest-src 'self'; block-all-mixed-content")
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|