webp_server_go/helper/metadata.go
BugFest 4003b03022
Multiple backends support (#207)
* Fix: h2non/filetype upgraded to support avif signatures

* Fix: make clean updated to include test/output dirs

* Feature: multi-backend support via IMG_MAP config key as described in #217

* feat: implement both local and remote (proxyMode) mappings for multi-backend

* Feature: multi-backend support via IMG_MAP config key as described in #217

* fix: go-is-svg should be direct import

* fix: imgMap paths are relative to CWD

* feature: IMG_MAP is parsed on start

---------

Co-authored-by: Nova Kwok <n0vad3v@riseup.net>
2023-08-02 23:33:54 +08:00

71 lines
1.9 KiB
Go

package helper
import (
"encoding/json"
"net/url"
"os"
"path"
"webp_server_go/config"
log "github.com/sirupsen/logrus"
)
func getId(p string) (string, string, string) {
var id string
if config.ProxyMode {
return HashString(p), "", ""
}
parsed, _ := url.Parse(p)
width := parsed.Query().Get("width")
height := parsed.Query().Get("height")
// santizedPath will be /webp_server.jpg?width=200\u0026height= in local mode when requesting /webp_server.jpg?width=200
// santizedPath will be https://docs.webp.sh/images/webp_server.jpg?width=400 in proxy mode when requesting /images/webp_server.jpg?width=400 with IMG_PATH = https://docs.webp.sh
santizedPath := parsed.Path + "?width=" + width + "&height=" + height
id = HashString(santizedPath)
return id, path.Join(config.Config.ImgPath, parsed.Path), santizedPath
}
func ReadMetadata(p, etag string, subdir string) config.MetaFile {
// try to read metadata, if we can't read, create one
var metadata config.MetaFile
var id, _, _ = getId(p)
buf, err := os.ReadFile(path.Join(config.Metadata, subdir, id+".json"))
if err != nil {
log.Warnf("can't read metadata: %s", err)
WriteMetadata(p, etag, subdir)
return ReadMetadata(p, etag, subdir)
}
err = json.Unmarshal(buf, &metadata)
if err != nil {
log.Warnf("unmarshal metadata error, possible corrupt file, re-building...: %s", err)
WriteMetadata(p, etag, subdir)
return ReadMetadata(p, etag, subdir)
}
return metadata
}
func WriteMetadata(p, etag string, subdir string) config.MetaFile {
_ = os.MkdirAll(path.Join(config.Metadata, subdir), 0755)
var id, filepath, sant = getId(p)
var data = config.MetaFile{
Id: id,
}
if config.ProxyMode {
data.Path = p
data.Checksum = HashString(etag)
} else {
data.Path = sant
data.Checksum = HashFile(filepath)
}
buf, _ := json.Marshal(data)
_ = os.WriteFile(path.Join(config.Metadata, subdir, data.Id+".json"), buf, 0644)
return data
}