mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-05-02 16:59:56 -05:00
implement functions to augment the context
This commit is contained in:
@@ -11,12 +11,6 @@ import (
|
||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnail/resolution"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const (
|
||||
authorization contextKey = imgsource.WebDavAuth
|
||||
)
|
||||
|
||||
// NewService returns a service implementation for Service.
|
||||
func NewService(opts ...Option) v0proto.ThumbnailServiceHandler {
|
||||
options := newOptions(opts...)
|
||||
@@ -68,7 +62,7 @@ func (g Thumbnail) GetThumbnail(ctx context.Context, req *v0proto.GetRequest, rs
|
||||
}
|
||||
|
||||
auth := req.Authorization
|
||||
sCtx := context.WithValue(ctx, authorization, auth)
|
||||
sCtx := imgsource.WithAuthorization(ctx, auth)
|
||||
img, err := g.source.Get(sCtx, tr.ImagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -5,7 +5,26 @@ import (
|
||||
"image"
|
||||
)
|
||||
|
||||
type key int
|
||||
|
||||
const (
|
||||
auth key = iota
|
||||
)
|
||||
|
||||
// Source defines the interface for image sources
|
||||
type Source interface {
|
||||
Get(ctx context.Context, path string) (image.Image, error)
|
||||
}
|
||||
|
||||
// WithAuthorization puts the authorization in the context.
|
||||
func WithAuthorization(parent context.Context, authorization string) context.Context {
|
||||
return context.WithValue(parent, auth, authorization)
|
||||
}
|
||||
|
||||
func authorization(ctx context.Context) string {
|
||||
val := ctx.Value(auth)
|
||||
if val == nil {
|
||||
return ""
|
||||
}
|
||||
return val.(string)
|
||||
}
|
||||
|
||||
@@ -23,11 +23,6 @@ type WebDav struct {
|
||||
baseURL string
|
||||
}
|
||||
|
||||
const (
|
||||
// WebDavAuth is the parameter name for the autorization token
|
||||
WebDavAuth = "Authorization"
|
||||
)
|
||||
|
||||
// Get downloads the file from a webdav service
|
||||
func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
u, _ := url.Parse(s.baseURL)
|
||||
@@ -37,8 +32,8 @@ func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error())
|
||||
}
|
||||
|
||||
auth, ok := authorization(ctx)
|
||||
if !ok {
|
||||
auth := authorization(ctx)
|
||||
if auth == "" {
|
||||
return nil, fmt.Errorf("could not get image \"%s\" error: authorization is missing", file)
|
||||
}
|
||||
req.Header.Add("Authorization", auth)
|
||||
@@ -59,8 +54,3 @@ func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func authorization(ctx context.Context) (string, bool) {
|
||||
auth, ok := ctx.Value(WebDavAuth).(string)
|
||||
return auth, ok
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user