From 3dab4982d647d9619ad5d055908749b9942e5a64 Mon Sep 17 00:00:00 2001 From: niubility000 <76441520+niubility000@users.noreply.github.com> Date: Fri, 17 Dec 2021 09:51:59 +0800 Subject: [PATCH] use fullpath as a identifier --- http/preview.go | 16 ++++++++-------- http/resource.go | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/http/preview.go b/http/preview.go index 4f88ab3a..2c7262a3 100644 --- a/http/preview.go +++ b/http/preview.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "net/http" - "path/filepath" "github.com/gorilla/mux" "github.com/filebrowser/filebrowser/v2/files" @@ -61,7 +60,7 @@ func previewHandler(imgSvc ImgService, fileCache FileCache, enableThumbnails, re switch file.Type { case "image": - return handleImagePreview(w, r, imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview) + return handleImagePreview(w, r, d, imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview) default: return http.StatusNotImplemented, fmt.Errorf("can't create preview for %s type", file.Type) } @@ -71,6 +70,7 @@ func previewHandler(imgSvc ImgService, fileCache FileCache, enableThumbnails, re func handleImagePreview( w http.ResponseWriter, r *http.Request, + d *data, imgSvc ImgService, fileCache FileCache, file *files.FileInfo, @@ -91,13 +91,13 @@ func handleImagePreview( return errToStatus(err), err } - cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize) + cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize, d) resizedImage, ok, err := fileCache.Load(r.Context(), cacheKey) if err != nil { return errToStatus(err), err } if !ok { - resizedImage, err = createPreview(imgSvc, fileCache, file, previewSize) + resizedImage, err = createPreview(imgSvc, d, fileCache, file, previewSize) if err != nil { return errToStatus(err), err } @@ -109,7 +109,7 @@ func handleImagePreview( return 0, nil } -func createPreview(imgSvc ImgService, fileCache FileCache, +func createPreview(imgSvc ImgService, d *data, fileCache FileCache, file *files.FileInfo, previewSize PreviewSize) ([]byte, error) { fd, err := file.Fs.Open(file.Path) if err != nil { @@ -142,7 +142,7 @@ func createPreview(imgSvc ImgService, fileCache FileCache, } go func() { - cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize) + cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize, d) if err := fileCache.Store(context.Background(), cacheKey, buf.Bytes()); err != nil { fmt.Printf("failed to cache resized image: %v", err) } @@ -151,7 +151,7 @@ func createPreview(imgSvc ImgService, fileCache FileCache, return buf.Bytes(), nil } -func previewCacheKey(fPath string, fTime int64, previewSize PreviewSize) string { - fPath = filepath.Base(fPath) +func previewCacheKey(fPath string, fTime int64, previewSize PreviewSize, d *data) string { + fPath = d.user.Scope + fPath return fmt.Sprintf("%x%x%x", fPath, fTime, previewSize) } diff --git a/http/resource.go b/http/resource.go index ea4fbc29..60b9f8ce 100644 --- a/http/resource.go +++ b/http/resource.go @@ -72,7 +72,7 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc { } // delete thumbnails - err = delThumbs(r.Context(), fileCache, file) + err = delThumbs(r.Context(), d, fileCache, file) if err != nil { return errToStatus(err), err } @@ -119,7 +119,7 @@ func resourcePostHandler(fileCache FileCache) handleFunc { return http.StatusForbidden, nil } - err = delThumbs(r.Context(), fileCache, file) + err = delThumbs(r.Context(), d, fileCache, file) if err != nil { return errToStatus(err), err } @@ -280,10 +280,10 @@ func writeFile(fs afero.Fs, dst string, in io.Reader) (os.FileInfo, error) { return info, nil } -func delThumbs(ctx context.Context, fileCache FileCache, file *files.FileInfo) error { +func delThumbs(ctx context.Context, d *data, fileCache FileCache, file *files.FileInfo) error { for _, previewSizeName := range PreviewSizeNames() { size, _ := ParsePreviewSize(previewSizeName) - if err := fileCache.Delete(ctx, previewCacheKey(file.Path, file.ModTime.Unix(), size)); err != nil { + if err := fileCache.Delete(ctx, previewCacheKey(file.Path, file.ModTime.Unix(), size, d)); err != nil { return err } } @@ -320,7 +320,7 @@ func patchAction(ctx context.Context, action, src, dst string, d *data, fileCach } // delete thumbnails - err = delThumbs(ctx, fileCache, file) + err = delThumbs(ctx, d, fileCache, file) if err != nil { return err }