Commit 682f9c560f23f81636f18c14196dc939ab7d3aff
1 parent
e2c3879a
增加验证传参方式接口
Showing
3 changed files
with
143 additions
and
1 deletions
assistant-dfs/src/main/java/com/diligrp/assistant/dfs/controller/FileObjectController.java
@@ -8,6 +8,8 @@ import com.diligrp.assistant.dfs.domain.FileMetadata; | @@ -8,6 +8,8 @@ import com.diligrp.assistant.dfs.domain.FileMetadata; | ||
8 | import com.diligrp.assistant.dfs.exception.DfsServiceException; | 8 | import com.diligrp.assistant.dfs.exception.DfsServiceException; |
9 | import com.diligrp.assistant.dfs.service.FileRepositoryService; | 9 | import com.diligrp.assistant.dfs.service.FileRepositoryService; |
10 | import com.diligrp.assistant.shared.ErrorCode; | 10 | import com.diligrp.assistant.shared.ErrorCode; |
11 | +import com.diligrp.assistant.shared.domain.AmisMessage; | ||
12 | +import com.diligrp.assistant.shared.domain.AmisUploadResponse; | ||
11 | import com.diligrp.assistant.shared.domain.Message; | 13 | import com.diligrp.assistant.shared.domain.Message; |
12 | import com.diligrp.assistant.shared.util.ObjectUtils; | 14 | import com.diligrp.assistant.shared.util.ObjectUtils; |
13 | import jakarta.annotation.Resource; | 15 | import jakarta.annotation.Resource; |
@@ -55,6 +57,23 @@ public class FileObjectController { | @@ -55,6 +57,23 @@ public class FileObjectController { | ||
55 | return Message.success(fileId); | 57 | return Message.success(fileId); |
56 | } | 58 | } |
57 | 59 | ||
60 | + @RequestMapping(value = "/amis/file/upload.do") | ||
61 | + public AmisMessage<AmisUploadResponse> amisFileUpload(@RequestPart("file") MultipartFile file, | ||
62 | + @RequestParam(Constants.HEADER_AUTHORIZATION) String authorization) throws IOException { | ||
63 | + DfsAccessToken accessToken = accessAuthorization(authorization); | ||
64 | + | ||
65 | + Optional<MediaType> optional = MediaTypeFactory.getMediaType(file.getOriginalFilename()); | ||
66 | + MimeType mimeType = optional.orElse(MediaType.APPLICATION_OCTET_STREAM); | ||
67 | + FileMetadata metadata = new FileMetadata(mimeType.toString()); | ||
68 | + | ||
69 | + String fileName = ObjectUtils.isNotEmpty(file.getOriginalFilename()) ? file.getOriginalFilename() : "unknown"; | ||
70 | + DfsFile fileObject = new DfsFile(fileName, file.getInputStream(), metadata); | ||
71 | + String fileId = fileRepositoryService.uploadFile(accessToken, fileObject); | ||
72 | + AmisUploadResponse response = new AmisUploadResponse(); | ||
73 | + response.setUrl(fileId); | ||
74 | + return AmisMessage.success(response); | ||
75 | + } | ||
76 | + | ||
58 | @RequestMapping(value = "/files/upload.do") | 77 | @RequestMapping(value = "/files/upload.do") |
59 | public Message<?> fileUploads(@RequestPart(value = "files") MultipartFile[] files, | 78 | public Message<?> fileUploads(@RequestPart(value = "files") MultipartFile[] files, |
60 | @RequestHeader(Constants.HEADER_AUTHORIZATION) String authorization) throws IOException { | 79 | @RequestHeader(Constants.HEADER_AUTHORIZATION) String authorization) throws IOException { |
@@ -127,7 +146,26 @@ public class FileObjectController { | @@ -127,7 +146,26 @@ public class FileObjectController { | ||
127 | } | 146 | } |
128 | response.flushBuffer(); | 147 | response.flushBuffer(); |
129 | } | 148 | } |
130 | - | 149 | + @RequestMapping(value = "/amis/file/download.do") |
150 | + public void amisDownload(@RequestParam("fileId") String fileId, @RequestParam("Dfs-Authorization") String authorization, | ||
151 | + HttpServletResponse response) throws IOException { | ||
152 | + DfsAccessToken accessToken = accessAuthorization(authorization); | ||
153 | + DfsFile file = fileRepositoryService.downloadFile(accessToken, fileId, null); | ||
154 | + InputStream fileStream = file.getStream(); | ||
155 | + response.setContentType(file.getMetadata().getMimeType()); | ||
156 | + // oss sdk存在bug, fileStream.available()和真实字节数存在差异 | ||
157 | + // response.setContentLength(fileStream.available()); | ||
158 | + System.out.println(file.getName()); | ||
159 | + String fileName = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8); | ||
160 | + System.out.println(URLDecoder.decode(fileName, StandardCharsets.UTF_8)); | ||
161 | + response.addHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName); | ||
162 | + try { | ||
163 | + IOUtils.copy(fileStream, response.getOutputStream()); | ||
164 | + } finally { | ||
165 | + IOUtils.closeQuietly(fileStream); | ||
166 | + } | ||
167 | + response.flushBuffer(); | ||
168 | + } | ||
131 | @RequestMapping(value = "/file/delete.do") | 169 | @RequestMapping(value = "/file/delete.do") |
132 | public Message<?> deleteFile(@RequestParam("fileId") String fileId, @RequestHeader(Constants.HEADER_AUTHORIZATION) String authorization) { | 170 | public Message<?> deleteFile(@RequestParam("fileId") String fileId, @RequestHeader(Constants.HEADER_AUTHORIZATION) String authorization) { |
133 | DfsAccessToken accessToken = accessAuthorization(authorization); | 171 | DfsAccessToken accessToken = accessAuthorization(authorization); |
assistant-shared/src/main/java/com/diligrp/assistant/shared/domain/AmisMessage.java
0 → 100644
1 | +package com.diligrp.assistant.shared.domain; | ||
2 | + | ||
3 | +import com.diligrp.assistant.shared.ErrorCode; | ||
4 | + | ||
5 | +/** | ||
6 | + * @author jy | ||
7 | + */ | ||
8 | +public class AmisMessage<T> { | ||
9 | + protected static final int CODE_SUCCESS = 0; | ||
10 | + protected static final int CODE_FAILURE = ErrorCode.SYSTEM_UNKNOWN_ERROR; | ||
11 | + protected static final String MSG_SUCCESS = "success"; | ||
12 | + private Integer status; | ||
13 | + private String msg; | ||
14 | + private T data; | ||
15 | + public AmisMessage() { | ||
16 | + } | ||
17 | + | ||
18 | + public Integer getStatus() { | ||
19 | + return status; | ||
20 | + } | ||
21 | + | ||
22 | + public void setStatus(Integer status) { | ||
23 | + this.status = status; | ||
24 | + } | ||
25 | + | ||
26 | + public String getMsg() { | ||
27 | + return msg; | ||
28 | + } | ||
29 | + | ||
30 | + public void setMsg(String msg) { | ||
31 | + this.msg = msg; | ||
32 | + } | ||
33 | + | ||
34 | + public T getData() { | ||
35 | + return this.data; | ||
36 | + } | ||
37 | + | ||
38 | + public void setData(T data) { | ||
39 | + this.data = data; | ||
40 | + } | ||
41 | + | ||
42 | + public static AmisMessage<?> success() { | ||
43 | + AmisMessage<?> result = new AmisMessage(); | ||
44 | + result.status = 0; | ||
45 | + result.msg = "success"; | ||
46 | + return result; | ||
47 | + } | ||
48 | + | ||
49 | + public static <E> AmisMessage<E> success(E data) { | ||
50 | + AmisMessage<E> result = new AmisMessage(); | ||
51 | + result.status = 0; | ||
52 | + result.data = data; | ||
53 | + result.msg = "success"; | ||
54 | + return result; | ||
55 | + } | ||
56 | + | ||
57 | + public static AmisMessage<?> failure(String message) { | ||
58 | + AmisMessage<?> result = new AmisMessage(); | ||
59 | + result.status = 1000; | ||
60 | + result.msg = message; | ||
61 | + return result; | ||
62 | + } | ||
63 | + | ||
64 | + public static AmisMessage<?> failure(int code, String message) { | ||
65 | + AmisMessage<?> result = new AmisMessage(); | ||
66 | + result.status = code; | ||
67 | + result.msg = message; | ||
68 | + return result; | ||
69 | + } | ||
70 | +} |
assistant-shared/src/main/java/com/diligrp/assistant/shared/domain/AmisUploadResponse.java
0 → 100644
1 | +package com.diligrp.assistant.shared.domain; | ||
2 | + | ||
3 | +/** | ||
4 | + * @author jy | ||
5 | + */ | ||
6 | +public class AmisUploadResponse { | ||
7 | + private String value; | ||
8 | + private String filename; | ||
9 | + private String url; | ||
10 | + | ||
11 | + public String getValue() { | ||
12 | + return value; | ||
13 | + } | ||
14 | + | ||
15 | + public void setValue(String value) { | ||
16 | + this.value = value; | ||
17 | + } | ||
18 | + | ||
19 | + public String getFilename() { | ||
20 | + return filename; | ||
21 | + } | ||
22 | + | ||
23 | + public void setFilename(String filename) { | ||
24 | + this.filename = filename; | ||
25 | + } | ||
26 | + | ||
27 | + public String getUrl() { | ||
28 | + return url; | ||
29 | + } | ||
30 | + | ||
31 | + public void setUrl(String url) { | ||
32 | + this.url = url; | ||
33 | + } | ||
34 | +} |