mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
Feature: validator for setting forms
This commit is contained in:
@@ -40,6 +40,10 @@ func setup_router() *fiber.App {
|
||||
server.Use(logger.New())
|
||||
server.Use(cache.New(
|
||||
cache.Config{
|
||||
Next: func(c *fiber.Ctx) bool {
|
||||
// Disable cache for settings page
|
||||
return strings.Contains(c.Path(), "/settings")
|
||||
},
|
||||
Expiration: 5 * time.Minute,
|
||||
|
||||
KeyGenerator: func(c *fiber.Ctx) string {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ProxyImage(URL string, target string) string {
|
||||
|
||||
if strings.Contains(URL, "s.pximg.net") {
|
||||
// This subdomain didn't get proxied
|
||||
return URL
|
||||
}
|
||||
|
||||
regex := regexp.MustCompile(`.*?pximg\.net`)
|
||||
proxy := "https://" + target
|
||||
|
||||
return regex.ReplaceAllString(URL, proxy)
|
||||
}
|
||||
|
||||
func ProxyShortArtworkSlice(artworks []IllustShort, proxy string) []IllustShort {
|
||||
for i := range artworks {
|
||||
artworks[i].Thumbnail = ProxyImage(artworks[i].Thumbnail, proxy)
|
||||
artworks[i].ArtistAvatar = ProxyImage(artworks[i].ArtistAvatar, proxy)
|
||||
}
|
||||
|
||||
return artworks
|
||||
}
|
||||
|
||||
func ProxyCommentsSlice(comments []Comment, proxy string) []Comment {
|
||||
for i := range comments {
|
||||
comments[i].Avatar = ProxyImage(comments[i].Avatar, proxy)
|
||||
}
|
||||
return comments
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
<div class="container">
|
||||
<h2>Settings</h2>
|
||||
{{ if isset(Message) }}
|
||||
{{ Message }}
|
||||
{{ end }}
|
||||
<form action="/settings/token" method="post">
|
||||
<fieldset class="settings-fieldset">
|
||||
<legend>Custom token</legend>
|
||||
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
"net/http"
|
||||
"pixivfe/configs"
|
||||
"pixivfe/models"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func get_session_value(c *fiber.Ctx, key string) *string {
|
||||
sess, err := configs.Store.Get(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
value := sess.Get(key)
|
||||
if value != nil {
|
||||
placeholder := value.(string)
|
||||
return &placeholder
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func artwork_page(c *fiber.Ctx) error {
|
||||
image_proxy := get_session_value(c, "image-proxy")
|
||||
if image_proxy == nil {
|
||||
image_proxy = &configs.ProxyServer
|
||||
}
|
||||
|
||||
id := c.Params("id")
|
||||
if _, err := strconv.Atoi(id); err != nil {
|
||||
return errors.New("Bad id")
|
||||
}
|
||||
|
||||
illust, err := PC.GetArtworkByID(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
illust.ProxyImages(*image_proxy)
|
||||
|
||||
related, err := PC.GetRelatedArtworks(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
related = models.ProxyShortArtworkSlice(related, *image_proxy)
|
||||
|
||||
comments, _ := PC.GetArtworkComments(id)
|
||||
comments = models.ProxyCommentsSlice(comments, *image_proxy)
|
||||
|
||||
// Optimize this
|
||||
return c.Render("artwork", fiber.Map{
|
||||
"Illust": illust,
|
||||
"Related": related,
|
||||
"Comments": comments,
|
||||
"Title": illust.Title,
|
||||
})
|
||||
}
|
||||
|
||||
func index_page(c *fiber.Ctx) error {
|
||||
// recommended, _ := handler.GetRecommendedIllust(c)
|
||||
// ranking, _ := handler.GetRankingIllust(c, "day")
|
||||
// spotlight := handler.GetSpotlightArticle(c)
|
||||
// newest, _ := handler.GetNewestIllust(c)
|
||||
// return c.Render(http.StatusOK, "index.html", fiber.Map{
|
||||
// "Recommended": recommended,
|
||||
// "Rankings": ranking,
|
||||
// "Spotlights": spotlight,
|
||||
// "Newest": newest,
|
||||
// })
|
||||
sess, err := configs.Store.Get(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
token := sess.Get("token")
|
||||
|
||||
if token != nil {
|
||||
println(token.(string))
|
||||
}
|
||||
|
||||
return c.Render("temp", fiber.Map{"Title": "Landing"})
|
||||
}
|
||||
|
||||
func user_page(c *fiber.Ctx) error {
|
||||
image_proxy := get_session_value(c, "image-proxy")
|
||||
if image_proxy == nil {
|
||||
image_proxy = &configs.ProxyServer
|
||||
}
|
||||
|
||||
id := c.Params("id")
|
||||
if _, err := strconv.Atoi(id); err != nil {
|
||||
return err
|
||||
}
|
||||
category := c.Params("category", "artworks")
|
||||
if !(category == "artworks" || category == "illustrations" || category == "manga" || category == "bookmarks") {
|
||||
return errors.New("Invalid work category: only illustrations, manga, artworks and bookmarks are available")
|
||||
}
|
||||
|
||||
page := c.Query("page", "1")
|
||||
pageInt, _ := strconv.Atoi(page)
|
||||
|
||||
user, err := PC.GetUserInformation(id, category, pageInt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user.ProxyImages(*image_proxy)
|
||||
|
||||
var worksCount int
|
||||
|
||||
worksCount = user.ArtworksCount
|
||||
pageLimit := math.Ceil(float64(worksCount)/30.0) + 1.0
|
||||
|
||||
return c.Render("user", fiber.Map{"Title": user.Name, "User": user, "Category": category, "PageLimit": int(pageLimit), "Page": pageInt})
|
||||
}
|
||||
|
||||
func ranking_page(c *fiber.Ctx) error {
|
||||
image_proxy := get_session_value(c, "image-proxy")
|
||||
if image_proxy == nil {
|
||||
image_proxy = &configs.ProxyServer
|
||||
}
|
||||
|
||||
mode := c.Query("mode", "daily")
|
||||
|
||||
content := c.Query("content", "all")
|
||||
|
||||
page := c.Query("page", "1")
|
||||
|
||||
pageInt, _ := strconv.Atoi(page)
|
||||
|
||||
response, err := PC.GetRanking(mode, content, page)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
artworks := response.Artworks
|
||||
|
||||
for i := range artworks {
|
||||
artworks[i].Image = models.ProxyImage(artworks[i].Image, *image_proxy)
|
||||
artworks[i].ArtistAvatar = models.ProxyImage(artworks[i].ArtistAvatar, *image_proxy)
|
||||
}
|
||||
|
||||
return c.Render("rank", fiber.Map{
|
||||
"Title": "Ranking",
|
||||
"Items": artworks,
|
||||
"Mode": mode,
|
||||
"Content": content,
|
||||
"Page": pageInt})
|
||||
}
|
||||
|
||||
func newest_artworks_page(c *fiber.Ctx) error {
|
||||
image_proxy := get_session_value(c, "image-proxy")
|
||||
if image_proxy == nil {
|
||||
image_proxy = &configs.ProxyServer
|
||||
}
|
||||
|
||||
worktype := c.Query("type", "illust")
|
||||
|
||||
r18 := c.Query("r18", "false")
|
||||
|
||||
works, err := PC.GetNewestArtworks(worktype, r18)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
works = models.ProxyShortArtworkSlice(works, *image_proxy)
|
||||
|
||||
return c.Render("newest", fiber.Map{
|
||||
"Items": works,
|
||||
"Title": "Newest works",
|
||||
})
|
||||
}
|
||||
|
||||
func search_page(c *fiber.Ctx) error {
|
||||
image_proxy := get_session_value(c, "image-proxy")
|
||||
if image_proxy == nil {
|
||||
image_proxy = &configs.ProxyServer
|
||||
}
|
||||
|
||||
name := c.Params("name")
|
||||
|
||||
page := c.Query("page", "1")
|
||||
|
||||
order := c.Query("order", "date_d")
|
||||
|
||||
mode := c.Query("mode", "safe")
|
||||
|
||||
category := c.Query("category", "artworks")
|
||||
|
||||
tag, err := PC.GetTagData(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := PC.GetSearch(category, name, order, mode, page)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result.ProxyImages(*image_proxy)
|
||||
|
||||
queries := map[string]string{
|
||||
"Page": page,
|
||||
"Order": order,
|
||||
"Mode": mode,
|
||||
"Category": category,
|
||||
}
|
||||
return c.Render("tag", fiber.Map{"Title": "Results for " + tag.Name, "Tag": tag, "Data": result, "Queries": queries})
|
||||
}
|
||||
|
||||
func search(c *fiber.Ctx) error {
|
||||
name := c.FormValue("name")
|
||||
|
||||
return c.Redirect("/tags/"+name, http.StatusFound)
|
||||
}
|
||||
|
||||
func discovery_page(c *fiber.Ctx) error {
|
||||
image_proxy := get_session_value(c, "image-proxy")
|
||||
if image_proxy == nil {
|
||||
image_proxy = &configs.ProxyServer
|
||||
}
|
||||
|
||||
mode := c.Query("mode", "safe")
|
||||
|
||||
artworks, err := PC.GetDiscoveryArtwork(mode, 100)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
}
|
||||
artworks = models.ProxyShortArtworkSlice(artworks, *image_proxy)
|
||||
|
||||
return c.Render("discovery", fiber.Map{"Title": "Discovery", "Artworks": artworks})
|
||||
}
|
||||
|
||||
func settings_page(c *fiber.Ctx) error {
|
||||
return c.Render("settings", fiber.Map{})
|
||||
}
|
||||
|
||||
func settings_post(c *fiber.Ctx) error {
|
||||
t := c.Params("type")
|
||||
error := ""
|
||||
|
||||
if t == "image_server" {
|
||||
error = set_image_server(c)
|
||||
}
|
||||
|
||||
if error != "" {
|
||||
return errors.New(error)
|
||||
}
|
||||
c.Redirect("/settings")
|
||||
return nil
|
||||
}
|
||||
+1
-2
@@ -52,8 +52,7 @@ func SetupRoutes(r *fiber.App) {
|
||||
|
||||
settings := r.Group("settings")
|
||||
settings.Get("/", settings_page)
|
||||
settings.Post("/token", set_token)
|
||||
settings.Post("/image_server", set_image_server)
|
||||
settings.Post("/:type", settings_post)
|
||||
|
||||
// 404 page
|
||||
// r.NoRoute(not_found_page)
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package views
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"pixivfe/configs"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/session"
|
||||
)
|
||||
|
||||
func get_storage(c *fiber.Ctx) *session.Session {
|
||||
// Get the storage
|
||||
sess, err := configs.Store.Get(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return sess
|
||||
}
|
||||
|
||||
func save_storage(sess *session.Session) {
|
||||
if err := sess.Save(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func test_request(URL string) error {
|
||||
req, _ := http.NewRequest("GET", URL, nil)
|
||||
|
||||
transport := &http.Transport{Proxy: http.ProxyFromEnvironment}
|
||||
client := &http.Client{
|
||||
Timeout: time.Duration(5000) * time.Millisecond,
|
||||
Transport: transport,
|
||||
}
|
||||
|
||||
// Add headers
|
||||
// for k, v := range p.Header {
|
||||
// req.Header.Add(k, v)
|
||||
// }
|
||||
// for k, v := range p.Cookie {
|
||||
// req.AddCookie(&http.Cookie{Name: k, Value: v})
|
||||
// }
|
||||
// Make a request
|
||||
resp, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func set_token(c *fiber.Ctx) error {
|
||||
name := "token"
|
||||
sess := get_storage(c)
|
||||
|
||||
// Parse the value from the form
|
||||
token := c.FormValue(name)
|
||||
if token != "" {
|
||||
// If token is valid, save it
|
||||
sess.Set(name, token)
|
||||
|
||||
save_storage(sess)
|
||||
|
||||
return c.Redirect("/settings", http.StatusFound)
|
||||
}
|
||||
c.Redirect("/settings", http.StatusNoContent)
|
||||
return nil
|
||||
}
|
||||
|
||||
func set_image_server(c *fiber.Ctx) string {
|
||||
name := "image-proxy"
|
||||
sess := get_storage(c)
|
||||
|
||||
// Parse the value from the form
|
||||
token := c.FormValue(name)
|
||||
if token != "" {
|
||||
if err := test_request("https://" + token + "/img-original/img/2023/06/10/19/51/55/108894158_p0.jpg"); err != nil {
|
||||
return "Cannot get image from " + token
|
||||
}
|
||||
// If token is valid, save it
|
||||
sess.Set(name, token)
|
||||
|
||||
save_storage(sess)
|
||||
|
||||
return ""
|
||||
}
|
||||
return "Empty form"
|
||||
}
|
||||
Reference in New Issue
Block a user