Skip to content

Commit

Permalink
Merge pull request #6 from scrambledeggs/develop
Browse files Browse the repository at this point in the history
Add image-module to master
  • Loading branch information
jlipa authored Sep 25, 2019
2 parents 230e1d1 + e1945a8 commit 3268ca3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ Usage:
- `ApplyEnvConfig()`- *Required.* Sets environment variables based on config read.
- `SetConfig(configFile, configFilePath, configFileType)` - *Optional.* Sets config file name and path. Defaults to **./config.yml**
- `SetCipherPass(passphrase)` - *Required when config contains encrypted keys.* Provide passphrase for the encrypted data

# [Module] photo/
Generates Booky's Image URL. Module is named `photo` as to not override go's `image` package

Usage: FormatImageURL(ID int, assetType string, filename string, extra ...string)
- ID - ID of entity.
- assetType - Type of entity (e.g. 'offers' or 'brands')
- filename - Image filename
- extra - Accepts up to two optional parameters. Sets imageSize(default:`original`) and imageType(default:`photo`).
- Sample Output: "https://assets1.phonebooky.com/brands/photos/000/000/020/original/sample.jpg"
5 changes: 5 additions & 0 deletions photo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/scrambledeggs/booky-go-common/photo

go 1.12

require github.com/stretchr/testify v1.4.0
10 changes: 10 additions & 0 deletions photo/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
47 changes: 47 additions & 0 deletions photo/photo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package photo

import (
"strconv"
"strings"
)

const (
ASSET_URL string = "https://assets1.phonebooky.com"
)

// Format image filename
// Accept asset id, asset type, image filename (Optional: image size, image type)
// Image URL Format : ASSET_URL/ASSET_TYPE/IMAGE_TYPE/ID_URL/IMAGE_SIZE/FILENAME
func FormatImageURL(id int, assetType string, filename string, extra ...string) string {
if filename == "" {
return ""
}

// default values
imageSize := "original"
imageType := "photos"
if len(extra) > 0 {
if extra[0] != "" {
imageSize = extra[0]
}

if len(extra) > 1 && extra[1] != "" {
imageType = extra[1]
}
}

return strings.Join([]string{ASSET_URL, assetType, imageType, FormatIDUrl(id), imageSize, filename}, "/")
}

// Format ID for URL
func FormatIDUrl(id int) string {
standardLength := 9

// Pad with 0 up to 9 digits
idURL := strings.Repeat("0", standardLength) + strconv.Itoa(id)
idURL = idURL[(len(idURL) - standardLength):]
// Add / for every len 3 e.g. 000/009/012
idURL = idURL[:3] + "/" + idURL[3:6] + "/" + idURL[6:9]

return idURL
}
24 changes: 24 additions & 0 deletions photo/photo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package photo

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFormatImageURL(t *testing.T) {
idURL := FormatIDUrl(20)
assert.Equal(t, "000/000/020", idURL)

imageUrl := FormatImageURL(20, "listings", "sample.jpg")
assert.Equal(t, "https://assets1.phonebooky.com/listings/photos/000/000/020/original/sample.jpg", imageUrl)

imageUrl = FormatImageURL(20, "listings", "sample.jpg", "medium")
assert.Equal(t, "https://assets1.phonebooky.com/listings/photos/000/000/020/medium/sample.jpg", imageUrl)

imageUrl = FormatImageURL(20, "listings", "sample.jpg", "medium", "logos")
assert.Equal(t, "https://assets1.phonebooky.com/listings/logos/000/000/020/medium/sample.jpg", imageUrl)

imageUrl = FormatImageURL(20, "listings", "")
assert.Equal(t, "", imageUrl)
}

0 comments on commit 3268ca3

Please sign in to comment.