zhangfeng
2023-07-26 dd59c95e87ba585c4e3e2f059e218853784402e5
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
package com.gk.hotwork.Controller;
import com.gk.hotwork.Controller.Base.BaseController;
import com.gk.hotwork.Domain.AttachmentInfo;
import com.gk.hotwork.Domain.Utils.Msg;
import com.gk.hotwork.Service.AttachmentService;
 
import com.gk.hotwork.common.FileProjectConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
/**
 * @email 1603559716@qq.com
 * @author: zf
 * @date: 2023/5/6
 * @time: 14:22
 */
@Api(tags = "附件上传接口")
@RestController
@RequestMapping("/attachment")
public class AttachmentController extends BaseController {
 
    @Autowired
    private AttachmentService attachmentService;
 
 
 /**
     * 根据标识查询文件具体信息
     * @param key
     * @return
     */
     @ApiOperation(value = "根据标识查询文件具体信息",response = Msg.class)
     @ApiImplicitParams({
             @ApiImplicitParam(name = "key",value = "文件标识")
     })
    @GetMapping("/get/key/{key}")
    public Msg findByKey(@PathVariable String key) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("查询成功");
        AttachmentInfo byKey = attachmentService.findByKey(key);
        msg.setResult(byKey);
        return msg;
    }
 
   /**
     * 根据id获取文件
     * @param id
     * @return
     */
   @ApiOperation(value = "根据id获取文件",response = Msg.class)
   @ApiImplicitParams({
           @ApiImplicitParam(name = "id",value = "文件id")
   })
    @GetMapping("/get/id/{id}")
    public Msg get(@PathVariable Long id) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("查询成功");
        AttachmentInfo byKey = attachmentService.findById(id);
        msg.setResult(byKey);
        return msg;
    }
 
    /**
     * 删除文件数据
     * @param id
     * @return
     */
    @ApiOperation(value = "删除文件数据",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文件id")
    })
    @DeleteMapping("/delete/{id}")
    public Msg delete(@PathVariable Long id) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("删除成功");
        attachmentService.delete(id,getUser());
        return msg;
 
    }
 
    /**
     * 单个文件上传
     * @Description 上传文件(返回文件标识)
     * @Param [file, module]
     **/
    @ApiOperation(value = "上传文件(返回文件标识)-单个文件",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件"),
            @ApiImplicitParam(name = "module",value = "checkPath")
    })
    @PostMapping("/upload/key")
    public Msg uploadReturnKey(@RequestParam("file") MultipartFile
                                     file, @RequestParam("module") String module) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("上传成功");
        Object returnStr = attachmentService.saveFileToPath(file, module, FileProjectConstants.ReturnType.KEY,getUser());
        msg.setResult(returnStr);
        return msg;
    }
 
    /**
     * 单个文件
     * @Description 上传文件(返回文件信息)
     * @Param [file, module]
     **/
    @ApiOperation(value = "上传文件(返回文件信息)-单个文件",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件"),
            @ApiImplicitParam(name = "module",value = "模块(checkPath)")
    })
    @PostMapping("/upload/detail")
    public Msg uploadReturnDetail(@RequestParam("file") MultipartFile
                                        file, @RequestParam("module") String module) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("上传成功");
        Object detail = attachmentService.saveFileToPath(file, module, FileProjectConstants.ReturnType.DETAIL,getUser());
        msg.setResult(detail);
        return msg;
    }
    /**
     * 单个文件
     * @Description 上传文件(返回访问路径)
     * @Date 2021/4/20 19:28
     * @Param [file, module]
     **/
    @ApiOperation(value = "上传文件(返回访问路径)-单个文件",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件"),
            @ApiImplicitParam(name = "module",value = "模块(checkPath)")
    })
    @PostMapping("/upload/url")
    public Msg uploadReturnUrl(@RequestParam("file") MultipartFile file, @RequestParam("module") String module) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("上传成功");
        Object returnStr = attachmentService.saveFileToPath(file, module, FileProjectConstants.ReturnType.URL,getUser());
        msg.setResult(returnStr);
        return msg;
    }
 
    /**
     * 批量文件
     * @Description 上传文件(返回文件信息)
     * @Param [file, module]
     **/
    @ApiOperation(value = "上传文件(返回访问路径)-批量",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件"),
            @ApiImplicitParam(name = "module",value = "模块(checkPath)")
    })
    @PostMapping("/upload/batch/detail")
    public Msg uploadBatchReturnDetail(@RequestParam("file") MultipartFile[]
                                          file, @RequestParam("module") String module) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("上传成功");
        Object detail = attachmentService.saveBatchFileToPath(file, module,getUser());
        msg.setResult(detail);
        return msg;
    }
 
    /**
     * @return org.springframework.http.ResponseEntity<byte [ ]>
     * @Description 获取下载文件流
     * @Param [key]
     **/
    @ApiOperation(value = "获取下载文件流",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key",value = "文件标识"),
    })
    @GetMapping("/downloadFile/{key}")
    public void download(@PathVariable String key, HttpServletResponse response) {
        attachmentService.downloadForStream(response, key);
    }
 
    /**
     * @return void
     * @Description 文件下载
     * @Param [key, response, request]
     **/
    @ApiOperation(value = "文件下载",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "文件id"),
    })
    @RequestMapping(value = "/downloadFileById/{id}", method = {RequestMethod.POST, RequestMethod.GET})
    public void downloadFileByKey(@PathVariable Long id, HttpServletResponse response, HttpServletRequest
            request) {
        attachmentService.downloadById(response, request, id);
    }
 
    /**
     * @Description 获取下载文件路径
     * @Param [key]
     **/
    @ApiOperation(value = "获取下载文件路径",response = Msg.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key",value = "文件key"),
    })
    @GetMapping("/downloadUrl/{key}")
    public Msg downLoad( @PathVariable String key) {
        Msg msg = new Msg();
        msg.setCode("200");
        msg.setMessage("查询成功");
        AttachmentInfo byKey = attachmentService.findByKey(key);
        msg.setResult(byKey);
        return msg;
    }
 
 
}