wangpengfei
2023-08-16 1b8811fe5024ad4e2d744a98dc9a49d65bb87a3b
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package v1
 
import (
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/httpx"
    "aps_crm/service"
    "aps_crm/utils/upload"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "io"
    "os"
    "path/filepath"
)
 
type FileApi struct{}
 
// Add
// @Tags        附件管理
// @Summary    添加附件
// @Produce    application/json
// @Param        object    formData    request.AddFile    true    "查询参数"
// @Param        file    formData    file    true    "上传文件"
// @Success    200        {object}    contextx.Response{}
// @Router        /api/file/add [post]
func (s *FileApi) Add(c *gin.Context) {
    var params request.AddFile
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
 
    _, header, err := c.Request.FormFile("file")
    if err != nil {
        ctx.Fail(ecode.ParamsErr)
        return
    }
 
    oss := upload.NewOss()
    filePath, key, uploadErr := oss.UploadFile(header)
    if uploadErr != nil {
        ctx.Fail(ecode.ParamsErr)
        return
    }
 
    _, filename := filepath.Split(header.Filename)
 
    fileRecord := &model.File{
        Name:          filename,
        Size:          header.Size,
        FilePath:      filePath,
        Key:           key,
        DownloadCount: 0,
        PreviewCount:  0,
        FileType:      httpx.GetMimeType(filePath),
        SourceType:    params.SourceType,
        SourceId:      params.SourceId,
    }
 
    errCode := service.NewFileService().AddFile(fileRecord)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
 
    ctx.Ok()
}
 
// Delete
// @Tags        附件管理
// @Summary    删除附件
// @Produce    application/json
// @Param        id    path        int    true    "查询参数"
// @Success    200    {object}    contextx.Response{}
// @Router        /api/file/delete/{id} [delete]
func (s *FileApi) Delete(c *gin.Context) {
    ctx, ok := contextx.NewContext(c, nil)
    if !ok {
        return
    }
 
    idx := c.Param("id")
    id := cast.ToUint(idx)
    file, err := model.NewFileSearch().SetId(id).First()
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "查找文件失败")
        return
    }
 
    errCode := service.NewFileService().DeleteFile(id)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
 
    err = os.Remove(file.FilePath)
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "删除文件失败")
        return
    }
 
    ctx.Ok()
}
 
//// Update
//// @Tags        附件管理
//// @Summary    更新附件
//// @Produce    application/json
//// @Param        object    body        request.UpdateFile    true    "查询参数"
//// @Success    200        {object}    contextx.Response{}
//// @Router        /api/file/update [put]
//func (s *FileApi) Update(c *gin.Context) {
//    var params request.UpdateFile
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//    if params.Id == 0 {
//        ctx.Fail(ecode.ParamsErr)
//    }
//    params.File.ID = params.Id
//
//    errCode := service.NewFileService().UpdateFile(&params.File)
//    if errCode != ecode.OK {
//        ctx.Fail(errCode)
//        return
//    }
//
//    ctx.Ok()
//}
 
//// List
//// @Tags        附件管理
//// @Summary    获取附件列表
//// @Produce    application/json
//// @Param        object    query        request.GetFileList    true    "参数"
//// @Success    200    {object}    response.ListResponse{data=[]model.File}
//// @Router        /api/file/list [get]
//func (s *FileApi) List(c *gin.Context) {
//    var params request.GetFileList
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//
//    file, total, errCode := service.NewFileService().GetFileList()
//    if errCode != ecode.OK {
//        ctx.Fail(errCode)
//        return
//    }
//
//    ctx.OkWithDetailed(response.ListResponse{
//        Data:  file,
//        Count: total,
//    })
//}
 
// Download
// @Tags    附件管理
// @Summary    附件下载
// @Produce    application/json
// @Param        object    body        request.DownloadFile    true    "参数"
// @Success    200        {object}    contextx.Response{}
// @Router        /api/file/download [post]
func (s *FileApi) Download(c *gin.Context) {
    var params request.DownloadFile
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    file, errCode := service.NewFileService().GetFile(params.Id)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    if file.SourceType != params.SourceType || file.SourceId != params.SourceId || file.Key != params.Key {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    f, err := os.Open(file.FilePath)
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    file.DownloadCount++
    service.NewFileService().UpdateFile(file)
 
    data, err := io.ReadAll(f)
    c.Writer.Header().Set("Content-Type", "application/octet-stream")
    c.Writer.Header().Set("Content-Disposition", "attachment;filename="+file.Name)
    c.Writer.Write(data)
}
 
// Preview
// @Tags    附件管理
// @Summary    附件预览
// @Produce    application/json
// @Param        object    body        request.DownloadFile    true    "参数"
// @Success    200        {object}    contextx.Response{}
// @Router        /api/file/preview [post]
func (s *FileApi) Preview(c *gin.Context) {
    var params request.DownloadFile
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    file, errCode := service.NewFileService().GetFile(params.Id)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
        return
    }
    if file.SourceType != params.SourceType || file.SourceId != params.SourceId || file.Key != params.Key {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    f, err := os.Open(file.FilePath)
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "文件不存在")
        return
    }
    file.PreviewCount++
    service.NewFileService().UpdateFile(file)
 
    data, err := io.ReadAll(f)
    c.Writer.Header().Set("Content-Type", "application/octect-stream")
    c.Writer.Header().Set("Content-Disposition", "attachment;filename="+file.Name)
    c.Writer.Write(data)
}