Files
ocis/pkg/thumbnail/thumbnails.go
T

83 lines
2.1 KiB
Go
Raw Normal View History

2020-03-26 09:49:37 +01:00
package thumbnail
2020-03-03 16:40:02 +01:00
import (
"bytes"
"image"
"github.com/nfnt/resize"
2020-03-11 15:11:12 +01:00
"github.com/owncloud/ocis-pkg/v2/log"
2020-03-26 09:49:37 +01:00
"github.com/owncloud/ocis-thumbnails/pkg/thumbnail/resolution"
"github.com/owncloud/ocis-thumbnails/pkg/thumbnail/storage"
2020-03-03 16:40:02 +01:00
)
2020-03-25 16:56:45 +01:00
// Request bundles information needed to generate a thumbnail for afile
type Request struct {
2020-03-26 09:49:37 +01:00
Resolution resolution.Resolution
ImagePath string
Encoder Encoder
ETag string
2020-03-03 16:40:02 +01:00
}
// Manager is responsible for generating thumbnails
type Manager interface {
// Get will return a thumbnail for a file
2020-03-25 16:56:45 +01:00
Get(Request, image.Image) ([]byte, error)
2020-03-09 17:03:33 +01:00
// GetStored loads the thumbnail from the storage.
// It will return nil if no image is stored for the given context.
2020-03-25 16:56:45 +01:00
GetStored(Request) []byte
2020-03-03 16:40:02 +01:00
}
2020-03-11 15:18:32 +01:00
// NewSimpleManager creates a new instance of SimpleManager
2020-03-11 15:11:12 +01:00
func NewSimpleManager(storage storage.Storage, logger log.Logger) SimpleManager {
return SimpleManager{
storage: storage,
logger: logger,
}
}
2020-03-03 16:40:02 +01:00
// SimpleManager is a simple implementation of Manager
type SimpleManager struct {
2020-03-11 15:11:12 +01:00
storage storage.Storage
logger log.Logger
2020-03-03 16:40:02 +01:00
}
// Get implements the Get Method of Manager
2020-03-25 16:56:45 +01:00
func (s SimpleManager) Get(r Request, img image.Image) ([]byte, error) {
thumbnail := s.generate(r, img)
2020-03-03 16:40:02 +01:00
2020-03-25 16:56:45 +01:00
key := s.storage.BuildKey(mapToStorageRequest(r))
2020-03-03 16:40:02 +01:00
buf := new(bytes.Buffer)
2020-03-25 16:56:45 +01:00
err := r.Encoder.Encode(buf, thumbnail)
2020-03-03 16:40:02 +01:00
if err != nil {
return nil, err
}
2020-03-05 17:13:19 +01:00
bytes := buf.Bytes()
2020-03-11 15:11:12 +01:00
err = s.storage.Set(key, bytes)
if err != nil {
s.logger.Warn().Err(err).Msg("could not store thumbnail")
}
2020-03-05 17:13:19 +01:00
return bytes, nil
2020-03-03 16:40:02 +01:00
}
2020-03-05 15:04:40 +01:00
// GetStored tries to get the stored thumbnail and return it.
2020-03-05 17:13:19 +01:00
// If there is no cached thumbnail it will return nil
2020-03-25 16:56:45 +01:00
func (s SimpleManager) GetStored(r Request) []byte {
key := s.storage.BuildKey(mapToStorageRequest(r))
2020-03-11 15:11:12 +01:00
stored := s.storage.Get(key)
2020-03-05 17:13:19 +01:00
return stored
2020-03-03 16:40:02 +01:00
}
2020-03-25 16:56:45 +01:00
func (s SimpleManager) generate(r Request, img image.Image) image.Image {
thumbnail := resize.Thumbnail(uint(r.Resolution.Width), uint(r.Resolution.Height), img, resize.Lanczos2)
2020-03-03 16:40:02 +01:00
return thumbnail
}
2020-03-25 16:56:45 +01:00
func mapToStorageRequest(r Request) storage.Request {
sR := storage.Request{
ETag: r.ETag,
Resolution: r.Resolution,
Types: r.Encoder.Types(),
2020-03-03 16:40:02 +01:00
}
2020-03-25 16:56:45 +01:00
return sR
2020-03-03 16:40:02 +01:00
}