diff --git a/assets b/assets index 380f844eb9e05d7b3b7746dca9f449a56bf69185..c5e374fd2c8cdfe8a4bece60dba7d27b047eb0f1 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 380f844eb9e05d7b3b7746dca9f449a56bf69185 +Subproject commit c5e374fd2c8cdfe8a4bece60dba7d27b047eb0f1 diff --git a/models/policy.go b/models/policy.go index c92d2dc95167161603cb70936ebe364089551ef1..fc9708425b8ba88ea523c8e58e11ff88bb068212 100644 --- a/models/policy.go +++ b/models/policy.go @@ -8,7 +8,9 @@ import ( "github.com/jinzhu/gorm" "net/url" "path" + "path/filepath" "strconv" + "strings" "time" ) @@ -48,6 +50,16 @@ type PolicyOption struct { OdRedirect string `json:"od_redirect,omitempty"` } +var thumbSuffix = map[string][]string{ + "local": {}, + "qiniu": {".psd", ".jpg", ".jpeg", ".png", ".gif", ".webp", ".tiff", ".bmp"}, + "oss": {".jpg", ".jpeg", ".png", ".gif", ".webp", ".tiff", ".bmp"}, + "cos": {}, + "upyun": {".svg", ".jpg", ".jpeg", ".png", ".gif", ".webp", ".tiff", ".bmp"}, + "remote": {}, + "onedrive": {"*"}, +} + func init() { // 注册缓存用到的复杂结构 gob.Register(Policy{}) @@ -178,6 +190,17 @@ func (policy *Policy) IsDirectlyPreview() bool { return policy.Type == "local" } +// IsThumbExist 给定文件名,返回此存储策略下是否可能存在缩略图 +func (policy *Policy) IsThumbExist(name string) bool { + if list, ok := thumbSuffix[policy.Type]; ok { + if len(list) == 1 && list[0] == "*" { + return true + } + return util.ContainsString(list, strings.ToLower(filepath.Ext(name))) + } + return false +} + // IsTransitUpload 返回此策略上传给定size文件时是否需要服务端中转 func (policy *Policy) IsTransitUpload(size uint64) bool { if policy.Type == "local" { @@ -199,11 +222,6 @@ func (policy *Policy) IsThumbGenerateNeeded() bool { return policy.Type == "local" } -// IsMockThumbNeeded 返回此策略是否需要在上传后默认当图像文件 -func (policy *Policy) IsMockThumbNeeded() bool { - return policy.Type == "onedrive" -} - // GetUploadURL 获取文件上传服务API地址 func (policy *Policy) GetUploadURL() string { server, err := url.Parse(policy.Server) diff --git a/pkg/filesystem/file.go b/pkg/filesystem/file.go index d6a71c7f12eac22a2889ba4df9f6a4a3f6918f1b..deaf98aa3d454044dcdc198683fd0097e3cc26f4 100644 --- a/pkg/filesystem/file.go +++ b/pkg/filesystem/file.go @@ -63,6 +63,11 @@ func (fs *FileSystem) AddFile(ctx context.Context, parent *model.Folder) (*model FolderID: parent.ID, PolicyID: fs.User.Policy.ID, } + + if fs.User.Policy.IsThumbExist(file.GetFileName()) { + newFile.PicInfo = "1,1" + } + _, err = newFile.Create() if err != nil { diff --git a/pkg/filesystem/hooks.go b/pkg/filesystem/hooks.go index 54dd80930e7fa8faa5755fe676c6692ef060b58e..1c39ee420ebee1ad17fb2f2bbb93df3fdd3fb2fc 100644 --- a/pkg/filesystem/hooks.go +++ b/pkg/filesystem/hooks.go @@ -297,8 +297,6 @@ func GenericAfterUpload(ctx context.Context, fs *FileSystem) error { // 异步尝试生成缩略图 if fs.User.Policy.IsThumbGenerateNeeded() { go fs.GenerateThumbnail(ctx, file) - } else if fs.User.Policy.IsMockThumbNeeded() { - file.UpdatePicInfo("1,1") } return nil