FileNodeController.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package com.bosshand.virgo.file.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.bosshand.virgo.core.response.Response;
  4. import com.bosshand.virgo.exception.BadRequestException;
  5. import com.bosshand.virgo.exception.Constant;
  6. import com.bosshand.virgo.file.model.FileNode;
  7. import com.bosshand.virgo.file.service.FileManagerService;
  8. import io.swagger.annotations.ApiOperation;
  9. import io.swagger.annotations.ApiParam;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.web.bind.annotation.*;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.net.URLEncoder;
  22. import java.util.Base64;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. @RestController
  27. public class FileNodeController {
  28. private final Logger logger = LoggerFactory.getLogger(getClass());
  29. @Autowired
  30. private FileManagerService fileManagerService;
  31. @RequestMapping(value = "/dir/{parentId}", method = RequestMethod.GET)
  32. public Response listDir( @PathVariable int parentId) {
  33. return Response.ok(fileManagerService.list(parentId));
  34. }
  35. @RequestMapping(value = "/dir/{parentId}/{currPage}/{pageSize}", method = RequestMethod.GET)
  36. public Response listPage( @PathVariable int parentId, @PathVariable int currPage, @PathVariable int pageSize) {
  37. List<FileNode> list = fileManagerService.list(parentId, currPage, pageSize);
  38. Map<String, Object> result = new HashMap<String, Object>();
  39. result.put("dataList", list);
  40. result.put("totalCount", fileManagerService.list(parentId).size());
  41. return Response.ok(result);
  42. }
  43. @RequestMapping(value = "/dir/{id}", method = RequestMethod.DELETE)
  44. public Response deleteDir( @PathVariable int id) {
  45. fileManagerService.deleteDir(id);
  46. return Response.ok();
  47. }
  48. @RequestMapping(value = "/dir/node/{id}", method = RequestMethod.DELETE)
  49. public Response delete( @PathVariable int id) {
  50. fileManagerService.delete(id);
  51. return Response.ok();
  52. }
  53. @RequestMapping(value = "/dir", method = RequestMethod.PUT)
  54. public Response updateDirName(@RequestBody FileNode fileNode) {
  55. fileManagerService.update(fileNode);
  56. return Response.ok();
  57. }
  58. @RequestMapping(value = "/dir/{parentId}", method = RequestMethod.POST)
  59. public Response createDir(@RequestBody FileNode fileNode, @PathVariable int parentId) {
  60. if(StringUtils.isBlank(fileNode.getName())) {
  61. return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR);
  62. }
  63. return Response.ok(fileManagerService.createDir(fileNode.getName(), parentId));
  64. }
  65. @RequestMapping(value = "/dir/folder", method = RequestMethod.POST)
  66. public Response folder(@RequestBody Integer [] ids) {
  67. return Response.ok(fileManagerService.folder(ids));
  68. }
  69. @RequestMapping(value = "/filenode/list/{ids}", method = RequestMethod.GET)
  70. public Response list(@PathVariable String ids) {
  71. return Response.ok(fileManagerService.list(ids));
  72. }
  73. @RequestMapping(value = "/filenode/{parentId}", method = RequestMethod.POST)
  74. public Response uploadFile(@ApiParam(name = "uploadFile", required = true) MultipartFile uploadFile, @PathVariable int parentId) {
  75. logger.info("Ready to upload file on parentId:" + parentId);
  76. InputStream inputStream = null;
  77. try {
  78. inputStream = uploadFile.getInputStream();
  79. return Response.ok(fileManagerService.create(inputStream, uploadFile.getOriginalFilename(), parentId));
  80. } catch (IOException e) {
  81. return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR);
  82. }finally {
  83. if(inputStream != null) {
  84. try {
  85. inputStream.close();
  86. } catch (IOException e) {
  87. }
  88. }
  89. }
  90. }
  91. @ApiOperation("上传base64")
  92. @RequestMapping(value = "/filenode/base64", method = RequestMethod.POST)
  93. public Response uploadFileBase64(@RequestBody JSONObject jsonObject) {
  94. InputStream inputStream = null;
  95. try {
  96. Base64.Decoder decoder = Base64.getDecoder();
  97. byte[] imageByte = decoder.decode(jsonObject.getString("base64").split(",")[1]);
  98. inputStream = new ByteArrayInputStream(imageByte);
  99. return Response.ok(fileManagerService.create(inputStream, "签名.png", -1));
  100. } finally {
  101. if (inputStream != null) {
  102. try {
  103. inputStream.close();
  104. } catch (IOException e) {
  105. }
  106. }
  107. }
  108. }
  109. @RequestMapping(value = "/filenode/{id}", method = RequestMethod.GET)
  110. public void getFile(@PathVariable int id, final HttpServletResponse response) throws Exception {
  111. logger.info("Ready to get file byte on parentId:" + id);
  112. byte[] data = null;
  113. OutputStream outputStream = null;
  114. try {
  115. FileNode fileNode = fileManagerService.get(id);
  116. data = fileManagerService.getByte(id);
  117. response.setCharacterEncoding("UTF-8");
  118. response.setContentType("application/x-msdownload");
  119. response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(fileNode.getName(), "UTF-8"));
  120. OutputStream outputSream = response.getOutputStream();
  121. outputSream.write(data);
  122. outputSream.flush();
  123. } catch (IOException e) {
  124. throw new BadRequestException("Fail to write stream", Constant.RET_DOCUMENT_ERROR, e);
  125. } finally {
  126. if (outputStream != null) {
  127. try {
  128. outputStream.close();
  129. } catch (IOException e) {
  130. }
  131. }
  132. }
  133. }
  134. @RequestMapping(value = "/filenode/{id}/type/{xx}", method = RequestMethod.GET)
  135. public void getFileTo(@PathVariable int id, @PathVariable String xx, final HttpServletResponse response) {
  136. logger.info("Ready to get file byte on parentId:" + id);
  137. byte[] data = null;
  138. OutputStream outputStream = null;
  139. try {
  140. data = fileManagerService.getByte(id);
  141. response.setCharacterEncoding("UTF-8");
  142. response.setContentType("application/x-msdownload");
  143. response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(xx, "UTF-8"));
  144. OutputStream outputSream = response.getOutputStream();
  145. outputSream.write(data);
  146. outputSream.flush();
  147. } catch (IOException e) {
  148. throw new BadRequestException("Fail to write stream", Constant.RET_DOCUMENT_ERROR, e);
  149. } finally {
  150. if (outputStream != null) {
  151. try {
  152. outputStream.close();
  153. } catch (IOException e) {
  154. }
  155. }
  156. }
  157. }
  158. @RequestMapping(value = "/filenode/{id}/render", method = RequestMethod.GET)
  159. public Response renderDocument( @PathVariable int id , final HttpServletResponse response) {
  160. byte[] data = null;
  161. OutputStream outputStream = null;
  162. try {
  163. data = fileManagerService.getByte(id);
  164. outputStream = response.getOutputStream();
  165. outputStream.write(data);
  166. outputStream.flush();
  167. } catch (IOException e) {
  168. throw new BadRequestException("Incorrect file kannode",Constant.RET_DOCUMENT_ERROR, e);
  169. }finally {
  170. if(outputStream != null) {
  171. try {
  172. outputStream.close();
  173. } catch (IOException e) {
  174. }
  175. }
  176. }
  177. return Response.ok();
  178. }
  179. }