|
|
@@ -1,11 +1,20 @@
|
|
|
package task
|
|
|
|
|
|
import (
|
|
|
+ "crypto/md5"
|
|
|
+ "encoding/hex"
|
|
|
+ "errors"
|
|
|
+ "go.uber.org/zap"
|
|
|
+ "gorm.io/gorm"
|
|
|
+ "io"
|
|
|
"log-server/global"
|
|
|
"log-server/model/task/response"
|
|
|
- "log-server/utils/upload"
|
|
|
+ "log-server/utils"
|
|
|
"mime/multipart"
|
|
|
+ "os"
|
|
|
+ "path"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type UploadFileService struct {
|
|
|
@@ -16,23 +25,73 @@ func (e *UploadFileService) Upload(file response.UploadFileResponse) error {
|
|
|
return global.GVA_DB.Table("upload_file").Create(&file).Error
|
|
|
}
|
|
|
|
|
|
+//更改的上传文件函数
|
|
|
+func (e *UploadFileService) UploadFileFunc(file *multipart.FileHeader) (string, string, string, error) {
|
|
|
+ // 读取文件后缀
|
|
|
+ ext := path.Ext(file.Filename)
|
|
|
+ // 读取文件名并加密
|
|
|
+ name := strings.TrimSuffix(file.Filename, ext)
|
|
|
+ name = utils.MD5V([]byte(name))
|
|
|
+ // 拼接新文件名
|
|
|
+ filename := name + "_" + time.Now().Format("20060102150405") + ext
|
|
|
+ // 尝试创建此路径
|
|
|
+ mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.StorePath, os.ModePerm)
|
|
|
+ if mkdirErr != nil {
|
|
|
+ global.GVA_LOG.Error("function os.MkdirAll() Filed", zap.Any("err", mkdirErr.Error()))
|
|
|
+ return "", "", "", errors.New("function os.MkdirAll() Filed, err:" + mkdirErr.Error())
|
|
|
+ }
|
|
|
+ // 拼接路径和文件名
|
|
|
+ p := global.GVA_CONFIG.Local.StorePath + "/" + filename
|
|
|
+ filepath := global.GVA_CONFIG.Local.Path + "/" + filename
|
|
|
+
|
|
|
+ f, openError := file.Open() // 读取文件
|
|
|
+ if openError != nil {
|
|
|
+ global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
|
|
|
+ return "", "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
|
|
|
+ }
|
|
|
+ defer f.Close() // 创建文件 defer 关闭
|
|
|
+
|
|
|
+ //生成md5值
|
|
|
+ md5h := md5.New()
|
|
|
+ io.Copy(md5h, f)
|
|
|
+ md5String := hex.EncodeToString(md5h.Sum(nil))
|
|
|
+
|
|
|
+ //
|
|
|
+ out, createErr := os.Create(p)
|
|
|
+ if createErr != nil {
|
|
|
+ global.GVA_LOG.Error("function os.Create() Filed", zap.Any("err", createErr.Error()))
|
|
|
+
|
|
|
+ return "", "", "", errors.New("function os.Create() Filed, err:" + createErr.Error())
|
|
|
+ }
|
|
|
+ defer out.Close() // 创建文件 defer 关闭
|
|
|
+
|
|
|
+ _, copyErr := io.Copy(out, f) // 传输(拷贝)文件
|
|
|
+ if copyErr != nil {
|
|
|
+ global.GVA_LOG.Error("function io.Copy() Filed", zap.Any("err", copyErr.Error()))
|
|
|
+ return "", "", "", errors.New("function io.Copy() Filed, err:" + copyErr.Error())
|
|
|
+ }
|
|
|
+ return filepath, filename, md5String, nil
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//上传文件
|
|
|
func (e *UploadFileService) UploadFile(gameId int, header *multipart.FileHeader) (file response.UploadFileResponse, err error) {
|
|
|
//查询数据库记录数
|
|
|
var count int64
|
|
|
//获取最后一条记录
|
|
|
var lastRecord response.UploadFileResponse
|
|
|
var version int
|
|
|
- oss := upload.NewOss()
|
|
|
- filePath, key, uploadErr := oss.UploadFile(header)
|
|
|
+ filePath, key, md5String, uploadErr := e.UploadFileFunc(header)
|
|
|
if uploadErr != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
|
|
|
- global.GVA_DB.Table("upload_file").Count(&count)
|
|
|
+ global.GVA_DB.Table("upload_file").Where("game_id = ?", gameId).Count(&count)
|
|
|
if count == 0 {
|
|
|
version = 1
|
|
|
} else {
|
|
|
- global.GVA_DB.Table("upload_file").Last(&lastRecord)
|
|
|
+ global.GVA_DB.Table("upload_file").Where("game_id = ?", gameId).Last(&lastRecord)
|
|
|
version = lastRecord.Version + 1
|
|
|
}
|
|
|
|
|
|
@@ -44,7 +103,41 @@ func (e *UploadFileService) UploadFile(gameId int, header *multipart.FileHeader)
|
|
|
Tag: s[len(s)-1],
|
|
|
Key: key,
|
|
|
Version: version,
|
|
|
+ Md5String: md5String,
|
|
|
}
|
|
|
return f, e.Upload(f)
|
|
|
+}
|
|
|
+
|
|
|
+//下载文件
|
|
|
+func (e *UploadFileService) DownloadFile(gameId int, md5String string) (info response.DownloadFile, err error) {
|
|
|
+ var file response.UploadFileResponse
|
|
|
|
|
|
-}
|
|
|
+ //数据库中无记录,即不需要更新
|
|
|
+ err = global.GVA_DB.Table("upload_file").Where("game_id = ?", gameId).Last(&file).Error
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ info = response.DownloadFile{
|
|
|
+ Url: "",
|
|
|
+ Md5String: "",
|
|
|
+ Flag: false,
|
|
|
+ }
|
|
|
+ return info, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if file.Md5String == md5String {
|
|
|
+ info = response.DownloadFile{
|
|
|
+ Url: "",
|
|
|
+ Md5String: md5String,
|
|
|
+ Flag: false,
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ info = response.DownloadFile{
|
|
|
+ Url: file.Url,
|
|
|
+ Md5String: file.Md5String,
|
|
|
+ Flag: true,
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return info, err
|
|
|
+
|
|
|
+
|
|
|
+}
|