Skip to content

Commit

Permalink
Merge pull request #1 from kumparan/feature/BAN-4273-generate-media-url
Browse files Browse the repository at this point in the history
feature: generate media URL
  • Loading branch information
zhenqianz authored Jan 6, 2020
2 parents 66768c1 + 9461a97 commit 4640eb3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# go-utils

<a name="v1.1.0"></a>
## [v1.1.0] - 2019-12-31
### New Features
- generate media URL


<a name="v1.0.0"></a>
## 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
42 changes: 42 additions & 0 deletions media.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package utils

import (
"strings"
)

const (
// _BigPicture :nodoc:
_BigPicture = "big_picture"
// _LargeIcon :nodoc:
_LargeIcon = "large_icon"
// _IOSAttachment :nodoc:
_IOSAttachment = "ios_attahcment"
)

// 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(mediaSrcURL, "/")
coverMediaFile := splitMediaSrcImageURL[len(splitMediaSrcImageURL)-2] + "/" + splitMediaSrcImageURL[len(splitMediaSrcImageURL)-1]

var param string
switch imageType {

case _LargeIcon:
param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_256,ar_1:1"

case _BigPicture:
param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1440,h_720"

case _IOSAttachment:
param = "image/upload/fl_progressive,fl_lossy,c_fill,g_face,q_auto:best,w_1024,h_1024"
}

mediaURL := cdnURL + "/" + param + "/" + coverMediaFile

return mediaURL
}
13 changes: 13 additions & 0 deletions media_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import (
"testing"

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

func Test_GeneratePushNotificationMediaURL(t *testing.T) {
externalMediaURL := "https://this.is/test/image/upload/123/234.jpg"
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)
}

0 comments on commit 4640eb3

Please sign in to comment.