mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-12-05 05:43:36 -05:00
fix(webdav): hash the source cache key like the thumbnail key
This commit is contained in:
1 parent
d27aaa403f
commit
081a0a71ec
2 files changed
+36
-28
No files matched your search
@@ -123,34 +123,23 @@ func fileIsProcessing(info *providerv1beta1.ResourceInfo) bool {
|
||||
return utils.ReadPlainFromOpaque(info.GetOpaque(), "status") == "processing"
|
||||
}
|
||||
|
||||
// thumbnailStorageKey builds the cache key: hash[:2]/hash[2:4]/hash[4:]/wxh-<operation>.format.
|
||||
// The <operation> segment (fit-in, fill, stretch, ...) distinguishes requests that
|
||||
// share a size but produce different images, so they never share a cache entry.
|
||||
// The key carries the snapped box, not the requested size, so every request that
|
||||
// rounds to the same box shares one entry. Caches regenerate when the configured
|
||||
// resolutions or operations change.
|
||||
//
|
||||
// The checksum is hashed rather than used as it comes: it is not this service's
|
||||
// to trust, an OCM share takes it from a remote PROPFIND, and it ends up as a
|
||||
// path on disk and as an object key. The source type is hashed with it, because
|
||||
// the same bytes under two types are not the same thumbnail: the type picks the
|
||||
// extractor, and what it finds depends on it.
|
||||
// thumbnailStorageKey is <prefix>/wxh-<operation>.format, with the snapped box as size.
|
||||
func thumbnailStorageKey(checksum, mimeType string, w, h int, format, operation string) string {
|
||||
return storageKeyPrefix(checksum, mimeType) + "/" + fmt.Sprintf("%dx%d-%s.%s", w, h, operation, format)
|
||||
}
|
||||
|
||||
// sourceStorageKey holds the extracted image for all sizes; empty means the file has none.
|
||||
func sourceStorageKey(checksum, mimeType string) string {
|
||||
return storageKeyPrefix(checksum, mimeType) + "/source"
|
||||
}
|
||||
|
||||
// storageKeyPrefix hashes the checksum because it is untrusted (OCM takes it from a
|
||||
// remote PROPFIND) and becomes a path; the type is included because it picks the extractor.
|
||||
func storageKeyPrefix(checksum, mimeType string) string {
|
||||
sum := sha256.Sum256([]byte(checksum + "\x00" + mimeType))
|
||||
hash := hex.EncodeToString(sum[:])
|
||||
|
||||
return hash[:2] + "/" + hash[2:4] + "/" + hash[4:] + "/" + fmt.Sprintf("%dx%d-%s.%s", w, h, operation, format)
|
||||
}
|
||||
|
||||
// sourceStorageKey is where the image an extractor made of a file is
|
||||
// remembered. What an extractor finds does not depend on the size that was
|
||||
// asked for, so this one entry answers every request for the file, and an
|
||||
// empty entry says the file carries no image at all.
|
||||
func sourceStorageKey(checksum string) string {
|
||||
if len(checksum) >= 6 {
|
||||
return checksum[:2] + "/" + checksum[2:4] + "/" + checksum[4:] + "/source"
|
||||
}
|
||||
return checksum + "/source"
|
||||
return hash[:2] + "/" + hash[2:4] + "/" + hash[4:]
|
||||
}
|
||||
|
||||
// ThumbnailWorkflow owns the complete thumbnail generation pipeline:
|
||||
@@ -564,7 +553,7 @@ func (w *ThumbnailWorkflow) sourceImage(ctx context.Context, ref *providerv1beta
|
||||
// what the extraction found is remembered per file, so a second size does
|
||||
// not send the file to Tika again, and a file that carries nothing is only
|
||||
// asked once. The answer is read before the file is downloaded at all.
|
||||
sourceKey := sourceStorageKey(checksum)
|
||||
sourceKey := sourceStorageKey(checksum, mimeType)
|
||||
if converter != nil && w.cache != nil {
|
||||
if cached, err := w.cache.Get(sourceKey); err == nil {
|
||||
if len(cached) == 0 {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -845,7 +846,7 @@ var _ = Describe("ThumbnailWorkflow", func() {
|
||||
_, err = wf2.Execute(context.Background(), tr, testToken, logger)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
cached, err := testCache.Get("1f/0c/9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b/source")
|
||||
cached, err := testCache.Get(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(cached).To(Equal(captured.data), "the cover art is kept as the source, not per size")
|
||||
})
|
||||
@@ -853,7 +854,7 @@ var _ = Describe("ThumbnailWorkflow", func() {
|
||||
It("sends the kept source instead of extracting a second time", func() {
|
||||
pngBytes, err := os.ReadFile(filepath.Join("..", "..", "preprocessor", "test_assets", "noise.png"))
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(testCache.Put("1f/0c/9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b/source", pngBytes)).To(Succeed())
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg"), pngBytes)).To(Succeed())
|
||||
|
||||
//the file itself is not an mp3: extracting it would fail, so what
|
||||
//reaches the generator can only come from the cache
|
||||
@@ -872,7 +873,7 @@ var _ = Describe("ThumbnailWorkflow", func() {
|
||||
})
|
||||
|
||||
It("asks once for a file that carries nothing, whatever size follows", func() {
|
||||
Expect(testCache.Put("1f/0c/9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b/source", nil)).To(Succeed())
|
||||
Expect(testCache.Put(sourceStorageKey("1f0c9f1b6e4b4b2a9c1d7f3e8a5b6c7d8e9f0a1b", "audio/mpeg"), nil)).To(Succeed())
|
||||
|
||||
wf2, captured := setupPreprocessTest(GinkgoT(), gatewayClient, logger, testCache, "audio/mpeg", "song.mp3", []byte("not an mp3"))
|
||||
|
||||
@@ -1731,3 +1732,21 @@ var _ = Describe("postToGenerator", func() {
|
||||
Expect(partDisposition).To(Equal(`form-data; name="image"; filename="we\"ird.png"`))
|
||||
})
|
||||
})
|
||||
|
||||
var _ = Describe("storage keys", func() {
|
||||
It("keeps a checksum from leaving the cache directory", func() {
|
||||
key := sourceStorageKey("../../../../../tmp/x", "audio/mpeg")
|
||||
Expect(key).To(MatchRegexp(`^[0-9a-f]{2}/[0-9a-f]{2}/[0-9a-f]{60}/source$`))
|
||||
})
|
||||
|
||||
It("gives the same bytes under two types two source entries", func() {
|
||||
Expect(sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "application/pdf")).
|
||||
ToNot(Equal(sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "text/plain")))
|
||||
})
|
||||
|
||||
It("files a thumbnail and its source under one prefix", func() {
|
||||
source := sourceStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "image/x-nikon-nef")
|
||||
thumb := thumbnailStorageKey("cb56752477cae6405f85b131872c60d21b967c6a", "image/x-nikon-nef", 64, 64, "jpg", "fill")
|
||||
Expect(path.Dir(thumb)).To(Equal(path.Dir(source)))
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user