add config for filesystem storage

This commit is contained in:
David Christofas
2020-03-09 16:01:20 +01:00
parent f55199f01b
commit 975387822b
4 changed files with 27 additions and 12 deletions

View File

@@ -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.

View File

@@ -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,
},
}
}

View File

@@ -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)

View File

@@ -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