liurunyu
2024-10-21 a408f749e77f3c13834770727dc2e1e6aafe062c
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
231
232
233
234
235
236
237
238
239
240
241
242
package com.dy.pipIrrGlobal.webCtrls;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pipIrrGlobal.dyFile.FileConstant;
import com.dy.pipIrrGlobal.dyFile.FileOperate;
import com.dy.pipIrrGlobal.dyFile.FileRestVo;
import com.dy.pipIrrGlobal.dyFile.FileVo;
import com.dy.pipIrrGlobal.pojoFi.WebFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.Date;
/**
 * web文件上传
 */
@Slf4j
@RestController
@RequestMapping(path="webFile")
public class WebFileCtrl {
 
    @Autowired
    private FileOperate fileOp ;
 
    @Autowired
    private WebFileSv sv ;
 
    @Value("${dy.webFile.fmUrl}")
    private String fmUrl ;
 
    //支持的文件类型
    @Value("${dy.webFile.supportedFileTypes}")
    private String supportedFileTypes ;
 
    /**
     * 上传照片图片文件 (当前只对png jpg格式图片支持缩略图)
     * @param file web端上传文件的post对象
     * @return 返回结果
     */
    @PostMapping("/upPhoto")
    //参考:https://blog.zhengru.top/posts/33486.html#%E5%8D%95%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0
    public BaseResponse<?> upPhoto(MultipartFile file) {
        try {
            if (file != null) {
                String[] fileNameGrp = fileOp.splitFileName(file) ;
                if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){
                    if(!fileNameGrp[1].trim().equals("")){
                        FileRestVo frVo = fileOp.saveFile(file,
                                fmUrl,
                                FileConstant.fileRequestMapping,
                                FileConstant.filePostMapping_photo,
                                FileConstant.NotRegionNum,
                                fileNameGrp[1],
                                null);
                        String fileMainName = fileOp.getFileMainName(frVo.fileName) ;
                        Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath);
                        FileVo fvo = new FileVo(id,
                                frVo.fileNameHash,
                                fileNameGrp[0],
                                fileNameGrp[1],
                                (frVo.getFileWebUrl() + frVo.getFileWebPath()),
                                fileOp.getImgFileZipPath(frVo.getFileWebUrl() + frVo.getFileWebPath()),
                                frVo.fileWebDownloadPath + id) ;
                        return  BaseResponseUtils.buildSuccess(fvo) ;
                    }else {
                        return BaseResponseUtils.buildError("未得到上传文件的扩展名");
                    }
                }else {
                    return BaseResponseUtils.buildError("未能拆分上传文件名称");
                }
            } else {
                return BaseResponseUtils.buildError("未上传文件");
            }
        } catch (Exception e) {
            log.error("上传照片文件异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
    /**
     * 上传录音音频文件
     * @param file web端上传文件的post对象
     * @return 返回结果
     */
    @PostMapping("/upPhone")
    public BaseResponse<?> upPhone(MultipartFile file) {
        try {
            if (file != null) {
                String[] fileNameGrp = fileOp.splitFileName(file) ;
                if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){
                    if(!fileNameGrp[1].trim().equals("")){
                        FileRestVo frVo = fileOp.saveFile(file,
                                fmUrl,
                                FileConstant.fileRequestMapping,
                                FileConstant.filePostMapping_phone,
                                FileConstant.NotRegionNum,
                                fileNameGrp[1],
                                null);
                        String fileMainName = fileOp.getFileMainName(frVo.fileName) ;
                        Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath);
                        FileVo fvo = new FileVo(id,
                                frVo.fileNameHash,
                                fileNameGrp[0],
                                fileNameGrp[1],
                                frVo.getFileWebUrl() + frVo.getFileWebPath(),
                                null,
                                frVo.fileWebDownloadPath + id) ;
                        return  BaseResponseUtils.buildSuccess(fvo) ;
                    }else {
                        return BaseResponseUtils.buildError("未得到上传文件的扩展名");
                    }
                }else {
                    return BaseResponseUtils.buildError("未能拆分上传文件名称");
                }
            } else {
                return BaseResponseUtils.buildError("未上传文件");
            }
        } catch (Exception e) {
            log.error("上传照片文件异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
 
    /**
     * 上传录像视频文件
     * @param file web端上传文件的post对象
     * @return 返回结果
     */
    @PostMapping("/upVideo")
    public BaseResponse<?> upVideo(MultipartFile file) {
        try {
            if (file != null) {
                String[] fileNameGrp = fileOp.splitFileName(file) ;
                if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){
                    if(!fileNameGrp[1].trim().equals("")){
                        FileRestVo frVo = fileOp.saveFile(file,
                                fmUrl,
                                FileConstant.fileRequestMapping,
                                FileConstant.filePostMapping_video,
                                FileConstant.NotRegionNum,
                                fileNameGrp[1],
                                null);
                        String fileMainName = fileOp.getFileMainName(frVo.fileName) ;
                        Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath);
                        FileVo fvo = new FileVo(id,
                                frVo.fileNameHash,
                                fileNameGrp[0],
                                fileNameGrp[1],
                                frVo.getFileWebUrl() + frVo.getFileWebPath(),
                                null,
                                frVo.fileWebDownloadPath + id) ;
                        return  BaseResponseUtils.buildSuccess(fvo) ;
                    }else {
                        return BaseResponseUtils.buildError("未得到上传文件的扩展名");
                    }
                }else {
                    return BaseResponseUtils.buildError("未能拆分上传文件名称");
                }
            } else {
                return BaseResponseUtils.buildError("未上传文件");
            }
        } catch (Exception e) {
            log.error("上传照片文件异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
 
    /**
     * 上传文档文件
     * @param file web端上传文件的post对象
     * @return 返回结果
     */
    @PostMapping("/upDocument")
    public BaseResponse<?> upDocument(MultipartFile file) {
        try {
            if (file != null) {
                String[] fileNameGrp = fileOp.splitFileName(file) ;
                if(fileNameGrp != null && fileNameGrp[0] != null && fileNameGrp[1] != null){
                    if(!fileNameGrp[1].trim().equals("")){
                       String fileExtName = fileNameGrp[1];
                       if(!supportedFileTypes.contains(fileExtName)){
                           return BaseResponseUtils.buildError("请上传Word,Excel,PDF类型文档");
                       }
                        FileRestVo frVo = fileOp.saveFile(file,
                                fmUrl,
                                FileConstant.fileRequestMapping,
                                FileConstant.filePostMapping_document,
                                FileConstant.NotRegionNum,
                                fileNameGrp[1],
                                null);
                        String fileMainName = fileOp.getFileMainName(frVo.fileName) ;
                        Long id = this.saveFileInfo(fileNameGrp[0], fileNameGrp[1], fileMainName, frVo.fileNameHash, frVo.fileWebPath);
                        FileVo fvo = new FileVo(id,
                                frVo.fileNameHash,
                                fileNameGrp[0], fileNameGrp[1],
                                frVo.getFileWebUrl() + frVo.getFileWebPath(),
                                null,
                                frVo.fileWebDownloadPath + id) ;
                        return  BaseResponseUtils.buildSuccess(fvo) ;
                    }else {
                        return BaseResponseUtils.buildError("未得到上传文件的扩展名");
                    }
                }else {
                    return BaseResponseUtils.buildError("未能拆分上传文件名称");
                }
            } else {
                return BaseResponseUtils.buildError("未上传文件");
            }
        } catch (Exception e) {
            log.error("上传照片文件异常", e);
            return BaseResponseUtils.buildException(e.getMessage());
        }
    }
 
 
    /**
     * 数据库存储
     * @param orgName 文件原名称
     * @param extName 文件扩展名
     * @param newName 文件新名称
     * @param hash  文件新名称的哈希值
     * @param filePath 文件服务端存储相对路径
     * @return 数据记录主键
     */
    private Long saveFileInfo(String orgName, String extName, String newName, Integer hash, String filePath){
        WebFile po = new WebFile() ;
        po.orgName = orgName ;
        po.extName = extName ;
        po.newName = newName ;
        po.hash = hash ;
        po.filePath = filePath ;
        po.dt = new Date() ;
        return this.sv.save(po) ;
    }
 
}