diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index 28b3052e84..5ae4cb095a 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -6,6 +6,7 @@ import ( "strconv" "github.com/go-chi/chi" + "github.com/owncloud/ocis-pkg/v2/log" "github.com/owncloud/ocis-thumbnails/pkg/config" "github.com/owncloud/ocis-thumbnails/pkg/thumbnails" "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/imgsource" @@ -28,10 +29,15 @@ func NewService(opts ...Option) Service { svc := Thumbnail{ config: options.Config, mux: m, - manager: thumbnails.SimpleManager{ - Storage: storage.NewFileSystemStorage(options.Config.FileSystemStorage), - }, + manager: thumbnails.NewSimpleManager( + storage.NewFileSystemStorage( + options.Config.FileSystemStorage, + options.Logger, + ), + options.Logger, + ), source: imgsource.NewWebDavSource(options.Config.WebDavSource), + logger: options.Logger, } m.Route(options.Config.HTTP.Root, func(r chi.Router) { @@ -47,6 +53,7 @@ type Thumbnail struct { mux *chi.Mux manager thumbnails.Manager source imgsource.Source + logger log.Logger } // ServeHTTP implements the Service interface. diff --git a/pkg/thumbnails/storage/filesystem.go b/pkg/thumbnails/storage/filesystem.go index e92e91bdf2..e59b77170e 100644 --- a/pkg/thumbnails/storage/filesystem.go +++ b/pkg/thumbnails/storage/filesystem.go @@ -8,25 +8,29 @@ import ( "path/filepath" "strconv" + "github.com/owncloud/ocis-pkg/v2/log" "github.com/owncloud/ocis-thumbnails/pkg/config" ) // NewFileSystemStorage creates a new instanz of FileSystem -func NewFileSystemStorage(cfg config.FileSystemStorage) FileSystem { +func NewFileSystemStorage(cfg config.FileSystemStorage, logger log.Logger) FileSystem { return FileSystem{ - dir: cfg.RootDirectory, + dir: cfg.RootDirectory, + logger: logger, } } // FileSystem represents a storage for the thumbnails using the local file system. type FileSystem struct { - dir string + dir string + logger log.Logger } // Get loads the image from the file system. func (s FileSystem) Get(key string) []byte { content, err := ioutil.ReadFile(filepath.Join(s.dir, key)) if err != nil { + s.logger.Warn().Err(err).Msgf("could not read file %s", key) return nil } @@ -43,13 +47,12 @@ func (s FileSystem) Set(key string, img []byte) error { f, err := os.Create(path) if err != nil { - fmt.Println(err.Error()) - return err + return fmt.Errorf("could not create file \"%s\" error: %s", key, err.Error()) } defer f.Close() _, err = f.Write(img) if err != nil { - return err + return fmt.Errorf("could not write to file \"%s\" error: %s", key, err.Error()) } return nil } diff --git a/pkg/thumbnails/thumbnails.go b/pkg/thumbnails/thumbnails.go index 7172bacd4c..c13f4c5b0c 100644 --- a/pkg/thumbnails/thumbnails.go +++ b/pkg/thumbnails/thumbnails.go @@ -5,6 +5,7 @@ import ( "image" "github.com/nfnt/resize" + "github.com/owncloud/ocis-pkg/v2/log" "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage" ) @@ -26,16 +27,24 @@ type Manager interface { GetStored(Context) []byte } +func NewSimpleManager(storage storage.Storage, logger log.Logger) SimpleManager { + return SimpleManager{ + storage: storage, + logger: logger, + } +} + // SimpleManager is a simple implementation of Manager type SimpleManager struct { - Storage storage.Storage + storage storage.Storage + logger log.Logger } // Get implements the Get Method of Manager func (s SimpleManager) Get(ctx Context, img image.Image) ([]byte, error) { thumbnail := s.generate(ctx, img) - key := s.Storage.BuildKey(mapToStorageContext(ctx)) + key := s.storage.BuildKey(mapToStorageContext(ctx)) buf := new(bytes.Buffer) err := ctx.Encoder.Encode(buf, thumbnail) @@ -43,15 +52,18 @@ func (s SimpleManager) Get(ctx Context, img image.Image) ([]byte, error) { return nil, err } bytes := buf.Bytes() - s.Storage.Set(key, bytes) + err = s.storage.Set(key, bytes) + if err != nil { + s.logger.Warn().Err(err).Msg("could not store thumbnail") + } return bytes, nil } // GetStored tries to get the stored thumbnail and return it. // If there is no cached thumbnail it will return nil func (s SimpleManager) GetStored(ctx Context) []byte { - key := s.Storage.BuildKey(mapToStorageContext(ctx)) - stored := s.Storage.Get(key) + key := s.storage.BuildKey(mapToStorageContext(ctx)) + stored := s.storage.Get(key) return stored }