From 9a6f34a2ae079e6035eaaa2b2a7e5296496e9aa5 Mon Sep 17 00:00:00 2001 From: Jenny Date: Tue, 24 Sep 2019 18:48:04 +0800 Subject: [PATCH 1/2] Photo url feature --- README.md | 10 ++++++++++ photo/photo.go | 47 +++++++++++++++++++++++++++++++++++++++++++++ photo/photo_test.go | 24 +++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 photo/photo.go create mode 100644 photo/photo_test.go diff --git a/README.md b/README.md index 0238782..3b5a3db 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,13 @@ Usage: # [Module] config/ Used to load environment variables given a valid environment, config file, and target 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" diff --git a/photo/photo.go b/photo/photo.go new file mode 100644 index 0000000..b174106 --- /dev/null +++ b/photo/photo.go @@ -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 +} diff --git a/photo/photo_test.go b/photo/photo_test.go new file mode 100644 index 0000000..e288374 --- /dev/null +++ b/photo/photo_test.go @@ -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) +} From 6baddb6cfbc26a202e600b0173dbc0c9fe71fe7a Mon Sep 17 00:00:00 2001 From: Jenny Date: Tue, 24 Sep 2019 18:59:34 +0800 Subject: [PATCH 2/2] Go mod and sum files --- photo/go.mod | 5 +++++ photo/go.sum | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 photo/go.mod create mode 100644 photo/go.sum diff --git a/photo/go.mod b/photo/go.mod new file mode 100644 index 0000000..d0fe363 --- /dev/null +++ b/photo/go.mod @@ -0,0 +1,5 @@ +module github.com/scrambledeggs/booky-go-common/photo + +go 1.12 + +require github.com/stretchr/testify v1.4.0 diff --git a/photo/go.sum b/photo/go.sum new file mode 100644 index 0000000..e863f51 --- /dev/null +++ b/photo/go.sum @@ -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=