mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-04-25 09:48:08 -04:00
rename context
This commit is contained in:
@@ -52,36 +52,36 @@ func (g Thumbnail) GetThumbnail(ctx context.Context, req *v0proto.GetRequest, rs
|
||||
return fmt.Errorf("can't be encoded. filetype %s not supported", req.Filetype.String())
|
||||
}
|
||||
r := g.resolutions.ClosestMatch(int(req.Width), int(req.Height))
|
||||
tCtx := thumbnails.Context{
|
||||
tr := thumbnails.Request{
|
||||
Resolution: r,
|
||||
ImagePath: req.Filepath,
|
||||
Encoder: encoder,
|
||||
ETag: req.Etag,
|
||||
}
|
||||
|
||||
thumbnail := g.manager.GetStored(tCtx)
|
||||
thumbnail := g.manager.GetStored(tr)
|
||||
if thumbnail != nil {
|
||||
rsp.Thumbnail = thumbnail
|
||||
rsp.Mimetype = tCtx.Encoder.MimeType()
|
||||
rsp.Mimetype = tr.Encoder.MimeType()
|
||||
return nil
|
||||
}
|
||||
|
||||
auth := req.Authorization
|
||||
sCtx := context.WithValue(ctx, imgsource.WebDavAuth, auth)
|
||||
// TODO: clean up error handling
|
||||
img, err := g.source.Get(sCtx, tCtx.ImagePath)
|
||||
img, err := g.source.Get(sCtx, tr.ImagePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if img == nil {
|
||||
return fmt.Errorf("could not retrieve image")
|
||||
}
|
||||
thumbnail, err = g.manager.Get(tCtx, img)
|
||||
thumbnail, err = g.manager.Get(tr, img)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rsp.Thumbnail = thumbnail
|
||||
rsp.Mimetype = tCtx.Encoder.MimeType()
|
||||
rsp.Mimetype = tr.Encoder.MimeType()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,10 +64,10 @@ func (s FileSystem) Set(key string, img []byte) error {
|
||||
// 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(ctx Context) string {
|
||||
etag := ctx.ETag
|
||||
filetype := ctx.Types[0]
|
||||
filename := ctx.Resolution.String() + "." + filetype
|
||||
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])
|
||||
|
||||
@@ -29,11 +29,11 @@ func (s InMemory) Set(key string, thumbnail []byte) error {
|
||||
}
|
||||
|
||||
// BuildKey generates a unique key to store and retrieve the thumbnail.
|
||||
func (s InMemory) BuildKey(ctx Context) string {
|
||||
func (s InMemory) BuildKey(r Request) string {
|
||||
parts := []string{
|
||||
ctx.ETag,
|
||||
ctx.Resolution.String(),
|
||||
strings.Join(ctx.Types, ","),
|
||||
r.ETag,
|
||||
r.Resolution.String(),
|
||||
strings.Join(r.Types, ","),
|
||||
}
|
||||
return strings.Join(parts, "+")
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ package storage
|
||||
|
||||
import "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/resolutions"
|
||||
|
||||
// Context combines different attributes needed for storage operations.
|
||||
type Context struct {
|
||||
// Request combines different attributes needed for storage operations.
|
||||
type Request struct {
|
||||
ETag string
|
||||
Types []string
|
||||
Resolution resolutions.Resolution
|
||||
@@ -13,5 +13,5 @@ type Context struct {
|
||||
type Storage interface {
|
||||
Get(string) []byte
|
||||
Set(string, []byte) error
|
||||
BuildKey(Context) string
|
||||
BuildKey(Request) string
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage"
|
||||
)
|
||||
|
||||
// Context bundles information needed to generate a thumbnail for afile
|
||||
type Context struct {
|
||||
// Request bundles information needed to generate a thumbnail for afile
|
||||
type Request struct {
|
||||
Resolution resolutions.Resolution
|
||||
ImagePath string
|
||||
Encoder Encoder
|
||||
@@ -21,10 +21,10 @@ type Context struct {
|
||||
// Manager is responsible for generating thumbnails
|
||||
type Manager interface {
|
||||
// Get will return a thumbnail for a file
|
||||
Get(Context, image.Image) ([]byte, error)
|
||||
Get(Request, image.Image) ([]byte, error)
|
||||
// GetStored loads the thumbnail from the storage.
|
||||
// It will return nil if no image is stored for the given context.
|
||||
GetStored(Context) []byte
|
||||
GetStored(Request) []byte
|
||||
}
|
||||
|
||||
// NewSimpleManager creates a new instance of SimpleManager
|
||||
@@ -42,13 +42,13 @@ type SimpleManager struct {
|
||||
}
|
||||
|
||||
// Get implements the Get Method of Manager
|
||||
func (s SimpleManager) Get(ctx Context, img image.Image) ([]byte, error) {
|
||||
thumbnail := s.generate(ctx, img)
|
||||
func (s SimpleManager) Get(r Request, img image.Image) ([]byte, error) {
|
||||
thumbnail := s.generate(r, img)
|
||||
|
||||
key := s.storage.BuildKey(mapToStorageContext(ctx))
|
||||
key := s.storage.BuildKey(mapToStorageRequest(r))
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
err := ctx.Encoder.Encode(buf, thumbnail)
|
||||
err := r.Encoder.Encode(buf, thumbnail)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -62,22 +62,22 @@ func (s SimpleManager) Get(ctx Context, img image.Image) ([]byte, error) {
|
||||
|
||||
// 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))
|
||||
func (s SimpleManager) GetStored(r Request) []byte {
|
||||
key := s.storage.BuildKey(mapToStorageRequest(r))
|
||||
stored := s.storage.Get(key)
|
||||
return stored
|
||||
}
|
||||
|
||||
func (s SimpleManager) generate(ctx Context, img image.Image) image.Image {
|
||||
thumbnail := resize.Thumbnail(uint(ctx.Resolution.Width), uint(ctx.Resolution.Height), img, resize.Lanczos2)
|
||||
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)
|
||||
return thumbnail
|
||||
}
|
||||
|
||||
func mapToStorageContext(ctx Context) storage.Context {
|
||||
sCtx := storage.Context{
|
||||
ETag: ctx.ETag,
|
||||
Resolution: ctx.Resolution,
|
||||
Types: ctx.Encoder.Types(),
|
||||
func mapToStorageRequest(r Request) storage.Request {
|
||||
sR := storage.Request{
|
||||
ETag: r.ETag,
|
||||
Resolution: r.Resolution,
|
||||
Types: r.Encoder.Types(),
|
||||
}
|
||||
return sCtx
|
||||
return sR
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user