supports local file system object storage and use it as the default avatar provider
This commit is contained in:
@@ -7,6 +7,15 @@ import (
|
||||
|
||||
const gravatarUrlFormat = "https://www.gravatar.com/avatar/%s"
|
||||
|
||||
// GetInternalAvatarUrl returns the internal avatar url
|
||||
func GetInternalAvatarUrl(uid int64, avatarFileExtesion string, webRootUrl string) string {
|
||||
if avatarFileExtesion == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%savatar/%d.%s", webRootUrl, uid, avatarFileExtesion)
|
||||
}
|
||||
|
||||
// GetGravatarUrl returns the Gravatar url according to the specified user email address
|
||||
func GetGravatarUrl(email string) string {
|
||||
email = strings.TrimSpace(email)
|
||||
|
||||
@@ -6,6 +6,16 @@ import (
|
||||
"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"
|
||||
|
||||
@@ -3,9 +3,29 @@ package utils
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var imageFileExtensionContentTypeMap = map[string]string{
|
||||
"jpg": "image/jpeg",
|
||||
"jpeg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
"gif": "image/gif",
|
||||
"webp": "image/webp",
|
||||
}
|
||||
|
||||
// GetImageContentType returns the content type of specified image file extension or returns empty when the file extension is not image or not supported
|
||||
func GetImageContentType(fileExtension string) string {
|
||||
contentType, exists := imageFileExtensionContentTypeMap[fileExtension]
|
||||
|
||||
if !exists {
|
||||
return ""
|
||||
}
|
||||
|
||||
return contentType
|
||||
}
|
||||
|
||||
// ListFileNamesWithPrefixAndSuffix returns file name list which has specified prefix and suffix
|
||||
func ListFileNamesWithPrefixAndSuffix(path string, prefix string, suffix string) []string {
|
||||
dir, err := os.Open(path)
|
||||
@@ -69,6 +89,29 @@ func WriteFile(path string, data []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// GetFileNameWithoutExtension returns the file name without extension
|
||||
func GetFileNameWithoutExtension(path string) string {
|
||||
fileName := filepath.Base(path)
|
||||
extension := filepath.Ext(fileName)
|
||||
|
||||
if len(extension) < 1 {
|
||||
return fileName
|
||||
}
|
||||
|
||||
return fileName[0 : len(fileName)-len(extension)]
|
||||
}
|
||||
|
||||
// GetFileNameExtension returns the file extension without dot
|
||||
func GetFileNameExtension(path string) string {
|
||||
extension := filepath.Ext(path)
|
||||
|
||||
if len(extension) < 1 || extension[0] != '.' {
|
||||
return extension
|
||||
}
|
||||
|
||||
return extension[1:]
|
||||
}
|
||||
|
||||
// IdentReader returns the original io reader
|
||||
func IdentReader(encoding string, input io.Reader) (io.Reader, error) {
|
||||
return input, nil
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetImageContentType(t *testing.T) {
|
||||
fileName := "gif"
|
||||
expectedContentType := "image/gif"
|
||||
actualContentType := GetImageContentType(fileName)
|
||||
assert.Equal(t, expectedContentType, actualContentType)
|
||||
|
||||
fileName = "bmp"
|
||||
expectedContentType = ""
|
||||
actualContentType = GetImageContentType(fileName)
|
||||
assert.Equal(t, expectedContentType, actualContentType)
|
||||
}
|
||||
|
||||
func TestGetFileNameWithoutExtension(t *testing.T) {
|
||||
fileName := "name.ext"
|
||||
expectedName := "name"
|
||||
actualName := GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "C:\\name.ext"
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "/root/name.ext"
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "name"
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "C:\\name"
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "/root/name"
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "name."
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "C:\\name."
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "/root/name."
|
||||
expectedName = "name"
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = ".ext"
|
||||
expectedName = ""
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "C:\\.ext"
|
||||
expectedName = ""
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
|
||||
fileName = "/root/.ext"
|
||||
expectedName = ""
|
||||
actualName = GetFileNameWithoutExtension(fileName)
|
||||
assert.Equal(t, expectedName, actualName)
|
||||
}
|
||||
|
||||
func TestGetFileNameExtension(t *testing.T) {
|
||||
fileName := "name.ext"
|
||||
expectedExt := "ext"
|
||||
actualExt := GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "C:\\name.ext"
|
||||
expectedExt = "ext"
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "/root/name.ext"
|
||||
expectedExt = "ext"
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "name"
|
||||
expectedExt = ""
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "C:\\name"
|
||||
expectedExt = ""
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "/root/name"
|
||||
expectedExt = ""
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "name."
|
||||
expectedExt = ""
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "C:\\name."
|
||||
expectedExt = ""
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "/root/name."
|
||||
expectedExt = ""
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = ".ext"
|
||||
expectedExt = "ext"
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "C:\\.ext"
|
||||
expectedExt = "ext"
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
|
||||
fileName = "/root/.ext"
|
||||
expectedExt = "ext"
|
||||
actualExt = GetFileNameExtension(fileName)
|
||||
assert.Equal(t, expectedExt, actualExt)
|
||||
}
|
||||
Reference in New Issue
Block a user