rename cache to storage
This commit is contained in:
@@ -7,8 +7,8 @@ import (
|
|||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"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/cache"
|
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/imgsource"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/imgsource"
|
||||||
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service defines the extension handlers.
|
// Service defines the extension handlers.
|
||||||
@@ -28,7 +28,7 @@ func NewService(opts ...Option) Service {
|
|||||||
config: options.Config,
|
config: options.Config,
|
||||||
mux: m,
|
mux: m,
|
||||||
manager: thumbnails.SimpleManager{
|
manager: thumbnails.SimpleManager{
|
||||||
Cache: cache.NewInMemoryCache(),
|
Storage: storage.NewInMemoryStorage(),
|
||||||
},
|
},
|
||||||
source: imgsource.WebDav{
|
source: imgsource.WebDav{
|
||||||
Basepath: "http://localhost:9140/remote.php/webdav/",
|
Basepath: "http://localhost:9140/remote.php/webdav/",
|
||||||
@@ -77,7 +77,7 @@ func (g Thumbnails) Thumbnails(w http.ResponseWriter, r *http.Request) {
|
|||||||
Encoder: encoder,
|
Encoder: encoder,
|
||||||
}
|
}
|
||||||
|
|
||||||
thumbnail := g.manager.GetCached(ctx)
|
thumbnail := g.manager.GetStored(ctx)
|
||||||
if thumbnail != nil {
|
if thumbnail != nil {
|
||||||
w.Write(thumbnail)
|
w.Write(thumbnail)
|
||||||
return
|
return
|
||||||
|
|||||||
Vendored
-22
@@ -1,22 +0,0 @@
|
|||||||
package cache
|
|
||||||
|
|
||||||
import "image"
|
|
||||||
|
|
||||||
func NewInMemoryCache() InMemoryCache {
|
|
||||||
return InMemoryCache{
|
|
||||||
store: make(map[string]image.Image),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type InMemoryCache struct {
|
|
||||||
store map[string]image.Image
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fsc InMemoryCache) Get(key string) image.Image {
|
|
||||||
return fsc.store[key]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fsc InMemoryCache) Set(key string, thumbnail image.Image) (image.Image, error) {
|
|
||||||
fsc.store[key] = thumbnail
|
|
||||||
return thumbnail, nil
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
package cache
|
package storage
|
||||||
|
|
||||||
// TODO: implement filesystem cache for longer persistence
|
// TODO: implement filesystem cache for longer persistence
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
|
||||||
|
func NewInMemoryStorage() InMemoryStorage {
|
||||||
|
return InMemoryStorage{
|
||||||
|
store: make(map[string]image.Image),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type InMemoryStorage struct {
|
||||||
|
store map[string]image.Image
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fsc InMemoryStorage) Get(key string) image.Image {
|
||||||
|
return fsc.store[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fsc InMemoryStorage) Set(key string, thumbnail image.Image) (image.Image, error) {
|
||||||
|
fsc.store[key] = thumbnail
|
||||||
|
return thumbnail, nil
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package cache
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Cache defines the interface for a thumbnail cache.
|
// Storage defines the interface for a thumbnail store.
|
||||||
type Cache interface {
|
type Storage interface {
|
||||||
Get(key string) image.Image
|
Get(key string) image.Image
|
||||||
Set(key string, thumbnail image.Image) (image.Image, error)
|
Set(key string, thumbnail image.Image) (image.Image, error)
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/cache"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ThumbnailContext bundles information needed to generate a thumbnail for afile
|
// ThumbnailContext bundles information needed to generate a thumbnail for afile
|
||||||
@@ -22,20 +22,20 @@ type ThumbnailContext struct {
|
|||||||
type Manager interface {
|
type Manager interface {
|
||||||
// Get will return a thumbnail for a file
|
// Get will return a thumbnail for a file
|
||||||
Get(ThumbnailContext, image.Image) ([]byte, error)
|
Get(ThumbnailContext, image.Image) ([]byte, error)
|
||||||
GetCached(ThumbnailContext) []byte
|
GetStored(ThumbnailContext) []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// SimpleManager is a simple implementation of Manager
|
// SimpleManager is a simple implementation of Manager
|
||||||
type SimpleManager struct {
|
type SimpleManager struct {
|
||||||
Cache cache.Cache
|
Storage storage.Storage
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get implements the Get Method of Manager
|
// Get implements the Get Method of Manager
|
||||||
func (s SimpleManager) Get(ctx ThumbnailContext, img image.Image) ([]byte, error) {
|
func (s SimpleManager) Get(ctx ThumbnailContext, img image.Image) ([]byte, error) {
|
||||||
thumbnail := s.generate(ctx, img)
|
thumbnail := s.generate(ctx, img)
|
||||||
|
|
||||||
key := buildCacheKey(ctx)
|
key := buildKey(ctx)
|
||||||
s.Cache.Set(key, thumbnail)
|
s.Storage.Set(key, thumbnail)
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
err := ctx.Encoder.Encode(buf, thumbnail)
|
err := ctx.Encoder.Encode(buf, thumbnail)
|
||||||
@@ -45,16 +45,16 @@ func (s SimpleManager) Get(ctx ThumbnailContext, img image.Image) ([]byte, error
|
|||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCached tries to get the cached 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 stored thumbnail it will return nil
|
||||||
func (s SimpleManager) GetCached(ctx ThumbnailContext) []byte {
|
func (s SimpleManager) GetStored(ctx ThumbnailContext) []byte {
|
||||||
key := buildCacheKey(ctx)
|
key := buildKey(ctx)
|
||||||
cached := s.Cache.Get(key)
|
stored := s.Storage.Get(key)
|
||||||
if cached == nil {
|
if stored == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
ctx.Encoder.Encode(buf, cached)
|
ctx.Encoder.Encode(buf, stored)
|
||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ func (s SimpleManager) generate(ctx ThumbnailContext, img image.Image) image.Ima
|
|||||||
return thumbnail
|
return thumbnail
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildCacheKey(ctx ThumbnailContext) string {
|
func buildKey(ctx ThumbnailContext) string {
|
||||||
parts := []string{
|
parts := []string{
|
||||||
ctx.ImagePath,
|
ctx.ImagePath,
|
||||||
string(ctx.Width) + "x" + string(ctx.Height),
|
string(ctx.Width) + "x" + string(ctx.Height),
|
||||||
|
|||||||
Reference in New Issue
Block a user