提交 0e1d3867 编写于 作者: Mr.奇淼('s avatar Mr.奇淼(

增加删除文件接口

上级 b7f6eaf6
......@@ -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
......
......@@ -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 {
......
......@@ -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
}
// 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"
}
}
},
......
......@@ -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"
}
}
},
......
......@@ -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:
......
......@@ -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) {
// 封装分页方法 调用即可 传入 当前的结构体和分页信息
......
......@@ -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) // 删除指定文件
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册