From 514e069113ffa27dc13b6d7c19eff6338ac6212e Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Wed, 22 Apr 2020 11:04:35 +0800 Subject: [PATCH] Refactor: decide if there is a thumbnail based on the file extension --- assets | 2 +- models/policy.go | 28 +++++++++++++++++++++++----- pkg/filesystem/file.go | 5 +++++ pkg/filesystem/hooks.go | 2 -- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/assets b/assets index 380f844..c5e374f 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 380f844eb9e05d7b3b7746dca9f449a56bf69185 +Subproject commit c5e374fd2c8cdfe8a4bece60dba7d27b047eb0f1 diff --git a/models/policy.go b/models/policy.go index c92d2dc..fc97084 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 d6a71c7..deaf98a 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 54dd809..1c39ee4 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 -- GitLab