rename package

This commit is contained in:
David Christofas
2020-03-26 09:49:37 +01:00
parent 0e2223819d
commit e9c2de26a5
12 changed files with 21 additions and 21 deletions
+82
View File
@@ -0,0 +1,82 @@
package storage
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"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, logger log.Logger) FileSystem {
return FileSystem{
dir: cfg.RootDirectory,
logger: logger,
}
}
// FileSystem represents a storage for the thumbnails using the local file system.
type FileSystem struct {
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
}
return content
}
// Set writes the image to the file system.
func (s FileSystem) Set(key string, img []byte) error {
path := filepath.Join(s.dir, key)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("error while creating directory %s", dir)
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("could not create file \"%s\" error: %s", key, err.Error())
}
defer f.Close()
_, err = f.Write(img)
if err != nil {
return fmt.Errorf("could not write to file \"%s\" error: %s", key, err.Error())
}
return nil
}
// BuildKey generate the unique key for a thumbnail.
// The key is structure as follows:
//
// <first two letters of etag>/<next two letters of etag>/<rest of etag>/<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 {
etag := r.ETag
filetype := r.Types[0]
filename := r.Resolution.String() + "." + filetype
key := new(bytes.Buffer)
key.WriteString(etag[:2])
key.WriteRune('/')
key.WriteString(etag[2:4])
key.WriteRune('/')
key.WriteString(etag[4:])
key.WriteRune('/')
key.WriteString(filename)
return key.String()
}
+39
View File
@@ -0,0 +1,39 @@
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
}
// Get loads the thumbnail from memory.
func (s InMemory) Get(key string) []byte {
return s.store[key]
}
// Set stores the thumbnail in memory.
func (s InMemory) Set(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.ETag,
r.Resolution.String(),
strings.Join(r.Types, ","),
}
return strings.Join(parts, "+")
}
+17
View File
@@ -0,0 +1,17 @@
package storage
import "github.com/owncloud/ocis-thumbnails/pkg/thumbnail/resolution"
// Request combines different attributes needed for storage operations.
type Request struct {
ETag string
Types []string
Resolution resolution.Resolution
}
// Storage defines the interface for a thumbnail store.
type Storage interface {
Get(string) []byte
Set(string, []byte) error
BuildKey(Request) string
}