Files
QSfera/Server/services/thumbnails/pkg/thumbnail/imgsource/cs3.go
T
Курнат Андрей 7135199f23
Server / deployment-config (push) Successful in 59s
Android / test-and-build (push) Failing after 1m14s
Server / vulnerability-scan (push) Failing after 8m54s
Make photo library offline-first and fix video thumbnails
2026-07-19 21:01:35 +03:00

181 lines
5.1 KiB
Go

package imgsource
import (
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"time"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/opencloud-eu/reva/v2/pkg/bytesize"
revactx "github.com/opencloud-eu/reva/v2/pkg/ctx"
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
"github.com/opencloud-eu/reva/v2/pkg/rhttp"
"github.com/opencloud-eu/reva/v2/pkg/storagespace"
"github.com/qsfera/server/services/thumbnails/pkg/config"
"github.com/qsfera/server/services/thumbnails/pkg/errors"
"google.golang.org/grpc/metadata"
)
const (
// TokenTransportHeader holds the header key for the reva transfer token
// "github.com/opencloud-eu/reva/v2/internal/http/services/datagateway" is internal so we redeclare it here
TokenTransportHeader = "X-Reva-Transfer"
)
// CS3 implements a CS3 image source
type CS3 struct {
gatewaySelector pool.Selectable[gateway.GatewayAPIClient]
insecure bool
maxImageFileSize uint64
maxVideoFileSize uint64
}
// NewCS3Source configures a new CS3 image source
func NewCS3Source(
cfg config.Thumbnail,
gatewaySelector pool.Selectable[gateway.GatewayAPIClient],
imageLimit, videoLimit bytesize.ByteSize,
) CS3 {
return CS3{
gatewaySelector: gatewaySelector,
insecure: cfg.CS3AllowInsecure,
maxImageFileSize: imageLimit.Bytes(),
maxVideoFileSize: videoLimit.Bytes(),
}
}
// Get downloads the file from a cs3 service
// The caller MUST make sure to close the returned ReadCloser
func (s CS3) Get(ctx context.Context, path string) (io.ReadCloser, error) {
auth, ok := ContextGetAuthorization(ctx)
if !ok {
return nil, errors.ErrCS3AuthorizationMissing
}
ref, err := storagespace.ParseReference(path)
if err != nil {
// If the path is not a spaces reference try to handle it like a plain
// path reference.
ref = provider.Reference{
Path: path,
}
}
// Preserve source metadata already attached to the request context. In particular, the
// video marker selects the separate video size limit; replacing the context here made
// every video larger than the image limit fail before ffmpeg was reached.
ctx = withCS3Authorization(ctx, auth)
err = s.checkImageFileSize(ctx, ref)
if err != nil {
return nil, err
}
gwc, err := s.gatewaySelector.Next()
if err != nil {
return nil, err
}
rsp, err := gwc.InitiateFileDownload(ctx, &provider.InitiateFileDownloadRequest{Ref: &ref})
if err != nil {
return nil, err
}
if rsp.GetStatus().GetCode() != rpc.Code_CODE_OK {
return nil, fmt.Errorf("could not load image: %s", rsp.GetStatus().GetMessage())
}
var ep, tk string
for _, p := range rsp.GetProtocols() {
if p.GetProtocol() == "spaces" {
ep, tk = p.GetDownloadEndpoint(), p.GetToken()
break
}
}
if (ep == "" || tk == "") && len(rsp.GetProtocols()) > 0 {
ep, tk = rsp.GetProtocols()[0].GetDownloadEndpoint(), rsp.GetProtocols()[0].GetToken()
}
downloadCtx := ctx
cancelDownload := func() {}
if contextIsVideoSource(ctx) {
downloadCtx, cancelDownload = detachedVideoDownloadContext(ctx)
}
httpReq, err := rhttp.NewRequest(downloadCtx, "GET", ep, nil)
if err != nil {
cancelDownload()
return nil, err
}
httpReq.Header.Set(revactx.TokenHeader, auth)
httpReq.Header.Set(TokenTransportHeader, tk)
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: s.insecure, //nolint:gosec
}
client := &http.Client{Transport: transport}
resp, err := client.Do(httpReq)
if err != nil {
cancelDownload()
return nil, err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
cancelDownload()
return nil, fmt.Errorf("could not get the image \"%s\". Request returned with statuscode %d ", path, resp.StatusCode)
}
if contextIsVideoSource(ctx) {
return cancelOnCloseReadCloser{ReadCloser: resp.Body, cancel: cancelDownload}, nil
}
return resp.Body, nil
}
type cancelOnCloseReadCloser struct {
io.ReadCloser
cancel context.CancelFunc
}
func (r cancelOnCloseReadCloser) Close() error {
err := r.ReadCloser.Close()
r.cancel()
return err
}
func detachedVideoDownloadContext(ctx context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.WithoutCancel(ctx), videoDownloadTimeout)
}
const videoDownloadTimeout = 15 * time.Minute
func withCS3Authorization(ctx context.Context, auth string) context.Context {
return metadata.AppendToOutgoingContext(ctx, revactx.TokenHeader, auth)
}
func (s CS3) checkImageFileSize(ctx context.Context, ref provider.Reference) error {
gwc, err := s.gatewaySelector.Next()
if err != nil {
return err
}
stat, err := gwc.Stat(ctx, &provider.StatRequest{Ref: &ref})
if err != nil {
return err
}
if stat.GetStatus().GetCode() != rpc.Code_CODE_OK {
return fmt.Errorf("could not stat image: %s", stat.GetStatus().GetMessage())
}
limit := s.maxImageFileSize
if contextIsVideoSource(ctx) {
limit = s.maxVideoFileSize
}
if stat.GetInfo().GetSize() > limit {
return errors.ErrImageTooLarge
}
return nil
}