preload artwork images on /artworks/*

cache http headers

what the cache
This commit is contained in:
iacore
2024-08-17 03:22:02 +00:00
parent 5a1e83cba7
commit 3fd77e10d6
5 changed files with 81 additions and 1 deletions
+4
View File
@@ -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 != "" {
+5 -1
View File
@@ -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,
+11
View File
@@ -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))
}
+60
View File
@@ -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
}
+1
View File
@@ -0,0 +1 @@
package tests