Fix thumbnail URLs for sharedbyme

This commit is contained in:
André Duffeck
2025-07-22 10:42:22 +02:00
parent 49ab88e980
commit a6cdbc710d
+16 -45
View File
@@ -2,50 +2,25 @@ package svc
import ( import (
"fmt" "fmt"
"github.com/CiscoM31/godata" "net/http"
"strings"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
"github.com/go-chi/render" "github.com/go-chi/render"
libregraph "github.com/opencloud-eu/libre-graph-api-go" libregraph "github.com/opencloud-eu/libre-graph-api-go"
"github.com/opencloud-eu/opencloud/services/graph/pkg/odata"
"github.com/opencloud-eu/opencloud/services/thumbnails/pkg/thumbnail" "github.com/opencloud-eu/opencloud/services/thumbnails/pkg/thumbnail"
"github.com/opencloud-eu/reva/v2/pkg/signedurl"
"net/http"
"slices"
"time"
"github.com/opencloud-eu/opencloud/services/graph/pkg/errorcode" "github.com/opencloud-eu/opencloud/services/graph/pkg/errorcode"
) )
type driveItemsByResourceID map[string]libregraph.DriveItem type driveItemsByResourceID map[string]libregraph.DriveItem
// parseShareByMeRequest parses the odata request and returns the parsed request and a boolean indicating if the request should expand thumbnails.
func parseShareByMeRequest(r *http.Request) (*godata.GoDataRequest, bool, error) {
odataReq, err := godata.ParseRequest(r.Context(), sanitizePath(r.URL.Path, APIVersion_1), r.URL.Query())
if err != nil {
return nil, false, errorcode.New(errorcode.InvalidRequest, err.Error())
}
exp, err := odata.GetExpandValues(odataReq.Query)
if err != nil {
return nil, false, errorcode.New(errorcode.InvalidRequest, err.Error())
}
expandThumbnails := slices.Contains(exp, "thumbnails")
return odataReq, expandThumbnails, nil
}
// GetSharedByMe implements the Service interface (/me/drives/sharedByMe endpoint) // GetSharedByMe implements the Service interface (/me/drives/sharedByMe endpoint)
func (g Graph) GetSharedByMe(w http.ResponseWriter, r *http.Request) { func (g Graph) GetSharedByMe(w http.ResponseWriter, r *http.Request) {
g.logger.Debug().Msg("Calling GetRootDriveChildren") g.logger.Debug().Msg("Calling GetRootDriveChildren")
ctx := r.Context() ctx := r.Context()
_, expandThumbnails, err := parseShareByMeRequest(r) driveItems, err := g.listUserShares(ctx, nil, make(driveItemsByResourceID))
if err != nil {
errorcode.RenderError(w, r, err)
return
}
fmt.Println("expandThumbnails:", expandThumbnails)
driveItems := make(driveItemsByResourceID)
driveItems, err = g.listUserShares(ctx, nil, driveItems)
if err != nil { if err != nil {
errorcode.RenderError(w, r, err) errorcode.RenderError(w, r, err)
return return
@@ -65,6 +40,8 @@ func (g Graph) GetSharedByMe(w http.ResponseWriter, r *http.Request) {
return return
} }
expand := r.URL.Query().Get("$expand")
expandThumbnails := strings.Contains(expand, "thumbnails")
if expandThumbnails { if expandThumbnails {
for k, item := range driveItems { for k, item := range driveItems {
mt := item.GetFile().MimeType mt := item.GetFile().MimeType
@@ -72,32 +49,26 @@ func (g Graph) GetSharedByMe(w http.ResponseWriter, r *http.Request) {
continue continue
} }
signer, err := signedurl.NewJWTSignedURL(signedurl.WithSecret("abcde"))
if err != nil {
panic("failed to create signer")
}
_, match := thumbnail.SupportedMimeTypes[*mt] _, match := thumbnail.SupportedMimeTypes[*mt]
if match { if match {
signedURL, err := signer.Sign("https://localhost:9200/", item.GetId(), 30*time.Minute) baseUrl := fmt.Sprintf("%s/dav/spaces/%s?scalingup=0&preview=1&processor=thumbnail",
if err != nil { g.config.Commons.OpenCloudURL,
g.logger.Error().Err(err).Msg("Failed to get thumbnail URL") item.GetId())
continue smallUrl := baseUrl + "&x=36&y=36"
} mediumUrl := baseUrl + "&x=48&y=48"
t := libregraph.NewThumbnail() largeUrl := baseUrl + "&x=96&y=96"
t.SetUrl(signedURL)
item.SetThumbnails([]libregraph.ThumbnailSet{ item.SetThumbnails([]libregraph.ThumbnailSet{
{ {
Small: t, Small: &libregraph.Thumbnail{Url: &smallUrl},
Medium: t, Medium: &libregraph.Thumbnail{Url: &mediumUrl},
Large: t, Large: &libregraph.Thumbnail{Url: &largeUrl},
}, },
}) })
driveItems[k] = item // assign modified item back to the map driveItems[k] = item // assign modified item back to the map
} }
} }
} }
res := make([]libregraph.DriveItem, 0, len(driveItems)) res := make([]libregraph.DriveItem, 0, len(driveItems))