Files
QSfera/pkg/thumbnails/imgsource/webdav.go
T
David Christofas 34dcaf9ffe 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.
2020-03-04 16:55:19 +01:00

42 lines
931 B
Go

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
}