diff --git a/handler/client.go b/handler/client.go index 76e234e..92fc23a 100644 --- a/handler/client.go +++ b/handler/client.go @@ -6,9 +6,7 @@ import ( "fmt" "io/ioutil" "net/http" - "pixivfe/configs" "pixivfe/models" - "strings" ) type PixivClient struct { @@ -92,7 +90,7 @@ func (p *PixivClient) PixivRequest(URL string) (json.RawMessage, error) { var response models.PixivResponse body, err := p.TextRequest(URL) - body = strings.ReplaceAll(body, "i.pximg.net", configs.ProxyServer) + // body = strings.ReplaceAll(body, "i.pximg.net", configs.ProxyServer) if err != nil { return nil, err } diff --git a/handler/template.go b/handler/template.go index 08ff8ab..db73aef 100644 --- a/handler/template.go +++ b/handler/template.go @@ -6,6 +6,7 @@ import ( "math/rand" "regexp" "strconv" + "strings" ) func GetRandomColor() string { @@ -56,17 +57,16 @@ func GetTemplateFunctions() template.FuncMap { return n }, - "proxyImage": func(url string) string { - // if strings.Contains(url, "s.pximg.net") { - // // This subdomain didn't get proxied - // return url - // } + "proxyImage": func(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://" + configs.ProxyServer + regex := regexp.MustCompile(`.*?pximg\.net`) + proxy := "https://" + target - // return regex.ReplaceAllString(url, proxy) - return url + return regex.ReplaceAllString(url, proxy) }, "parseEmojis": func(s string) template.HTML { regex := regexp.MustCompile(`\(([^)]+)\)`) diff --git a/main.go b/main.go index 4c69b93..4ce6111 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,7 @@ import ( "github.com/gofiber/template/jet/v2" ) -func setupRouter() *fiber.App { +func setup_router() *fiber.App { // HTML templates, automatically loaded engine := jet.New("./template", ".jet.html") @@ -67,6 +67,32 @@ func setupRouter() *fiber.App { return c.Next() }) + // server.Use(func(c *fiber.Ctx) error { + // sess, err := configs.Store.Get(c) + // if err != nil { + // return err + // } + + // var token_string, image_string string + + // token := sess.Get("token") + // if token != nil { + // token_string = token.(string) + // } else { + // token_string = configs.ProxyServer + // } + + // image := sess.Get("image-proxy") + // if image != nil { + // image_string = image.(string) + // } + + // c.Bind(fiber.Map{ + // "Token": token_string, + // "ImageProxy": image_string, + // }) + // return c.Next() + // }) // Static files server.Static("/favicon.ico", "./template/favicon.ico") @@ -90,7 +116,7 @@ func main() { panic(err) } - r := setupRouter() + r := setup_router() if strings.Contains(configs.Port, "/") { ln, err := net.Listen("unix", configs.Port) diff --git a/models/models.go b/models/models.go index 1497029..c5523e4 100644 --- a/models/models.go +++ b/models/models.go @@ -114,6 +114,19 @@ type Illust struct { RecentWorks []IllustShort } +func (s *Illust) ProxyImages(proxy string) { + for i := range s.Images { + s.Images[i].Small = ProxyImage(s.Images[i].Small, proxy) + s.Images[i].Medium = ProxyImage(s.Images[i].Medium, proxy) + s.Images[i].Large = ProxyImage(s.Images[i].Large, proxy) + s.Images[i].Original = ProxyImage(s.Images[i].Original, proxy) + } + for i := range s.RecentWorks { + s.RecentWorks[i].Thumbnail = ProxyImage(s.RecentWorks[i].Thumbnail, proxy) + } + s.User.Avatar = ProxyImage(s.User.Avatar, proxy) +} + type IllustShort struct { ID string `json:"id"` Title string `json:"title"` @@ -150,6 +163,12 @@ type User struct { FrequentTags []FrequentTag } +func (s *User) ProxyImages(proxy string) { + s.Avatar = ProxyImage(s.Avatar, proxy) + s.BackgroundImage = ProxyImage(s.BackgroundImage, proxy) + s.Artworks = ProxyShortArtworkSlice(s.Artworks, proxy) +} + type UserShort struct { ID string `json:"userId"` Name string `json:"name"` @@ -188,3 +207,9 @@ type SearchResult struct { Popular PopularArtworks `json:"popular"` RelatedTags []string `json:"relatedTags"` } + +func (s *SearchResult) ProxyImages(proxy string) { + s.Artworks.Artworks = ProxyShortArtworkSlice(s.Artworks.Artworks, proxy) + s.Popular.Permanent = ProxyShortArtworkSlice(s.Popular.Permanent, proxy) + s.Popular.Recent = ProxyShortArtworkSlice(s.Popular.Recent, proxy) +} diff --git a/template/settings.jet.html b/template/settings.jet.html index 70d3b08..c245796 100644 --- a/template/settings.jet.html +++ b/template/settings.jet.html @@ -1,40 +1,44 @@

Settings

-
+
Custom token - + +
-
-
- Proxy server - - +
+ + Image proxy server + +
- - + +
- - + +
- - + +
- - + +
- - + +

- + + +
+
-
-
- + +
diff --git a/views/routes.go b/views/routes.go index 93ff830..1dc99b7 100644 --- a/views/routes.go +++ b/views/routes.go @@ -1,12 +1,9 @@ package views import ( - "errors" - "math" "net/http" "pixivfe/configs" "pixivfe/handler" - "strconv" "time" "github.com/gofiber/fiber/v2" @@ -14,189 +11,6 @@ import ( var PC *handler.PixivClient -func artwork_page(c *fiber.Ctx) error { - 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 - } - - related, err := PC.GetRelatedArtworks(id) - if err != nil { - return err - } - - comments, _ := PC.GetArtworkComments(id) - - // 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 { - 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 - } - - 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 { - 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 - } - - return c.Render("rank", fiber.Map{ - "Title": "Ranking", - "Items": response.Artworks, - "Mode": mode, - "Content": content, - "Page": pageInt}) -} - -func newest_artworks_page(c *fiber.Ctx) error { - worktype := c.Query("type", "illust") - - r18 := c.Query("r18", "false") - - works, err := PC.GetNewestArtworks(worktype, r18) - if err != nil { - return err - } - - return c.Render("newest", fiber.Map{ - "Items": works, - "Title": "Newest works", - }) -} - -func search_page(c *fiber.Ctx) error { - 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 - } - - 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 { - mode := c.Query("mode", "safe") - - artworks, err := PC.GetDiscoveryArtwork(mode, 100) - if err != nil { - return err - } - - 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_handler(c *fiber.Ctx) error { - sess, err := configs.Store.Get(c) - if err != nil { - panic(err) - } - - token := c.FormValue("token") - if token != "" { - sess.Set("token", token) - - if err := sess.Save(); err != nil { - panic(err) - } - return c.Redirect("/settings/", http.StatusAccepted) - } - return c.Redirect("/settings/", http.StatusNoContent) -} - // func not_found_page(c *fiber.Ctx) { // return c.Render(http.StatusNotFound, "error.html", fiber.Map{ // "Title": "Not found", @@ -236,8 +50,10 @@ func SetupRoutes(r *fiber.App) { r.Get("discovery", discovery_page) r.Post("tags", search) - r.Get("settings", settings_page) - r.Post("setting", settings_handler) + settings := r.Group("settings") + settings.Get("/", settings_page) + settings.Post("/token", set_token) + settings.Post("/image_server", set_image_server) // 404 page // r.NoRoute(not_found_page)