From 3fd77e10d68f80bb61a12d9d258780799befba7e Mon Sep 17 00:00:00 2001 From: iacore Date: Sat, 17 Aug 2024 03:22:02 +0000 Subject: [PATCH] preload artwork images on /artworks/* cache http headers what the cache --- main.go | 4 ++++ pages/artwork.go | 6 ++++- pages/utils.go | 11 +++++++++ serve/preload.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test.go | 1 + 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 pages/utils.go create mode 100644 serve/preload.go diff --git a/main.go b/main.go index 271f070..2c0806c 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,7 @@ func CanRequestSkipLimiter(c *fiber.Ctx) bool { } func CanRequestSkipLogger(c *fiber.Ctx) bool { + // return false path := c.Path() return CanRequestSkipLimiter(c) || strings.HasPrefix(path, "/proxy/i.pximg.net/") @@ -189,6 +190,7 @@ func main() { }, Expiration: 5 * time.Minute, CacheControl: true, + StoreResponseHeaders: true, KeyGenerator: func(c *fiber.Ctx) string { key := utils.CopyString(c.OriginalURL()) @@ -207,6 +209,8 @@ func main() { )) } + // redirect any round with ?r=url + // could this be unsafe with cross-site scripting? server.Use(func(c *fiber.Ctx) error { ret := c.Query("r") if ret != "" { diff --git a/pages/artwork.go b/pages/artwork.go index 6970fe8..b09bcae 100644 --- a/pages/artwork.go +++ b/pages/artwork.go @@ -24,7 +24,11 @@ func ArtworkPage(c *fiber.Ctx) error { metaDescription += "#" + i.Name + ", " } - // todo: passing ArtWorkData{} here will not work. maybe lowercase? + // monkey patching. assuming illust.Images[_].Large is used + for _, img := range illust.Images { + PreloadImage(c, img.Large) + } + return c.Render("artwork", fiber.Map{ "Illust": illust, "Title": illust.Title, diff --git a/pages/utils.go b/pages/utils.go new file mode 100644 index 0000000..53b7e02 --- /dev/null +++ b/pages/utils.go @@ -0,0 +1,11 @@ +package pages + +import ( + "fmt" + + "github.com/gofiber/fiber/v2" +) + +func PreloadImage(c *fiber.Ctx, url string) { + c.Response().Header.Add("Link", fmt.Sprintf("<%s>; rel=preload; as=image", url)) +} diff --git a/serve/preload.go b/serve/preload.go new file mode 100644 index 0000000..c290d4f --- /dev/null +++ b/serve/preload.go @@ -0,0 +1,60 @@ +package serve + +import ( + "bytes" + "io" + "log" + "strings" + + "github.com/gofiber/fiber/v2" + "golang.org/x/net/html" +) + +// not working as middleware for some reason + +func Rewrite_Link_preload(c *fiber.Ctx) error { + err := c.Next() + if err != nil { + return err + } + + contentType := string(c.Response().Header.ContentType()) + // log.Print("ct ", contentType) + if contentType == "text/html; charset=utf-8" { + images_to_preload := []string{} + + // here, the response body is not valid + // no idea how to get c.Render to work here. + // reading gofiber's code, c.Render uses bytebufferpool + // i fail to understand how that makes sense + z := html.NewTokenizer(bytes.NewReader(c.Response().Body())) + scan_tokens: + for { + token_type := z.Next() + switch token_type { + case html.ErrorToken: + err := z.Err() + if err == io.EOF { + break scan_tokens + } + return err + case html.StartTagToken, html.SelfClosingTagToken, html.EndTagToken: + tag_name, hasAttr := z.TagName() + log.Print("tag name ", string(tag_name)) + if string(tag_name) == "img" && hasAttr { + for hasAttr { + var key, val []byte + key, val, hasAttr = z.TagAttr() + log.Print("key val ", string(key), string(val)) + if string(key) == "src" && strings.HasPrefix(string(val), "/proxy/i.pximg.net/img-master/") { + images_to_preload = append(images_to_preload, string(val)) + } + } + } + } + } + log.Print("preload ", images_to_preload) + } + + return nil +} diff --git a/tests/test.go b/tests/test.go index e69de29..7fdc98f 100644 --- a/tests/test.go +++ b/tests/test.go @@ -0,0 +1 @@ +package tests \ No newline at end of file