Add 'thumbnails/' from commit '2ebf7b2f23af96b3b499c401fe5498a9349403d8'

git-subtree-dir: thumbnails
git-subtree-mainline: ccc651a11e
git-subtree-split: 2ebf7b2f23
This commit is contained in:
A.Unger
2020-09-18 13:02:40 +02:00
80 changed files with 5649 additions and 0 deletions
@@ -0,0 +1,39 @@
package imgsource
import (
"context"
"fmt"
"image"
"os"
"path/filepath"
"github.com/owncloud/ocis-thumbnails/pkg/config"
)
// NewFileSystemSource return a new FileSystem instance
func NewFileSystemSource(cfg config.FileSystemSource) FileSystem {
return FileSystem{
basePath: cfg.BasePath,
}
}
// FileSystem is an image source using the local file system
type FileSystem struct {
basePath string
}
// Get retrieves an image from the filesystem.
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())
}
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
return img, nil
}
@@ -0,0 +1,30 @@
package imgsource
import (
"context"
"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)
}
@@ -0,0 +1,61 @@
package imgsource
import (
"context"
"crypto/tls"
"fmt"
"image"
"net/http"
"net/url"
"path"
"github.com/owncloud/ocis-thumbnails/pkg/config"
)
// NewWebDavSource creates a new webdav instance.
func NewWebDavSource(cfg config.WebDavSource) WebDav {
return WebDav{
baseURL: cfg.BaseURL,
insecure: cfg.Insecure,
}
}
// WebDav implements the Source interface for webdav services
type WebDav struct {
baseURL string
insecure bool
}
// 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)
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())
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: s.insecure}
auth := authorization(ctx)
if auth == "" {
return nil, fmt.Errorf("could not get image \"%s\" error: authorization is missing", file)
}
req.Header.Add("Authorization", auth)
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())
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not get the image \"%s\". Request returned with statuscode %d ", file, resp.StatusCode)
}
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 img, nil
}