Refactor: decide if there is a thumbnail based on the file extension
This commit is contained in:
parent
7b571499a7
commit
514e069113
4 changed files with 29 additions and 8 deletions
2
assets
2
assets
|
@ -1 +1 @@
|
|||
Subproject commit 380f844eb9e05d7b3b7746dca9f449a56bf69185
|
||||
Subproject commit c5e374fd2c8cdfe8a4bece60dba7d27b047eb0f1
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue