From 8407a43ee08ebd0f8e4b500cfcd83dcace95337c Mon Sep 17 00:00:00 2001 From: wangpengfei <274878379@qq.com> Date: 星期六, 26 八月 2023 15:02:30 +0800 Subject: [PATCH] fix --- api/v1/example/exa_breakpoint_continue.go | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++ go.sum | 6 ++ go.mod | 7 + 3 files changed, 161 insertions(+), 2 deletions(-) diff --git a/api/v1/example/exa_breakpoint_continue.go b/api/v1/example/exa_breakpoint_continue.go new file mode 100644 index 0000000..450b3dd --- /dev/null +++ b/api/v1/example/exa_breakpoint_continue.go @@ -0,0 +1,150 @@ +package example + +import ( + "fmt" + "io" + "mime/multipart" + "strconv" + + "srm/model/example" + + "github.com/gin-gonic/gin" + "go.uber.org/zap" + "srm/global" + "srm/model/common/response" + exampleRes "srm/model/example/response" + "srm/utils" +) + +// BreakpointContinue +// @Tags ExaFileUploadAndDownload +// @Summary 鏂偣缁紶鍒版湇鍔″櫒 +// @Security ApiKeyAuth +// @accept multipart/form-data +// @Produce application/json +// @Param file formData file true "an example for breakpoint resume, 鏂偣缁紶绀轰緥" +// @Success 200 {object} response.Response{msg=string} "鏂偣缁紶鍒版湇鍔″櫒" +// @Router /fileUploadAndDownload/breakpointContinue [post] +func (b *FileUploadAndDownloadApi) BreakpointContinue(c *gin.Context) { + fileMd5 := c.Request.FormValue("fileMd5") + fileName := c.Request.FormValue("fileName") + chunkMd5 := c.Request.FormValue("chunkMd5") + chunkNumber, _ := strconv.Atoi(c.Request.FormValue("chunkNumber")) + chunkTotal, _ := strconv.Atoi(c.Request.FormValue("chunkTotal")) + _, FileHeader, err := c.Request.FormFile("file") + if err != nil { + global.GVA_LOG.Error("鎺ユ敹鏂囦欢澶辫触!", zap.Error(err)) + response.FailWithMessage("鎺ユ敹鏂囦欢澶辫触", c) + return + } + f, err := FileHeader.Open() + if err != nil { + global.GVA_LOG.Error("鏂囦欢璇诲彇澶辫触!", zap.Error(err)) + response.FailWithMessage("鏂囦欢璇诲彇澶辫触", c) + return + } + defer func(f multipart.File) { + err := f.Close() + if err != nil { + fmt.Println(err) + } + }(f) + cen, _ := io.ReadAll(f) + if !utils.CheckMd5(cen, chunkMd5) { + global.GVA_LOG.Error("妫�鏌d5澶辫触!", zap.Error(err)) + response.FailWithMessage("妫�鏌d5澶辫触", c) + return + } + file, err := fileUploadAndDownloadService.FindOrCreateFile(fileMd5, fileName, chunkTotal) + if err != nil { + global.GVA_LOG.Error("鏌ユ壘鎴栧垱寤鸿褰曞け璐�!", zap.Error(err)) + response.FailWithMessage("鏌ユ壘鎴栧垱寤鸿褰曞け璐�", c) + return + } + pathC, err := utils.BreakPointContinue(cen, fileName, chunkNumber, chunkTotal, fileMd5) + if err != nil { + global.GVA_LOG.Error("鏂偣缁紶澶辫触!", zap.Error(err)) + response.FailWithMessage("鏂偣缁紶澶辫触", c) + return + } + + if err = fileUploadAndDownloadService.CreateFileChunk(file.ID, pathC, chunkNumber); err != nil { + global.GVA_LOG.Error("鍒涘缓鏂囦欢璁板綍澶辫触!", zap.Error(err)) + response.FailWithMessage("鍒涘缓鏂囦欢璁板綍澶辫触", c) + return + } + response.OkWithMessage("鍒囩墖鍒涘缓鎴愬姛", c) +} + +// FindFile +// @Tags ExaFileUploadAndDownload +// @Summary 鏌ユ壘鏂囦欢 +// @Security ApiKeyAuth +// @accept multipart/form-data +// @Produce application/json +// @Param file formData file true "Find the file, 鏌ユ壘鏂囦欢" +// @Success 200 {object} response.Response{data=exampleRes.FileResponse,msg=string} "鏌ユ壘鏂囦欢,杩斿洖鍖呮嫭鏂囦欢璇︽儏" +// @Router /fileUploadAndDownload/findFile [post] +func (b *FileUploadAndDownloadApi) FindFile(c *gin.Context) { + fileMd5 := c.Query("fileMd5") + fileName := c.Query("fileName") + chunkTotal, _ := strconv.Atoi(c.Query("chunkTotal")) + file, err := fileUploadAndDownloadService.FindOrCreateFile(fileMd5, fileName, chunkTotal) + if err != nil { + global.GVA_LOG.Error("鏌ユ壘澶辫触!", zap.Error(err)) + response.FailWithMessage("鏌ユ壘澶辫触", c) + } else { + response.OkWithDetailed(exampleRes.FileResponse{File: file}, "鏌ユ壘鎴愬姛", c) + } +} + +// BreakpointContinueFinish +// @Tags ExaFileUploadAndDownload +// @Summary 鍒涘缓鏂囦欢 +// @Security ApiKeyAuth +// @accept multipart/form-data +// @Produce application/json +// @Param file formData file true "涓婁紶鏂囦欢瀹屾垚" +// @Success 200 {object} response.Response{data=exampleRes.FilePathResponse,msg=string} "鍒涘缓鏂囦欢,杩斿洖鍖呮嫭鏂囦欢璺緞" +// @Router /fileUploadAndDownload/findFile [post] +func (b *FileUploadAndDownloadApi) BreakpointContinueFinish(c *gin.Context) { + fileMd5 := c.Query("fileMd5") + fileName := c.Query("fileName") + filePath, err := utils.MakeFile(fileName, fileMd5) + if err != nil { + global.GVA_LOG.Error("鏂囦欢鍒涘缓澶辫触!", zap.Error(err)) + response.FailWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "鏂囦欢鍒涘缓澶辫触", c) + } else { + response.OkWithDetailed(exampleRes.FilePathResponse{FilePath: filePath}, "鏂囦欢鍒涘缓鎴愬姛", c) + } +} + +// RemoveChunk +// @Tags ExaFileUploadAndDownload +// @Summary 鍒犻櫎鍒囩墖 +// @Security ApiKeyAuth +// @accept multipart/form-data +// @Produce application/json +// @Param file formData file true "鍒犻櫎缂撳瓨鍒囩墖" +// @Success 200 {object} response.Response{msg=string} "鍒犻櫎鍒囩墖" +// @Router /fileUploadAndDownload/removeChunk [post] +func (b *FileUploadAndDownloadApi) RemoveChunk(c *gin.Context) { + var file example.ExaFile + err := c.ShouldBindJSON(&file) + if err != nil { + response.FailWithMessage(err.Error(), c) + return + } + err = utils.RemoveChunk(file.FileMd5) + if err != nil { + global.GVA_LOG.Error("缂撳瓨鍒囩墖鍒犻櫎澶辫触!", zap.Error(err)) + return + } + err = fileUploadAndDownloadService.DeleteFileChunk(file.FileMd5, file.FilePath) + if err != nil { + global.GVA_LOG.Error(err.Error(), zap.Error(err)) + response.FailWithMessage(err.Error(), c) + return + } + response.OkWithMessage("缂撳瓨鍒囩墖鍒犻櫎鎴愬姛", c) +} diff --git a/go.mod b/go.mod index 4013695..304e5ba 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ github.com/sashabaranov/go-openai v1.14.1 github.com/shirou/gopsutil/v3 v3.23.6 github.com/songzhibin97/gkit v1.2.11 + github.com/spf13/cast v1.5.1 github.com/spf13/viper v1.16.0 github.com/stretchr/testify v1.8.4 github.com/swaggo/gin-swagger v1.3.0 @@ -38,6 +39,8 @@ golang.org/x/crypto v0.10.0 golang.org/x/sync v0.3.0 golang.org/x/text v0.11.0 + google.golang.org/grpc v1.55.0 + google.golang.org/protobuf v1.30.0 gorm.io/driver/mysql v1.5.1 gorm.io/driver/postgres v1.5.2 gorm.io/driver/sqlserver v1.5.1 @@ -70,6 +73,7 @@ github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect + github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-querystring v1.0.0 // indirect github.com/google/uuid v1.3.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect @@ -102,7 +106,6 @@ github.com/rogpeppe/go-internal v1.11.0 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/spf13/afero v1.9.5 // indirect - github.com/spf13/cast v1.5.1 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.4.2 // indirect @@ -120,7 +123,7 @@ golang.org/x/sys v0.10.0 // indirect golang.org/x/time v0.1.0 // indirect golang.org/x/tools v0.9.1 // indirect - google.golang.org/protobuf v1.30.0 // indirect + google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect gorm.io/plugin/dbresolver v1.4.1 // indirect diff --git a/go.sum b/go.sum index 6f18b8d..d17cab9 100644 --- a/go.sum +++ b/go.sum @@ -206,6 +206,7 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= @@ -800,6 +801,8 @@ google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210108203827-ffc7fda8c3d7/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20210226172003-ab064af71705/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A= +google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -816,6 +819,8 @@ google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8= google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= +google.golang.org/grpc v1.55.0 h1:3Oj82/tFSCeUrRTg/5E/7d/W5A1tj6Ky1ABAuZuv5ag= +google.golang.org/grpc v1.55.0/go.mod h1:iYEXKGkEBhg1PjZQvoYEVPTDkHo1/bjTnfwTeGONTY8= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -827,6 +832,7 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -- Gitblit v1.8.0