Skip to content

Commit

Permalink
fix: update image cache when replacing
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiresviana committed Mar 10, 2021
1 parent 0b92d94 commit 81b6f4d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 39 deletions.
6 changes: 5 additions & 1 deletion frontend/src/components/files/ListingItem.vue
Expand Up @@ -78,7 +78,11 @@ export default {
},
thumbnailUrl () {
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 () {
return enableThumbs
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/views/files/Preview.vue
Expand Up @@ -102,10 +102,13 @@ export default {
return `${baseURL}/api/raw${url.encodePath(this.req.path)}?auth=${this.jwt}`
},
previewUrl () {
// reload the image when the file is replaced
const key = Date.parse(this.req.modified)
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 () {
return `${this.previewUrl}&inline=true`
Expand Down
2 changes: 1 addition & 1 deletion http/http.go
Expand Up @@ -54,7 +54,7 @@ func NewHandler(

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(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(resourcePatchHandler, "/api/resources")).Methods("PATCH")

Expand Down
6 changes: 3 additions & 3 deletions http/preview.go
Expand Up @@ -79,7 +79,7 @@ func handleImagePreview(w http.ResponseWriter, r *http.Request, imgSvc ImgServic
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)
if err != nil {
return errToStatus(err), err
Expand Down Expand Up @@ -133,6 +133,6 @@ func handleImagePreview(w http.ResponseWriter, r *http.Request, imgSvc ImgServic
return 0, nil
}

func previewCacheKey(fPath string, previewSize PreviewSize) string {
return fPath + previewSize.String()
func previewCacheKey(fPath string, fTime int64, previewSize PreviewSize) string {
return fmt.Sprintf("%x%x%x", fPath, fTime, previewSize)
}
89 changes: 57 additions & 32 deletions http/resource.go
@@ -1,6 +1,7 @@
package http

import (
"context"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -71,11 +72,9 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
}

// delete thumbnails
for _, previewSizeName := range PreviewSizeNames() {
size, _ := ParsePreviewSize(previewSizeName)
if err := fileCache.Delete(r.Context(), previewCacheKey(file.Path, size)); err != nil { //nolint:govet
return errToStatus(err), err
}
err = delThumbs(r.Context(), fileCache, file)
if err != nil {
return errToStatus(err), err
}

err = d.RunHook(func() error {
Expand All @@ -90,41 +89,56 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
})
}

var resourcePostHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if !d.user.Perm.Create {
return http.StatusForbidden, nil
}
func resourcePostHandler(fileCache FileCache) handleFunc {
return withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if !d.user.Perm.Create {
return http.StatusForbidden, nil
}

defer func() {
_, _ = io.Copy(ioutil.Discard, r.Body)
}()
defer func() {
_, _ = io.Copy(ioutil.Discard, r.Body)
}()

// Directories creation on POST.
if strings.HasSuffix(r.URL.Path, "/") {
err := d.user.Fs.MkdirAll(r.URL.Path, 0775)
return errToStatus(err), err
}
// Directories creation on POST.
if strings.HasSuffix(r.URL.Path, "/") {
err := d.user.Fs.MkdirAll(r.URL.Path, 0775)
return errToStatus(err), err
}

if r.URL.Query().Get("override") != "true" {
if _, err := d.user.Fs.Stat(r.URL.Path); err == nil {
return http.StatusConflict, nil
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" {
return http.StatusConflict, nil
}

err = delThumbs(r.Context(), fileCache, file)
if err != nil {
return errToStatus(err), err
}
}
}

err := d.RunHook(func() error {
info, _ := writeFile(d.user.Fs, r.URL.Path, r.Body)
err = d.RunHook(func() error {
info, _ := writeFile(d.user.Fs, r.URL.Path, r.Body)

etag := fmt.Sprintf(`"%x%x"`, info.ModTime().UnixNano(), info.Size())
w.Header().Set("ETag", etag)
return nil
}, "upload", r.URL.Path, "", d.user)
etag := fmt.Sprintf(`"%x%x"`, info.ModTime().UnixNano(), info.Size())
w.Header().Set("ETag", etag)
return nil
}, "upload", r.URL.Path, "", d.user)

if err != nil {
_ = d.user.Fs.RemoveAll(r.URL.Path)
}
if err != nil {
_ = d.user.Fs.RemoveAll(r.URL.Path)
}

return errToStatus(err), err
})
return errToStatus(err), err
})
}

var resourcePutHandler = withUser(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if !d.user.Perm.Modify {
Expand Down Expand Up @@ -264,3 +278,14 @@ 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 {
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
}

0 comments on commit 81b6f4d

Please sign in to comment.