|
|
@@ -27,16 +27,24 @@ import (
|
|
|
)
|
|
|
|
|
|
type TaskConfService struct {
|
|
|
-
|
|
|
}
|
|
|
|
|
|
+//添加数据库记录
|
|
|
func (e *TaskConfService) Upload(file response.UploadFileResponse) error {
|
|
|
+ c := context.Background()
|
|
|
+ key := fmt.Sprintf(SCRIPTDOWNLOADCHECK, file.TaskID)
|
|
|
+ //添加redis缓存
|
|
|
+ err := global.GVA_REDIS.Set(c, key, file.Md5String, 0).Err()
|
|
|
+ if err != nil {
|
|
|
+ global.GVA_LOG.Info("添加脚本md5值缓存失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ }
|
|
|
return global.GVA_DB.Table("upload_file").Create(&file).Error
|
|
|
}
|
|
|
|
|
|
const ACCESSKEY string = "B81Gsvry2StqKVE3txS-7v9GBBfqykC9zhebmxnW"
|
|
|
const SECRETKEY string = "YEZJuYcdeF7vRvzffxpopAVR-jMPZg9pZ-4IKTVW"
|
|
|
const BUCKET string = "pinaishop"
|
|
|
+const SCRIPTDOWNLOADCHECK string = "script:%d"
|
|
|
|
|
|
//更改的上传文件函数
|
|
|
func (e *TaskConfService) UploadFileFunc(file *multipart.FileHeader, taskId string) (string, string, string, error) {
|
|
|
@@ -64,8 +72,8 @@ func (e *TaskConfService) UploadFileFunc(file *multipart.FileHeader, taskId stri
|
|
|
return "", "", "", errors.New("function UploadFileToQiNiu Filed, err:" + uploadErr.Error())
|
|
|
}
|
|
|
//f, openError := file.Open() // 读取文件
|
|
|
- f1, openErr := file.Open() // 读取文件生成MD5值
|
|
|
- if openErr != nil {
|
|
|
+ f1, openErr := file.Open() // 读取文件生成MD5值
|
|
|
+ if openErr != nil {
|
|
|
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openErr.Error()))
|
|
|
return "", "", "", errors.New("function file.Open() Filed, err:" + openErr.Error())
|
|
|
}
|
|
|
@@ -94,7 +102,7 @@ func (e *TaskConfService) UploadFileFunc(file *multipart.FileHeader, taskId stri
|
|
|
return key, filename, md5String, nil
|
|
|
}
|
|
|
|
|
|
-func (e *TaskConfService) UploadFileToQiNiu(fileName string, taskId string, file *multipart.FileHeader) ( error) {
|
|
|
+func (e *TaskConfService) UploadFileToQiNiu(fileName string, taskId string, file *multipart.FileHeader) error {
|
|
|
accessKey := "B81Gsvry2StqKVE3txS-7v9GBBfqykC9zhebmxnW"
|
|
|
secretKey := "YEZJuYcdeF7vRvzffxpopAVR-jMPZg9pZ-4IKTVW"
|
|
|
mac := qbox.NewMac(accessKey, secretKey)
|
|
|
@@ -102,8 +110,7 @@ func (e *TaskConfService) UploadFileToQiNiu(fileName string, taskId string, file
|
|
|
putPolicy := storage.PutPolicy{
|
|
|
Scope: bucket,
|
|
|
}
|
|
|
- key :="shuyou/script/" + taskId + "/" + fileName
|
|
|
-
|
|
|
+ key := "shuyou/script/" + taskId + "/" + fileName
|
|
|
|
|
|
upToken := putPolicy.UploadToken(mac)
|
|
|
cfg := storage.Config{}
|
|
|
@@ -123,7 +130,7 @@ func (e *TaskConfService) UploadFileToQiNiu(fileName string, taskId string, file
|
|
|
// Get the file size
|
|
|
fileSize := file.Size
|
|
|
f, openError := file.Open() // 读取文件
|
|
|
- if openError != nil {
|
|
|
+ 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())
|
|
|
}
|
|
|
@@ -168,21 +175,21 @@ func (e *TaskConfService) UploadFile(taskId int, user string, header *multipart.
|
|
|
fmt.Println(recordArr)
|
|
|
}
|
|
|
|
|
|
- filePath, key, md5String, uploadErr := e.UploadFileFunc(header,strconv.Itoa(taskId) )
|
|
|
+ filePath, key, md5String, uploadErr := e.UploadFileFunc(header, strconv.Itoa(taskId))
|
|
|
if uploadErr != nil {
|
|
|
panic(err)
|
|
|
}
|
|
|
s := strings.Split(header.Filename, ".")
|
|
|
f := response.UploadFileResponse{
|
|
|
- TaskID: taskId,
|
|
|
+ TaskID: taskId,
|
|
|
ResponsiblePerson: user,
|
|
|
- Url: prefix + filePath,
|
|
|
- Name: header.Filename,
|
|
|
- Tag: s[len(s)-1],
|
|
|
- Key: key,
|
|
|
- Version: version,
|
|
|
- Md5String: md5String,
|
|
|
- State : 1,
|
|
|
+ Url: prefix + filePath,
|
|
|
+ Name: header.Filename,
|
|
|
+ Tag: s[len(s)-1],
|
|
|
+ Key: key,
|
|
|
+ Version: version,
|
|
|
+ Md5String: md5String,
|
|
|
+ State: 1,
|
|
|
}
|
|
|
return f, e.Upload(f)
|
|
|
}
|
|
|
@@ -193,31 +200,68 @@ func (e *TaskConfService) DownloadFile(taskId int, md5String string) (info respo
|
|
|
//改由存储至数据库的时候拼接前缀
|
|
|
//var prefix string = global.GVA_CONFIG.PrefixUrl.PrefixLocal
|
|
|
//数据库中无记录,即不需要更新
|
|
|
- err = global.GVA_DB.Table("upload_file").Where("task_id = ? and state = 1", taskId).Order("id desc").First(&file).Error
|
|
|
- if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
- info = response.DownloadFile{
|
|
|
- Url: "",
|
|
|
- Md5String: "",
|
|
|
- Flag: false,
|
|
|
- }
|
|
|
+ //err = global.GVA_DB.Table("upload_file").Where("task_id = ? and state = 1", taskId).Order("id desc").First(&file).Error
|
|
|
+ //if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ // info = response.DownloadFile{
|
|
|
+ // Url: "",
|
|
|
+ // Md5String: "",
|
|
|
+ // Flag: false,
|
|
|
+ // }
|
|
|
+ // return info, errors.New("无对应脚本")
|
|
|
+ //}
|
|
|
+ //
|
|
|
+ //if file.Md5String == md5String {
|
|
|
+ // info = response.DownloadFile{
|
|
|
+ // Url: "",
|
|
|
+ // Md5String: "",
|
|
|
+ // Flag: false,
|
|
|
+ // }
|
|
|
+ // return info, errors.New("不需要更新")
|
|
|
+ //} else {
|
|
|
+ // fullUrl := file.Url
|
|
|
+ // info = response.DownloadFile{
|
|
|
+ // Url: fullUrl,
|
|
|
+ // Md5String: file.Md5String,
|
|
|
+ // Flag: true,
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+
|
|
|
+ //先对比缓存中的md5string,如果缓存中没有,返回无对应脚本;如果缓存中md5一致,则返回不需要更新
|
|
|
+ //最后更新的时候再查询数据库
|
|
|
+ c := context.Background()
|
|
|
+ key := fmt.Sprintf(SCRIPTDOWNLOADCHECK, taskId)
|
|
|
+ existMd5, err := global.GVA_REDIS.Get(c, key).Result()
|
|
|
+ if err != nil {
|
|
|
+ //不存在脚本
|
|
|
return info, errors.New("无对应脚本")
|
|
|
}
|
|
|
|
|
|
- if file.Md5String == md5String {
|
|
|
- info = response.DownloadFile{
|
|
|
- Url: "",
|
|
|
- Md5String: "",
|
|
|
- Flag: false,
|
|
|
- }
|
|
|
+ if existMd5 == "无数据" {
|
|
|
+ //脚本不存在
|
|
|
+ return info, errors.New("脚本不存在")
|
|
|
+ }
|
|
|
+
|
|
|
+ if existMd5 == md5String {
|
|
|
+ //不需要更新
|
|
|
return info, errors.New("不需要更新")
|
|
|
+ }
|
|
|
+
|
|
|
+ err = global.GVA_DB.Table("upload_file").Where("task_id = ? and state = 1", taskId).Order("id desc").First(&file).Error
|
|
|
+ if err != nil {
|
|
|
+ return info, err
|
|
|
} else {
|
|
|
fullUrl := file.Url
|
|
|
info = response.DownloadFile{
|
|
|
- Url: fullUrl,
|
|
|
+ Url: fullUrl,
|
|
|
Md5String: file.Md5String,
|
|
|
- Flag: true,
|
|
|
+ Flag: true,
|
|
|
}
|
|
|
}
|
|
|
+ //将查找出来的md5值重新加回去
|
|
|
+ err = global.GVA_REDIS.Set(c, key, file.Md5String, 0).Err()
|
|
|
+ if err != nil {
|
|
|
+ global.GVA_LOG.Info("添加脚本md5值缓存失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ }
|
|
|
|
|
|
return info, err
|
|
|
|
|
|
@@ -277,11 +321,20 @@ func (e *TaskConfService) GetScriptList(script task.GameScriptResponse, info req
|
|
|
|
|
|
//更改脚本状态
|
|
|
func (e *TaskConfService) UpdateScriptStatus(c taskReq.UpdateScriptStatusRequest) (err error) {
|
|
|
- err = global.GVA_DB.Table("upload_file").Where("id = ?", c.Id).Error
|
|
|
+ var entity response.UploadFileResponse
|
|
|
+ err = global.GVA_DB.Table("upload_file").Where("id = ?", c.Id).First(&entity).Error
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
return errors.New("请传入正确的Id")
|
|
|
}
|
|
|
+ //更新数据库记录
|
|
|
err = global.GVA_DB.Table("upload_file").Where("id = ?", c.Id).Update("state", c.State).Error
|
|
|
+
|
|
|
+ //更新缓存状态
|
|
|
+ err1 := e.UpdateRedisCache(entity.TaskID)
|
|
|
+ if err1 != nil {
|
|
|
+ global.GVA_LOG.Info("更新脚本缓存失败:" + err1.Error() + time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ }
|
|
|
+
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
@@ -299,7 +352,7 @@ func (e *TaskConfService) JudgeOperate(taskConf task.TaskConf) (err error) {
|
|
|
}
|
|
|
|
|
|
//删除上传脚本
|
|
|
-func (e *TaskConfService) DeleteScript(file task.GameScriptResponse) (err error) {
|
|
|
+func (e *TaskConfService) DeleteScript(file task.GameScriptResponse) (err error) {
|
|
|
//七牛云删除所需要的条件
|
|
|
mac := qbox.NewMac(ACCESSKEY, SECRETKEY)
|
|
|
cfg := storage.Config{
|
|
|
@@ -323,11 +376,44 @@ func (e *TaskConfService) DeleteScript(file task.GameScriptResponse) (err error)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+ //更新缓存状态
|
|
|
+ err = e.UpdateRedisCache(entity.TaskID)
|
|
|
+ if err != nil {
|
|
|
+ global.GVA_LOG.Info("更新脚本缓存失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ }
|
|
|
|
|
|
//删除七牛云的记录
|
|
|
err = bucketManager.Delete(BUCKET, key)
|
|
|
fmt.Println(err)
|
|
|
return err
|
|
|
|
|
|
+}
|
|
|
|
|
|
+//更新缓存状态
|
|
|
+func (e *TaskConfService) UpdateRedisCache (taskId int) (err error) {
|
|
|
+ var file response.UploadFileResponse
|
|
|
+ contexts := context.Background()
|
|
|
+ //查找合格的file
|
|
|
+ key := fmt.Sprintf(SCRIPTDOWNLOADCHECK, taskId)
|
|
|
+ err = global.GVA_DB.Table("upload_file").Where("task_id = ? and state = 1", taskId).Order("id desc").First(&file).Error
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ //已无符合条件脚本
|
|
|
+ //删除redis缓存,并且打印日志说明
|
|
|
+ fmt.Println("无可用脚本,尝试删除")
|
|
|
+
|
|
|
+ err := global.GVA_REDIS.Set(contexts, key, "无数据", time.Second * 1).Err()
|
|
|
+ if err != nil {
|
|
|
+ global.GVA_LOG.Info("删除脚本md5值缓存失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ //找到了数据库记录,更新缓存
|
|
|
+ fmt.Println("更新缓存记录")
|
|
|
+ err := global.GVA_REDIS.Set(contexts, key, file.Md5String, 0).Err()
|
|
|
+ if err != nil {
|
|
|
+ global.GVA_LOG.Info("更新脚本md5值缓存失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return err
|
|
|
}
|