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