Skip to content

Commit 9442393

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

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

thumbnails/handler.go

Lines changed: 39 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)
149175

150-
path := r.URL.Path
151-
path, _ = url.PathUnescape(path)
176+
urlPath, _ := url.PathUnescape(r.URL.Path)
177+
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,6 +399,8 @@ 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
@@ -402,29 +438,6 @@ func (s *Thumbnails) writeHTTPError(w http.ResponseWriter, err error) {
402438
_, _ = w.Write([]byte(err.Error()))
403439
}
404440

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-
428441
func checkMethods(r *http.Request, methods ...string) bool {
429442
for _, m := range methods {
430443
if r.Method == m {

0 commit comments

Comments
 (0)