From 58e71aae079d4e3fde39d8b0257af89b22a1715e Mon Sep 17 00:00:00 2001 From: Samuel Mulatua Date: Tue, 31 Dec 2019 14:33:18 +0700 Subject: [PATCH 1/6] feature: generate media URL --- media.go | 34 ++++++++++++++++++++++++++++++++++ media_test.go | 13 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 media.go create mode 100644 media_test.go diff --git a/media.go b/media.go new file mode 100644 index 0000000..fde62d1 --- /dev/null +++ b/media.go @@ -0,0 +1,34 @@ +package utils + +import "strings" + +const ( + _BigPicture = "big_picture" + _LargeIcon = "large_icon" + _IOSAttachment = "ios_attahcment" +) + +// GenerateMediaURL :nodoc: +func GenerateMediaURL(cdnURL, externalURL, imageType string) string { + if externalURL == "" { + return "" + } + splittedMediaExternalURL := strings.Split(externalURL, "/") + coverMediaFile := splittedMediaExternalURL[len(splittedMediaExternalURL)-2] + "/" + splittedMediaExternalURL[len(splittedMediaExternalURL)-1] + var param string + switch imageType { + + case _LargeIcon: + param = "fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1" + + case _BigPicture: + param = "fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1440,h_720" + + case _IOSAttachment: + param = "fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1024,h_1024" + } + + mediaURL := cdnURL + "/image/upload/" + param + "/" + coverMediaFile + + return mediaURL +} diff --git a/media_test.go b/media_test.go new file mode 100644 index 0000000..6b2156c --- /dev/null +++ b/media_test.go @@ -0,0 +1,13 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGenerateMediaURL(t *testing.T) { + externalMediaURL := "https://res.cloudinary.com/kumpar/image/upload/v1481882219/qaejp6n1amazr7ud2aaf.jpg" + res := GenerateMediaURL("http://blue.kumparan.com", externalMediaURL, _LargeIcon) + assert.Equal(t, "http://blue.kumparan.com/image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1/v1481882219/qaejp6n1amazr7ud2aaf.jpg", res) +} From 11d9f448af73a5d1386589d4b1a28a9ed1d8f37f Mon Sep 17 00:00:00 2001 From: Samuel Mulatua Date: Tue, 31 Dec 2019 14:40:37 +0700 Subject: [PATCH 2/6] bump: changelog --- CHANGELOG.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a4e0fd..6b33120 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,16 @@ # go-utils + +## [v1.1.0] - 2019-12-31 +### New Features +- generate media URL + + ## v1.0.0 - 2019-12-23 ### New Features - init go-utils -[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.0.0...HEAD +[Unreleased]: https://github.com/kumparan/kumnats/compare/v1.1.0...HEAD +[v1.1.0]: https://github.com/kumparan/kumnats/compare/v1.0.0...v1.1.0 From 41834e8232474b548242807185f4e1a2833f72c3 Mon Sep 17 00:00:00 2001 From: Samuel Mulatua Date: Tue, 31 Dec 2019 14:54:39 +0700 Subject: [PATCH 3/6] update naming --- media.go | 8 ++++---- media_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/media.go b/media.go index fde62d1..2fe6a1e 100644 --- a/media.go +++ b/media.go @@ -8,13 +8,13 @@ const ( _IOSAttachment = "ios_attahcment" ) -// GenerateMediaURL :nodoc: -func GenerateMediaURL(cdnURL, externalURL, imageType string) string { +// GeneratePushNotificationMediaURL :nodoc: +func GeneratePushNotificationMediaURL(cdnURL, externalURL, imageType string) string { if externalURL == "" { return "" } - splittedMediaExternalURL := strings.Split(externalURL, "/") - coverMediaFile := splittedMediaExternalURL[len(splittedMediaExternalURL)-2] + "/" + splittedMediaExternalURL[len(splittedMediaExternalURL)-1] + splitMediaExternalURL := strings.Split(externalURL, "/") + coverMediaFile := splitMediaExternalURL[len(splitMediaExternalURL)-2] + "/" + splitMediaExternalURL[len(splitMediaExternalURL)-1] var param string switch imageType { diff --git a/media_test.go b/media_test.go index 6b2156c..3314461 100644 --- a/media_test.go +++ b/media_test.go @@ -6,8 +6,8 @@ import ( "github.com/stretchr/testify/assert" ) -func TestGenerateMediaURL(t *testing.T) { - externalMediaURL := "https://res.cloudinary.com/kumpar/image/upload/v1481882219/qaejp6n1amazr7ud2aaf.jpg" - res := GenerateMediaURL("http://blue.kumparan.com", externalMediaURL, _LargeIcon) - assert.Equal(t, "http://blue.kumparan.com/image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1/v1481882219/qaejp6n1amazr7ud2aaf.jpg", res) +func Test_GeneratePushNotificationMediaURL(t *testing.T) { + externalMediaURL := "https://this.is/test/image/upload/123/234.jpg" + res := GeneratePushNotificationMediaURL("http://blue.kumparan.com", externalMediaURL, _LargeIcon) + assert.Equal(t, "http://blue.kumparan.com/image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1/123/234.jpg", res) } From e2a4edb8a660c3406ecdd54d974f37cd1485fcf7 Mon Sep 17 00:00:00 2001 From: Samuel Mulatua Date: Fri, 3 Jan 2020 14:29:28 +0700 Subject: [PATCH 4/6] fix testing and path concating --- media.go | 37 ++++++++++++++++++++++--------------- media_test.go | 4 ++-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/media.go b/media.go index 2fe6a1e..c690d53 100644 --- a/media.go +++ b/media.go @@ -1,34 +1,41 @@ package utils -import "strings" +import ( + "strings" +) const ( - _BigPicture = "big_picture" - _LargeIcon = "large_icon" - _IOSAttachment = "ios_attahcment" + // BigPicture :nodoc: + BigPicture = "big_picture" + // LargeIcon :nodoc: + LargeIcon = "large_icon" + // IOSAttachment :nodoc: + IOSAttachment = "ios_attahcment" ) // GeneratePushNotificationMediaURL :nodoc: -func GeneratePushNotificationMediaURL(cdnURL, externalURL, imageType string) string { - if externalURL == "" { +func GeneratePushNotificationMediaURL(baseImageURL, srcImageURL, imageType string) string { + if srcImageURL == "" || baseImageURL == "" { return "" } - splitMediaExternalURL := strings.Split(externalURL, "/") - coverMediaFile := splitMediaExternalURL[len(splitMediaExternalURL)-2] + "/" + splitMediaExternalURL[len(splitMediaExternalURL)-1] + + splitMediasrcImageURL := strings.Split(srcImageURL, "/") + coverMediaFile := splitMediasrcImageURL[len(splitMediasrcImageURL)-2] + "/" + splitMediasrcImageURL[len(splitMediasrcImageURL)-1] + var param string switch imageType { - case _LargeIcon: - param = "fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1" + case LargeIcon: + param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1" - case _BigPicture: - param = "fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1440,h_720" + case BigPicture: + param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1440,h_720" - case _IOSAttachment: - param = "fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1024,h_1024" + case IOSAttachment: + param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1024,h_1024" } - mediaURL := cdnURL + "/image/upload/" + param + "/" + coverMediaFile + mediaURL := baseImageURL + "/" + param + "/" + coverMediaFile return mediaURL } diff --git a/media_test.go b/media_test.go index 3314461..a0a5841 100644 --- a/media_test.go +++ b/media_test.go @@ -8,6 +8,6 @@ import ( func Test_GeneratePushNotificationMediaURL(t *testing.T) { externalMediaURL := "https://this.is/test/image/upload/123/234.jpg" - res := GeneratePushNotificationMediaURL("http://blue.kumparan.com", externalMediaURL, _LargeIcon) - assert.Equal(t, "http://blue.kumparan.com/image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1/123/234.jpg", res) + res := GeneratePushNotificationMediaURL("https://this.is", externalMediaURL, LargeIcon) + assert.Equal(t, "https://this.is/image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1/123/234.jpg", res) } From a45e2ed88f3ed8bbb4b703be6e27028c9c968f7b Mon Sep 17 00:00:00 2001 From: Samuel Mulatua Date: Fri, 3 Jan 2020 14:51:17 +0700 Subject: [PATCH 5/6] fix naming --- media.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/media.go b/media.go index c690d53..5a4fa8a 100644 --- a/media.go +++ b/media.go @@ -13,14 +13,15 @@ const ( IOSAttachment = "ios_attahcment" ) -// GeneratePushNotificationMediaURL :nodoc: -func GeneratePushNotificationMediaURL(baseImageURL, srcImageURL, imageType string) string { - if srcImageURL == "" || baseImageURL == "" { +// GeneratePushNotificationMediaURL Generates manipulated media URL for push notification purpose +// e.g. GeneratePushNotificationMediaURL("http://mycdn.com", "http://my.image.com/image/upload/v123/image.jpg", LargeIcon) => http://mycdn.com/image/upload/v123/image.jpg +func GeneratePushNotificationMediaURL(cdnURL, mediaSrcURL, imageType string) string { + if mediaSrcURL == "" || cdnURL == "" { return "" } - splitMediasrcImageURL := strings.Split(srcImageURL, "/") - coverMediaFile := splitMediasrcImageURL[len(splitMediasrcImageURL)-2] + "/" + splitMediasrcImageURL[len(splitMediasrcImageURL)-1] + splitMediaSrcImageURL := strings.Split(mediaSrcURL, "/") + coverMediaFile := splitMediaSrcImageURL[len(splitMediaSrcImageURL)-2] + "/" + splitMediaSrcImageURL[len(splitMediaSrcImageURL)-1] var param string switch imageType { @@ -35,7 +36,7 @@ func GeneratePushNotificationMediaURL(baseImageURL, srcImageURL, imageType strin param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1024,h_1024" } - mediaURL := baseImageURL + "/" + param + "/" + coverMediaFile + mediaURL := cdnURL + "/" + param + "/" + coverMediaFile return mediaURL } From 9461a97588524bbcaf30de6ab45277c228f54c21 Mon Sep 17 00:00:00 2001 From: Samuel Mulatua Date: Mon, 6 Jan 2020 10:59:13 +0700 Subject: [PATCH 6/6] update const naming --- media.go | 18 +++++++++--------- media_test.go | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/media.go b/media.go index 5a4fa8a..079e68a 100644 --- a/media.go +++ b/media.go @@ -5,12 +5,12 @@ import ( ) const ( - // BigPicture :nodoc: - BigPicture = "big_picture" - // LargeIcon :nodoc: - LargeIcon = "large_icon" - // IOSAttachment :nodoc: - IOSAttachment = "ios_attahcment" + // _BigPicture :nodoc: + _BigPicture = "big_picture" + // _LargeIcon :nodoc: + _LargeIcon = "large_icon" + // _IOSAttachment :nodoc: + _IOSAttachment = "ios_attahcment" ) // GeneratePushNotificationMediaURL Generates manipulated media URL for push notification purpose @@ -26,13 +26,13 @@ func GeneratePushNotificationMediaURL(cdnURL, mediaSrcURL, imageType string) str var param string switch imageType { - case LargeIcon: + case _LargeIcon: param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1" - case BigPicture: + case _BigPicture: param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1440,h_720" - case IOSAttachment: + case _IOSAttachment: param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1024,h_1024" } diff --git a/media_test.go b/media_test.go index a0a5841..c9edd51 100644 --- a/media_test.go +++ b/media_test.go @@ -8,6 +8,6 @@ import ( func Test_GeneratePushNotificationMediaURL(t *testing.T) { externalMediaURL := "https://this.is/test/image/upload/123/234.jpg" - res := GeneratePushNotificationMediaURL("https://this.is", externalMediaURL, LargeIcon) + res := GeneratePushNotificationMediaURL("https://this.is", externalMediaURL, _LargeIcon) assert.Equal(t, "https://this.is/image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1/123/234.jpg", res) }