diff --git a/business/jxutils/jxutils.go b/business/jxutils/jxutils.go index 849394f7c..ece22750d 100644 --- a/business/jxutils/jxutils.go +++ b/business/jxutils/jxutils.go @@ -733,14 +733,13 @@ func BatchString2Slice(strs ...string) (strList []string) { } func GetShortNameFromURL(strURL string) (shortName string) { - index := strings.LastIndex(strURL, "/") - index2 := strings.LastIndex(strURL, "?") + index := strings.Index(strURL, "?") if index != -1 { - if index2 == -1 { - shortName = strURL[index+1:] - } else { - shortName = strURL[index+1 : index2] - } + strURL = strURL[:index] + } + index = strings.LastIndex(strURL, "/") + if index != -1 { + shortName = strURL[index+1:] } return shortName } diff --git a/business/jxutils/jxutils_test.go b/business/jxutils/jxutils_test.go index c0ddbde4e..32d16ef68 100644 --- a/business/jxutils/jxutils_test.go +++ b/business/jxutils/jxutils_test.go @@ -188,3 +188,20 @@ func TestGetLastTimeFromList(t *testing.T) { } } + +func TestGetShortNameFromURL(t *testing.T) { + for _, v := range [][]string{ + []string{ + "20b569a7cf7e8045b2d7bb550900cf86.jpeg", + "http://image.jxc4.com/20b569a7cf7e8045b2d7bb550900cf86.jpeg?imageMogr2/thumbnail/x800/gravity/Center/crop/800x800", + }, + []string{ + "91b8dcb5cb6b462bc96078b7210470b7.jpg", + "http://image.jxc4.com/image/91b8dcb5cb6b462bc96078b7210470b7.jpg", + }, + } { + if str := GetShortNameFromURL(v[1]); str != v[0] { + t.Errorf("%s failed, result:%s, expect:%s", v[1], str, v[0]) + } + } +}