mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-20 03:20:44 -06:00
add proper logging
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user