zhangqian
2023-08-29 efea3aaac3a6358cbe74891d47221b835a38d297
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package example
 
import (
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
    "srm/global"
    "srm/model/common/request"
    "srm/model/common/response"
    "srm/model/example"
    exampleRes "srm/model/example/response"
)
 
type FileUploadAndDownloadApi struct{}
 
// UploadFile
// @Tags      ExaFileUploadAndDownload
// @Summary   上传文件示例
// @Security  ApiKeyAuth
// @accept    multipart/form-data
// @Produce   application/json
// @Param     file  formData  file                                                           true  "上传文件示例"
// @Success   200   {object}  response.Response{data=exampleRes.ExaFileResponse,msg=string}  "上传文件示例,返回包括文件详情"
// @Router    /fileUploadAndDownload/upload [post]
func (b *FileUploadAndDownloadApi) UploadFile(c *gin.Context) {
    var file example.ExaFileUploadAndDownload
    noSave := c.DefaultQuery("noSave", "0")
    _, header, err := c.Request.FormFile("file")
    if err != nil {
        global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
        response.FailWithMessage("接收文件失败", c)
        return
    }
    file, err = fileUploadAndDownloadService.UploadFile(header, noSave) // 文件上传后拿到文件路径
    if err != nil {
        global.GVA_LOG.Error("修改数据库链接失败!", zap.Error(err))
        response.FailWithMessage("修改数据库链接失败", c)
        return
    }
    response.OkWithDetailed(exampleRes.ExaFileResponse{File: file}, "上传成功", c)
}
 
// EditFileName 编辑文件名或者备注
func (b *FileUploadAndDownloadApi) EditFileName(c *gin.Context) {
    var file example.ExaFileUploadAndDownload
    err := c.ShouldBindJSON(&file)
    if err != nil {
        response.FailWithMessage(err.Error(), c)
        return
    }
    err = fileUploadAndDownloadService.EditFileName(file)
    if err != nil {
        global.GVA_LOG.Error("编辑失败!", zap.Error(err))
        response.FailWithMessage("编辑失败", c)
        return
    }
    response.OkWithMessage("编辑成功", c)
}
 
// DeleteFile
// @Tags      ExaFileUploadAndDownload
// @Summary   删除文件
// @Security  ApiKeyAuth
// @Produce   application/json
// @Param     data  body      example.ExaFileUploadAndDownload  true  "传入文件里面id即可"
// @Success   200   {object}  response.Response{msg=string}     "删除文件"
// @Router    /fileUploadAndDownload/deleteFile [post]
func (b *FileUploadAndDownloadApi) DeleteFile(c *gin.Context) {
    var file example.ExaFileUploadAndDownload
    err := c.ShouldBindJSON(&file)
    if err != nil {
        response.FailWithMessage(err.Error(), c)
        return
    }
    if err := fileUploadAndDownloadService.DeleteFile(file); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
        response.FailWithMessage("删除失败", c)
        return
    }
    response.OkWithMessage("删除成功", c)
}
 
// GetFileList
// @Tags      ExaFileUploadAndDownload
// @Summary   分页文件列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.PageInfo                                        true  "页码, 每页大小"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string}  "分页文件列表,返回包括列表,总数,页码,每页数量"
// @Router    /fileUploadAndDownload/getFileList [post]
func (b *FileUploadAndDownloadApi) GetFileList(c *gin.Context) {
    var pageInfo request.PageInfo
    err := c.ShouldBindJSON(&pageInfo)
    if err != nil {
        response.FailWithMessage(err.Error(), c)
        return
    }
    list, total, err := fileUploadAndDownloadService.GetFileRecordInfoList(pageInfo)
    if err != nil {
        global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
        return
    }
    response.OkWithDetailed(response.PageResult{
        List:     list,
        Total:    total,
        Page:     pageInfo.Page,
        PageSize: pageInfo.PageSize,
    }, "获取成功", c)
}