feat: use real image path to calculate cache key

pull/1841/head
Oleg Lobanov 2022-02-21 19:59:22 +01:00
parent cf85404dd2
commit c1987237d0
No known key found for this signature in database
GPG Key ID: 65FF3DB864FE3D2A
3 changed files with 18 additions and 5 deletions

View File

@ -185,6 +185,19 @@ func (i *FileInfo) Checksum(algo string) error {
return nil
}
func (i *FileInfo) RealPath() string {
if realPathFs, ok := i.Fs.(interface {
RealPath(name string) (fPath string, err error)
}); ok {
realPath, err := realPathFs.RealPath(i.Path)
if err == nil {
return realPath
}
}
return i.Path
}
//nolint:goconst
//TODO: use constants
func (i *FileInfo) detectType(modify, saveContent, readHeader bool) error {

View File

@ -91,7 +91,7 @@ func handleImagePreview(
return errToStatus(err), err
}
cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize)
cacheKey := previewCacheKey(file, previewSize)
resizedImage, ok, err := fileCache.Load(r.Context(), cacheKey)
if err != nil {
return errToStatus(err), err
@ -142,7 +142,7 @@ func createPreview(imgSvc ImgService, fileCache FileCache,
}
go func() {
cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize)
cacheKey := previewCacheKey(file, previewSize)
if err := fileCache.Store(context.Background(), cacheKey, buf.Bytes()); err != nil {
fmt.Printf("failed to cache resized image: %v", err)
}
@ -151,6 +151,6 @@ func createPreview(imgSvc ImgService, fileCache FileCache,
return buf.Bytes(), nil
}
func previewCacheKey(fPath string, fTime int64, previewSize PreviewSize) string {
return fmt.Sprintf("%x%x%x", fPath, fTime, previewSize)
func previewCacheKey(f *files.FileInfo, previewSize PreviewSize) string {
return fmt.Sprintf("%x%x%x", f.RealPath(), f.ModTime.Unix(), previewSize)
}

View File

@ -283,7 +283,7 @@ func writeFile(fs afero.Fs, dst string, in io.Reader) (os.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)); err != nil {
if err := fileCache.Delete(ctx, previewCacheKey(file, size)); err != nil {
return err
}
}