mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: client-upload file in oss
This commit is contained in:
@@ -111,7 +111,7 @@ func (policy *Policy) GeneratePath(uid uint, origin string) string {
|
||||
func (policy *Policy) GenerateFileName(uid uint, origin string) string {
|
||||
// 未开启自动重命名时,直接返回原始文件名
|
||||
if !policy.AutoRename {
|
||||
return origin
|
||||
return policy.getOriginNameRule(origin)
|
||||
}
|
||||
|
||||
fileRule := policy.FileNameRule
|
||||
@@ -125,28 +125,31 @@ func (policy *Policy) GenerateFileName(uid uint, origin string) string {
|
||||
"{date}": time.Now().Format("20060102"),
|
||||
}
|
||||
|
||||
replaceTable["{originname}"] = policy.getOriginNameRule(origin)
|
||||
|
||||
fileRule = util.Replace(replaceTable, fileRule)
|
||||
return fileRule
|
||||
}
|
||||
|
||||
func (policy Policy) getOriginNameRule(origin string) string {
|
||||
// 部分存储策略可以使用{origin}代表原始文件名
|
||||
if origin == "" {
|
||||
// 如果上游未传回原始文件名,则使用占位符,让云存储端替换
|
||||
switch policy.Type {
|
||||
case "qiniu":
|
||||
// 七牛会将$(fname)自动替换为原始文件名
|
||||
replaceTable["{originname}"] = "$(fname)"
|
||||
return "$(fname)"
|
||||
case "local", "remote":
|
||||
replaceTable["{originname}"] = origin
|
||||
return origin
|
||||
case "oss":
|
||||
// OSS会将${filename}自动替换为原始文件名
|
||||
replaceTable["{originname}"] = "${filename}"
|
||||
return "${filename}"
|
||||
case "upyun":
|
||||
// Upyun会将{filename}{.suffix}自动替换为原始文件名
|
||||
replaceTable["{originname}"] = "{filename}{.suffix}"
|
||||
return "{filename}{.suffix}"
|
||||
}
|
||||
} else {
|
||||
replaceTable["{originname}"] = origin
|
||||
}
|
||||
|
||||
fileRule = util.Replace(replaceTable, fileRule)
|
||||
return fileRule
|
||||
return origin
|
||||
}
|
||||
|
||||
// IsDirectlyPreview 返回此策略下文件是否可以直接预览(不需要重定向)
|
||||
@@ -172,6 +175,8 @@ func (policy *Policy) GetUploadURL() string {
|
||||
controller, _ = url.Parse("/api/v3/file/upload")
|
||||
case "remote":
|
||||
controller, _ = url.Parse("/api/v3/slave/upload")
|
||||
case "oss":
|
||||
return policy.BaseURL
|
||||
default:
|
||||
controller, _ = url.Parse("")
|
||||
}
|
||||
|
||||
@@ -91,47 +91,62 @@ func TestPolicy_GeneratePath(t *testing.T) {
|
||||
|
||||
func TestPolicy_GenerateFileName(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
testPolicy := Policy{
|
||||
AutoRename: true,
|
||||
// 重命名关闭
|
||||
{
|
||||
testPolicy := Policy{
|
||||
AutoRename: false,
|
||||
}
|
||||
testPolicy.FileNameRule = "{randomkey16}"
|
||||
asserts.Equal("123.txt", testPolicy.GenerateFileName(1, "123.txt"))
|
||||
|
||||
testPolicy.Type = "oss"
|
||||
asserts.Equal("${filename}", testPolicy.GenerateFileName(1, ""))
|
||||
}
|
||||
|
||||
testPolicy.FileNameRule = "{randomkey16}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 16)
|
||||
// 重命名开启
|
||||
{
|
||||
testPolicy := Policy{
|
||||
AutoRename: true,
|
||||
}
|
||||
|
||||
testPolicy.FileNameRule = "{randomkey8}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
|
||||
testPolicy.FileNameRule = "{randomkey16}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 16)
|
||||
|
||||
testPolicy.FileNameRule = "{timestamp}"
|
||||
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.FormatInt(time.Now().Unix(), 10))
|
||||
testPolicy.FileNameRule = "{randomkey8}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
|
||||
|
||||
testPolicy.FileNameRule = "{uid}"
|
||||
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.Itoa(int(1)))
|
||||
testPolicy.FileNameRule = "{timestamp}"
|
||||
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.FormatInt(time.Now().Unix(), 10))
|
||||
|
||||
testPolicy.FileNameRule = "{datetime}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 14)
|
||||
testPolicy.FileNameRule = "{uid}"
|
||||
asserts.Equal(testPolicy.GenerateFileName(1, "123.txt"), strconv.Itoa(int(1)))
|
||||
|
||||
testPolicy.FileNameRule = "{date}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
|
||||
testPolicy.FileNameRule = "{datetime}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 14)
|
||||
|
||||
testPolicy.FileNameRule = "123{date}ss{datetime}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 27)
|
||||
testPolicy.FileNameRule = "{date}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 8)
|
||||
|
||||
// 支持{originname}的策略
|
||||
testPolicy.Type = "local"
|
||||
testPolicy.FileNameRule = "123{originname}"
|
||||
asserts.Equal("123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
|
||||
testPolicy.FileNameRule = "123{date}ss{datetime}"
|
||||
asserts.Len(testPolicy.GenerateFileName(1, "123.txt"), 27)
|
||||
|
||||
testPolicy.Type = "qiniu"
|
||||
testPolicy.FileNameRule = "{uid}123{originname}"
|
||||
asserts.Equal("1123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
|
||||
// 支持{originname}的策略
|
||||
testPolicy.Type = "local"
|
||||
testPolicy.FileNameRule = "123{originname}"
|
||||
asserts.Equal("123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
|
||||
|
||||
testPolicy.Type = "oss"
|
||||
testPolicy.FileNameRule = "{uid}123{originname}"
|
||||
asserts.Equal("1123${filename}", testPolicy.GenerateFileName(1, ""))
|
||||
testPolicy.Type = "qiniu"
|
||||
testPolicy.FileNameRule = "{uid}123{originname}"
|
||||
asserts.Equal("1123123.txt", testPolicy.GenerateFileName(1, "123.txt"))
|
||||
|
||||
testPolicy.Type = "upyun"
|
||||
testPolicy.FileNameRule = "{uid}123{originname}"
|
||||
asserts.Equal("1123{filename}{.suffix}", testPolicy.GenerateFileName(1, ""))
|
||||
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, ""))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user