zhangf
2024-07-26 698995469a3fcdc3164fc486d18bdbe059b6c92e
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
package com.gkhy.fourierSpecialGasMonitor.api.controller.attachment;
import com.gkhy.fourierSpecialGasMonitor.api.controller.attachment.converter.AttachmentApiConverter;
import com.gkhy.fourierSpecialGasMonitor.api.controller.common.BaseController;
import com.gkhy.fourierSpecialGasMonitor.application.attachment.service.AttachmentAppService;
import com.gkhy.fourierSpecialGasMonitor.application.attachment.service.dto.resp.AttachmentAppRespDTO;
import com.gkhy.fourierSpecialGasMonitor.commons.domain.Result;
import com.gkhy.fourierSpecialGasMonitor.commons.enums.ResultCode;
import com.gkhy.fourierSpecialGasMonitor.domain.attachment.enums.FileProjectConstants;
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
 */
@RestController
@RequestMapping("/attachment")
public class AttachmentController extends BaseController {
 
    @Autowired
    private AttachmentAppService attachmentAppService;
    @Autowired
    private AttachmentApiConverter converter;
 
    /**
     * 根据标识查询文件具体信息
     * @param key
     * @return
     */
    @GetMapping("/get/{key}")
    public Result get(@PathVariable String key) {
        AttachmentAppRespDTO byKey = attachmentAppService.findByKey(key);
        return new Result(ResultCode.OK, converter.getApiRespDTO(byKey));
    }
 
    /**
     * 根据id获取文件
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        AttachmentAppRespDTO byId = attachmentAppService.findById(id);
        return new Result(ResultCode.OK,converter.getApiRespDTO(byId));
    }
 
 
    /**
     * 删除文件数据
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        attachmentAppService.delete(id);
        return Result.success();
    }
 
 
 
    /**
     * @Description 上传文件(返回文件标识)
     * @Param [file, module]
     **/
    @PostMapping("/upload/key")
    public Result uploadReturnKey(@RequestParam("file") MultipartFile
                                     file, @RequestParam("module") String module) {
        Object returnStr = attachmentAppService.saveFileToPath(file, module, FileProjectConstants.ReturnType.KEY,getCurrentUserId());
        return new Result(ResultCode.OK,returnStr);
    }
 
    /**
     * @Description 上传文件(返回文件信息)
     * @Param [file, module]
     **/
    @PostMapping("/upload/detail")
    public Result uploadReturnDetail(@RequestParam("file") MultipartFile
                                        file, @RequestParam("module") String module) {
        Object detail = attachmentAppService.saveFileToPath(file, module, FileProjectConstants.ReturnType.DETAIL,getCurrentUserId());
        return new Result(ResultCode.OK,detail);
    }
    /**
     * @Description 上传文件(返回访问路径)
     * @Date 2021/4/20 19:28
     * @Param [file, module]
     **/
    @PostMapping("/upload/url")
    public Result uploadReturnUrl(@RequestParam("file") MultipartFile file, @RequestParam("module") String module) {
        Object returnStr = attachmentAppService.saveFileToPath(file, module, FileProjectConstants.ReturnType.URL,getCurrentUserId());
        return new Result(ResultCode.OK,returnStr);
    }
 
    /**
     * @return org.springframework.http.ResponseEntity<byte [ ]>
     * @Description 获取下载文件流
     * @Param [key]
     **/
    @GetMapping("/downloadFile/{key}")
    public void download(@PathVariable String key, HttpServletResponse response) {
        attachmentAppService.downloadForStream(response, key);
    }
 
    /**
     * @return void
     * @Description 文件下载(改写)
     * @Param [key, response, request]
     **/
    @RequestMapping(value = "/downloadFileByKey/{key}", method = {RequestMethod.POST, RequestMethod.GET})
    public void downloadFileByKey(@PathVariable String key, HttpServletResponse response, HttpServletRequest
            request) {
        attachmentAppService.downloadByKey(response, request, key);
    }
 
 
    /**
     * @Description 获取下载文件路径
     * @Param [key]
     **/
    @GetMapping("/downloadUrl/{key}")
    public Result downLoad( @PathVariable String key) {
        AttachmentAppRespDTO byKey = attachmentAppService.findByKey(key);
        return new Result(ResultCode.OK,byKey.getFileUrl());
    }
 
 
}