fix: update image cache when replacing

pull/1321/head
Ramires Viana 2021-03-10 15:14:01 +00:00
parent 0b92d94570
commit 81b6f4d6f6
5 changed files with 74 additions and 42 deletions

View File

@ -78,7 +78,11 @@ export default {
}, },
thumbnailUrl () { thumbnailUrl () {
const path = this.url.replace(/^\/files\//, '') const path = this.url.replace(/^\/files\//, '')
return `${baseURL}/api/preview/thumb/${path}?auth=${this.jwt}&inline=true`
// reload the image when the file is replaced
const key = Date.parse(this.modified)
return `${baseURL}/api/preview/thumb/${path}?auth=${this.jwt}&inline=true&k=${key}`
}, },
isThumbsEnabled () { isThumbsEnabled () {
return enableThumbs return enableThumbs

View File

@ -102,10 +102,13 @@ export default {
return `${baseURL}/api/raw${url.encodePath(this.req.path)}?auth=${this.jwt}` return `${baseURL}/api/raw${url.encodePath(this.req.path)}?auth=${this.jwt}`
}, },
previewUrl () { previewUrl () {
// reload the image when the file is replaced
const key = Date.parse(this.req.modified)
if (this.req.type === 'image' && !this.fullSize) { if (this.req.type === 'image' && !this.fullSize) {
return `${baseURL}/api/preview/big${url.encodePath(this.req.path)}?auth=${this.jwt}` return `${baseURL}/api/preview/big${url.encodePath(this.req.path)}?auth=${this.jwt}&k=${key}`
} }
return `${baseURL}/api/raw${url.encodePath(this.req.path)}?auth=${this.jwt}` return `${baseURL}/api/raw${url.encodePath(this.req.path)}?auth=${this.jwt}&k=${key}`
}, },
raw () { raw () {
return `${this.previewUrl}&inline=true` return `${this.previewUrl}&inline=true`

View File

@ -54,7 +54,7 @@ func NewHandler(
api.PathPrefix("/resources").Handler(monkey(resourceGetHandler, "/api/resources")).Methods("GET") api.PathPrefix("/resources").Handler(monkey(resourceGetHandler, "/api/resources")).Methods("GET")
api.PathPrefix("/resources").Handler(monkey(resourceDeleteHandler(fileCache), "/api/resources")).Methods("DELETE") api.PathPrefix("/resources").Handler(monkey(resourceDeleteHandler(fileCache), "/api/resources")).Methods("DELETE")
api.PathPrefix("/resources").Handler(monkey(resourcePostHandler, "/api/resources")).Methods("POST") api.PathPrefix("/resources").Handler(monkey(resourcePostHandler(fileCache), "/api/resources")).Methods("POST")
api.PathPrefix("/resources").Handler(monkey(resourcePutHandler, "/api/resources")).Methods("PUT") api.PathPrefix("/resources").Handler(monkey(resourcePutHandler, "/api/resources")).Methods("PUT")
api.PathPrefix("/resources").Handler(monkey(resourcePatchHandler, "/api/resources")).Methods("PATCH") api.PathPrefix("/resources").Handler(monkey(resourcePatchHandler, "/api/resources")).Methods("PATCH")

View File

@ -79,7 +79,7 @@ func handleImagePreview(w http.ResponseWriter, r *http.Request, imgSvc ImgServic
return errToStatus(err), err return errToStatus(err), err
} }
cacheKey := previewCacheKey(file.Path, previewSize) cacheKey := previewCacheKey(file.Path, file.ModTime.Unix(), previewSize)
cachedFile, ok, err := fileCache.Load(r.Context(), cacheKey) cachedFile, ok, err := fileCache.Load(r.Context(), cacheKey)
if err != nil { if err != nil {
return errToStatus(err), err return errToStatus(err), err
@ -133,6 +133,6 @@ func handleImagePreview(w http.ResponseWriter, r *http.Request, imgSvc ImgServic
return 0, nil return 0, nil
} }
func previewCacheKey(fPath string, previewSize PreviewSize) string { func previewCacheKey(fPath string, fTime int64, previewSize PreviewSize) string {
return fPath + previewSize.String() return fmt.Sprintf("%x%x%x", fPath, fTime, previewSize)
} }

View File

@ -1,6 +1,7 @@
package http package http
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -71,12 +72,10 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
} }
// delete thumbnails // delete thumbnails
for _, previewSizeName := range PreviewSizeNames() { err = delThumbs(r.Context(), fileCache, file)
size, _ := ParsePreviewSize(previewSizeName) if err != nil {
if err := fileCache.Delete(r.Context(), previewCacheKey(file.Path, size)); err != nil { //nolint:govet
return errToStatus(err), err return errToStatus(err), err
} }
}
err = d.RunHook(func() error { err = d.RunHook(func() error {
return d.user.Fs.RemoveAll(r.URL.Path) return d.user.Fs.RemoveAll(r.URL.Path)
@ -90,7 +89,8 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
}) })
} }
var resourcePostHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { func resourcePostHandler(fileCache FileCache) handleFunc {
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if !d.user.Perm.Create { if !d.user.Perm.Create {
return http.StatusForbidden, nil return http.StatusForbidden, nil
} }
@ -105,13 +105,26 @@ var resourcePostHandler = withUser(func(w http.ResponseWriter, r *http.Request,
return errToStatus(err), err return errToStatus(err), err
} }
file, err := files.NewFileInfo(files.FileOptions{
Fs: d.user.Fs,
Path: r.URL.Path,
Modify: d.user.Perm.Modify,
Expand: true,
ReadHeader: d.server.TypeDetectionByHeader,
Checker: d,
})
if err == nil {
if r.URL.Query().Get("override") != "true" { if r.URL.Query().Get("override") != "true" {
if _, err := d.user.Fs.Stat(r.URL.Path); err == nil {
return http.StatusConflict, nil return http.StatusConflict, nil
} }
err = delThumbs(r.Context(), fileCache, file)
if err != nil {
return errToStatus(err), err
}
} }
err := d.RunHook(func() error { err = d.RunHook(func() error {
info, _ := writeFile(d.user.Fs, r.URL.Path, r.Body) info, _ := writeFile(d.user.Fs, r.URL.Path, r.Body)
etag := fmt.Sprintf(`"%x%x"`, info.ModTime().UnixNano(), info.Size()) etag := fmt.Sprintf(`"%x%x"`, info.ModTime().UnixNano(), info.Size())
@ -124,7 +137,8 @@ var resourcePostHandler = withUser(func(w http.ResponseWriter, r *http.Request,
} }
return errToStatus(err), err return errToStatus(err), err
}) })
}
var resourcePutHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) { var resourcePutHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if !d.user.Perm.Modify { if !d.user.Perm.Modify {
@ -264,3 +278,14 @@ func writeFile(fs afero.Fs, dst string, in io.Reader) (os.FileInfo, error) {
return info, nil return info, nil
} }
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 {
return err
}
}
return nil
}