mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: file upload handler
This commit is contained in:
@@ -2,7 +2,10 @@ package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/HFO4/cloudreve/pkg/util"
|
||||
"github.com/jinzhu/gorm"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Policy 存储策略
|
||||
@@ -64,3 +67,50 @@ func (policy *Policy) SerializeOptions() (err error) {
|
||||
policy.Options = string(optionsValue)
|
||||
return err
|
||||
}
|
||||
|
||||
// GeneratePath 生成存储文件的路径
|
||||
func (policy *Policy) GeneratePath(uid uint) string {
|
||||
dirRule := policy.DirNameRule
|
||||
replaceTable := map[string]string{
|
||||
"{randomkey16}": util.RandStringRunes(16),
|
||||
"{randomkey8}": util.RandStringRunes(8),
|
||||
"{timestamp}": strconv.FormatInt(time.Now().Unix(), 10),
|
||||
"{uid}": strconv.Itoa(int(uid)),
|
||||
"{datetime}": time.Now().Format("20060102150405"),
|
||||
"{date}": time.Now().Format("20060102"),
|
||||
}
|
||||
dirRule = util.Replace(replaceTable, dirRule)
|
||||
return dirRule
|
||||
}
|
||||
|
||||
// GenerateFileName 生成存储文件名
|
||||
func (policy *Policy) GenerateFileName(uid uint, origin string) string {
|
||||
fileRule := policy.FileNameRule
|
||||
|
||||
replaceTable := map[string]string{
|
||||
"{randomkey16}": util.RandStringRunes(16),
|
||||
"{randomkey8}": util.RandStringRunes(8),
|
||||
"{timestamp}": strconv.FormatInt(time.Now().Unix(), 10),
|
||||
"{uid}": strconv.Itoa(int(uid)),
|
||||
"{datetime}": time.Now().Format("20060102150405"),
|
||||
"{date}": time.Now().Format("20060102"),
|
||||
}
|
||||
|
||||
// 部分存储策略可以使用{origin}代表原始文件名
|
||||
switch policy.Type {
|
||||
case "qiniu":
|
||||
// 七牛会将$(fname)自动替换为原始文件名
|
||||
replaceTable["{originname}"] = "$(fname)"
|
||||
case "local":
|
||||
replaceTable["{originname}"] = origin
|
||||
case "oss":
|
||||
// OSS会将${filename}自动替换为原始文件名
|
||||
replaceTable["{originname}"] = "${filename}"
|
||||
case "upyun":
|
||||
// Upyun会将{filename}{.suffix}自动替换为原始文件名
|
||||
replaceTable["{originname}"] = "{filename}{.suffix}"
|
||||
}
|
||||
|
||||
fileRule = util.Replace(replaceTable, fileRule)
|
||||
return fileRule
|
||||
}
|
||||
|
||||
@@ -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, ""))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user