refactor thumbnails

Signed-off-by: Christian Richter <crichter@owncloud.com>
This commit is contained in:
Christian Richter
2022-04-13 17:04:38 +02:00
parent afea0b4304
commit a919195f34
64 changed files with 69 additions and 67 deletions
@@ -0,0 +1,88 @@
package storage
import (
"io/fs"
"os"
"path/filepath"
"strconv"
"github.com/owncloud/ocis/extensions/thumbnails/pkg/config"
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/pkg/errors"
)
const (
filesDir = "files"
)
// NewFileSystemStorage creates a new instance of FileSystem
func NewFileSystemStorage(cfg config.FileSystemStorage, logger log.Logger) FileSystem {
return FileSystem{
root: cfg.RootDirectory,
logger: logger,
}
}
// FileSystem represents a storage for the thumbnails using the local file system.
type FileSystem struct {
root string
logger log.Logger
}
func (s FileSystem) Stat(key string) bool {
img := filepath.Join(s.root, filesDir, key)
if _, err := os.Stat(img); err != nil {
return false
}
return true
}
func (s FileSystem) Get(key string) ([]byte, error) {
img := filepath.Join(s.root, filesDir, key)
content, err := os.ReadFile(img)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
s.logger.Debug().Str("err", err.Error()).Str("key", key).Msg("could not load thumbnail from store")
}
return nil, err
}
return content, nil
}
func (s FileSystem) Put(key string, img []byte) error {
imgPath := filepath.Join(s.root, filesDir, key)
dir := filepath.Dir(imgPath)
if err := os.MkdirAll(dir, 0700); err != nil {
return errors.Wrapf(err, "error while creating directory %s", dir)
}
if _, err := os.Stat(imgPath); os.IsNotExist(err) {
f, err := os.Create(imgPath)
if err != nil {
return errors.Wrapf(err, "could not create file \"%s\"", key)
}
defer f.Close()
if _, err = f.Write(img); err != nil {
return errors.Wrapf(err, "could not write to file \"%s\"", key)
}
}
return nil
}
// BuildKey generate the unique key for a thumbnail.
// The key is structure as follows:
//
// <first two letters of checksum>/<next two letters of checksum>/<rest of checksum>/<width>x<height>.<filetype>
//
// e.g. 97/9f/4c8db98f7b82e768ef478d3c8612/500x300.png
//
// The key also represents the path to the thumbnail in the filesystem under the configured root directory.
func (s FileSystem) BuildKey(r Request) string {
checksum := r.Checksum
filetype := r.Types[0]
filename := strconv.Itoa(r.Resolution.Dx()) + "x" + strconv.Itoa(r.Resolution.Dy()) + "." + filetype
return filepath.Join(checksum[:2], checksum[2:4], checksum[4:], filename)
}
@@ -0,0 +1,44 @@
package storage
import (
"strings"
)
// NewInMemoryStorage creates a new InMemory instance.
func NewInMemoryStorage() InMemory {
return InMemory{
store: make(map[string][]byte),
}
}
// InMemory represents an in memory storage for thumbnails
// Can be used during development
type InMemory struct {
store map[string][]byte
}
func (s InMemory) Stat(key string) bool {
_, exists := s.store[key]
return exists
}
// Get loads the thumbnail from memory.
func (s InMemory) Get(key string) ([]byte, error) {
return s.store[key], nil
}
// Set stores the thumbnail in memory.
func (s InMemory) Put(key string, thumbnail []byte) error {
s.store[key] = thumbnail
return nil
}
// BuildKey generates a unique key to store and retrieve the thumbnail.
func (s InMemory) BuildKey(r Request) string {
parts := []string{
r.Checksum,
r.Resolution.String(),
strings.Join(r.Types, ","),
}
return strings.Join(parts, "+")
}
@@ -0,0 +1,26 @@
package storage
import (
"image"
)
// Request combines different attributes needed for storage operations.
type Request struct {
// The checksum of the source file
// Will be used to determine if a thumbnail exists
Checksum string
// Types provided by the encoder.
// Contains the mimetypes of the thumbnail.
// In case of jpg/jpeg it will contain both.
Types []string
// The resolution of the thumbnail
Resolution image.Rectangle
}
// Storage defines the interface for a thumbnail store.
type Storage interface {
Stat(string) bool
Get(string) ([]byte, error)
Put(string, []byte) error
BuildKey(Request) string
}