Shorten processed image filenames

Fixes #12688
Fixes #12656
This commit is contained in:
Bjørn Erik Pedersen
2024-07-30 16:47:16 +02:00
parent e67886c038
commit 216a69a1ef
111 changed files with 77 additions and 73 deletions

View File

@@ -490,36 +490,16 @@ func (i *imageResource) relTargetPathFromConfig(conf images.ImageConfig) interna
if conf.TargetFormat != i.Format {
p2 = conf.TargetFormat.DefaultExtension()
}
h := i.hash()
idStr := fmt.Sprintf("_hu%d_%d", h, i.size())
// Do not change for no good reason.
const md5Threshold = 100
key := conf.GetKey(i.Format)
// It is useful to have the key in clear text, but when nesting transforms, it
// can easily be too long to read, and maybe even too long
// for the different OSes to handle.
if len(p1)+len(idStr)+len(p2) > md5Threshold {
key = hashing.MD5FromStringHexEncoded(p1 + key + p2)
huIdx := strings.Index(p1, "_hu")
if huIdx != -1 {
p1 = p1[:huIdx]
} else {
// This started out as a very long file name. Making it even longer
// could melt ice in the Arctic.
p1 = ""
}
} else if strings.Contains(p1, idStr) {
// On scaling an already scaled image, we get the file info from the original.
// Repeating the same info in the filename makes it stuttery for no good reason.
idStr = ""
const prefix = "_hu"
huIdx := strings.LastIndex(p1, prefix)
incomingID := "i"
if huIdx > -1 {
incomingID = p1[huIdx+len(prefix):]
p1 = p1[:huIdx]
}
hash := hashing.HashUint64(incomingID, i.hash(), conf.GetKey(i.Format))
rp := i.getResourcePaths()
rp.File = fmt.Sprintf("%s%s_%s%s", p1, idStr, key, p2)
rp.File = fmt.Sprintf("%s%s%d%s", p1, prefix, hash, p2)
return rp
}