small refactorings
This commit is contained in:
@@ -8,7 +8,7 @@ import (
|
|||||||
v0proto "github.com/owncloud/ocis-thumbnails/pkg/proto/v0"
|
v0proto "github.com/owncloud/ocis-thumbnails/pkg/proto/v0"
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails"
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/imgsource"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/imgsource"
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/resolution"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/resolutions"
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ import (
|
|||||||
func NewService(opts ...Option) v0proto.ThumbnailServiceHandler {
|
func NewService(opts ...Option) v0proto.ThumbnailServiceHandler {
|
||||||
options := newOptions(opts...)
|
options := newOptions(opts...)
|
||||||
logger := options.Logger
|
logger := options.Logger
|
||||||
resolutions, err := resolution.Init(options.Config.Thumbnail.Resolutions)
|
resolutions, err := resolutions.New(options.Config.Thumbnail.Resolutions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal().Err(err).Msg("resolutions not configured correctly")
|
logger.Fatal().Err(err).Msg("resolutions not configured correctly")
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,7 @@ func NewService(opts ...Option) v0proto.ThumbnailServiceHandler {
|
|||||||
// Thumbnail implements the GRPC handler.
|
// Thumbnail implements the GRPC handler.
|
||||||
type Thumbnail struct {
|
type Thumbnail struct {
|
||||||
manager thumbnails.Manager
|
manager thumbnails.Manager
|
||||||
resolutions resolution.Resolutions
|
resolutions resolutions.Resolutions
|
||||||
source imgsource.Source
|
source imgsource.Source
|
||||||
logger log.Logger
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package resolution
|
package resolutions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package resolution
|
package resolutions
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
+4
-5
@@ -1,4 +1,4 @@
|
|||||||
package resolution
|
package resolutions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init creates an instance of Resolutions from resolution strings.
|
// New creates an instance of Resolutions from resolution strings.
|
||||||
func Init(rStrs []string) (Resolutions, error) {
|
func New(rStrs []string) (Resolutions, error) {
|
||||||
var rs Resolutions
|
var rs Resolutions
|
||||||
for _, rStr := range rStrs {
|
for _, rStr := range rStrs {
|
||||||
r, err := Parse(rStr)
|
r, err := Parse(rStr)
|
||||||
@@ -47,8 +47,7 @@ func (r Resolutions) ClosestMatch(width, height int) Resolution {
|
|||||||
var match Resolution
|
var match Resolution
|
||||||
minDiff := math.MaxInt32
|
minDiff := math.MaxInt32
|
||||||
|
|
||||||
for i := 1; i < len(r); i++ {
|
for _, current := range r {
|
||||||
current := r[i]
|
|
||||||
len := dimensionLength(current, isLandscape)
|
len := dimensionLength(current, isLandscape)
|
||||||
diff := givenLen - len
|
diff := givenLen - len
|
||||||
if diff > 0 {
|
if diff > 0 {
|
||||||
+9
-9
@@ -1,11 +1,11 @@
|
|||||||
package resolution
|
package resolutions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInitWithEmptyArray(t *testing.T) {
|
func TestInitWithEmptyArray(t *testing.T) {
|
||||||
rs, err := Init([]string{})
|
rs, err := New([]string{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Init with an empty array should not fail. Error: %s.\n", err.Error())
|
t.Errorf("Init with an empty array should not fail. Error: %s.\n", err.Error())
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ func TestInitWithEmptyArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInitWithNil(t *testing.T) {
|
func TestInitWithNil(t *testing.T) {
|
||||||
rs, err := Init(nil)
|
rs, err := New(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Init with nil parameter should not fail. Error: %s.\n", err.Error())
|
t.Errorf("Init with nil parameter should not fail. Error: %s.\n", err.Error())
|
||||||
}
|
}
|
||||||
@@ -25,14 +25,14 @@ func TestInitWithNil(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInitWithInvalidValuesInArray(t *testing.T) {
|
func TestInitWithInvalidValuesInArray(t *testing.T) {
|
||||||
_, err := Init([]string{"invalid"})
|
_, err := New([]string{"invalid"})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("Init with invalid parameter should fail.\n")
|
t.Error("Init with invalid parameter should fail.\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInit(t *testing.T) {
|
func TestInit(t *testing.T) {
|
||||||
rs, err := Init([]string{"16x16"})
|
rs, err := New([]string{"16x16"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Init with valid parameter should not fail. Error: %s.\n", err.Error())
|
t.Errorf("Init with valid parameter should not fail. Error: %s.\n", err.Error())
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@ func TestInit(t *testing.T) {
|
|||||||
|
|
||||||
func TestInitWithMultipleResolutions(t *testing.T) {
|
func TestInitWithMultipleResolutions(t *testing.T) {
|
||||||
rStrs := []string{"16x16", "32x32", "64x64", "128x128"}
|
rStrs := []string{"16x16", "32x32", "64x64", "128x128"}
|
||||||
rs, err := Init(rStrs)
|
rs, err := New(rStrs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Init with valid parameter should not fail. Error: %s.\n", err.Error())
|
t.Errorf("Init with valid parameter should not fail. Error: %s.\n", err.Error())
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ func TestInitWithMultipleResolutions(t *testing.T) {
|
|||||||
|
|
||||||
func TestInitWithMultipleResolutionsShouldBeSorted(t *testing.T) {
|
func TestInitWithMultipleResolutionsShouldBeSorted(t *testing.T) {
|
||||||
rStrs := []string{"32x32", "64x64", "16x16", "128x128"}
|
rStrs := []string{"32x32", "64x64", "16x16", "128x128"}
|
||||||
rs, err := Init(rStrs)
|
rs, err := New(rStrs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Init with valid parameter should not fail. Error: %s.\n", err.Error())
|
t.Errorf("Init with valid parameter should not fail. Error: %s.\n", err.Error())
|
||||||
}
|
}
|
||||||
@@ -72,7 +72,7 @@ func TestInitWithMultipleResolutionsShouldBeSorted(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
func TestClosestMatchWithEmptyResolutions(t *testing.T) {
|
func TestClosestMatchWithEmptyResolutions(t *testing.T) {
|
||||||
rs, _ := Init(nil)
|
rs, _ := New(nil)
|
||||||
width := 24
|
width := 24
|
||||||
height := 24
|
height := 24
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ func TestClosestMatchWithEmptyResolutions(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestClosestMatch(t *testing.T) {
|
func TestClosestMatch(t *testing.T) {
|
||||||
rs, _ := Init([]string{"16x16", "24x24", "32x32", "64x64", "128x128"})
|
rs, _ := New([]string{"16x16", "24x24", "32x32", "64x64", "128x128"})
|
||||||
table := [][]int{
|
table := [][]int{
|
||||||
// width, height, expectedWidth, expectedHeight
|
// width, height, expectedWidth, expectedHeight
|
||||||
[]int{17, 17, 24, 24},
|
[]int{17, 17, 24, 24},
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
import "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/resolution"
|
import "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/resolutions"
|
||||||
|
|
||||||
// Context combines different attributes needed for storage operations.
|
// Context combines different attributes needed for storage operations.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
ETag string
|
ETag string
|
||||||
Types []string
|
Types []string
|
||||||
Resolution resolution.Resolution
|
Resolution resolutions.Resolution
|
||||||
}
|
}
|
||||||
|
|
||||||
// Storage defines the interface for a thumbnail store.
|
// Storage defines the interface for a thumbnail store.
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
|
|
||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
"github.com/owncloud/ocis-pkg/v2/log"
|
"github.com/owncloud/ocis-pkg/v2/log"
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/resolution"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/resolutions"
|
||||||
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage"
|
"github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context bundles information needed to generate a thumbnail for afile
|
// Context bundles information needed to generate a thumbnail for afile
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Resolution resolution.Resolution
|
Resolution resolutions.Resolution
|
||||||
ImagePath string
|
ImagePath string
|
||||||
Encoder Encoder
|
Encoder Encoder
|
||||||
ETag string
|
ETag string
|
||||||
|
|||||||
Reference in New Issue
Block a user