Skip to content

Commit 6319ca3

Browse files
committed
Fix: make thumbnails processor to work with spaces
1 parent 4715941 commit 6319ca3

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

thumbnails/handler.go

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io"
2727
"net/http"
2828
"net/url"
29+
"path"
2930
"path/filepath"
3031
"strconv"
3132
"strings"
@@ -45,6 +46,7 @@ import (
4546
"github.com/cs3org/reva/v3/pkg/rhttp/global"
4647
"github.com/cs3org/reva/v3/pkg/rhttp/router"
4748
"github.com/cs3org/reva/v3/pkg/sharedconf"
49+
"github.com/cs3org/reva/v3/pkg/spaces"
4850
"github.com/cs3org/reva/v3/pkg/storage/utils/downloader"
4951
"github.com/cs3org/reva/v3/pkg/utils/cfg"
5052
"github.com/pkg/errors"
@@ -143,22 +145,54 @@ func New(ctx context.Context, m map[string]interface{}) (global.Service, error)
143145
return s, nil
144146
}
145147

148+
func (s *Thumbnails) Handler() http.Handler {
149+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
150+
var head string
151+
152+
head, r.URL.Path = router.ShiftPath(r.URL.Path)
153+
switch head {
154+
case "files":
155+
if !checkMethods(r, http.MethodGet) {
156+
w.WriteHeader(http.StatusNotFound)
157+
return
158+
}
159+
s.davUserContext(s.Thumbnail(w, r)).ServeHTTP(w, r)
160+
return
161+
case "public-files":
162+
if !checkMethods(r, http.MethodGet, http.MethodHead) {
163+
w.WriteHeader(http.StatusNotFound)
164+
return
165+
}
166+
s.davPublicContext(s.Thumbnail(w, r)).ServeHTTP(w, r)
167+
}
168+
})
169+
}
170+
146171
func (s *Thumbnails) davUserContext(next http.Handler) http.Handler {
147172
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
148173
ctx := r.Context()
174+
log := appctx.GetLogger(ctx)
175+
176+
urlPath, _ := url.PathUnescape(r.URL.Path)
149177

150-
path := r.URL.Path
151-
path, _ = url.PathUnescape(path)
178+
storageSpaceID, resourcePath := router.ShiftPath(urlPath)
179+
_, spacePath, ok := spaces.DecodeStorageSpaceID(storageSpaceID)
180+
if !ok {
181+
s.writeHTTPError(w, errtypes.NotFound(""))
182+
return
183+
}
184+
resourcePath = path.Join(spacePath, resourcePath)
152185

153186
res, err := s.statRes(ctx, &provider.Reference{
154-
Path: path,
187+
Path: resourcePath,
155188
})
156189
if err != nil {
157190
s.writeHTTPError(w, err)
158191
return
159192
}
160193

161194
ctx = ContextSetResource(ctx, res)
195+
log.Info().Msgf("FindMe - set resource %s", res.Path)
162196

163197
next.ServeHTTP(w, r.WithContext(ctx))
164198
})
@@ -365,12 +399,16 @@ func parseDimension(d, name string, defaultValue int) (int, error) {
365399
func (s *Thumbnails) Thumbnail(w http.ResponseWriter, r *http.Request) http.Handler {
366400
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
367401
thumbReq, err := s.parseThumbnailRequest(r)
402+
log := appctx.GetLogger(r.Context())
403+
log.Info().Msgf("FindMe - Handling thumbnail request %s", thumbReq.File)
368404
if err != nil {
369405
s.writeHTTPError(w, err)
370406
return
371407
}
372408

373409
data, mimetype, err := s.thumbnail.GetThumbnail(r.Context(), thumbReq.File, thumbReq.ETag, thumbReq.Width, thumbReq.Height, thumbReq.OutputType)
410+
log.Info().Msgf("FindMe - Got thumbnail for %s", thumbReq.File)
411+
374412
if err != nil {
375413
s.writeHTTPError(w, err)
376414
return
@@ -402,29 +440,6 @@ func (s *Thumbnails) writeHTTPError(w http.ResponseWriter, err error) {
402440
_, _ = w.Write([]byte(err.Error()))
403441
}
404442

405-
func (s *Thumbnails) Handler() http.Handler {
406-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
407-
var head string
408-
409-
head, r.URL.Path = router.ShiftPath(r.URL.Path)
410-
switch head {
411-
case "files":
412-
if !checkMethods(r, http.MethodGet) {
413-
w.WriteHeader(http.StatusNotFound)
414-
return
415-
}
416-
s.davUserContext(s.Thumbnail(w, r)).ServeHTTP(w, r)
417-
return
418-
case "public-files":
419-
if !checkMethods(r, http.MethodGet, http.MethodHead) {
420-
w.WriteHeader(http.StatusNotFound)
421-
return
422-
}
423-
s.davPublicContext(s.Thumbnail(w, r)).ServeHTTP(w, r)
424-
}
425-
})
426-
}
427-
428443
func checkMethods(r *http.Request, methods ...string) bool {
429444
for _, m := range methods {
430445
if r.Method == m {

thumbnails/manager/thumbnail.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ func (t *Thumbnail) GetThumbnail(ctx context.Context, file, etag string, width,
9999
log.Debug().Msg("thumbnails: cache miss")
100100

101101
// the thumbnail was not found in the cache
102+
log.Debug().Msgf("thumbnails: starting download %s", file)
102103
r, err := t.downloader.Download(ctx, file, "")
104+
log.Debug().Msgf("thumbnails: finished download %s", file)
103105
if err != nil {
104106
return nil, "", errors.Wrap(err, "thumbnails: error downloading file "+file)
105107
}

0 commit comments

Comments
 (0)