From 0e1d3867a2c68f7af0bd66a7d2d4833a0095347f Mon Sep 17 00:00:00 2001 From: pixel <303176530@qq.com> Date: Wed, 20 Nov 2019 16:55:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=A0=E9=99=A4=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/api/fileUploadAndDownload.go | 32 ++++++++++- QMPlusServer/controller/api/user.go | 2 +- QMPlusServer/controller/servers/upload.go | 27 +++++++-- QMPlusServer/docs/docs.go | 56 +++++++++++++++++-- QMPlusServer/docs/swagger.json | 54 ++++++++++++++++-- QMPlusServer/docs/swagger.yaml | 34 ++++++++++- .../model/dbModel/fileUploadAndDownload.go | 12 ++++ QMPlusServer/router/fileUploadAndDownload.go | 5 +- 8 files changed, 202 insertions(+), 20 deletions(-) diff --git a/QMPlusServer/controller/api/fileUploadAndDownload.go b/QMPlusServer/controller/api/fileUploadAndDownload.go index 56cee1e2..cbe6c6c1 100644 --- a/QMPlusServer/controller/api/fileUploadAndDownload.go +++ b/QMPlusServer/controller/api/fileUploadAndDownload.go @@ -23,7 +23,7 @@ func UploadFile(c *gin.Context) { servers.ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{}) } else { //文件上传后拿到文件路径 - err, filePath := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) + err, filePath, key := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) if err != nil { servers.ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{}) } else { @@ -33,6 +33,7 @@ func UploadFile(c *gin.Context) { file.Name = header.Filename s := strings.Split(file.Name, ".") file.Tag = s[len(s)-1] + file.Key = key err := file.Upload() if err != nil { servers.ReportFormat(c, false, fmt.Sprintf("修改数据库链接失败,%v", err), gin.H{}) @@ -43,6 +44,35 @@ func UploadFile(c *gin.Context) { } } +// @Tags FileUploadAndDownload +// @Summary 删除文件 +// @Security ApiKeyAuth +// @Produce application/json +// @Param data body dbModel.FileUploadAndDownload true "传入文件里面id即可" +// @Success 200 {string} json "{"success":true,"data":{},"msg":"返回成功"}" +// @Router /fileUploadAndDownload/deleteFile [post] +func DeleteFile(c *gin.Context) { + var file dbModel.FileUploadAndDownload + _ = c.ShouldBind(&file) + err, f := file.FindFile() + if err != nil { + servers.ReportFormat(c, false, fmt.Sprintf("删除失败,%v", err), gin.H{}) + } else { + err = servers.DeleteFile(USER_HEADER_BUCKET, f.Key) + if err != nil { + servers.ReportFormat(c, false, fmt.Sprintf("删除失败,%v", err), gin.H{}) + } else { + err = file.DeleteFile() + if err != nil { + servers.ReportFormat(c, false, fmt.Sprintf("删除失败,%v", err), gin.H{}) + } else { + servers.ReportFormat(c, true, fmt.Sprintf("删除成功,%v", err), gin.H{}) + } + } + } + +} + // @Tags FileUploadAndDownload // @Summary 分页文件列表 // @Security ApiKeyAuth diff --git a/QMPlusServer/controller/api/user.go b/QMPlusServer/controller/api/user.go index 400183ab..91100aae 100644 --- a/QMPlusServer/controller/api/user.go +++ b/QMPlusServer/controller/api/user.go @@ -136,7 +136,7 @@ func UploadHeaderImg(c *gin.Context) { servers.ReportFormat(c, false, fmt.Sprintf("上传文件失败,%v", err), gin.H{}) } else { //文件上传后拿到文件路径 - err, filePath := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) + err, filePath, _ := servers.Upload(header, USER_HEADER_BUCKET, USER_HEADER_IMG_PATH) if err != nil { servers.ReportFormat(c, false, fmt.Sprintf("接收返回值失败,%v", err), gin.H{}) } else { diff --git a/QMPlusServer/controller/servers/upload.go b/QMPlusServer/controller/servers/upload.go index fc856d0c..202bd12d 100644 --- a/QMPlusServer/controller/servers/upload.go +++ b/QMPlusServer/controller/servers/upload.go @@ -13,7 +13,7 @@ var accessKey string = "25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ" // 你在七 var secretKey string = "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY" // 你在七牛云的secretKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西 // 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字 -func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err error, path string) { +func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err error, path string, key string) { putPolicy := storage.PutPolicy{ Scope: bucket, } @@ -36,7 +36,7 @@ func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err erro f, e := file.Open() if e != nil { fmt.Println(e) - return e, "" + return e, "", "" } dataLen := file.Size fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) // 文件名格式 自己可以改 建议保证唯一性 @@ -44,7 +44,26 @@ func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err erro if err != nil { fmt.Println(err) //qmlog.QMLog.Info(err) - return err, "" + return err, "", "" } - return err, urlPath + "/" + ret.Key + return err, urlPath + "/" + ret.Key, ret.Key +} + +func DeleteFile(bucket string, key string) error { + + mac := qbox.NewMac(accessKey, secretKey) + cfg := storage.Config{ + // 是否使用https域名进行资源管理 + UseHTTPS: false, + } + // 指定空间所在的区域,如果不指定将自动探测 + // 如果没有特殊需求,默认不需要指定 + //cfg.Zone=&storage.ZoneHuabei + bucketManager := storage.NewBucketManager(mac, &cfg) + err := bucketManager.Delete(bucket, key) + if err != nil { + fmt.Println(err) + return err + } + return nil } diff --git a/QMPlusServer/docs/docs.go b/QMPlusServer/docs/docs.go index e57725db..78ec4520 100644 --- a/QMPlusServer/docs/docs.go +++ b/QMPlusServer/docs/docs.go @@ -1,6 +1,6 @@ // GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // This file was generated by swaggo/swag at -// 2019-11-20 10:46:22.2795763 +0800 CST m=+0.053896201 +// 2019-11-20 16:37:42.2606856 +0800 CST m=+0.064825001 package docs @@ -505,6 +505,42 @@ var doc = `{ } } }, + "/fileUploadAndDownload/deleteFile": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "FileUploadAndDownload" + ], + "summary": "删除文件", + "parameters": [ + { + "description": "传入文件里面id即可", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "object", + "$ref": "#/definitions/dbModel.FileUploadAndDownload" + } + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"返回成功\"}", + "schema": { + "type": "string" + } + } + } + } + }, "/fileUploadAndDownload/getFileList": { "post": { "security": [ @@ -1251,6 +1287,20 @@ var doc = `{ } } }, + "dbModel.FileUploadAndDownload": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "tag": { + "type": "string" + }, + "url": { + "type": "string" + } + } + }, "dbModel.Workflow": { "type": "object", "properties": { @@ -1297,10 +1347,6 @@ var doc = `{ "stepNo": { "description": "步骤id (第几步)", "type": "number" - }, - "workflowID": { - "description": "所属工作流ID", - "type": "integer" } } }, diff --git a/QMPlusServer/docs/swagger.json b/QMPlusServer/docs/swagger.json index 6008ffd4..bfcd8a53 100644 --- a/QMPlusServer/docs/swagger.json +++ b/QMPlusServer/docs/swagger.json @@ -488,6 +488,42 @@ } } }, + "/fileUploadAndDownload/deleteFile": { + "post": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "produces": [ + "application/json" + ], + "tags": [ + "FileUploadAndDownload" + ], + "summary": "删除文件", + "parameters": [ + { + "description": "传入文件里面id即可", + "name": "data", + "in": "body", + "required": true, + "schema": { + "type": "object", + "$ref": "#/definitions/dbModel.FileUploadAndDownload" + } + } + ], + "responses": { + "200": { + "description": "{\"success\":true,\"data\":{},\"msg\":\"返回成功\"}", + "schema": { + "type": "string" + } + } + } + } + }, "/fileUploadAndDownload/getFileList": { "post": { "security": [ @@ -1234,6 +1270,20 @@ } } }, + "dbModel.FileUploadAndDownload": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "tag": { + "type": "string" + }, + "url": { + "type": "string" + } + } + }, "dbModel.Workflow": { "type": "object", "properties": { @@ -1280,10 +1330,6 @@ "stepNo": { "description": "步骤id (第几步)", "type": "number" - }, - "workflowID": { - "description": "所属工作流ID", - "type": "integer" } } }, diff --git a/QMPlusServer/docs/swagger.yaml b/QMPlusServer/docs/swagger.yaml index 3327952a..e1df82bd 100644 --- a/QMPlusServer/docs/swagger.yaml +++ b/QMPlusServer/docs/swagger.yaml @@ -112,6 +112,15 @@ definitions: title: type: string type: object + dbModel.FileUploadAndDownload: + properties: + name: + type: string + tag: + type: string + url: + type: string + type: object dbModel.Workflow: properties: workflowDescription: @@ -146,9 +155,6 @@ definitions: stepNo: description: 步骤id (第几步) type: number - workflowID: - description: 所属工作流ID - type: integer type: object modelInterface.PageInfo: properties: @@ -460,6 +466,28 @@ paths: summary: 用户注册账号 tags: - Base + /fileUploadAndDownload/deleteFile: + post: + parameters: + - description: 传入文件里面id即可 + in: body + name: data + required: true + schema: + $ref: '#/definitions/dbModel.FileUploadAndDownload' + type: object + produces: + - application/json + responses: + "200": + description: '{"success":true,"data":{},"msg":"返回成功"}' + schema: + type: string + security: + - ApiKeyAuth: [] + summary: 删除文件 + tags: + - FileUploadAndDownload /fileUploadAndDownload/getFileList: post: consumes: diff --git a/QMPlusServer/model/dbModel/fileUploadAndDownload.go b/QMPlusServer/model/dbModel/fileUploadAndDownload.go index 22d1307b..879f6b02 100644 --- a/QMPlusServer/model/dbModel/fileUploadAndDownload.go +++ b/QMPlusServer/model/dbModel/fileUploadAndDownload.go @@ -12,6 +12,7 @@ type FileUploadAndDownload struct { Name string `json:"name"` Url string `json:"url"` Tag string `json:"tag"` + Key string `json:"key"` } func (f *FileUploadAndDownload) Upload() error { @@ -19,6 +20,17 @@ func (f *FileUploadAndDownload) Upload() error { return err } +func (f *FileUploadAndDownload) DeleteFile() error { + err := qmsql.DEFAULTDB.Where("id = ?", f.ID).Delete(f).Error + return err +} + +func (f *FileUploadAndDownload) FindFile() (error, FileUploadAndDownload) { + var file FileUploadAndDownload + err := qmsql.DEFAULTDB.Where("id = ?", f.ID).First(&file).Error + return err, file +} + // 分页获取数据 需要分页实现这个接口即可 func (f *FileUploadAndDownload) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) { // 封装分页方法 调用即可 传入 当前的结构体和分页信息 diff --git a/QMPlusServer/router/fileUploadAndDownload.go b/QMPlusServer/router/fileUploadAndDownload.go index 18a8dcac..048710a8 100644 --- a/QMPlusServer/router/fileUploadAndDownload.go +++ b/QMPlusServer/router/fileUploadAndDownload.go @@ -3,13 +3,14 @@ package router import ( "github.com/gin-gonic/gin" "main/controller/api" - "main/middleware" ) func InitFileUploadAndDownloadRouter(Router *gin.Engine) { - FileUploadAndDownloadGroup := Router.Group("fileUploadAndDownload").Use(middleware.JWTAuth()) + FileUploadAndDownloadGroup := Router.Group("fileUploadAndDownload") + //.Use(middleware.JWTAuth()) { FileUploadAndDownloadGroup.POST("/upload", api.UploadFile) // 上传文件 FileUploadAndDownloadGroup.POST("/getFileList", api.GetFileList) // 获取上传文件列表 + FileUploadAndDownloadGroup.POST("/deleteFile", api.DeleteFile) // 删除指定文件 } }