change image library and refactor thumbnails service
This commit is contained in:
@@ -2,12 +2,12 @@ package imgsource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"image"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/owncloud/ocis/thumbnails/pkg/config"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// NewFileSystemSource return a new FileSystem instance
|
||||
@@ -27,12 +27,12 @@ func (s FileSystem) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
imgPath := filepath.Join(s.basePath, file)
|
||||
f, err := os.Open(filepath.Clean(imgPath))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load the file %s from %s error %s", file, imgPath, err.Error())
|
||||
return nil, errors.Wrapf(err, "failed to load the file %s from %s", file, imgPath)
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "Get: Decode:")
|
||||
}
|
||||
|
||||
return img, nil
|
||||
|
||||
@@ -16,15 +16,16 @@ 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 {
|
||||
// ContextSetAuthorization puts the authorization in the context.
|
||||
func ContextSetAuthorization(parent context.Context, authorization string) context.Context {
|
||||
return context.WithValue(parent, auth, authorization)
|
||||
}
|
||||
|
||||
func authorization(ctx context.Context) string {
|
||||
// ContextGetAuthorization gets the authorization from the context.
|
||||
func ContextGetAuthorization(ctx context.Context) (string, bool) {
|
||||
val := ctx.Value(auth)
|
||||
if val == nil {
|
||||
return ""
|
||||
return "", false
|
||||
}
|
||||
return val.(string)
|
||||
return val.(string), true
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"path"
|
||||
|
||||
"github.com/owncloud/ocis/thumbnails/pkg/config"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// NewWebDavSource creates a new webdav instance.
|
||||
@@ -32,13 +33,13 @@ func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
u.Path = path.Join(u.Path, file)
|
||||
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error())
|
||||
return nil, errors.Wrapf(err, `could not get the image "%s"`, file)
|
||||
}
|
||||
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: s.insecure}
|
||||
|
||||
auth := authorization(ctx)
|
||||
if auth == "" {
|
||||
auth, ok := ContextGetAuthorization(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("could not get image \"%s\" error: authorization is missing", file)
|
||||
}
|
||||
req.Header.Add("Authorization", auth)
|
||||
@@ -46,7 +47,7 @@ func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error())
|
||||
return nil, errors.Wrapf(err, `could not get the image "%s"`, file)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
@@ -55,7 +56,7 @@ func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
|
||||
img, _, err := image.Decode(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not decode the image \"%s\". error: %s", file, err.Error())
|
||||
return nil, errors.Wrapf(err, `could not decode the image "%s"`, file)
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user