DownloadController.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2019 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * >>
  17. *
  18. */
  19. package edp.davinci.controller;
  20. import edp.core.annotation.AuthIgnore;
  21. import edp.core.annotation.AuthShare;
  22. import edp.core.annotation.CurrentUser;
  23. import edp.core.utils.FileUtils;
  24. import edp.davinci.common.controller.BaseController;
  25. import edp.davinci.core.common.Constants;
  26. import edp.davinci.core.common.ResultMap;
  27. import edp.davinci.core.enums.DownloadType;
  28. import edp.davinci.core.enums.FileTypeEnum;
  29. import edp.davinci.dto.viewDto.DownloadViewExecuteParam;
  30. import edp.davinci.model.DownloadRecord;
  31. import edp.davinci.model.ShareDownloadRecord;
  32. import edp.davinci.model.User;
  33. import edp.davinci.service.DownloadService;
  34. import edp.davinci.service.ShareDownloadService;
  35. import edp.davinci.service.share.ShareOperation;
  36. import edp.davinci.service.share.ShareType;
  37. import io.swagger.annotations.Api;
  38. import io.swagger.annotations.ApiOperation;
  39. import io.swagger.annotations.ApiResponse;
  40. import io.swagger.annotations.ApiResponses;
  41. import lombok.extern.slf4j.Slf4j;
  42. import org.apache.tomcat.util.http.fileupload.util.Streams;
  43. import org.springframework.beans.factory.annotation.Autowired;
  44. import org.springframework.http.MediaType;
  45. import org.springframework.http.ResponseEntity;
  46. import org.springframework.web.bind.annotation.*;
  47. import springfox.documentation.annotations.ApiIgnore;
  48. import javax.servlet.http.HttpServletRequest;
  49. import javax.servlet.http.HttpServletResponse;
  50. import javax.validation.Valid;
  51. import java.io.File;
  52. import java.io.FileInputStream;
  53. import java.io.UnsupportedEncodingException;
  54. import java.net.URLEncoder;
  55. import java.nio.charset.StandardCharsets;
  56. import java.util.Arrays;
  57. import java.util.List;
  58. /**
  59. * Created by IntelliJ IDEA.
  60. *
  61. * @Author daemon
  62. * @Date 19/5/27 20:30
  63. * To change this template use File | Settings | File Templates.
  64. */
  65. @Api(value = "/download", tags = "download", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  66. @ApiResponses(@ApiResponse(code = 404, message = "download not found"))
  67. @Slf4j
  68. @RestController
  69. @RequestMapping(value = Constants.BASE_API_PATH + "/download")
  70. public class DownloadController extends BaseController {
  71. @Autowired
  72. private DownloadService downloadService;
  73. @Autowired
  74. private ShareDownloadService shareDownloadService;
  75. @ApiOperation(value = "get download record page")
  76. @GetMapping(value = "/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  77. public ResponseEntity getDownloadRecordPage(@ApiIgnore @CurrentUser User user,
  78. HttpServletRequest request) {
  79. List<DownloadRecord> records = downloadService.queryDownloadRecordPage(user.getId());
  80. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(records));
  81. }
  82. @ApiOperation(value = "get download record file")
  83. @GetMapping(value = "/record/file/{id}/{token:.*}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  84. @AuthIgnore
  85. public ResponseEntity getDownloadRecordFile(@PathVariable Long id,
  86. @PathVariable String token,
  87. HttpServletRequest request,
  88. HttpServletResponse response) {
  89. DownloadRecord record = downloadService.downloadById(id, token);
  90. FileInputStream is = null;
  91. try {
  92. encodeFileName(request, response, record.getName() + FileTypeEnum.XLSX.getFormat());
  93. is = new FileInputStream(new File(record.getPath()));
  94. Streams.copy(is, response.getOutputStream(), true);
  95. } catch (Exception e) {
  96. log.error("GetDownloadRecordFile error, id=" + id, e);
  97. } finally {
  98. FileUtils.closeCloseable(is);
  99. }
  100. return null;
  101. }
  102. @ApiOperation(value = "get download record file")
  103. @PostMapping(value = "/submit/{type}/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  104. public ResponseEntity submitDownloadTask(@PathVariable String type,
  105. @PathVariable Long id,
  106. @ApiIgnore @CurrentUser User user,
  107. @Valid @RequestBody(required = false) DownloadViewExecuteParam[] params,
  108. HttpServletRequest request) {
  109. List<DownloadViewExecuteParam> downloadViewExecuteParams = Arrays.asList(params);
  110. boolean rst = downloadService.submit(DownloadType.getDownloadType(type), id, user, downloadViewExecuteParams);
  111. return ResponseEntity.ok(rst ? new ResultMap(tokenUtils).successAndRefreshToken(request).payload(null) :
  112. new ResultMap(tokenUtils).failAndRefreshToken(request).payload(null));
  113. }
  114. @ApiOperation(value = "submit share download")
  115. @PostMapping(value = "/share/submit/{type}/{uuid}/{token:.*}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  116. @AuthShare(type = ShareType.DATA, operation = ShareOperation.DOWNLOAD)
  117. public ResponseEntity submitShareDownloadTask(@PathVariable(name = "token") String token,
  118. @RequestParam(required = false) String password,
  119. @PathVariable(name = "uuid") String uuid,
  120. @PathVariable(name = "type") String type,
  121. @ApiIgnore @CurrentUser User user,
  122. @Valid @RequestBody(required = false) DownloadViewExecuteParam[] params) {
  123. List<DownloadViewExecuteParam> downloadViewExecuteParams = Arrays.asList(params);
  124. boolean rst = shareDownloadService.submit(DownloadType.getDownloadType(type), uuid, downloadViewExecuteParams);
  125. return ResponseEntity.ok(rst ? new ResultMap().success() : new ResultMap().fail());
  126. }
  127. @ApiOperation(value = "get download record file")
  128. @GetMapping(value = "/share/record/file/{id}/{uuid}/{token:.*}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
  129. @AuthShare(type = ShareType.FILE, operation = ShareOperation.DOWNLOAD)
  130. public ResponseEntity getShareDownloadRecordFile(@PathVariable(name = "token") String token,
  131. @RequestParam(required = false) String password,
  132. @PathVariable(name = "uuid") String uuid,
  133. @PathVariable(name = "id") String id,
  134. HttpServletRequest request,
  135. HttpServletResponse response) {
  136. ShareDownloadRecord record = shareDownloadService.downloadById(id, uuid);
  137. FileInputStream is = null;
  138. try {
  139. encodeFileName(request, response, record.getName() + FileTypeEnum.XLSX.getFormat());
  140. is = new FileInputStream(new File(record.getPath()));
  141. Streams.copy(is, response.getOutputStream(), true);
  142. } catch (Exception e) {
  143. log.error("GetShareDownloadRecordFile error, id=" + id, e);
  144. } finally {
  145. FileUtils.closeCloseable(is);
  146. }
  147. return null;
  148. }
  149. @ApiOperation(value = "get share download record page")
  150. @GetMapping(value = "/share/page/{uuid}/{token:.*}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  151. @AuthShare(type = ShareType.RECORD, operation = ShareOperation.DOWNLOAD)
  152. public ResponseEntity getShareDownloadRecordPage(@PathVariable(name = "token") String token,
  153. @RequestParam(required = false) String password,
  154. @PathVariable(name = "uuid") String uuid,
  155. @ApiIgnore @CurrentUser User user,
  156. HttpServletRequest request) {
  157. List<ShareDownloadRecord> records = shareDownloadService.queryDownloadRecordPage(uuid);
  158. if (null == user || user.getId() == null) {
  159. return ResponseEntity.ok(new ResultMap(tokenUtils).payloads(records));
  160. } else {
  161. return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(records));
  162. }
  163. }
  164. private void encodeFileName(HttpServletRequest request, HttpServletResponse response, String filename) throws UnsupportedEncodingException {
  165. response.setHeader("Content-Type", "application/force-download");
  166. if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
  167. // firefox浏览器
  168. filename = new String(filename.getBytes(StandardCharsets.UTF_8), "ISO8859-1");
  169. } else if (isIE(request)) {
  170. //IE
  171. filename = URLEncoder.encode(filename, "UTF-8");
  172. } else {
  173. filename = new String(filename.getBytes(StandardCharsets.UTF_8), "ISO8859-1");
  174. }
  175. response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
  176. }
  177. private static boolean isIE(HttpServletRequest request) {
  178. String ua = request.getHeader("User-Agent").toLowerCase();
  179. return ((ua.indexOf("rv") > 0 && ua.contains("like gecko")) || ua.indexOf("msie") > 0);
  180. }
  181. }