Feat: file upload handler

This commit is contained in:
HFO4
2019-11-17 13:50:14 +08:00
parent 841832bb65
commit 99e7eecab7
12 changed files with 242 additions and 25 deletions

View File

@@ -4,7 +4,9 @@ import (
"encoding/json"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
"time"
)
func TestGetPolicyByID(t *testing.T) {
@@ -38,3 +40,72 @@ func TestPolicy_BeforeSave(t *testing.T) {
asserts.Equal(string(expected), testPolicy.Options)
}
func TestPolicy_GeneratePath(t *testing.T) {
asserts := assert.New(t)
testPolicy := Policy{}
testPolicy.DirNameRule = "{randomkey16}"
asserts.Len(testPolicy.GeneratePath(1), 16)
testPolicy.DirNameRule = "{randomkey8}"
asserts.Len(testPolicy.GeneratePath(1), 8)
testPolicy.DirNameRule = "{timestamp}"
asserts.Equal(testPolicy.GeneratePath(1), strconv.FormatInt(time.Now().Unix(), 10))
testPolicy.DirNameRule = "{uid}"
asserts.Equal(testPolicy.GeneratePath(1), strconv.Itoa(int(1)))
testPolicy.DirNameRule = "{datetime}"
asserts.Len(testPolicy.GeneratePath(1), 14)
testPolicy.DirNameRule = "{date}"
asserts.Len(testPolicy.GeneratePath(1), 8)
testPolicy.DirNameRule = "123{date}ss{datetime}"
asserts.Len(testPolicy.GeneratePath(1), 27)
}
func TestPolicy_GenerateFileName(t *testing.T) {
asserts := assert.New(t)
testPolicy := Policy{}
testPolicy.FileNameRule = "{randomkey16}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 16)
testPolicy.FileNameRule = "{randomkey8}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
testPolicy.FileNameRule = "{timestamp}"
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.FormatInt(time.Now().Unix(), 10))
testPolicy.FileNameRule = "{uid}"
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.Itoa(int(1)))
testPolicy.FileNameRule = "{datetime}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 14)
testPolicy.FileNameRule = "{date}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
testPolicy.FileNameRule = "123{date}ss{datetime}"
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 27)
// 支持{originname}的策略
testPolicy.Type = "local"
testPolicy.FileNameRule = "123{originname}"
asserts.Equal("123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
testPolicy.Type = "qiniu"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123$(fname)", testPolicy.GenerateFileName(1, "123.txt"))
testPolicy.Type = "oss"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123${filename}", testPolicy.GenerateFileName(1, ""))
testPolicy.Type = "upyun"
testPolicy.FileNameRule = "{uid}123{originname}"
asserts.Equal("1123{filename}{.suffix}", testPolicy.GenerateFileName(1, ""))
}