测试用户
2023-04-13 43393f2bb11cbf9e6af40077bbc5284660e8a754
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.deloitte.system.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.common.aws.util.S3Util;
import com.common.core.constant.GlobalConst;
import com.common.core.enums.ResultCodeEnum;
import com.common.core.exception.BizException;
import com.common.core.utils.UUID;
import com.common.core.utils.*;
import com.common.redis.util.RedisUtil;
import com.deloitte.system.request.FileRequest;
import com.j256.simplemagic.ContentInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Exception;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
 
import static com.common.core.constant.GlobalConst.PIPL_UNCONFIRM_FILE;
 
@Slf4j
@Service
public class FileService {
    @Autowired
    private RedisUtil redisUtil;
    @Value("${salesforce.referer}")
    private String refererUrl;
    @Value("${aws.s3.bucketName}")
    private String bucketName;
 
    public String upload(String txId,FileRequest fileRequest){
        log.info("上传文件txId:"+txId);
        String key=uploadFile(fileRequest);
        String keyName = PIPL_UNCONFIRM_FILE + txId;
        redisUtil.set(keyName, new JSONArray(Arrays.asList(key)), GlobalConst.DATA_EXPIRE);
        return key;
    }
 
    public List<String> batchUpload(String txId, List<FileRequest> fileList) {
        log.info("上传文件txId:"+txId);
        List<String> keys=new ArrayList<>();
        try {
            fileList.forEach(fileRequest -> {
                String key = uploadFile(fileRequest);
                keys.add(key);
            });
            String keyName = PIPL_UNCONFIRM_FILE + txId;
            redisUtil.set(keyName, new JSONArray((List)keys), GlobalConst.DATA_EXPIRE);
        }catch (Exception e){
            this.cleanS3File(keys);
            throw e;
        }
        return keys;
    }
 
    public String uploadFile(FileRequest fileRequest){
        if (fileRequest == null) {
            throw new BizException(ResultCodeEnum.NO_DATA);
        }
        log.info("fileName:{}", fileRequest.getFileName());
        //文件大小限制,最大单文件30M
        if(fileRequest.getSize()>31457280){
            throw new BizException(ResultCodeEnum.FILE_SIZE_EXCEED);
        }
        if(fileRequest.getFile().startsWith("base64,")){
            String contentType= ContentInfoUtil.findExtensionMatch(fileRequest.getFileName())==null?
                    "text/plain": ContentInfoUtil.findExtensionMatch(fileRequest.getFileName()).getMimeType();
            fileRequest.setFile("data:"+contentType+";"+fileRequest.getFile());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String oldFileName = fileRequest.getFileName();
        String suffix=oldFileName.lastIndexOf(".")==-1?"":oldFileName.substring(oldFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replace("-","")+suffix;
        String key = sdf.format(new Date()) + "/" + newFileName;
        // 将文件base64存入缓存
        MultipartFile multipartFile = null;
        try {
            multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(fileRequest.getFile());
        } catch (IOException e) {
            log.error("上传文件失败:",e);
        }
        if (multipartFile == null) {
            throw new BizException("未找到文件");
        }
        String bucketName = SpringContextUtils.getEnvParam("aws.s3.bucketName");
        S3Util.uploadFile(multipartFile, key,bucketName);
        return key;
    }
 
    public String uploadFileNoSize(FileRequest fileRequest){
        if (fileRequest == null) {
            throw new BizException(ResultCodeEnum.NO_DATA);
        }
        log.info("fileName:{}", fileRequest.getFileName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String oldFileName = fileRequest.getFileName();
        String suffix=oldFileName.lastIndexOf(".")==-1?"":oldFileName.substring(oldFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString().replace("-","")+suffix;
        String key = sdf.format(new Date()) + "/" + newFileName;
        // 将文件base64存入缓存
        MultipartFile multipartFile = null;
        try {
            multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(fileRequest.getFile());
        } catch (IOException e) {
            log.error("上传文件失败:",e);
        }
        if (multipartFile == null) {
            throw new BizException("未找到文件");
        }
        String bucketName = SpringContextUtils.getEnvParam("aws.s3.bucketName");
        S3Util.uploadFile(multipartFile, key,bucketName);
        return key;
    }
 
    public void cleanS3File(Collection<String> keyList){
        if (!keyList.isEmpty()){
            String bucketName = SpringContextUtils.getEnvParam("aws.s3.bucketName");
            S3Util.deleteByKeys(new ArrayList<String>(keyList),bucketName);
        }
    }
 
    private boolean checkReferer(String referer){
        String[] refererUrls=refererUrl.split(",");
        for (String str : refererUrls) {
            if(str.equalsIgnoreCase(referer)||str.substring(0,str.length()-1).equalsIgnoreCase(referer)){
                return true;
            }
        }
        return false;
    }
 
    public void preview(HttpServletRequest request,HttpServletResponse response,String key){
        String referer = request.getHeader("Referer");
        if (StrUtil.isBlank(referer)) {
            throw new BizException(ResultCodeEnum.NO_PERMISSION);
        }
        //referer 截取
        String replace = referer.replace("https://", "");
        String newReferer = "https://"+replace.substring(0, replace.indexOf("/")+1);
        if (!checkReferer(newReferer)) {
            throw new BizException(ResultCodeEnum.NO_PERMISSION);
        }
        String fileName = key.substring(key.lastIndexOf("/")+1);
        log.info("文件预览key:{},fileName:{}",key,fileName);
        OutputStream out = null;
        try {
            response.setCharacterEncoding("utf-8");
            String s = ContentInfoUtil.findExtensionMatch(fileName).getMimeType();
            log.info("文件预览key:{},contentType:{}",key,s);
            if (StrUtil.isNotBlank(s)) {
                response.setContentType(s);
            } else{
                response.setContentType(MediaType.ALL_VALUE);
            }
            String agent = request.getHeader("User-Agent");
            if(agent.toLowerCase().contains("firefox")) {
                response.setHeader("Content-Disposition", "inline; filename==?UTF-8?B?" + new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))+"?=");
            }else {
                response.setHeader("Content-Disposition", "inline; filename=" +  new String(fileName.getBytes("gbk"),"iso8859-1"));
            }
            ResponseBytes<GetObjectResponse> objectBytes = S3Util.downloadFile(key,bucketName);
            InputStream inputStream = objectBytes.asInputStream();
            BufferedInputStream bufInputStream = new BufferedInputStream(inputStream);
            out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = bufInputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            response.flushBuffer();
            out.close();
        } catch (IOException e) {
            log.error("文件预览失败:",e);
            throw new BizException(ResultCodeEnum.RT_ERROR);
        }catch (S3Exception e) {
            log.error("文件预览失败:",e);
            throw new BizException(e.getMessage());
        }
 
    }
    
    public void download(HttpServletRequest request,HttpServletResponse response,String key){
        String referer = request.getHeader("Referer");
 
        log.info("referer:"+referer);
 
        if (StrUtil.isBlank(referer)) {
            throw new BizException(ResultCodeEnum.NO_PERMISSION);
        }
        //referer 截取
        String replace = referer.replace("https://", "");
        String newReferer = "https://"+replace.substring(0, replace.indexOf("/")+1);
        if (!checkReferer(newReferer)) {
            throw new BizException(ResultCodeEnum.NO_PERMISSION);
        }
 
        OutputStream out = null;
        try {
            String fileName = request.getParameter("fileName");
            if (StrUtil.isBlank(fileName)) {
                throw new BizException("fileName不能为空");
            }
            log.info("文件下载key:{},fileName:{}",key,fileName);
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "No-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("application/octet-stream");
            String agent = request.getHeader("User-Agent");
            if (null != agent && agent.contains("MSIE") || null != agent && agent.contains("Trident") || null != agent && agent.contains("Edge")) {
                // ie浏览器及Edge浏览器
               fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
            } else if (null != agent && agent.contains("Mozilla")) {
                // 火狐,Chrome等浏览器
                fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            }else {
                // 其它浏览器
                fileName = URLEncoder.encode(fileName, "utf-8");
            }
            response.setHeader("Content-Disposition", "attachment; filename="+fileName);
            ResponseBytes<GetObjectResponse> objectBytes = S3Util.downloadFile(key,bucketName);
            InputStream inputStream = objectBytes.asInputStream();
            BufferedInputStream bufInputStream = new BufferedInputStream(inputStream);
            out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = bufInputStream.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            response.flushBuffer();
            out.close();
        } catch (IOException e) {
            log.error("文件下载失败:",e);
            throw new BizException(ResultCodeEnum.RT_ERROR);
        }catch (S3Exception e) {
            log.error("文件下载失败:",e);
            throw new BizException(e.getMessage());
        }
 
    }
 
    public void deleteFile(List<String> keys){
        if (CollUtil.isEmpty(keys)) {
            throw new BizException(ResultCodeEnum.NO_DATA);
        }
        String bucketName = SpringContextUtils.getEnvParam("aws.s3.bucketName");
        S3Util.deleteByKeys(keys,bucketName);
    }
 
    public void convert(HttpServletRequest request,HttpServletResponse response) {
        //从param参数中获取fileName,如果为空则取当前时间为文件名
        String fileName = request.getParameter("fileName");
        fileName = fileName==null? DateUtils.format(new Date()) : fileName;
        response.setContentType("application/pdf");
        //要保存的文件名
        response.setHeader("Content-Disposition", "inline;filename=" + fileName + ".pdf");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
 
        ServletOutputStream outputStream = null;
        BufferedReader bufferReader=null;
        String base64Str="";
        try {
            if(StringUtils.isNotBlank(request.getParameter("base64Str"))){
                base64Str=request.getParameter("base64Str");
            }else {
                bufferReader = new BufferedReader(request.getReader());
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = bufferReader.readLine()) != null) {
                    sb.append(line);
                }
                String body = sb.toString();
                String[] strings = body.split("&");
                Map<String, String> valueMap = new HashMap<>();
                for (String string : strings) {
                    String[] keyValue = string.split("=", 2);
                    String key = keyValue[0];
                    String value = keyValue[1];
                    valueMap.put(URLDecoder.decode(key, "UTF-8"), URLDecoder.decode(value, "UTF-8"));
                }
                base64Str = valueMap.get("base64Str");
            }
            outputStream = response.getOutputStream();
            byte[] b = DatatypeConverter.parseBase64Binary(base64Str);
            outputStream.write(b, 0, b.length);
        } catch (IOException e) {
            log.error("文件转换异常:",e);
            throw new BizException(ResultCodeEnum.RT_ERROR);
        }finally {
            try {
                if(bufferReader!=null)bufferReader.close();
            } catch (IOException e) {
            }
        }
    }
 
    public static String getFilecharset(InputStream inputStream) {
        //默认GBK
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        try(BufferedInputStream bis = new BufferedInputStream(inputStream)) {
            bis.mark(0);
            int read = bis.read(first3Bytes, 0, 3);
            // 文件编码为 ANSI
            if (read == -1) {
                return charset;
            }
            // 文件编码为 Unicode
            if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                return  "UTF-16LE";
            }
            // 文件编码为 Unicode big endian
            if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
                return "UTF-16BE";
            }
            // 文件编码为 UTF-8
            if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) {
                return "UTF-8";
            }
            bis.reset();
 
            int loc = 0;
            while ((read = bis.read()) != -1) {
                loc++;
                if (read >= 0xF0) {
                    break;
                }
                // 单独出现BF以下的,也算是GBK
                if (0x80 <= read && read <= 0xBF) {
                    break;
                }
                if (0xC0 <= read && read <= 0xDF) {
                    read = bis.read();
                    // 双字节 (0xC0 - 0xDF)
                    if (0x80 <= read && read <= 0xBF) {
                        // (0x80
                        // - 0xBF),也可能在GB编码内
                        continue;
                    }
                    break;
                }
                // 也有可能出错,但是几率较小
                if (0xE0 <= read && read <= 0xEF) {
                    read = bis.read();
                    if (0x80 <= read && read <= 0xBF) {
                        read = bis.read();
                        if (0x80 <= read && read <= 0xBF) {
                            charset = "UTF-8";
                        }
                    }
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return charset;
    }
}