From 975387822b537f2be4bdf95f4dbe70f78b5fcf41 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Mon, 9 Mar 2020 16:01:20 +0100 Subject: [PATCH] add config for filesystem storage --- pkg/config/config.go | 16 +++++++++++----- pkg/flagset/flagset.go | 7 +++++++ pkg/thumbnails/imgsource/webdav.go | 4 ++-- pkg/thumbnails/storage/filesystem.go | 12 +++++++----- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index c92c9820c3..13b7a8ea3c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -33,11 +33,17 @@ type Tracing struct { // Config combines all available configuration parts. type Config struct { - File string - Log Log - Debug Debug - HTTP HTTP - Tracing Tracing + File string + Log Log + Debug Debug + HTTP HTTP + Tracing Tracing + FilesystemStorage FilesystemStorage +} + +// FilesystemStorage defines the available filesystem storage configuration. +type FilesystemStorage struct { + RootDirectory string } // New initializes a new configuration with or without defaults. diff --git a/pkg/flagset/flagset.go b/pkg/flagset/flagset.go index 39d4005b7f..b9f4f30b1b 100644 --- a/pkg/flagset/flagset.go +++ b/pkg/flagset/flagset.go @@ -134,5 +134,12 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag { EnvVars: []string{"THUMBNAILS_HTTP_ROOT"}, Destination: &cfg.HTTP.Root, }, + &cli.StringFlag{ + Name: "filesystemstorage-root", + Value: "/tmp/ocis-thumbnails/", + Usage: "Root path of the filesystem storage directory", + EnvVars: []string{"THUMBNAILS_FILESYSTEMSTORAGE_ROOT"}, + Destination: &cfg.FilesystemStorage.RootDirectory, + }, } } diff --git a/pkg/thumbnails/imgsource/webdav.go b/pkg/thumbnails/imgsource/webdav.go index 5637d943db..b3c1ba8a21 100644 --- a/pkg/thumbnails/imgsource/webdav.go +++ b/pkg/thumbnails/imgsource/webdav.go @@ -24,7 +24,7 @@ func (s WebDav) Get(file string, ctx SourceContext) (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 file %s error: %s", file, err.Error()) + return nil, fmt.Errorf("could not get the file \"%s\" error: %s", file, err.Error()) } auth := ctx.GetString(WebDavAuth) @@ -33,7 +33,7 @@ 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 file \"%s\" error: %s", file, err.Error()) } img, _, _ := image.Decode(resp.Body) diff --git a/pkg/thumbnails/storage/filesystem.go b/pkg/thumbnails/storage/filesystem.go index 0466a1f3f3..e306a1149d 100644 --- a/pkg/thumbnails/storage/filesystem.go +++ b/pkg/thumbnails/storage/filesystem.go @@ -7,15 +7,16 @@ import ( "os" "path/filepath" "strconv" + + "github.com/owncloud/ocis-thumbnails/pkg/config" ) -const BasePath = "/home/corby/tmp/thumbnails/fs/" - type FileSystem struct { + cfg config.FilesystemStorage } func (s FileSystem) Get(key string) []byte { - content, err := ioutil.ReadFile(BasePath + key) + content, err := ioutil.ReadFile(filepath.Join(s.cfg.RootDirectory, key)) if err != nil { return nil } @@ -24,12 +25,13 @@ func (s FileSystem) Get(key string) []byte { } func (s FileSystem) Set(key string, img []byte) error { - folder := filepath.Dir(BasePath + key) + path := filepath.Join(s.cfg.RootDirectory, key) + folder := filepath.Dir(path) if err := createFolderIfNotExists(folder); err != nil { return fmt.Errorf("error while creating folder %s", folder) } - f, err := os.Create(BasePath + key) + f, err := os.Create(path) if err != nil { fmt.Println(err.Error()) return err