DocInfoController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. package com.doc.biz.controller;
  2. import com.aspose.pdf.Document;
  3. import com.aspose.pdf.SaveFormat;
  4. import com.doc.biz.domain.*;
  5. import com.doc.biz.service.*;
  6. import com.doc.biz.vo.DocumentVO;
  7. import com.doc.biz.vo.UserVO;
  8. import com.doc.common.annotation.Log;
  9. import com.doc.common.constant.Constants;
  10. import com.doc.common.core.controller.BaseController;
  11. import com.doc.common.core.domain.AjaxResult;
  12. import com.doc.common.core.domain.entity.SysUser;
  13. import com.doc.common.core.page.TableDataInfo;
  14. import com.doc.common.enums.BusinessType;
  15. import com.doc.common.enums.EventLevel;
  16. import com.doc.common.utils.SecurityUtils;
  17. import com.doc.common.utils.StringUtils;
  18. import com.doc.common.utils.bean.BeanUtils;
  19. import com.doc.common.utils.file.FileUtils;
  20. import com.doc.common.utils.poi.ExcelUtil;
  21. import com.doc.system.service.ISysUserService;
  22. import io.swagger.annotations.*;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.http.HttpHeaders;
  25. import org.springframework.http.ResponseEntity;
  26. import org.springframework.web.bind.annotation.*;
  27. import org.springframework.web.multipart.MultipartFile;
  28. import org.yaml.snakeyaml.util.UriEncoder;
  29. import javax.annotation.Resource;
  30. import javax.servlet.ServletOutputStream;
  31. import javax.servlet.http.HttpServletResponse;
  32. import java.io.InputStream;
  33. import java.util.*;
  34. /**
  35. * 文件基本信息表Controller
  36. *
  37. * @author wukai
  38. * @date 2023-08-15
  39. */
  40. @Api(tags = "文件基本信息表")
  41. @RestController
  42. @RequestMapping("/biz/info")
  43. @Slf4j
  44. public class DocInfoController extends BaseController {
  45. @Resource
  46. private IDocInfoService docInfoService;
  47. @Resource
  48. private IMongoService mongoService;
  49. @Resource
  50. private IDocDirService dirService;
  51. @Resource
  52. private IDocFavoriteService favoriteService;
  53. @Resource
  54. private IDocRecentService recentService;
  55. @Resource
  56. private IElasticSearchService elasticSearchService;
  57. @Resource
  58. private IDocInfoDelService delService;
  59. @Resource
  60. private IDocActorUserService actorUserService;
  61. @Resource
  62. private IDocDirUserService dirUserService;
  63. @Resource
  64. private ISysUserService userService;
  65. /**
  66. * 文件上传
  67. *
  68. * @param file 文件
  69. * @return 上传结果
  70. */
  71. @ApiOperation("文件上传-单文件")
  72. @Log(title = "文件基本信息表", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
  73. @PostMapping("/upload")
  74. public AjaxResult uploadFile(@ApiParam(value = "文件", required = true) @RequestPart(value = "file") MultipartFile file, @ApiParam(value = "空间ID", required = true) @RequestParam Long spaceId, @ApiParam(value = "目录ID", required = true) @RequestParam Long dirId) {
  75. try {
  76. DocumentVO vo = mongoService.uploadFile(file);
  77. docInfoService.upload(vo, spaceId, dirId);
  78. return success();
  79. } catch (Exception e) {
  80. log.error("文件上传失败:", e);
  81. return error(e.getMessage());
  82. }
  83. }
  84. /**
  85. * 多文件上传
  86. *
  87. * @param files 文件列表
  88. * @return 返回
  89. */
  90. @ApiOperation("文件上传-多文件")
  91. @PostMapping("/uploadFiles")
  92. @Log(title = "文件基本信息表", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
  93. public AjaxResult uploadFile(@ApiParam(value = "文件", required = true) @RequestPart(value = "files") List<MultipartFile> files, @ApiParam(value = "空间ID", required = true) @RequestParam Long spaceId, @ApiParam(value = "目录ID", required = true) @RequestParam Long dirId) {
  94. try {
  95. mongoService.uploadFiles(files).forEach(vo -> {
  96. docInfoService.upload(vo, spaceId, dirId);
  97. });
  98. return success();
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. return error(e.getMessage());
  102. }
  103. }
  104. /**
  105. * 新建文件
  106. */
  107. @ApiOperation("新建文件")
  108. @GetMapping("/create")
  109. @ApiImplicitParams({
  110. @ApiImplicitParam(name = "type", value = "文件类型 \"word\":word文档 \"excel\":excel文件 \"ppt\":ppt文件 \"txt\":文本文件", required = true, dataTypeClass = Long.class),
  111. @ApiImplicitParam(name = "spaceId", value = "空间ID", required = true, dataTypeClass = Long.class),
  112. @ApiImplicitParam(name = "dirId", value = "目录ID", required = true, dataTypeClass = String.class),
  113. @ApiImplicitParam(name = "name", value = "文件名", required = true, dataTypeClass = String.class)
  114. })
  115. @Log(title = "文件基本信息表", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
  116. public AjaxResult create(Long spaceId, Long dirId, String type, String name) {
  117. try {
  118. String filePath = "";
  119. String fileName = "";
  120. switch (type) {
  121. case "word":
  122. filePath = "file/new.docx";
  123. fileName = StringUtils.isNotEmpty(name) ? name + ".docx" : "新建word文档.docx";
  124. break;
  125. case "excel":
  126. filePath = "file/new.xlsx";
  127. fileName = StringUtils.isNotEmpty(name) ? name + ".xlsx" : "新建Excel文档.xlsx";
  128. break;
  129. case "ppt":
  130. filePath = "file/new.pptx";
  131. fileName = StringUtils.isNotEmpty(name) ? name + ".pptx" : "新建ppt文档.pptx";
  132. break;
  133. case "txt":
  134. filePath = "file/new.txt";
  135. fileName = StringUtils.isNotEmpty(name) ? name + ".txt" : "新建文本文档.txt";
  136. break;
  137. }
  138. InputStream is = this.getClass().getResourceAsStream("/" + filePath);
  139. MultipartFile multipartFile = FileUtils.getMultipartFile(is, fileName);
  140. DocumentVO vo = mongoService.uploadFile(multipartFile);
  141. DocInfo docInfo = docInfoService.upload(vo, spaceId, dirId);
  142. return success(docInfo);
  143. } catch (Exception e) {
  144. log.error("新建文件出错啦:{}", e.getMessage());
  145. return error("新建文件出错");
  146. }
  147. }
  148. /**
  149. * 文件移动
  150. */
  151. @ApiOperation("文件移动")
  152. @GetMapping("/move")
  153. @Log(title = "文件基本信息表", businessType = BusinessType.UPDATE, eventLevel = EventLevel.MIDDLE)
  154. @ApiImplicitParams({@ApiImplicitParam(name = "docId", value = "文件ID", required = true, dataTypeClass = Long.class),
  155. @ApiImplicitParam(name = "spaceId", value = "空间ID", required = true, dataTypeClass = Long.class),
  156. @ApiImplicitParam(name = "dirId", value = "新目录ID", required = true, dataTypeClass = Long.class)})
  157. public AjaxResult move(Long docId, Long dirId) {
  158. DocInfo info = docInfoService.selectDocInfoByDocId(docId);
  159. if (info == null) {
  160. return error("该文件已失效!");
  161. }
  162. info.setDirId(dirId);
  163. info.setUpdateBy(SecurityUtils.getUsername());
  164. docInfoService.updateDocInfo(info);
  165. return success();
  166. }
  167. /**
  168. * 复制文件
  169. */
  170. @ApiOperation("文件复制")
  171. @GetMapping("/copy")
  172. @Log(title = "文件基本信息表", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
  173. @ApiImplicitParams({@ApiImplicitParam(name = "docId", value = "文件ID", required = true, dataTypeClass = Long.class),
  174. @ApiImplicitParam(name = "spaceId", value = "空间ID", required = true, dataTypeClass = Long.class),
  175. @ApiImplicitParam(name = "dirId", value = "新目录ID", required = true, dataTypeClass = Long.class)})
  176. public AjaxResult copy(Long docId, Long spaceId, Long dirId) {
  177. DocInfo info = docInfoService.selectDocInfoByDocId(docId);
  178. if (info == null) {
  179. return error("该文件已失效!");
  180. }
  181. String ext = info.getFileType();
  182. if (StringUtils.isNotEmpty(ext)) {
  183. //因为数据库存的是带 . 的,所以要先截取掉这个 .
  184. ext = ext.substring(1);
  185. if (Arrays.asList(Constants.ALLOW_EDIT).contains(ext)) {
  186. //判断是否 onlyoffice允许的可编辑文件格式,如果是,则需要复制一份,如果不是就不管
  187. DocumentVO vo = mongoService.copy(info.getFileId());
  188. info.setFileId(vo.getFileId());
  189. }
  190. }
  191. info.setDocId(null);
  192. info.setSpaceId(spaceId);
  193. info.setDirId(dirId);
  194. info.setFileName(info.getFileName());
  195. docInfoService.insertDocInfo(info);
  196. return success(info);
  197. }
  198. /**
  199. * 重命名
  200. */
  201. @ApiOperation("文件重命名")
  202. @GetMapping("/rename")
  203. @Log(title = "文件基本信息表", businessType = BusinessType.UPDATE, eventLevel = EventLevel.MIDDLE)
  204. @ApiImplicitParams({@ApiImplicitParam(name = "docId", value = "文件ID", required = true, dataTypeClass = Long.class),
  205. @ApiImplicitParam(name = "name", value = "新文件名", required = true, dataTypeClass = Long.class)})
  206. public AjaxResult rename(Long docId, String name) {
  207. DocInfo info = new DocInfo();
  208. info.setDocId(docId);
  209. info.setFileName(name);
  210. info.setUpdateBy(SecurityUtils.getUsername());
  211. return success(docInfoService.updateDocInfo(info));
  212. }
  213. /**
  214. * 查询文件基本信息表列表
  215. */
  216. // @ApiOperation("文件列表")
  217. //@PreAuthorize("@ss.hasPermi('biz:info:list')")
  218. @GetMapping("/list")
  219. public TableDataInfo list(DocInfo docInfo) {
  220. startPage();
  221. List<DocInfo> list = docInfoService.selectDocInfoList(docInfo);
  222. return getDataTable(list);
  223. }
  224. /**
  225. * 查询所有已失效的文件,就是mysql中有,mongo中没有的
  226. */
  227. @GetMapping("/lose")
  228. public AjaxResult list() {
  229. List<DocInfo> docInfoList = docInfoService.selectLoseList();
  230. return AjaxResult.success(docInfoList);
  231. }
  232. @GetMapping("/content/{docId}")
  233. @ApiOperation("根据ID查询文本内容")
  234. public AjaxResult content(@ApiParam(value = "文件ID", required = true) @PathVariable Long docId) {
  235. EsDocInfo info = elasticSearchService.getEsDocInfo(docId);
  236. return AjaxResult.success(info);
  237. }
  238. /**
  239. * 文件搜索
  240. */
  241. @ApiOperation("文件搜索")
  242. //@PreAuthorize("@ss.hasPermi('biz:info:list')")
  243. @GetMapping("/search")
  244. public TableDataInfo search(@ApiParam(value = "搜索关键字", required = true) @RequestParam String keyword, @ApiParam(value = "空间类型(1.公共 2.部门 3.个人 不传该参数则为全部)") String type) {
  245. Map<String, Object> temp = dirService.selectDirByUser(SecurityUtils.getUserId(), type);
  246. List<DocDir> dirList = (List<DocDir>) temp.get("dir");
  247. List<DocSpace> spaceList = (List<DocSpace>) temp.get("space");
  248. Map<Long, DocDir> dirMap = new HashMap<>(16);
  249. Map<Long, DocSpace> spaceMap = new HashMap<>(16);
  250. List<Long> dirIds = new ArrayList<>();
  251. dirList.forEach(dir -> {
  252. dirIds.add(dir.getDirId());
  253. dirMap.put(dir.getDirId(), dir);
  254. });
  255. DocInfo docInfo = new DocInfo();
  256. spaceList.forEach(space -> {
  257. if (type != null && type.equals(space.getSpaceType())) {
  258. docInfo.setSpaceId(space.getSpaceId());
  259. }
  260. spaceMap.put(space.getSpaceId(), space);
  261. });
  262. docInfo.setFileName(keyword);
  263. if (dirIds.size() > 0) {
  264. Map<String, Object> map = new HashMap<>(4);
  265. map.put("dirIds", dirIds);
  266. docInfo.setParams(map);
  267. }
  268. startPage();
  269. List<DocInfo> list = docInfoService.selectDocInfoList(docInfo);
  270. list.forEach(info -> {
  271. info.setDir(dirMap.get(info.getDirId()));
  272. info.setSpace(spaceMap.get(info.getSpaceId()));
  273. });
  274. return getDataTable(list);
  275. }
  276. /**
  277. * 查询文件基本信息表列表
  278. */
  279. @ApiOperation("根据目录ID查询文件列表")
  280. //@PreAuthorize("@ss.hasPermi('biz:info:list')")
  281. @GetMapping("/list/{dirId}")
  282. public TableDataInfo list4dir(@PathVariable("dirId") Long dirId) {
  283. startPage();
  284. DocInfo docInfo = new DocInfo();
  285. docInfo.setDirId(dirId);
  286. List<DocInfo> list = docInfoService.selectDocInfoList(docInfo);
  287. return getDataTable(list);
  288. }
  289. /**
  290. * 导出文件基本信息表列表
  291. */
  292. // @ApiOperation("导出文件基本信息表列表")
  293. //@PreAuthorize("@ss.hasPermi('biz:info:export')")
  294. @Log(title = "文件基本信息表", businessType = BusinessType.EXPORT)
  295. @PostMapping("/export")
  296. public void export(HttpServletResponse response, DocInfo docInfo) {
  297. List<DocInfo> list = docInfoService.selectDocInfoList(docInfo);
  298. ExcelUtil<DocInfo> util = new ExcelUtil<DocInfo>(DocInfo.class);
  299. util.exportExcel(response, list, "文件基本信息表数据");
  300. }
  301. /**
  302. * 获取文件基本信息表详细信息
  303. */
  304. @ApiOperation("文件详细信息")
  305. //@PreAuthorize("@ss.hasPermi('biz:info:query')")
  306. @GetMapping(value = "/{docId}")
  307. public AjaxResult getInfo(@PathVariable("docId") Long docId) {
  308. return success(docInfoService.selectDocInfoByDocId(docId));
  309. }
  310. /**
  311. * 新增文件基本信息表
  312. */
  313. // @ApiOperation("新增文件基本信息表")
  314. //@PreAuthorize("@ss.hasPermi('biz:info:add')")
  315. @Log(title = "文件基本信息表", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
  316. @PostMapping
  317. public AjaxResult add(@RequestBody DocInfo docInfo) {
  318. return toAjax(docInfoService.insertDocInfo(docInfo));
  319. }
  320. /**
  321. * 修改文件基本信息表
  322. */
  323. // @ApiOperation("修改文件基本信息表")
  324. //@PreAuthorize("@ss.hasPermi('biz:info:edit')")
  325. @Log(title = "文件基本信息表", businessType = BusinessType.UPDATE, eventLevel = EventLevel.MIDDLE)
  326. @PutMapping
  327. public AjaxResult edit(@RequestBody DocInfo docInfo) {
  328. docInfo.setUpdateBy(SecurityUtils.getUsername());
  329. return toAjax(docInfoService.updateDocInfo(docInfo));
  330. }
  331. /**
  332. * 删除文件基本信息表
  333. */
  334. @ApiOperation("文件删除")
  335. //@PreAuthorize("@ss.hasPermi('biz:info:remove')")
  336. @Log(title = "文件基本信息表", businessType = BusinessType.DELETE, eventLevel = EventLevel.MIDDLE)
  337. @DeleteMapping("/{docIds}")
  338. public AjaxResult remove(@PathVariable Long[] docIds) {
  339. List<DocInfo> list = new ArrayList<>();
  340. for (Long docId : docIds) {
  341. DocInfo info = docInfoService.selectDocInfoByDocId(docId);
  342. list.add(info);
  343. }
  344. int i = docInfoService.deleteDocInfoByDocIds(docIds);
  345. list.forEach(info -> {
  346. DocInfoDel del = new DocInfoDel();
  347. del.setRemark(getUsername());
  348. BeanUtils.copyProperties(info, del);
  349. delService.insertDocInfoDel(del);
  350. /**取消删除mongo
  351. try {
  352. //删除mongo记录
  353. mongoService.removeFile(info.getFileId());
  354. } catch (Exception ignored) {
  355. }*/
  356. try {
  357. //删除es记录
  358. elasticSearchService.delete(info.getDocId());
  359. } catch (Exception ignored) {
  360. }
  361. //删除收藏记录
  362. favoriteService.delete("N", info.getDocId());
  363. //删除最近记录
  364. recentService.delete("N", info.getDocId());
  365. });
  366. return toAjax(i);
  367. }
  368. /**
  369. * 文件下载
  370. */
  371. @ApiOperation("文件预览")
  372. @GetMapping("/access/{docId}")
  373. public ResponseEntity<Object> access(@PathVariable(name = "docId") Long docId) {
  374. DocInfo info = docInfoService.selectDocInfoByDocId(docId);
  375. //插入最近文件
  376. DocRecent recent = new DocRecent();
  377. recent.setIsFolder("N");
  378. recent.setOwner(SecurityUtils.getUserId());
  379. recent.setRelaId(info.getDocId());
  380. recentService.insertDocRecent(recent);
  381. return mongoService.download(info.getFileId(), false);
  382. }
  383. @ApiOperation("使用该文档的人员")
  384. @GetMapping("/users/{docId}")
  385. public AjaxResult users(@PathVariable(name = "docId") Long docId) {
  386. DocInfo info = docInfoService.selectDocInfoByDocId(docId);
  387. List<UserVO> voList = new ArrayList<>();
  388. String yes = "Y";
  389. if (yes.equals(info.getIsActor())) {
  390. actorUserService.selectDocActorUserListByDocId(docId).forEach(u -> {
  391. UserVO vo = new UserVO();
  392. vo.setUserId(u.getUserId());
  393. vo.setUserName(u.getName());
  394. SysUser user = userService.selectUserById(u.getUserId());
  395. vo.setDeptName(user.getDept().getDeptName());
  396. voList.add(vo);
  397. });
  398. } else {
  399. DocDirUser dirUser = new DocDirUser();
  400. dirUser.setDirId(info.getDirId());
  401. dirUserService.selectDocDirUserList(dirUser).forEach(u -> {
  402. UserVO vo = new UserVO();
  403. vo.setUserId(u.getUserId());
  404. vo.setUserName(u.getUserName());
  405. SysUser user = userService.selectUserById(u.getUserId());
  406. vo.setDeptName(user.getDept().getDeptName());
  407. voList.add(vo);
  408. });
  409. ;
  410. // dirService.
  411. //TODO
  412. // }
  413. }
  414. return success(voList);
  415. }
  416. /**
  417. * 文件下载
  418. */
  419. @ApiOperation("文件下载")
  420. @GetMapping("/download/{docId}")
  421. public ResponseEntity<Object> download(@PathVariable(name = "docId") Long docId) {
  422. DocInfo info = docInfoService.selectDocInfoByDocId(docId);
  423. return mongoService.download(info.getFileId(), true);
  424. }
  425. @ApiOperation("pdf转word")
  426. @Log(title = "PDF转WORD", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
  427. @PostMapping("/pdf2word")
  428. public void pdf2word(@ApiParam(value = "文件", required = true) @RequestPart(value = "file") MultipartFile file, HttpServletResponse response) {
  429. try (Document doc = new Document(file.getInputStream());) {
  430. write(doc, file.getOriginalFilename(), response);
  431. } catch (Exception e) {
  432. logger.error("Pdf 转 Word 失败{}", e.getMessage());
  433. e.printStackTrace();
  434. }
  435. }
  436. /**
  437. * 写入文件流
  438. *
  439. * @param doc doc
  440. * @param fileName 文件流
  441. * @param response res
  442. */
  443. private void write(Document doc, String fileName, HttpServletResponse response) {
  444. try (ServletOutputStream os = response.getOutputStream();) {
  445. //doc是将要被转化的word文档
  446. String disposition = "attachment; filename=\"" + UriEncoder.encode(fileName) + "\"";
  447. response.addHeader(HttpHeaders.CONTENT_DISPOSITION, disposition);
  448. response.addHeader(HttpHeaders.CONTENT_TYPE, "application/octet-stream");
  449. response.setContentType("application/octet-stream");
  450. //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
  451. doc.save(os, SaveFormat.DocX);
  452. os.flush();
  453. } catch (Exception e) {
  454. logger.error("Pdf 转 Word 失败{}", e.getMessage());
  455. e.printStackTrace();
  456. }
  457. }
  458. @ApiOperation("pdf转word")
  459. @Log(title = "PDF转WORD", businessType = BusinessType.INSERT, eventLevel = EventLevel.MIDDLE)
  460. @GetMapping("/pdf2word/{fileId}")
  461. public void pdf2word(@PathVariable(name = "fileId") String fileId, HttpServletResponse response) {
  462. DocumentVO vo = mongoService.downloadFile(fileId);
  463. try (Document doc = new Document(vo.getData())) {
  464. write(doc, vo.getFileName(), response);
  465. } catch (Exception e) {
  466. logger.error("Pdf 转 Word 失败{}", e.getMessage());
  467. e.printStackTrace();
  468. }
  469. }
  470. }