3
3
import cn .hutool .core .io .IoUtil ;
4
4
import cn .hutool .core .util .StrUtil ;
5
5
import cn .hutool .crypto .SecureUtil ;
6
+ import com .auth0 .jwt .interfaces .Claim ;
6
7
import com .google .common .collect .Lists ;
7
8
import com .jiaruiblog .common .MessageConstant ;
8
9
import com .jiaruiblog .entity .FileDocument ;
16
17
import com .jiaruiblog .service .TaskExecuteService ;
17
18
import com .jiaruiblog .util .BaseApiResult ;
18
19
import com .jiaruiblog .util .FileContentTypeUtils ;
20
+ import com .jiaruiblog .util .JwtUtil ;
19
21
import io .swagger .annotations .Api ;
20
22
import io .swagger .annotations .ApiOperation ;
21
23
import lombok .extern .slf4j .Slf4j ;
@@ -79,14 +81,24 @@ public List<FileDocument> list(@ModelAttribute BasePageDTO basePageDTO) {
79
81
*/
80
82
@ ApiOperation (value = "查询文档预览结果" )
81
83
@ GetMapping ("/view/{id}" )
82
- public ResponseEntity <Object > serveFileOnline (@ PathVariable String id ) throws UnsupportedEncodingException {
84
+ public ResponseEntity <Object > serveFileOnline (@ PathVariable String id ,
85
+ @ RequestParam ("token" ) String token ,
86
+ HttpServletResponse response )
87
+ throws UnsupportedEncodingException {
88
+ Map <String , Claim > userData = JwtUtil .verifyToken (token );
89
+ if (CollectionUtils .isEmpty (userData )) {
90
+ response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
91
+ return null ;
92
+ }
83
93
Optional <FileDocument > file = fileService .getById (id );
84
94
if (file .isPresent ()) {
85
95
return ResponseEntity .ok ()
86
96
// 这里需要进行中文编码
87
- .header (HttpHeaders .CONTENT_DISPOSITION , "fileName=" + URLEncoder .encode (file .get ().getName (), "utf-8" ))
97
+ .header (HttpHeaders .CONTENT_DISPOSITION ,
98
+ "fileName=" + URLEncoder .encode (file .get ().getName (), "utf-8" ))
88
99
.header (HttpHeaders .CONTENT_TYPE , file .get ().getContentType ())
89
- .header (HttpHeaders .CONTENT_LENGTH , file .get ().getSize () + "" ).header ("Connection" , "close" )
100
+ .header (HttpHeaders .CONTENT_LENGTH , file .get ().getSize () + "" )
101
+ .header ("Connection" , "close" )
90
102
.header (HttpHeaders .CONTENT_LENGTH , file .get ().getSize () + "" )
91
103
.body (file .get ().getContent ());
92
104
} else {
@@ -98,7 +110,7 @@ public ResponseEntity<Object> serveFileOnline(@PathVariable String id) throws Un
98
110
* 在线显示文件
99
111
*
100
112
* @param id 文件id
101
- * @return
113
+ * @return ResponseEntity<Object> 返回实体
102
114
*/
103
115
@ GetMapping ("/view2/{id}" )
104
116
public ResponseEntity <Object > previewFileOnline (@ PathVariable String id ) throws UnsupportedEncodingException {
@@ -119,8 +131,8 @@ public ResponseEntity<Object> previewFileOnline(@PathVariable String id) throws
119
131
/**
120
132
* 下载附件
121
133
*
122
- * @param id
123
- * @return
134
+ * @param id 请求文件id
135
+ * @return ResponseEntity<Object>
124
136
* @throws UnsupportedEncodingException
125
137
*/
126
138
@ GetMapping ("/{id}" )
@@ -288,7 +300,7 @@ public BaseApiResult uploadBatch(FileUploadDTO fileUploadDTO, HttpServletRequest
288
300
tags = tags .subList (0 , 10 );
289
301
}
290
302
// 当只上传一个文档的时候,跳过错误肯定是False
291
- if (files .length <2 ) {
303
+ if (files .length < 2 ) {
292
304
skipError = Boolean .FALSE ;
293
305
}
294
306
return fileService .uploadBatch (category , tags , description , skipError , files , userId , username );
@@ -325,11 +337,11 @@ public BaseApiResult uploadByUrl(@RequestBody UrlUploadDTO urlUploadDTO, HttpSer
325
337
}
326
338
327
339
/**
340
+ * @return java.lang.Boolean
328
341
* @Author luojiarui
329
342
* @Description 文件上传时的参数检查:长度要求;格式要求;敏感词要求
330
343
* @Date 16:14 2023/4/22
331
344
* @Param [tags, category, description, name]
332
- * @return java.lang.Boolean
333
345
**/
334
346
private static Boolean checkParam (List <String > tags , String category , String description , String name ) {
335
347
@@ -427,7 +439,14 @@ public ResponseModel deleteFileByGetMethod(@PathVariable String id) {
427
439
**/
428
440
@ GetMapping (value = "/image/{thumbId}" , produces = MediaType .IMAGE_PNG_VALUE )
429
441
@ ResponseBody
430
- public byte [] previewThumb (@ PathVariable String thumbId ) throws Exception {
442
+ public byte [] previewThumb (@ PathVariable String thumbId ,
443
+ @ RequestParam ("token" ) String token ,
444
+ HttpServletResponse response ) throws Exception {
445
+ Map <String , Claim > userData = JwtUtil .verifyToken (token );
446
+ if (CollectionUtils .isEmpty (userData )) {
447
+ response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
448
+ return new byte []{};
449
+ }
431
450
InputStream inputStream = fileService .getFileThumb (thumbId );
432
451
FileInputStream fileInputStream = (FileInputStream ) (inputStream );
433
452
if (inputStream == null ) {
@@ -471,7 +490,14 @@ public ResponseEntity<Object> previewThumb1(@PathVariable String id) {
471
490
472
491
@ GetMapping (value = "/image2/{thumbId}" , produces = MediaType .IMAGE_PNG_VALUE )
473
492
@ ResponseBody
474
- public byte [] previewThumb2 (@ PathVariable String thumbId , HttpServletResponse response ) {
493
+ public byte [] previewThumb2 (@ PathVariable String thumbId ,
494
+ @ RequestParam ("token" ) String token ,
495
+ HttpServletResponse response ) {
496
+ Map <String , Claim > userData = JwtUtil .verifyToken (token );
497
+ if (CollectionUtils .isEmpty (userData )) {
498
+ response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
499
+ return new byte []{};
500
+ }
475
501
// 设置响应头,缓存 1 小时
476
502
response .setHeader ("Cache-Control" , "max-age=3600, public" );
477
503
return fileService .getFileBytes (thumbId );
@@ -583,7 +609,6 @@ public BaseApiResult temporaryFileDownloadLink() {
583
609
// }
584
610
585
611
586
-
587
612
return BaseApiResult .success ();
588
613
}
589
614
}
0 commit comments