Skip to content

Commit

Permalink
feat: use real image path to calculate cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
o1egl committed Feb 21, 2022
1 parent cf85404 commit c198723
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
13 changes: 13 additions & 0 deletions files/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions http/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion http/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit c198723

Please sign in to comment.