From 6b259f6637b0aa9ff191fe13f1a107afe063edc2 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Fri, 22 Apr 2022 11:13:10 +0200 Subject: [PATCH 1/8] add tumbnails proxy route for /dav and /webdav --- extensions/proxy/pkg/config/defaults/defaultconfig.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/extensions/proxy/pkg/config/defaults/defaultconfig.go b/extensions/proxy/pkg/config/defaults/defaultconfig.go index ec1bd9af8ee..d43dda36fb9 100644 --- a/extensions/proxy/pkg/config/defaults/defaultconfig.go +++ b/extensions/proxy/pkg/config/defaults/defaultconfig.go @@ -106,6 +106,16 @@ func DefaultPolicies() []config.Policy { Endpoint: "/remote.php/dav/", Backend: "http://localhost:9115", // TODO use registry? }, + { + Type: config.QueryRoute, + Endpoint: "/dav/?preview=1", + Backend: "http://localhost:9115", + }, + { + Type: config.QueryRoute, + Endpoint: "/webdav/?preview=1", + Backend: "http://localhost:9115", + }, { Endpoint: "/remote.php/", Service: "ocdav", From 79970b08129cc38fb74166a5df60c757b62a428f Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Fri, 22 Apr 2022 11:13:34 +0200 Subject: [PATCH 2/8] add omitempty to proxy routes to slim down configuration example --- extensions/proxy/pkg/config/config.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/extensions/proxy/pkg/config/config.go b/extensions/proxy/pkg/config/config.go index a8c394f36c8..c0d6ea4e8db 100644 --- a/extensions/proxy/pkg/config/config.go +++ b/extensions/proxy/pkg/config/config.go @@ -45,15 +45,15 @@ type Policy struct { // Route defines forwarding routes type Route struct { - Type RouteType `yaml:"type"` + Type RouteType `yaml:"type,omitempty"` // Method optionally limits the route to this HTTP method - Method string `yaml:"method"` - Endpoint string `yaml:"endpoint"` + Method string `yaml:"method,omitempty"` + Endpoint string `yaml:"endpoint,omitempty"` // Backend is a static URL to forward the request to - Backend string `yaml:"backend"` + Backend string `yaml:"backend,omitempty"` // Service name to look up in the registry - Service string `yaml:"service"` - ApacheVHost bool `yaml:"apache_vhost"` + Service string `yaml:"service,omitempty"` + ApacheVHost bool `yaml:"apache_vhost,omitempty"` } // RouteType defines the type of a route From 9db5216bc40466894fa1d7ac47acbacf165ddee9 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Fri, 22 Apr 2022 11:19:28 +0200 Subject: [PATCH 3/8] add route handler for all possible webdav urls --- extensions/webdav/pkg/service/v0/service.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/extensions/webdav/pkg/service/v0/service.go b/extensions/webdav/pkg/service/v0/service.go index 43d9929b70d..844a252d8b2 100644 --- a/extensions/webdav/pkg/service/v0/service.go +++ b/extensions/webdav/pkg/service/v0/service.go @@ -78,6 +78,21 @@ func NewService(opts ...Option) (Service, error) { r.Get("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnail) r.Head("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnailHead) + r.Get("/remote.php/webdav/spaces/{id}/*", svc.SpacesThumbnail) + r.Get("/remote.php/webdav/files/{id}/*", svc.Thumbnail) + r.Get("/remote.php/webdav/public-files/{token}/*", svc.PublicThumbnail) + r.Head("/remote.php/webdav/public-files/{token}/*", svc.PublicThumbnailHead) + + r.Get("/dav/spaces/{id}/*", svc.SpacesThumbnail) + r.Get("/dav/files/{id}/*", svc.Thumbnail) + r.Get("/dav/public-files/{token}/*", svc.PublicThumbnail) + r.Head("/dav/public-files/{token}/*", svc.PublicThumbnailHead) + + r.Get("/webdav/spaces/{id}/*", svc.SpacesThumbnail) + r.Get("/webdav/files/{id}/*", svc.Thumbnail) + r.Get("/webdav/public-files/{token}/*", svc.PublicThumbnail) + r.Head("/webdav/public-files/{token}/*", svc.PublicThumbnailHead) + // r.MethodFunc("REPORT", "/remote.php/dav/files/{id}/*", svc.Search) // This is a workaround for the go-chi concurrent map read write issue. From a04c5c2f6f0df0c61138454e30907171685c6afa Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Fri, 22 Apr 2022 16:31:33 +0200 Subject: [PATCH 4/8] implement thumbnails also for webdav and non remote.php routes --- extensions/proxy/pkg/middleware/basic_auth.go | 1 + extensions/webdav/pkg/constants/constants.go | 8 ++ .../webdav/pkg/dav/requests/thumbnail.go | 41 ++----- extensions/webdav/pkg/service/v0/service.go | 113 +++++++++++++++--- 4 files changed, 113 insertions(+), 50 deletions(-) create mode 100644 extensions/webdav/pkg/constants/constants.go diff --git a/extensions/proxy/pkg/middleware/basic_auth.go b/extensions/proxy/pkg/middleware/basic_auth.go index 425fc4ca0a4..4b810ae2791 100644 --- a/extensions/proxy/pkg/middleware/basic_auth.go +++ b/extensions/proxy/pkg/middleware/basic_auth.go @@ -115,6 +115,7 @@ func (m basicAuth) isPublicLink(req *http.Request) bool { } publicPaths := []string{ + "/dav/public-files/", "/remote.php/dav/public-files/", "/remote.php/ocs/apps/files_sharing/api/v1/tokeninfo/unprotected", "/ocs/v1.php/cloud/capabilities", diff --git a/extensions/webdav/pkg/constants/constants.go b/extensions/webdav/pkg/constants/constants.go new file mode 100644 index 00000000000..3456ab83093 --- /dev/null +++ b/extensions/webdav/pkg/constants/constants.go @@ -0,0 +1,8 @@ +package constants + +type contextKey int + +const ( + ContextKeyID contextKey = iota + ContextKeyPath +) diff --git a/extensions/webdav/pkg/dav/requests/thumbnail.go b/extensions/webdav/pkg/dav/requests/thumbnail.go index 48b80ad4882..ccb6016d3b6 100644 --- a/extensions/webdav/pkg/dav/requests/thumbnail.go +++ b/extensions/webdav/pkg/dav/requests/thumbnail.go @@ -7,9 +7,10 @@ import ( "net/url" "path/filepath" "strconv" - "strings" "github.com/go-chi/chi/v5" + + "github.com/owncloud/ocis/extensions/webdav/pkg/constants" ) const ( @@ -39,12 +40,16 @@ type ThumbnailRequest struct { // ParseThumbnailRequest extracts all required parameters from a http request. func ParseThumbnailRequest(r *http.Request) (*ThumbnailRequest, error) { - fp, id, err := extractFilePath(r) - if err != nil { - return nil, err + ctx := r.Context() + + fp := ctx.Value(constants.ContextKeyPath).(string) + if fp == "" { + return nil, errors.New("invalid file path") } - q := r.URL.Query() + id := ctx.Value(constants.ContextKeyID).(string) + + q := r.URL.Query() width, height, err := parseDimensions(q) if err != nil { return nil, err @@ -61,32 +66,6 @@ func ParseThumbnailRequest(r *http.Request) (*ThumbnailRequest, error) { }, nil } -// the url looks as followed -// -// /remote.php/dav/files// -// -// User and filepath are dynamic and filepath can contain slashes -// So using the URLParam function is not possible. -func extractFilePath(r *http.Request) (string, string, error) { - id := chi.URLParam(r, "id") - id, err := url.QueryUnescape(id) - if err != nil { - return "", "", errors.New("could not unescape user") - } - if id != "" { - parts := strings.SplitN(r.URL.Path, id, 2) - return parts[1], id, nil - } - - // This is for public links - token := chi.URLParam(r, "token") - if token != "" { - parts := strings.SplitN(r.URL.Path, token, 2) - return parts[1], "", nil - } - return "", "", errors.New("could not extract file path") -} - func parseDimensions(q url.Values) (int64, int64, error) { width, err := parseDimension(q.Get("x"), "width", DefaultWidth) if err != nil { diff --git a/extensions/webdav/pkg/service/v0/service.go b/extensions/webdav/pkg/service/v0/service.go index 844a252d8b2..9e1e38882a7 100644 --- a/extensions/webdav/pkg/service/v0/service.go +++ b/extensions/webdav/pkg/service/v0/service.go @@ -1,9 +1,14 @@ package svc import ( + "context" "encoding/xml" "io" "net/http" +<<<<<<< HEAD +======= + "net/url" +>>>>>>> 60f1081e8 (implement thumbnails also for webdav and non remote.php routes) "path" "path/filepath" "strings" @@ -11,6 +16,7 @@ import ( gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" + revactx "github.com/cs3org/reva/v2/pkg/ctx" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/v2/pkg/storage/utils/templates" "github.com/go-chi/render" @@ -21,11 +27,19 @@ import ( "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" "github.com/go-chi/chi/v5" +<<<<<<< HEAD "github.com/owncloud/ocis/v2/extensions/webdav/pkg/config" "github.com/owncloud/ocis/v2/extensions/webdav/pkg/dav/requests" thumbnailsmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/thumbnails/v0" searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0" thumbnailssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/thumbnails/v0" +======= + "github.com/owncloud/ocis/extensions/webdav/pkg/config" + "github.com/owncloud/ocis/extensions/webdav/pkg/constants" + "github.com/owncloud/ocis/extensions/webdav/pkg/dav/requests" + thumbnailsmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/thumbnails/v0" + thumbnailssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/thumbnails/v0" +>>>>>>> 60f1081e8 (implement thumbnails also for webdav and non remote.php routes) ) const ( @@ -73,26 +87,30 @@ func NewService(opts ...Option) (Service, error) { } m.Route(options.Config.HTTP.Root, func(r chi.Router) { - r.Get("/remote.php/dav/spaces/{id}/*", svc.SpacesThumbnail) - r.Get("/remote.php/dav/files/{id}/*", svc.Thumbnail) - r.Get("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnail) - r.Head("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnailHead) - - r.Get("/remote.php/webdav/spaces/{id}/*", svc.SpacesThumbnail) - r.Get("/remote.php/webdav/files/{id}/*", svc.Thumbnail) - r.Get("/remote.php/webdav/public-files/{token}/*", svc.PublicThumbnail) - r.Head("/remote.php/webdav/public-files/{token}/*", svc.PublicThumbnailHead) - - r.Get("/dav/spaces/{id}/*", svc.SpacesThumbnail) - r.Get("/dav/files/{id}/*", svc.Thumbnail) - r.Get("/dav/public-files/{token}/*", svc.PublicThumbnail) - r.Head("/dav/public-files/{token}/*", svc.PublicThumbnailHead) - - r.Get("/webdav/spaces/{id}/*", svc.SpacesThumbnail) - r.Get("/webdav/files/{id}/*", svc.Thumbnail) - r.Get("/webdav/public-files/{token}/*", svc.PublicThumbnail) - r.Head("/webdav/public-files/{token}/*", svc.PublicThumbnailHead) + r.Group(func(r chi.Router) { + r.Use(svc.DavContext()) + + r.Get("/remote.php/dav/spaces/{id}/*", svc.SpacesThumbnail) + r.Get("/dav/spaces/{id}/*", svc.SpacesThumbnail) + + r.Get("/remote.php/dav/files/{id}/*", svc.Thumbnail) + r.Get("/dav/files/{id}/*", svc.Thumbnail) + + r.Head("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnailHead) + r.Head("/dav/public-files/{token}/*", svc.PublicThumbnailHead) + + r.Get("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnail) + r.Get("/dav/public-files/{token}/*", svc.PublicThumbnail) + }) + + r.Group(func(r chi.Router) { + r.Use(svc.WebdavContext()) + + r.Get("/remote.php/webdav/*", svc.Thumbnail) + r.Get("/webdav/*", svc.Thumbnail) + }) + // r.MethodFunc("REPORT", "/remote.php/dav/files/{id}/*", svc.Search) // This is a workaround for the go-chi concurrent map read write issue. @@ -129,6 +147,63 @@ func (g Webdav) ServeHTTP(w http.ResponseWriter, r *http.Request) { g.mux.ServeHTTP(w, r) } +func (g Webdav) DavContext() func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + id := chi.URLParam(r, "id") + id, err := url.QueryUnescape(id) + if err == nil && id != "" { + ctx = context.WithValue(ctx, constants.ContextKeyID, id) + } + + filePath := r.URL.Path + if id != "" { + filePath = strings.TrimPrefix(filePath, path.Join("/remote.php/dav/spaces", id)+"/") + filePath = strings.TrimPrefix(filePath, path.Join("/dav/spaces", id)+"/") + + filePath = strings.TrimPrefix(filePath, path.Join("/remote.php/dav/files", id)+"/") + filePath = strings.TrimPrefix(filePath, path.Join("/dav/files", id)+"/") + } + + // token for public links + token := chi.URLParam(r, "token") + + if token != "" { + filePath = strings.TrimPrefix(filePath, path.Join("/remote.php/dav/public-files", token)+"/") + filePath = strings.TrimPrefix(filePath, path.Join("/dav/public-files", token)+"/") + } + ctx = context.WithValue(ctx, constants.ContextKeyPath, filePath) + + next.ServeHTTP(w, r.WithContext(ctx)) + + }) + } +} + +func (g Webdav) WebdavContext() func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + u, ok := revactx.ContextGetUser(r.Context()) + if ok { + if u.Id == nil || u.Id.OpaqueId != "" { + ctx = context.WithValue(ctx, constants.ContextKeyID, u.Id.OpaqueId) + } + } + + filePath := r.URL.Path + filePath = strings.TrimPrefix(filePath, "/remote.php/webdav/") + filePath = strings.TrimPrefix(filePath, "/webdav/") + ctx = context.WithValue(ctx, constants.ContextKeyPath, filePath) + + next.ServeHTTP(w, r.WithContext(ctx)) + }) + } +} + // SpacesThumbnail is the endpoint for retrieving thumbnails inside of spaces. func (g Webdav) SpacesThumbnail(w http.ResponseWriter, r *http.Request) { tr, err := requests.ParseThumbnailRequest(r) From ea9cbb8c2d0fb7ed9d3a5ac00eb663c45871c197 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Tue, 26 Apr 2022 14:07:15 +0200 Subject: [PATCH 5/8] return 404 for /remote.php/webdav and /webdav/ previews --- extensions/webdav/pkg/service/v0/service.go | 32 ++++----------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/extensions/webdav/pkg/service/v0/service.go b/extensions/webdav/pkg/service/v0/service.go index 9e1e38882a7..c99afb87fb4 100644 --- a/extensions/webdav/pkg/service/v0/service.go +++ b/extensions/webdav/pkg/service/v0/service.go @@ -16,7 +16,6 @@ import ( gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" - revactx "github.com/cs3org/reva/v2/pkg/ctx" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/v2/pkg/storage/utils/templates" "github.com/go-chi/render" @@ -105,10 +104,11 @@ func NewService(opts ...Option) (Service, error) { }) r.Group(func(r chi.Router) { - r.Use(svc.WebdavContext()) - - r.Get("/remote.php/webdav/*", svc.Thumbnail) - r.Get("/webdav/*", svc.Thumbnail) + // currently not implemented + // If we implement this endpoint, we need to get the user from the access token. + // We decided against doing it, so that this service can be started without the JWT secret. + r.Get("/remote.php/webdav/*", http.NotFound) + r.Get("/webdav/*", http.NotFound) }) // r.MethodFunc("REPORT", "/remote.php/dav/files/{id}/*", svc.Search) @@ -182,28 +182,6 @@ func (g Webdav) DavContext() func(next http.Handler) http.Handler { } } -func (g Webdav) WebdavContext() func(next http.Handler) http.Handler { - return func(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - - u, ok := revactx.ContextGetUser(r.Context()) - if ok { - if u.Id == nil || u.Id.OpaqueId != "" { - ctx = context.WithValue(ctx, constants.ContextKeyID, u.Id.OpaqueId) - } - } - - filePath := r.URL.Path - filePath = strings.TrimPrefix(filePath, "/remote.php/webdav/") - filePath = strings.TrimPrefix(filePath, "/webdav/") - ctx = context.WithValue(ctx, constants.ContextKeyPath, filePath) - - next.ServeHTTP(w, r.WithContext(ctx)) - }) - } -} - // SpacesThumbnail is the endpoint for retrieving thumbnails inside of spaces. func (g Webdav) SpacesThumbnail(w http.ResponseWriter, r *http.Request) { tr, err := requests.ParseThumbnailRequest(r) From 2ddc7f73995d8f7ad1275b1e48f040f545e40214 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Tue, 26 Apr 2022 14:14:24 +0200 Subject: [PATCH 6/8] split context functions for public and user thumbnails --- extensions/webdav/pkg/service/v0/service.go | 27 ++++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/extensions/webdav/pkg/service/v0/service.go b/extensions/webdav/pkg/service/v0/service.go index c99afb87fb4..7dcb09fe6ef 100644 --- a/extensions/webdav/pkg/service/v0/service.go +++ b/extensions/webdav/pkg/service/v0/service.go @@ -88,13 +88,17 @@ func NewService(opts ...Option) (Service, error) { m.Route(options.Config.HTTP.Root, func(r chi.Router) { r.Group(func(r chi.Router) { - r.Use(svc.DavContext()) + r.Use(svc.DavUserContext()) r.Get("/remote.php/dav/spaces/{id}/*", svc.SpacesThumbnail) r.Get("/dav/spaces/{id}/*", svc.SpacesThumbnail) r.Get("/remote.php/dav/files/{id}/*", svc.Thumbnail) r.Get("/dav/files/{id}/*", svc.Thumbnail) + }) + + r.Group(func(r chi.Router) { + r.Use(svc.DavPublicContext()) r.Head("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnailHead) r.Head("/dav/public-files/{token}/*", svc.PublicThumbnailHead) @@ -147,10 +151,11 @@ func (g Webdav) ServeHTTP(w http.ResponseWriter, r *http.Request) { g.mux.ServeHTTP(w, r) } -func (g Webdav) DavContext() func(next http.Handler) http.Handler { +func (g Webdav) DavUserContext() func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() + filePath := r.URL.Path id := chi.URLParam(r, "id") id, err := url.QueryUnescape(id) @@ -158,7 +163,6 @@ func (g Webdav) DavContext() func(next http.Handler) http.Handler { ctx = context.WithValue(ctx, constants.ContextKeyID, id) } - filePath := r.URL.Path if id != "" { filePath = strings.TrimPrefix(filePath, path.Join("/remote.php/dav/spaces", id)+"/") filePath = strings.TrimPrefix(filePath, path.Join("/dav/spaces", id)+"/") @@ -167,10 +171,21 @@ func (g Webdav) DavContext() func(next http.Handler) http.Handler { filePath = strings.TrimPrefix(filePath, path.Join("/dav/files", id)+"/") } - // token for public links - token := chi.URLParam(r, "token") + ctx = context.WithValue(ctx, constants.ContextKeyPath, filePath) + + next.ServeHTTP(w, r.WithContext(ctx)) + + }) + } +} + +func (g Webdav) DavPublicContext() func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + filePath := r.URL.Path - if token != "" { + if token := chi.URLParam(r, "token"); token != "" { filePath = strings.TrimPrefix(filePath, path.Join("/remote.php/dav/public-files", token)+"/") filePath = strings.TrimPrefix(filePath, path.Join("/dav/public-files", token)+"/") } From 5b49432d2d83ec52e6e164e9aad97640a85d1675 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Tue, 26 Apr 2022 14:21:47 +0200 Subject: [PATCH 7/8] add changelog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- changelog/unreleased/fix-thumbnails-dav.md | 10 ++++++++++ extensions/webdav/pkg/service/v0/service.go | 21 +++++---------------- 2 files changed, 15 insertions(+), 16 deletions(-) create mode 100644 changelog/unreleased/fix-thumbnails-dav.md diff --git a/changelog/unreleased/fix-thumbnails-dav.md b/changelog/unreleased/fix-thumbnails-dav.md new file mode 100644 index 00000000000..74212c20946 --- /dev/null +++ b/changelog/unreleased/fix-thumbnails-dav.md @@ -0,0 +1,10 @@ +Bugfix: Thumbnails for `/dav/xxx?preview=1` requests + +We've added the thumbnail rendering for `/dav/xxx?preview=1` requests, which was previously not supported because of missing routes. It now returns the same thumbnails as for +`/remote.php/dav/xxx?preview=1`. + +We've also ensured that `/remote.php/webdav/xxx?preview=1` and `/webdav/xxx?preview=1` will be +routed to the correct service and always return a 404 Not Found, because Thumbnails are currently +not implemented for that route. + +https://github.com/owncloud/ocis/pull/3567 diff --git a/extensions/webdav/pkg/service/v0/service.go b/extensions/webdav/pkg/service/v0/service.go index 7dcb09fe6ef..4977e5034b0 100644 --- a/extensions/webdav/pkg/service/v0/service.go +++ b/extensions/webdav/pkg/service/v0/service.go @@ -5,10 +5,7 @@ import ( "encoding/xml" "io" "net/http" -<<<<<<< HEAD -======= "net/url" ->>>>>>> 60f1081e8 (implement thumbnails also for webdav and non remote.php routes) "path" "path/filepath" "strings" @@ -18,27 +15,19 @@ import ( rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" "github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool" "github.com/cs3org/reva/v2/pkg/storage/utils/templates" + "github.com/go-chi/chi/v5" "github.com/go-chi/render" merrors "go-micro.dev/v4/errors" "google.golang.org/grpc/metadata" - "github.com/owncloud/ocis/v2/ocis-pkg/log" - "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" - - "github.com/go-chi/chi/v5" -<<<<<<< HEAD "github.com/owncloud/ocis/v2/extensions/webdav/pkg/config" + "github.com/owncloud/ocis/v2/extensions/webdav/pkg/constants" "github.com/owncloud/ocis/v2/extensions/webdav/pkg/dav/requests" + "github.com/owncloud/ocis/v2/ocis-pkg/log" + "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc" thumbnailsmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/thumbnails/v0" searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0" thumbnailssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/thumbnails/v0" -======= - "github.com/owncloud/ocis/extensions/webdav/pkg/config" - "github.com/owncloud/ocis/extensions/webdav/pkg/constants" - "github.com/owncloud/ocis/extensions/webdav/pkg/dav/requests" - thumbnailsmsg "github.com/owncloud/ocis/protogen/gen/ocis/messages/thumbnails/v0" - thumbnailssvc "github.com/owncloud/ocis/protogen/gen/ocis/services/thumbnails/v0" ->>>>>>> 60f1081e8 (implement thumbnails also for webdav and non remote.php routes) ) const ( @@ -114,7 +103,7 @@ func NewService(opts ...Option) (Service, error) { r.Get("/remote.php/webdav/*", http.NotFound) r.Get("/webdav/*", http.NotFound) }) - + // r.MethodFunc("REPORT", "/remote.php/dav/files/{id}/*", svc.Search) // This is a workaround for the go-chi concurrent map read write issue. From fd995475b6556b20bee79ca282d8f807b89fba11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn=20Friedrich=20Dreyer?= Date: Thu, 5 May 2022 14:45:24 +0000 Subject: [PATCH 8/8] use WhoAmI to look up user for legacy webdav endpoints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jörn Friedrich Dreyer --- changelog/unreleased/fix-thumbnails-dav.md | 6 +- .../webdav/pkg/dav/requests/thumbnail.go | 8 ++- extensions/webdav/pkg/service/v0/service.go | 61 ++++++++++++++----- 3 files changed, 53 insertions(+), 22 deletions(-) diff --git a/changelog/unreleased/fix-thumbnails-dav.md b/changelog/unreleased/fix-thumbnails-dav.md index 74212c20946..0db2fb4f30a 100644 --- a/changelog/unreleased/fix-thumbnails-dav.md +++ b/changelog/unreleased/fix-thumbnails-dav.md @@ -1,10 +1,6 @@ Bugfix: Thumbnails for `/dav/xxx?preview=1` requests -We've added the thumbnail rendering for `/dav/xxx?preview=1` requests, which was previously not supported because of missing routes. It now returns the same thumbnails as for +We've added the thumbnail rendering for `/dav/xxx?preview=1`, `/remote.php/webdav/{relative path}?preview=1` and `/webdav/{relative path}?preview=1` requests, which was previously not supported because of missing routes. It now returns the same thumbnails as for `/remote.php/dav/xxx?preview=1`. -We've also ensured that `/remote.php/webdav/xxx?preview=1` and `/webdav/xxx?preview=1` will be -routed to the correct service and always return a 404 Not Found, because Thumbnails are currently -not implemented for that route. - https://github.com/owncloud/ocis/pull/3567 diff --git a/extensions/webdav/pkg/dav/requests/thumbnail.go b/extensions/webdav/pkg/dav/requests/thumbnail.go index ccb6016d3b6..c34da246661 100644 --- a/extensions/webdav/pkg/dav/requests/thumbnail.go +++ b/extensions/webdav/pkg/dav/requests/thumbnail.go @@ -10,7 +10,7 @@ import ( "github.com/go-chi/chi/v5" - "github.com/owncloud/ocis/extensions/webdav/pkg/constants" + "github.com/owncloud/ocis/v2/extensions/webdav/pkg/constants" ) const ( @@ -47,7 +47,11 @@ func ParseThumbnailRequest(r *http.Request) (*ThumbnailRequest, error) { return nil, errors.New("invalid file path") } - id := ctx.Value(constants.ContextKeyID).(string) + id := "" + v := ctx.Value(constants.ContextKeyID) + if v != nil { + id = v.(string) + } q := r.URL.Query() width, height, err := parseDimensions(q) diff --git a/extensions/webdav/pkg/service/v0/service.go b/extensions/webdav/pkg/service/v0/service.go index 4977e5034b0..dd3fbb66fd2 100644 --- a/extensions/webdav/pkg/service/v0/service.go +++ b/extensions/webdav/pkg/service/v0/service.go @@ -97,11 +97,9 @@ func NewService(opts ...Option) (Service, error) { }) r.Group(func(r chi.Router) { - // currently not implemented - // If we implement this endpoint, we need to get the user from the access token. - // We decided against doing it, so that this service can be started without the JWT secret. - r.Get("/remote.php/webdav/*", http.NotFound) - r.Get("/webdav/*", http.NotFound) + r.Use(svc.WebDAVContext()) + r.Get("/remote.php/webdav/*", svc.Thumbnail) + r.Get("/webdav/*", svc.Thumbnail) }) // r.MethodFunc("REPORT", "/remote.php/dav/files/{id}/*", svc.Search) @@ -185,6 +183,21 @@ func (g Webdav) DavPublicContext() func(next http.Handler) http.Handler { }) } } +func (g Webdav) WebDAVContext() func(next http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + filePath := r.URL.Path + filePath = strings.TrimPrefix(filePath, "/remote.php") + filePath = strings.TrimPrefix(filePath, "/webdav/") + + ctx := context.WithValue(r.Context(), constants.ContextKeyPath, filePath) + + next.ServeHTTP(w, r.WithContext(ctx)) + + }) + } +} // SpacesThumbnail is the endpoint for retrieving thumbnails inside of spaces. func (g Webdav) SpacesThumbnail(w http.ResponseWriter, r *http.Request) { @@ -238,18 +251,36 @@ func (g Webdav) Thumbnail(w http.ResponseWriter, r *http.Request) { } t := r.Header.Get(TokenHeader) - ctx := metadata.AppendToOutgoingContext(r.Context(), TokenHeader, t) - userRes, err := g.revaClient.GetUserByClaim(ctx, &userv1beta1.GetUserByClaimRequest{ - Claim: "username", - Value: tr.Identifier, - }) - if err != nil || userRes.Status.Code != rpcv1beta1.Code_CODE_OK { - g.log.Error().Err(err).Msg("could not get user") - renderError(w, r, errInternalError("could not get user")) - return + + var user *userv1beta1.User + + if tr.Identifier == "" { + // look up user from token via WhoAmI + userRes, err := g.revaClient.WhoAmI(r.Context(), &gatewayv1beta1.WhoAmIRequest{ + Token: t, + }) + if err != nil || userRes.Status.Code != rpcv1beta1.Code_CODE_OK { + g.log.Error().Err(err).Msg("could not get user") + renderError(w, r, errInternalError("could not get user")) + return + } + user = userRes.GetUser() + } else { + // look up user from URL via GetUserByClaim + ctx := metadata.AppendToOutgoingContext(r.Context(), TokenHeader, t) + userRes, err := g.revaClient.GetUserByClaim(ctx, &userv1beta1.GetUserByClaimRequest{ + Claim: "username", + Value: tr.Identifier, + }) + if err != nil || userRes.Status.Code != rpcv1beta1.Code_CODE_OK { + g.log.Error().Err(err).Msg("could not get user") + renderError(w, r, errInternalError("could not get user")) + return + } + user = userRes.GetUser() } - fullPath := filepath.Join(templates.WithUser(userRes.User, g.config.WebdavNamespace), tr.Filepath) + fullPath := filepath.Join(templates.WithUser(user, g.config.WebdavNamespace), tr.Filepath) rsp, err := g.thumbnailsClient.GetThumbnail(r.Context(), &thumbnailssvc.GetThumbnailRequest{ Filepath: strings.TrimLeft(tr.Filepath, "/"), ThumbnailType: extensionToThumbnailType(strings.TrimLeft(tr.Extension, ".")),