This commit is contained in:
孟帅
2023-07-20 18:01:10 +08:00
parent 9113fc5297
commit 373d9627fb
492 changed files with 12170 additions and 6982 deletions

View File

@@ -18,13 +18,16 @@ import (
// 文件归属分类
const (
KindImg = "images" // 图片
KindDoc = "document" // 文档
KindAudio = "audio" // 音频
KindVideo = "video" // 视频
KindOther = "other" // 其他
KindImg = "image" // 图片
KindDoc = "doc" // 文档
KindAudio = "audio" // 音频
KindVideo = "video" // 视频
KindZip = "zip" // 压缩包
KindOther = "other" // 其他
)
var KindSlice = []string{KindImg, KindDoc, KindAudio, KindVideo, KindZip, KindOther}
var (
// 图片类型
imgType = g.MapStrStr{
@@ -45,12 +48,33 @@ var (
// 文档类型
docType = g.MapStrStr{
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"xls": "application/vnd.ms-excel",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"ppt": "application/vnd.ms-powerpoint",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"doc": "application/msword",
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"dot": "application/msword",
"xls": "application/vnd.ms-excel",
"xlt": "application/vnd.ms-excel",
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"xltx": "application/vnd.openxmlformats-officedocument.spreadsheetml.template",
"ppt": "application/vnd.ms-powerpoint",
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"pdf": "application/pdf",
"txt": "text/plain",
"csv": "text/csv",
"html": "text/html",
"xml": "text/xml",
"pptm": "application/vnd.ms-powerpoint.presentation.macroEnabled.12",
"pot": "application/vnd.ms-powerpoint",
"wpd": "application/wordperfect",
"md": "text/markdown",
"json": "application/json",
"yaml": "application/x-yaml",
"markdown": "text/markdown",
"asciidoc": "text/asciidoc",
"xsl": "application/xml",
"wps": "application/vnd.ms-works",
"sxi": "application/vnd.sun.xml.impress",
"sti": "application/vnd.sun.xml.impress.template",
"odp": "application/vnd.oasis.opendocument.presentation-template",
}
// 音频类型
@@ -79,6 +103,23 @@ var (
"flv": "video/x-flv",
"3gp": "video/3gpp",
}
// 压缩包
zipType = g.MapStrStr{
"zip": "application/zip",
"rar": "application/x-rar-compressed",
"tar": "application/x-tar",
"gz": "application/gzip",
"7z": "application/octet-stream",
"tar.gz": "application/octet-stream",
}
// 其他
otherType = g.MapStrStr{
"dwf": "model/vnd.dwf",
"ics": "text/calendar",
"vcard": "text/vcard",
}
)
// IsImgType 判断是否为图片
@@ -105,16 +146,17 @@ func IsVideoType(ext string) bool {
return ok
}
// GetImgType 获取图片类型
func GetImgType(ext string) (string, error) {
if mime, ok := imgType[ext]; ok {
return mime, nil
}
return "", gerror.New("Invalid image type")
// IsZipType 判断是否为压缩包
func IsZipType(ext string) bool {
_, ok := zipType[ext]
return ok
}
// GetFileType 获取文件类型
func GetFileType(ext string) (string, error) {
// GetFileMimeType 获取文件扩展类型
// 如果文件类型没有加入系统映射类型,默认认为不是合法的文件类型。建议将常用的上传文件类型加入映射关系。
// 当然你也可以不做限制,可以上传任意文件。但需要谨慎处理和设置相应的安全措施。
// 获取任意扩展名的扩展类型mime.TypeByExtension(".xls")
func GetFileMimeType(ext string) (string, error) {
if mime, ok := imgType[ext]; ok {
return mime, nil
}
@@ -127,10 +169,16 @@ func GetFileType(ext string) (string, error) {
if mime, ok := videoType[ext]; ok {
return mime, nil
}
if mime, ok := zipType[ext]; ok {
return mime, nil
}
if mime, ok := otherType[ext]; ok {
return mime, nil
}
return "", gerror.Newf("Invalid file type:%v", ext)
}
// GetFileKind 获取文件所属分类
// GetFileKind 获取文件上传类型
func GetFileKind(ext string) string {
if _, ok := imgType[ext]; ok {
return KindImg
@@ -144,12 +192,15 @@ func GetFileKind(ext string) string {
if _, ok := videoType[ext]; ok {
return KindVideo
}
if _, ok := zipType[ext]; ok {
return KindZip
}
return KindOther
}
// Ext 获取文件后缀
func Ext(baseName string) string {
return gstr.StrEx(path.Ext(baseName), ".")
return gstr.ToLower(gstr.StrEx(path.Ext(baseName), "."))
}
// UploadFileByte 获取上传文件的byte

View File

@@ -4,9 +4,9 @@ package storager
type FileMeta struct {
Filename string // 文件名称
Size int64 // 文件大小
Kind string // 文件所属分类
MetaType string // 文件类型
Kind string // 文件上传类型
MimeType string // 文件扩展类型
NaiveType string // NaiveUI类型
Ext string // 文件后缀
Ext string // 文件扩展
Md5 string // 文件hash
}

View File

@@ -52,7 +52,7 @@ func New(name ...string) UploadDrive {
}
// DoUpload 上传入口
func DoUpload(ctx context.Context, file *ghttp.UploadFile, typ int) (result *entity.SysAttachment, err error) {
func DoUpload(ctx context.Context, typ string, file *ghttp.UploadFile) (result *entity.SysAttachment, err error) {
if file == nil {
err = gerror.New("文件必须!")
return
@@ -63,17 +63,12 @@ func DoUpload(ctx context.Context, file *ghttp.UploadFile, typ int) (result *ent
return
}
if _, err = GetFileType(meta.Ext); err != nil {
if _, err = GetFileMimeType(meta.Ext); err != nil {
return
}
switch typ {
case consts.UploadTypeFile:
if config.FileSize > 0 && meta.Size > config.FileSize*1024*1024 {
err = gerror.Newf("文件大小不能超过%vMB", config.FileSize)
return
}
case consts.UploadTypeImage:
case KindImg:
if !IsImgType(meta.Ext) {
err = gerror.New("上传的文件不是图片")
return
@@ -82,24 +77,34 @@ func DoUpload(ctx context.Context, file *ghttp.UploadFile, typ int) (result *ent
err = gerror.Newf("图片大小不能超过%vMB", config.ImageSize)
return
}
case consts.UploadTypeDoc:
case KindDoc:
if !IsDocType(meta.Ext) {
err = gerror.New("上传的文件不是文档")
return
}
case consts.UploadTypeAudio:
case KindAudio:
if !IsAudioType(meta.Ext) {
err = gerror.New("上传的文件不是音频")
return
}
case consts.UploadTypeVideo:
case KindVideo:
if !IsVideoType(meta.Ext) {
err = gerror.New("上传的文件不是视频")
return
}
case KindZip:
if !IsZipType(meta.Ext) {
err = gerror.New("上传的文件不是压缩文件")
return
}
case KindOther:
fallthrough
default:
err = gerror.Newf("无效的上传类型:%v", typ)
return
// 默认为通用的文件上传
if config.FileSize > 0 && meta.Size > config.FileSize*1024*1024 {
err = gerror.Newf("文件大小不能超过%vMB", config.FileSize)
return
}
}
result, err = hasFile(ctx, meta.Md5)
@@ -149,7 +154,7 @@ func GetFileMeta(file *ghttp.UploadFile) (meta *FileMeta, err error) {
meta.Size = file.Size
meta.Ext = Ext(file.Filename)
meta.Kind = GetFileKind(meta.Ext)
meta.MetaType, err = GetFileType(meta.Ext)
meta.MimeType, err = GetFileMimeType(meta.Ext)
if err != nil {
return
}
@@ -185,7 +190,7 @@ func write(ctx context.Context, meta *FileMeta, fullPath string) (models *entity
FileUrl: fullPath,
Name: meta.Filename,
Kind: meta.Kind,
MetaType: meta.MetaType,
MimeType: meta.MimeType,
NaiveType: meta.NaiveType,
Ext: meta.Ext,
Md5: meta.Md5,

View File

@@ -36,7 +36,7 @@ func (d *CosDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath
},
})
fullPath = GenFullPath(config.UCloudPath, gfile.Ext(file.Filename))
fullPath = GenFullPath(config.CosPath, gfile.Ext(file.Filename))
_, err = client.Object.Put(ctx, fullPath, f2, nil)
return
}

View File

@@ -36,7 +36,7 @@ func (d *OssDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPath
return
}
fullPath = GenFullPath(config.UCloudPath, gfile.Ext(file.Filename))
fullPath = GenFullPath(config.OssPath, gfile.Ext(file.Filename))
err = bucket.PutObject(fullPath, f2)
return
}

View File

@@ -46,7 +46,7 @@ func (d *QiNiuDrive) Upload(ctx context.Context, file *ghttp.UploadFile) (fullPa
return
}
fullPath = GenFullPath(config.UCloudPath, gfile.Ext(file.Filename))
fullPath = GenFullPath(config.QiNiuPath, gfile.Ext(file.Filename))
err = storage.NewFormUploader(&cfg).Put(ctx, &storage.PutRet{}, token, fullPath, f2, file.Size, &storage.PutExtra{})
return
}