feat: caching headers
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
)
|
||||
|
||||
func UIDFromPath(path string) (uid string, err error) {
|
||||
var b bytes.Buffer
|
||||
// NewWriter with NoDict (nil) creates a raw DEFLATE compressor
|
||||
zw, _ := flate.NewWriter(&b, flate.BestCompression)
|
||||
|
||||
_, err = zw.Write([]byte(path))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = zw.Close() // Essential to flush the final bits
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return base64.RawURLEncoding.EncodeToString(b.Bytes()), nil
|
||||
}
|
||||
|
||||
func PathFromUID(uid string) (string, error) {
|
||||
// 1. Decode the Base64 string back to compressed bytes
|
||||
compressedBytes, err := base64.RawURLEncoding.DecodeString(uid)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 2. Create a flate reader (NoDict/nil since you used nil in the writer)
|
||||
// flate.NewReader returns an io.ReadCloser
|
||||
reader := flate.NewReader(bytes.NewReader(compressedBytes))
|
||||
defer reader.Close()
|
||||
|
||||
// 3. Read the decompressed data into a buffer
|
||||
var out bytes.Buffer
|
||||
_, err = io.Copy(&out, reader)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return out.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user