Sfoglia il codice sorgente

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	config.yaml
#	initialize/timer.go
wangbin 3 anni fa
parent
commit
e1494ae9c2

+ 1 - 1
README.md

@@ -52,7 +52,7 @@
 | `--timer` | timer | 定时器接口封装 |
 | `--upload`      | oss                  | oss接口封装        |
 
-## 在项目目录下运行
+## 在项目目录下运行1
 go mod tidy
 
 go run main.go

+ 17 - 0
api/v1/task/upload_file.go

@@ -1,6 +1,7 @@
 package task
 
 import (
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"go.uber.org/zap"
 	"log-server/global"
@@ -32,3 +33,19 @@ func (b *UploadFileApi) UploadFile(c *gin.Context) {
 	}
 	response.OkWithDetailed(taskRes.InfoResponse{fileRes}, "上传成功", c)
 }
+
+func (b *UploadFileApi) DownloadFile(c *gin.Context) {
+	gameIdStr := c.Query("gameId")
+	md5String := c.Query("md5String")
+
+	fmt.Println(gameIdStr)
+	gameId, _ := strconv.Atoi(gameIdStr)
+	downloadFile, err := uploadFileService.DownloadFile(gameId, md5String)
+	if err != nil {
+		global.GVA_LOG.Error("查询失败!", zap.Error(err))
+		response.FailWithMessage("查询失败", c)
+	} else {
+		response.OkWithDetailed(taskRes.DownloadInfoResponse{downloadFile}, "查询成功", c)
+	}
+
+}

+ 5 - 1
config.yaml

@@ -162,4 +162,8 @@ send-url:
   keyword-send-url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=34cbfa5a-c31d-464f-baf8-8363d6f4ac6a"
 extranet-domain:
   #control: "http://bpm.kfzs.com:7089"
-  control: "http://192.168.50.35:8099"
+  control: "http://192.168.50.35:8099"
+#extranet-domain:
+#  control: "http://192.168.50.35:8099"
+prefix-url:
+  prefix-local: "http://192.168.50.17:8080/api/"

+ 1 - 1
config/config.go

@@ -23,7 +23,7 @@ type Server struct {
 
 	Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
 	Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"`
-
+	PrefixUrl PrefixUrl `mapstructure:"prefix-url" json:"prefix-url" yaml:"prefix-url"`
 	// 跨域配置
 	Cors           CORS           `mapstructure:"cors" json:"cors" yaml:"cors"`
 	SendUrl        SendUrl        `mapstructure:"send-url" json:"send-url" yaml:"send-url"`

+ 5 - 0
config/prefix_url.go

@@ -0,0 +1,5 @@
+package config
+
+type PrefixUrl struct {
+	PrefixLocal string `mapstructure:"prefix-local" json:"prefix-local" yaml:"prefix-local"`
+}

+ 12 - 12
initialize/timer.go

@@ -41,17 +41,17 @@ func Timer() {
 		}
 	}
 
-	// 定时更新前一天的统计数据
-	_, err := global.GVA_Timer.AddTaskByFunc("StatisticsLog", "2 8 5 * * * ", serviceStatisticsLog.CreateStatisticsLog)
-	if err != nil {
-		fmt.Println("add taskCode timer error:", err)
-	}
-
-	//定时同步电脑相关的统计数据
-	_, err = global.GVA_Timer.AddTaskByFunc("CreateComputerStatisticsData", "40 9,19,29,39,49,59 7-23 * * * ", serviceStatisticsLog.CreateComputerStatisticsData)
-	if err != nil {
-		fmt.Println("add CreateComputerStatisticsData timer error:", err)
-	}
+	//// 定时更新前一天的统计数据
+	//_, err := global.GVA_Timer.AddTaskByFunc("StatisticsLog", "2 8 5 * * * ", serviceStatisticsLog.CreateStatisticsLog)
+	//if err != nil {
+	//	fmt.Println("add taskCode timer error:", err)
+	//}
+	//
+	////定时同步电脑相关的统计数据
+	//_, err = global.GVA_Timer.AddTaskByFunc("CreateComputerStatisticsData", "40 9,19,29,39,49,59 7-23 * * * ", serviceStatisticsLog.CreateComputerStatisticsData)
+	//if err != nil {
+	//	fmt.Println("add CreateComputerStatisticsData timer error:", err)
+	//}
 	//// 同步游戏相关的统计数据
 	//_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 8,18,28,38,48,58 6-23 * * * ", serviceStatisticsLog.TodayCreateStatisticsGameInfoLog)
 	//if err != nil {
@@ -88,7 +88,7 @@ func Timer() {
 	//	fmt.Println("add EveryDaySyncTaskData timer error:", err)
 	//}
 	////同步任务数据
-	//_, err = global.GVA_Timer.AddTaskByFunc("SyncTaskData", "35 0/3 1-23 * * *", syncData.SyncTaskData)
+	////_, err = global.GVA_Timer.AddTaskByFunc("SyncTaskData", "35 0/3 1-23 * * *", syncData.SyncTaskData)
 	//if err != nil {
 	//	fmt.Println("add SyncTaskData timer error:", err)
 	//}

+ 6 - 0
model/task/request/download_file.go

@@ -0,0 +1,6 @@
+package request
+
+type DownloadRequest struct {
+	GameId int `json:"game_id"`
+	Md5String string `json:"md5_string"`
+}

+ 7 - 0
model/task/response/download_file.go

@@ -0,0 +1,7 @@
+package response
+
+type DownloadFile struct {
+	Url string `json:"url"`
+	Md5String string `json:"md5_string"`
+	Flag bool `json:"flag"`
+}

+ 5 - 0
model/task/response/upload_file.go

@@ -7,8 +7,13 @@ type UploadFileResponse struct {
 	Tag  string `json:"tag" gorm:"comment:文件标签"` // 文件标签
 	Key  string `json:"key" gorm:"comment:编号"`   // 编号
 	Version int `json:"version"`	//版本号
+	Md5String string `json:"md5_string"`	//md5值
 }
 
 type InfoResponse struct {
 	File UploadFileResponse `json:"file"`
+}
+
+type DownloadInfoResponse struct {
+	Info DownloadFile `json:"info"`
 }

+ 1 - 0
router/task/upload_file.go

@@ -14,6 +14,7 @@ func (e *UploadFileRouter) InitUploadFileRouter(Router *gin.RouterGroup) {
 	UploadFileApi := v1.ApiGroupApp.GroupTask.UploadFileApi
 	{
 		GameTaskRouter.POST("addFileUpdate", UploadFileApi.UploadFile)
+		GameTaskRouter.GET("downloadFile", UploadFileApi.DownloadFile)
 
 	}
 }

+ 101 - 6
service/task/upload_file.go

@@ -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,74 @@ 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() // 读取文件
+	f1, openErr := file.Open()	//	读取文件生成MD5值
+	if openError != nil || openErr != 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 关闭
+	defer f1.Close()
+	//生成md5值
+	md5h := md5.New()
+	io.Copy(md5h, f1)
+	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 +104,42 @@ 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
+	var prefix string = global.GVA_CONFIG.PrefixUrl.PrefixLocal
+	//数据库中无记录,即不需要更新
+	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, errors.New("无对应game_id")
+	}
+
+	if file.Md5String == md5String {
+		info = response.DownloadFile{
+			Url: "",
+			Md5String: "",
+			Flag: false,
+		}
+	} else {
+		fullUrl := prefix + file.Url
+		info = response.DownloadFile{
+			Url: fullUrl,
+			Md5String: file.Md5String,
+			Flag: true,
+		}
+	}
+
+	return info, err
 
-}
+
+}

BIN
uploads/file/f3fec2e20827e0864de04b7efacf5f6c_20230224154304.dll


BIN
uploads/file/f3fec2e20827e0864de04b7efacf5f6c_20230224155919.dll


BIN
uploads/file/f3fec2e20827e0864de04b7efacf5f6c_20230224160026.dll


BIN
uploads/file/f3fec2e20827e0864de04b7efacf5f6c_20230224160750.dll


BIN
uploads/file/f3fec2e20827e0864de04b7efacf5f6c_20230224165651.dll


BIN
uploads/file/f3fec2e20827e0864de04b7efacf5f6c_20230224153010.dll