clean up and add comments
This commit is contained in:
@@ -39,6 +39,7 @@ type Config struct {
|
||||
HTTP HTTP
|
||||
Tracing Tracing
|
||||
FileSystemStorage FileSystemStorage
|
||||
WebDavSource WebDavSource
|
||||
}
|
||||
|
||||
// FileSystemStorage defines the available filesystem storage configuration.
|
||||
@@ -46,6 +47,11 @@ type FileSystemStorage struct {
|
||||
RootDirectory string
|
||||
}
|
||||
|
||||
// WebDavSource defines the available webdav source configuration.
|
||||
type WebDavSource struct {
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
// New initializes a new configuration with or without defaults.
|
||||
func New() *Config {
|
||||
return &Config{}
|
||||
|
||||
@@ -139,7 +139,14 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
|
||||
Value: "/tmp/ocis-thumbnails/",
|
||||
Usage: "Root path of the filesystem storage directory",
|
||||
EnvVars: []string{"THUMBNAILS_FILESYSTEMSTORAGE_ROOT"},
|
||||
Destination: &cfg.FilesystemStorage.RootDirectory,
|
||||
Destination: &cfg.FileSystemStorage.RootDirectory,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "webdavsource-baseurl",
|
||||
Value: "http://localhost:9140/remote.php/webdav/",
|
||||
Usage: "Base url for a webdav api",
|
||||
EnvVars: []string{"THUMBNAILS_WEBDAVSOURCE_BASEURL"},
|
||||
Destination: &cfg.WebDavSource.BaseURL,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,9 +30,7 @@ func NewService(opts ...Option) Service {
|
||||
manager: thumbnails.SimpleManager{
|
||||
Storage: storage.NewFileSystemStorage(options.Config.FileSystemStorage),
|
||||
},
|
||||
source: imgsource.WebDav{
|
||||
Basepath: "http://localhost:9140/remote.php/webdav/",
|
||||
},
|
||||
source: imgsource.NewWebDavSource(options.Config.WebDavSource),
|
||||
}
|
||||
|
||||
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
|
||||
|
||||
@@ -6,11 +6,20 @@ import (
|
||||
"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,
|
||||
}
|
||||
}
|
||||
|
||||
// WebDav implements the Source interface for webdav services
|
||||
type WebDav struct {
|
||||
Basepath string
|
||||
baseURL string
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -20,11 +29,11 @@ const (
|
||||
|
||||
// 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, _ := 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 file \"%s\" error: %s", file, err.Error())
|
||||
return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error())
|
||||
}
|
||||
|
||||
auth := ctx.GetString(WebDavAuth)
|
||||
@@ -33,9 +42,16 @@ func (s WebDav) Get(file string, ctx SourceContext) (image.Image, error) {
|
||||
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())
|
||||
return nil, fmt.Errorf("could not get the image \"%s\" error: %s", file, err.Error())
|
||||
}
|
||||
|
||||
img, _, _ := image.Decode(resp.Body)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -4,25 +4,31 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// NewInMemoryStorage creates a new InMemory instance.
|
||||
func NewInMemoryStorage() InMemory {
|
||||
return InMemory{
|
||||
store: make(map[string][]byte),
|
||||
}
|
||||
}
|
||||
|
||||
// InMemory represents an in memory storage for thumbnails
|
||||
// Can be used during development
|
||||
type InMemory struct {
|
||||
store map[string][]byte
|
||||
}
|
||||
|
||||
// Get loads the thumbnail from memory.
|
||||
func (s InMemory) Get(key string) []byte {
|
||||
return s.store[key]
|
||||
}
|
||||
|
||||
// Set stores the thumbnail in memory.
|
||||
func (s InMemory) Set(key string, thumbnail []byte) error {
|
||||
s.store[key] = thumbnail
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildKey generates a unique key to store and retrieve the thumbnail.
|
||||
func (s InMemory) BuildKey(ctx Context) string {
|
||||
parts := []string{
|
||||
ctx.ETag,
|
||||
|
||||
Reference in New Issue
Block a user