support thumbnails in spaces

This commit is contained in:
David Christofas committed 2022-02-23 15:52:02 +01:00
1 parent d92dc8951a
commit caeb2b684d
5 files changed
+99 -19

No files matched your search

@@ -0,0 +1,5 @@
Enhancement: Thumbnails in spaces
Added support for thumbnails in spaces.
https://github.com/owncloud/ocis/pull/3219
+16 -3
View File
@@ -217,11 +217,24 @@ func (g Thumbnail) handleWebdavSource(ctx context.Context, req *thumbnailssvc.Ge
func (g Thumbnail) stat(path, auth string) (*provider.StatResponse, error) { func (g Thumbnail) stat(path, auth string) (*provider.StatResponse, error) {
ctx := metadata.AppendToOutgoingContext(context.Background(), revactx.TokenHeader, auth) ctx := metadata.AppendToOutgoingContext(context.Background(), revactx.TokenHeader, auth)
req := &provider.StatRequest{ var ref *provider.Reference
Ref: &provider.Reference{ if strings.Contains(path, "!") {
parts := strings.Split(path, "!")
spaceID, path := parts[0], parts[1]
ref = &provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: spaceID,
OpaqueId: spaceID,
},
Path: path, Path: path,
}, }
} else {
ref = &provider.Reference{
Path: path,
}
} }
req := &provider.StatRequest{Ref: ref}
rsp, err := g.cs3Client.Stat(ctx, req) rsp, err := g.cs3Client.Stat(ctx, req)
if err != nil { if err != nil {
g.logger.Error().Err(err).Str("path", path).Msg("could not stat file") g.logger.Error().Err(err).Str("path", path).Msg("could not stat file")
+18 -5
View File
@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strings"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
@@ -42,12 +43,24 @@ func (s CS3) Get(ctx context.Context, path string) (io.ReadCloser, error) {
if !ok { if !ok {
return nil, errors.New("cs3source: authorization missing") return nil, errors.New("cs3source: authorization missing")
} }
ctx = metadata.AppendToOutgoingContext(context.Background(), revactx.TokenHeader, auth) var ref *provider.Reference
rsp, err := s.client.InitiateFileDownload(ctx, &provider.InitiateFileDownloadRequest{ if strings.Contains(path, "!") {
Ref: &provider.Reference{ parts := strings.Split(path, "!")
spaceID, path := parts[0], parts[1]
ref = &provider.Reference{
ResourceId: &provider.ResourceId{
StorageId: spaceID,
OpaqueId: spaceID,
},
Path: path, Path: path,
}, }
}) } else {
ref = &provider.Reference{
Path: path,
}
}
ctx = metadata.AppendToOutgoingContext(context.Background(), revactx.TokenHeader, auth)
rsp, err := s.client.InitiateFileDownload(ctx, &provider.InitiateFileDownloadRequest{Ref: ref})
if err != nil { if err != nil {
return nil, err return nil, err
+10 -9
View File
@@ -33,13 +33,13 @@ type ThumbnailRequest struct {
Height int32 Height int32
// In case of a public share the public link token. // In case of a public share the public link token.
PublicLinkToken string PublicLinkToken string
// The username from the requested URL // The Identifier from the requested URL
Username string Identifier string
} }
// ParseThumbnailRequest extracts all required parameters from a http request. // ParseThumbnailRequest extracts all required parameters from a http request.
func ParseThumbnailRequest(r *http.Request) (*ThumbnailRequest, error) { func ParseThumbnailRequest(r *http.Request) (*ThumbnailRequest, error) {
fp, username, err := extractFilePath(r) fp, id, err := extractFilePath(r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -57,7 +57,7 @@ func ParseThumbnailRequest(r *http.Request) (*ThumbnailRequest, error) {
Width: int32(width), Width: int32(width),
Height: int32(height), Height: int32(height),
PublicLinkToken: chi.URLParam(r, "token"), PublicLinkToken: chi.URLParam(r, "token"),
Username: username, Identifier: id,
}, nil }, nil
} }
@@ -68,16 +68,17 @@ func ParseThumbnailRequest(r *http.Request) (*ThumbnailRequest, error) {
// User and filepath are dynamic and filepath can contain slashes // User and filepath are dynamic and filepath can contain slashes
// So using the URLParam function is not possible. // So using the URLParam function is not possible.
func extractFilePath(r *http.Request) (string, string, error) { func extractFilePath(r *http.Request) (string, string, error) {
user := chi.URLParam(r, "user") id := chi.URLParam(r, "id")
user, err := url.QueryUnescape(user) id, err := url.QueryUnescape(id)
if err != nil { if err != nil {
return "", "", errors.New("could not unescape user") return "", "", errors.New("could not unescape user")
} }
if user != "" { if id != "" {
parts := strings.SplitN(r.URL.Path, user, 2) parts := strings.SplitN(r.URL.Path, id, 2)
return parts[1], user, nil return parts[1], id, nil
} }
// This is for public links
token := chi.URLParam(r, "token") token := chi.URLParam(r, "token")
if token != "" { if token != "" {
parts := strings.SplitN(r.URL.Path, token, 2) parts := strings.SplitN(r.URL.Path, token, 2)
+50 -2
View File
@@ -66,7 +66,8 @@ func NewService(opts ...Option) (Service, error) {
} }
m.Route(options.Config.HTTP.Root, func(r chi.Router) { m.Route(options.Config.HTTP.Root, func(r chi.Router) {
r.Get("/remote.php/dav/files/{user}/*", svc.Thumbnail) 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.Get("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnail)
r.Head("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnailHead) r.Head("/remote.php/dav/public-files/{token}/*", svc.PublicThumbnailHead)
}) })
@@ -88,6 +89,53 @@ func (g Webdav) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.mux.ServeHTTP(w, r) g.mux.ServeHTTP(w, r)
} }
// 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)
if err != nil {
g.log.Error().Err(err).Msg("could not create Request")
renderError(w, r, errBadRequest(err.Error()))
return
}
t := r.Header.Get(TokenHeader)
fullPath := tr.Identifier + "!" + tr.Filepath
rsp, err := g.thumbnailsClient.GetThumbnail(r.Context(), &thumbnailssvc.GetThumbnailRequest{
Filepath: strings.TrimLeft(tr.Filepath, "/"),
ThumbnailType: extensionToThumbnailType(strings.TrimLeft(tr.Extension, ".")),
Width: tr.Width,
Height: tr.Height,
Source: &thumbnailssvc.GetThumbnailRequest_Cs3Source{
Cs3Source: &thumbnailsmsg.CS3Source{
Path: fullPath,
Authorization: t,
},
},
})
if err != nil {
e := merrors.Parse(err.Error())
switch e.Code {
case http.StatusNotFound:
// StatusNotFound is expected for unsupported files
renderError(w, r, errNotFound(notFoundMsg(tr.Filename)))
return
case http.StatusBadRequest:
renderError(w, r, errBadRequest(err.Error()))
default:
renderError(w, r, errInternalError(err.Error()))
}
g.log.Error().Err(err).Msg("could not get thumbnail")
return
}
if len(rsp.Thumbnail) == 0 {
renderError(w, r, errNotFound(""))
return
}
g.mustRender(w, r, newThumbnailResponse(rsp))
}
// Thumbnail implements the Service interface. // Thumbnail implements the Service interface.
func (g Webdav) Thumbnail(w http.ResponseWriter, r *http.Request) { func (g Webdav) Thumbnail(w http.ResponseWriter, r *http.Request) {
tr, err := requests.ParseThumbnailRequest(r) tr, err := requests.ParseThumbnailRequest(r)
@@ -101,7 +149,7 @@ func (g Webdav) Thumbnail(w http.ResponseWriter, r *http.Request) {
ctx := metadata.AppendToOutgoingContext(r.Context(), TokenHeader, t) ctx := metadata.AppendToOutgoingContext(r.Context(), TokenHeader, t)
userRes, err := g.revaClient.GetUserByClaim(ctx, &userv1beta1.GetUserByClaimRequest{ userRes, err := g.revaClient.GetUserByClaim(ctx, &userv1beta1.GetUserByClaimRequest{
Claim: "username", Claim: "username",
Value: tr.Username, Value: tr.Identifier,
}) })
if err != nil || userRes.Status.Code != rpcv1beta1.Code_CODE_OK { if err != nil || userRes.Status.Code != rpcv1beta1.Code_CODE_OK {
g.log.Error().Err(err).Msg("could not get user") g.log.Error().Err(err).Msg("could not get user")