implement file sources

If the file is not cached we have to get it from somewhere. For that I
implemented a webdav file source which gets the file from reva.
This commit is contained in:
David Christofas
2020-03-04 16:55:19 +01:00
parent a98df0b11f
commit 34dcaf9ffe
5 changed files with 123 additions and 28 deletions
+33
View File
@@ -0,0 +1,33 @@
package imgsource
import "image"
// Source defines the interface for image sources
type Source interface {
Get(path string, ctx SourceContext) (image.Image, error)
}
// NewContext creates a new SourceContext instance
func NewContext() SourceContext {
return SourceContext{
m: make(map[string]interface{}),
}
}
// SourceContext is used to pass source specific parameters
type SourceContext struct {
m map[string]interface{}
}
// GetString tries to cast the value to a string
func (s SourceContext) GetString(key string) string {
if s, ok := s.m[key].(string); ok {
return s
}
return ""
}
// Set sets a value
func (s SourceContext) Set(key string, val interface{}) {
s.m[key] = val
}
+41
View File
@@ -0,0 +1,41 @@
package imgsource
import (
"fmt"
"image"
"net/http"
"net/url"
"path"
)
// WebDav implements the Source interface for webdav services
type WebDav struct {
Basepath 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(file string, ctx SourceContext) (image.Image, error) {
u, _ := url.Parse(s.Basepath)
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 file %s error: %s", file, err.Error())
}
auth := ctx.GetString(WebDavAuth)
req.Header.Add("Authorization", auth)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("could not get the file %s error: %s", file, err.Error())
}
img, _, _ := image.Decode(resp.Body)
return img, nil
}