code refactor

This commit is contained in:
MaysWind
2024-08-19 23:47:55 +08:00
parent 8fa19df113
commit 4977979b08
16 changed files with 273 additions and 69 deletions
-25
View File
@@ -1,25 +0,0 @@
package utils
import (
"fmt"
"strings"
)
const gravatarUrlFormat = "https://www.gravatar.com/avatar/%s"
// GetInternalAvatarUrl returns the internal avatar url
func GetInternalAvatarUrl(uid int64, avatarFileExtension string, webRootUrl string) string {
if avatarFileExtension == "" {
return ""
}
return fmt.Sprintf("%savatar/%d.%s", webRootUrl, uid, avatarFileExtension)
}
// GetGravatarUrl returns the Gravatar url according to the specified user email address
func GetGravatarUrl(email string) string {
email = strings.TrimSpace(email)
email = strings.ToLower(email)
emailMd5 := MD5EncodeToString([]byte(email))
return fmt.Sprintf(gravatarUrlFormat, emailMd5)
}
-24
View File
@@ -1,24 +0,0 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetInternalAvatarUrl(t *testing.T) {
expectedValue := "https://demo.ezbookkeeping.mayswind.net/avatar/1234567890.jpg"
actualValue := GetInternalAvatarUrl(1234567890, "jpg", "https://demo.ezbookkeeping.mayswind.net/")
assert.Equal(t, expectedValue, actualValue)
expectedValue = ""
actualValue = GetInternalAvatarUrl(1234567890, "", "https://demo.ezbookkeeping.mayswind.net/")
assert.Equal(t, expectedValue, actualValue)
}
func TestGetGravatarUrl(t *testing.T) {
// Reference: https://en.gravatar.com/site/implement/hash/
expectedValue := "https://www.gravatar.com/avatar/0bc83cb571cd1c50ba6f3e8a78ef1346"
actualValue := GetGravatarUrl("MyEmailAddress@example.com")
assert.Equal(t, expectedValue, actualValue)
}