From 0e2223819d37d4280232ad4e672be44da2fe9ac8 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Wed, 25 Mar 2020 16:56:45 +0100 Subject: [PATCH] rename context --- pkg/service/v0/service.go | 12 +++++----- pkg/thumbnails/storage/filesystem.go | 8 +++---- pkg/thumbnails/storage/inmemory.go | 8 +++---- pkg/thumbnails/storage/storage.go | 6 ++--- pkg/thumbnails/thumbnails.go | 36 ++++++++++++++-------------- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index c480d8ac1e..ca68414f43 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -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 } diff --git a/pkg/thumbnails/storage/filesystem.go b/pkg/thumbnails/storage/filesystem.go index c8f6042456..dc45d9de03 100644 --- a/pkg/thumbnails/storage/filesystem.go +++ b/pkg/thumbnails/storage/filesystem.go @@ -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]) diff --git a/pkg/thumbnails/storage/inmemory.go b/pkg/thumbnails/storage/inmemory.go index 74a78befd5..12017eed0c 100644 --- a/pkg/thumbnails/storage/inmemory.go +++ b/pkg/thumbnails/storage/inmemory.go @@ -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, "+") } diff --git a/pkg/thumbnails/storage/storage.go b/pkg/thumbnails/storage/storage.go index ca5ddf557c..48953a329b 100644 --- a/pkg/thumbnails/storage/storage.go +++ b/pkg/thumbnails/storage/storage.go @@ -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 } diff --git a/pkg/thumbnails/thumbnails.go b/pkg/thumbnails/thumbnails.go index fa22d398c7..2a0600795d 100644 --- a/pkg/thumbnails/thumbnails.go +++ b/pkg/thumbnails/thumbnails.go @@ -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 }