using realpath

This commit is contained in:
niubility000 2021-12-22 14:08:36 +08:00 committed by GitHub
parent 550ff87a7a
commit 77cf40dca6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 15 deletions

View File

@ -9,7 +9,8 @@ import (
"net/http"
"github.com/gorilla/mux"
"path/filepath"
"github.com/spf13/afero"
"github.com/filebrowser/filebrowser/v2/files"
"github.com/filebrowser/filebrowser/v2/img"
)
@ -61,7 +62,7 @@ func previewHandler(imgSvc ImgService, fileCache FileCache, enableThumbnails, re
switch file.Type {
case "image":
return handleImagePreview(w, r, d, imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview)
return handleImagePreview(w, r, imgSvc, fileCache, file, previewSize, enableThumbnails, resizePreview)
default:
return http.StatusNotImplemented, fmt.Errorf("can't create preview for %s type", file.Type)
}
@ -71,7 +72,6 @@ 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,
@ -92,13 +92,13 @@ func handleImagePreview(
return errToStatus(err), err
}
cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize, d)
cacheKey, err := previewCacheKey(file, previewSize)
resizedImage, ok, err := fileCache.Load(r.Context(), cacheKey)
if err != nil {
return errToStatus(err), err
}
if !ok {
resizedImage, err = createPreview(imgSvc, d, fileCache, file, previewSize)
resizedImage, err = createPreview(imgSvc, fileCache, file, previewSize)
if err != nil {
return errToStatus(err), err
}
@ -110,7 +110,7 @@ func handleImagePreview(
return 0, nil
}
func createPreview(imgSvc ImgService, d *data, fileCache FileCache,
func createPreview(imgSvc ImgService, fileCache FileCache,
file *files.FileInfo, previewSize PreviewSize) ([]byte, error) {
fd, err := file.Fs.Open(file.Path)
if err != nil {
@ -143,7 +143,10 @@ func createPreview(imgSvc ImgService, d *data, fileCache FileCache,
}
go func() {
cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize, d)
cacheKey, err := previewCacheKey(file, previewSize)
if err != nil {
return
}
if err := fileCache.Store(context.Background(), cacheKey, buf.Bytes()); err != nil {
fmt.Printf("failed to cache resized image: %v", err)
}
@ -152,7 +155,10 @@ func createPreview(imgSvc ImgService, d *data, fileCache FileCache,
return buf.Bytes(), nil
}
func previewCacheKey(fPath string, fTime int64, previewSize PreviewSize, d *data) string {
fPath = filepath.Clean("/"+ d.user.Scope + fPath)
return fmt.Sprintf("%x%x%x", fPath, fTime, previewSize)
func previewCacheKey(file *files.FileInfo, previewSize PreviewSize) (string, error) {
fPath, err := file.Fs.(*afero.BasePathFs).RealPath(file.Path)
if err != nil {
return file.Path, err
}
return fmt.Sprintf("%x%x%x", fPath, file.ModTime.Unix(), previewSize), nil
}

View File

@ -72,7 +72,7 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
}
// delete thumbnails
err = delThumbs(r.Context(), d, fileCache, file)
err = delThumbs(r.Context(), 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(), d, fileCache, file)
err = delThumbs(r.Context(), fileCache, file)
if err != nil {
return errToStatus(err), err
}
@ -280,10 +280,14 @@ func writeFile(fs afero.Fs, dst string, in io.Reader) (os.FileInfo, error) {
return info, nil
}
func delThumbs(ctx context.Context, d *data, fileCache FileCache, file *files.FileInfo) error {
func delThumbs(ctx context.Context, 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, d)); err != nil {
cacheKey, err := previewCacheKey(file, size)
if err != nil {
return err
}
if err := fileCache.Delete(ctx, cacheKey); err != nil {
return err
}
}
@ -320,7 +324,7 @@ func patchAction(ctx context.Context, action, src, dst string, d *data, fileCach
}
// delete thumbnails
err = delThumbs(ctx, d, fileCache, file)
err = delThumbs(ctx, fileCache, file)
if err != nil {
return err
}