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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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("检查md5失败!", zap.Error(err))
        response.FailWithMessage("检查md5失败", 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)
}