Skip to content

Commit

Permalink
Merge pull request #97 from thoas/refac-application
Browse files Browse the repository at this point in the history
Refac application
  • Loading branch information
thoas authored Jul 11, 2018
2 parents b16a9e6 + 3530bf2 commit 9bd845d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 82 deletions.
135 changes: 70 additions & 65 deletions application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,104 +193,109 @@ func Delete(ctx context.Context, filepath string) error {

// ImageFileFromContext generates an ImageFile from gin context
func ImageFileFromContext(c *gin.Context, async bool, load bool) (*image.ImageFile, error) {
key := c.MustGet("key").(string)
storeKey := c.MustGet("key").(string)

k := kvstore.FromContext(c)

cfg := config.FromContext(c)

l := logger.FromContext(c)

destStorage := storage.DestinationFromContext(c)

var file = &image.ImageFile{
Key: key,
Storage: destStorage,
Headers: map[string]string{},
}
var err error
var filepath string

storeKey := key

// Image from the KVStore found
imageKey, err := k.Get(storeKey)
if err != nil {
return nil, err
}

if imageKey != nil {
stored, err := conv.String(imageKey)
if err != nil {
return nil, err
}

file.Filepath = stored

l.Infof("Key %s found in kvstore: %s", storeKey, stored)

if load {
file, err = image.FromStorage(destStorage, stored)

if err != nil {
return nil, err
}
}
} else {
l.Infof("Key %s not found in kvstore", storeKey)

u, exists := c.Get("url")
return fileFromStorage(c, l, storeKey, imageKey, load)
}

parameters := c.MustGet("parameters").(map[string]string)
// Image not found from the KVStore, we need to process it
// URL available in Query String
l.Infof("Key %s not found in kvstore", storeKey)
return processImage(c, l, storeKey, async)
}

// Image not found from the KVStore, we need to process it
// URL available in Query String
if exists {
file, err = image.FromURL(u.(*url.URL), cfg.Options.DefaultUserAgent)
} else {
// URL provided we use http protocol to retrieve it
s := storage.SourceFromContext(c)
func fileFromStorage(c *gin.Context, l logger.Logger, storeKey string, imageKey interface{}, load bool) (*image.ImageFile, error) {
stored, err := conv.String(imageKey)
if err != nil {
return nil, err
}

filepath = parameters["path"]
l.Infof("Key %s found in kvstore: %s", storeKey, stored)

if !s.Exists(filepath) {
return nil, errs.ErrFileNotExists
}
destStorage := storage.DestinationFromContext(c)

file, err = image.FromStorage(s, filepath)
}
file := &image.ImageFile{
Key: storeKey,
Storage: destStorage,
Filepath: stored,
Headers: map[string]string{},
}

if load {
file, err = image.FromStorage(destStorage, stored)
if err != nil {
return nil, err
}
}

op := c.MustGet("op").(engine.Operation)
file.Headers["ETag"] = storeKey
return file, nil
}

file, err = engine.FromContext(c).Transform(file, op, parameters)
func processImage(c *gin.Context, l logger.Logger, storeKey string, async bool) (*image.ImageFile, error) {
cfg := config.FromContext(c)
destStorage := storage.DestinationFromContext(c)

if err != nil {
return nil, err
file := &image.ImageFile{
Key: storeKey,
Storage: destStorage,
Headers: map[string]string{},
}

var filepath string
parameters := c.MustGet("parameters").(map[string]string)

var err error
u, exists := c.Get("url")
if exists {
file, err = image.FromURL(u.(*url.URL), cfg.Options.DefaultUserAgent)
} else {
// URL provided we use http protocol to retrieve it
s := storage.SourceFromContext(c)

filepath = parameters["path"]
if !s.Exists(filepath) {
return nil, errs.ErrFileNotExists
}

filename := ShardFilename(c, key)
file, err = image.FromStorage(s, filepath)
}
if err != nil {
return nil, err
}

file.Filepath = fmt.Sprintf("%s.%s", filename, file.Format())
op := c.MustGet("op").(engine.Operation)
file, err = engine.FromContext(c).Transform(file, op, parameters)
if err != nil {
return nil, err
}

file.Key = key
filename := ShardFilename(c, storeKey)
file.Filepath = fmt.Sprintf("%s.%s", filename, file.Format())
file.Storage = destStorage
file.Key = storeKey
file.Headers["ETag"] = storeKey

file.Headers["ETag"] = key

if imageKey == nil {
if async == true {
go Store(c, filepath, file)
} else {
err = Store(c, filepath, file)
if async == true {
go Store(c, filepath, file)
} else {
err = Store(c, filepath, file)
if err != nil {
return nil, err
}
}

return file, err
return file, nil
}

// ShardFilename shards a filename based on config
Expand Down
19 changes: 2 additions & 17 deletions signature/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"fmt"
"net/url"
"regexp"

"github.com/thoas/picfit/util"
)

var signRegex = regexp.MustCompile("&?sig=[^&]*")
Expand All @@ -17,7 +15,7 @@ var signRegex = regexp.MustCompile("&?sig=[^&]*")
func VerifyParameters(key string, qs map[string]string) bool {
params := url.Values{}

for k, v := range util.SortMapString(qs) {
for k, v := range qs {
params.Set(k, v)
}

Expand All @@ -37,24 +35,11 @@ func Sign(key string, qs string) string {
// SignRaw encodes raw query string (not sorted) using a key
func SignRaw(key string, queryString string) (string, error) {
values, err := url.ParseQuery(queryString)

if err != nil {
return "", err
}

qs := url.Values{}

params := map[string]string{}

for k, v := range values {
params[k] = v[0]
}

for k, v := range util.SortMapString(params) {
qs.Set(k, v)
}

return Sign(key, qs.Encode()), nil
return Sign(key, values.Encode()), nil
}

// AppendSign appends the signature to query string
Expand Down

0 comments on commit 9bd845d

Please sign in to comment.