195 lines
4.1 KiB
Go
195 lines
4.1 KiB
Go
package services
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
)
|
|
|
|
type FileUploadParams struct {
|
|
Source string
|
|
Scope string
|
|
Tid uint64
|
|
Tuid uint64
|
|
Uid uint64
|
|
Name string
|
|
Ext string
|
|
Cate uint64
|
|
Size uint64
|
|
Src string
|
|
ObjectKey string
|
|
Storage string
|
|
MD5 string
|
|
}
|
|
|
|
func FindDuplicate(source, scope string, tid, tuid uint64, md5Str string) (*models.SystemFile, error) {
|
|
if md5Str == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
qs := models.Orm.QueryTable(new(models.SystemFile)).
|
|
Filter("source", source).
|
|
Filter("scope", scope).
|
|
Filter("tid", tid).
|
|
Filter("md5", md5Str).
|
|
Filter("delete_time__isnull", true)
|
|
|
|
if scope == ScopeUser {
|
|
qs = qs.Filter("tuid", tuid)
|
|
} else {
|
|
qs = qs.Filter("tuid__isnull", true)
|
|
}
|
|
|
|
var f models.SystemFile
|
|
if err := qs.OrderBy("-id").One(&f); err != nil {
|
|
return nil, nil
|
|
}
|
|
return &f, nil
|
|
}
|
|
|
|
func CreateFileRecord(p FileUploadParams) (uint64, error) {
|
|
|
|
uid := p.Uid
|
|
row := &models.SystemFile{
|
|
Tid: p.Tid,
|
|
Uid: &uid,
|
|
Name: p.Name,
|
|
Type: DetectFileType(p.Ext),
|
|
Cate: p.Cate,
|
|
Size: p.Size,
|
|
Src: p.Src,
|
|
Uploader: p.Uid,
|
|
Md5: p.MD5,
|
|
Source: p.Source,
|
|
Scope: p.Scope,
|
|
Storage: p.Storage,
|
|
ObjectKey: p.ObjectKey,
|
|
}
|
|
if p.Tuid > 0 {
|
|
tuid := p.Tuid
|
|
row.Tuid = &tuid
|
|
}
|
|
if row.Source == "" {
|
|
row.Source = SourceBackend
|
|
}
|
|
if row.Scope == "" {
|
|
row.Scope = ScopeTenant
|
|
}
|
|
|
|
id, err := models.Orm.Insert(row)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return uint64(id), nil
|
|
}
|
|
|
|
func RemovePhysical(svc StorageService, objectKey, src string) error {
|
|
if svc == nil {
|
|
return fmt.Errorf("存储服务未初始化")
|
|
}
|
|
key := strings.TrimSpace(objectKey)
|
|
if key == "" {
|
|
key = KeyFromSrc(src, svc)
|
|
}
|
|
if key == "" {
|
|
return fmt.Errorf("无法解析文件路径: %s", src)
|
|
}
|
|
return svc.Delete(key)
|
|
}
|
|
|
|
func KeyFromSrc(src string, svc StorageService) string {
|
|
src = strings.TrimSpace(src)
|
|
if src == "" {
|
|
return ""
|
|
}
|
|
switch s := svc.(type) {
|
|
case *LocalStorage:
|
|
base := filepath.ToSlash(s.BaseDir)
|
|
rel := src
|
|
if i := strings.Index(rel, base+"/"); i >= 0 {
|
|
rel = rel[i+len(base)+1:]
|
|
}
|
|
rel = strings.TrimPrefix(rel, "/")
|
|
if i := strings.IndexAny(rel, "?#"); i >= 0 {
|
|
rel = rel[:i]
|
|
}
|
|
return rel
|
|
case *QiniuStorage:
|
|
domain := strings.TrimRight(s.Domain, "/")
|
|
rel := src
|
|
if domain != "" && strings.HasPrefix(rel, domain) {
|
|
rel = strings.TrimPrefix(strings.TrimPrefix(rel, domain), "/")
|
|
} else if u, err := url.Parse(rel); err == nil && u.Host != "" {
|
|
rel = strings.TrimPrefix(u.Path, "/")
|
|
}
|
|
rel = strings.TrimPrefix(rel, "/")
|
|
if i := strings.IndexAny(rel, "?#"); i >= 0 {
|
|
rel = rel[:i]
|
|
}
|
|
return rel
|
|
}
|
|
return ""
|
|
}
|
|
|
|
var fileTypeByCategory = map[string]uint8{
|
|
"image": 1,
|
|
"document": 2,
|
|
"video": 3,
|
|
"audio": 4,
|
|
"appsupgrade": 2,
|
|
}
|
|
|
|
var allowedExtByCategory = map[string][]string{
|
|
"image": {"jpg", "jpeg", "png", "gif", "bmp", "webp"},
|
|
"document": {"pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt"},
|
|
"video": {"mp4", "webm", "mov"},
|
|
"audio": {"mp3", "wav", "ogg"},
|
|
"appsupgrade": {"zip", "exe", "dmg", "msi", "msix", "apk", "deb", "rpm", "7z", "tar", "gz", "pkg"},
|
|
}
|
|
|
|
func FileExt(name string) string {
|
|
name = strings.TrimSpace(name)
|
|
if i := strings.LastIndex(name, "."); i >= 0 && i < len(name)-1 {
|
|
return strings.ToLower(name[i+1:])
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func DetectFileType(ext string) uint8 {
|
|
ext = strings.ToLower(strings.TrimPrefix(ext, "."))
|
|
for cat, exts := range allowedExtByCategory {
|
|
for _, e := range exts {
|
|
if e == ext {
|
|
if t, ok := fileTypeByCategory[cat]; ok {
|
|
return t
|
|
}
|
|
return 2
|
|
}
|
|
}
|
|
}
|
|
return 2
|
|
}
|
|
|
|
func SoftDeleteFiles(ids []uint64) (int64, error) {
|
|
if len(ids) == 0 {
|
|
return 0, nil
|
|
}
|
|
return models.Orm.QueryTable(new(models.SystemFile)).
|
|
Filter("id__in", ids).
|
|
Filter("delete_time__isnull", true).
|
|
Update(map[string]interface{}{"delete_time": time.Now()})
|
|
}
|
|
|
|
func DeleteFilesPermanently(ids []uint64) (int64, error) {
|
|
if len(ids) == 0 {
|
|
return 0, nil
|
|
}
|
|
return models.Orm.QueryTable(new(models.SystemFile)).
|
|
Filter("id__in", ids).
|
|
Delete()
|
|
}
|