package com.bosshand.virgo.file.controller; import com.alibaba.fastjson.JSONObject; import com.bosshand.virgo.core.response.Response; import com.bosshand.virgo.exception.BadRequestException; import com.bosshand.virgo.exception.Constant; import com.bosshand.virgo.file.model.FileNode; import com.bosshand.virgo.file.service.FileManagerService; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.util.Base64; import java.util.HashMap; import java.util.List; import java.util.Map; @RestController public class FileNodeController { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private FileManagerService fileManagerService; @RequestMapping(value = "/dir/{parentId}", method = RequestMethod.GET) public Response listDir( @PathVariable int parentId) { return Response.ok(fileManagerService.list(parentId)); } @RequestMapping(value = "/dir/{parentId}/{currPage}/{pageSize}", method = RequestMethod.GET) public Response listPage( @PathVariable int parentId, @PathVariable int currPage, @PathVariable int pageSize) { List list = fileManagerService.list(parentId, currPage, pageSize); Map result = new HashMap(); result.put("dataList", list); result.put("totalCount", fileManagerService.list(parentId).size()); return Response.ok(result); } @RequestMapping(value = "/dir/{id}", method = RequestMethod.DELETE) public Response deleteDir( @PathVariable int id) { fileManagerService.deleteDir(id); return Response.ok(); } @RequestMapping(value = "/dir/node/{id}", method = RequestMethod.DELETE) public Response delete( @PathVariable int id) { fileManagerService.delete(id); return Response.ok(); } @RequestMapping(value = "/dir", method = RequestMethod.PUT) public Response updateDirName(@RequestBody FileNode fileNode) { fileManagerService.update(fileNode); return Response.ok(); } @RequestMapping(value = "/dir/{parentId}", method = RequestMethod.POST) public Response createDir(@RequestBody FileNode fileNode, @PathVariable int parentId) { if(StringUtils.isBlank(fileNode.getName())) { return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR); } return Response.ok(fileManagerService.createDir(fileNode.getName(), parentId)); } @RequestMapping(value = "/dir/folder", method = RequestMethod.POST) public Response folder(@RequestBody Integer [] ids) { return Response.ok(fileManagerService.folder(ids)); } @RequestMapping(value = "/filenode/list/{ids}", method = RequestMethod.GET) public Response list(@PathVariable String ids) { return Response.ok(fileManagerService.list(ids)); } @RequestMapping(value = "/filenode/{parentId}", method = RequestMethod.POST) public Response uploadFile(@ApiParam(name = "uploadFile", required = true) MultipartFile uploadFile, @PathVariable int parentId) { logger.info("Ready to upload file on parentId:" + parentId); InputStream inputStream = null; try { inputStream = uploadFile.getInputStream(); return Response.ok(fileManagerService.create(inputStream, uploadFile.getOriginalFilename(), parentId)); } catch (IOException e) { return Response.fail(Constant.CODE_BAD_REQUEST, Constant.RET_INPUT_ERROR); }finally { if(inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } } } @ApiOperation("上传base64") @RequestMapping(value = "/filenode/base64", method = RequestMethod.POST) public Response uploadFileBase64(@RequestBody JSONObject jsonObject) { InputStream inputStream = null; try { Base64.Decoder decoder = Base64.getDecoder(); byte[] imageByte = decoder.decode(jsonObject.getString("base64").split(",")[1]); inputStream = new ByteArrayInputStream(imageByte); return Response.ok(fileManagerService.create(inputStream, "签名.png", -1)); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } } } @RequestMapping(value = "/filenode/{id}", method = RequestMethod.GET) public void getFile(@PathVariable int id, final HttpServletResponse response) throws Exception { logger.info("Ready to get file byte on parentId:" + id); byte[] data = null; OutputStream outputStream = null; try { FileNode fileNode = fileManagerService.get(id); data = fileManagerService.getByte(id); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(fileNode.getName(), "UTF-8")); OutputStream outputSream = response.getOutputStream(); outputSream.write(data); outputSream.flush(); } catch (IOException e) { throw new BadRequestException("Fail to write stream", Constant.RET_DOCUMENT_ERROR, e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } } } @RequestMapping(value = "/filenode/{id}/type/{xx}", method = RequestMethod.GET) public void getFileTo(@PathVariable int id, @PathVariable String xx, final HttpServletResponse response) { logger.info("Ready to get file byte on parentId:" + id); byte[] data = null; OutputStream outputStream = null; try { data = fileManagerService.getByte(id); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(xx, "UTF-8")); OutputStream outputSream = response.getOutputStream(); outputSream.write(data); outputSream.flush(); } catch (IOException e) { throw new BadRequestException("Fail to write stream", Constant.RET_DOCUMENT_ERROR, e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } } } @RequestMapping(value = "/filenode/{id}/render", method = RequestMethod.GET) public Response renderDocument( @PathVariable int id , final HttpServletResponse response) { byte[] data = null; OutputStream outputStream = null; try { data = fileManagerService.getByte(id); outputStream = response.getOutputStream(); outputStream.write(data); outputStream.flush(); } catch (IOException e) { throw new BadRequestException("Incorrect file kannode",Constant.RET_DOCUMENT_ERROR, e); }finally { if(outputStream != null) { try { outputStream.close(); } catch (IOException e) { } } } return Response.ok(); } }