|
@@ -1,20 +1,27 @@
|
|
|
package com.doc.biz.controller;
|
|
|
|
|
|
import com.doc.biz.domain.DocInfo;
|
|
|
+import com.doc.biz.domain.DocSpace;
|
|
|
+import com.doc.biz.domain.EsDocInfo;
|
|
|
import com.doc.biz.service.IDocInfoService;
|
|
|
+import com.doc.biz.service.IEsDocInfoService;
|
|
|
import com.doc.biz.service.IMongoService;
|
|
|
import com.doc.biz.vo.DocumentVO;
|
|
|
import com.doc.common.annotation.Log;
|
|
|
+import com.doc.common.config.EsConfig;
|
|
|
import com.doc.common.core.controller.BaseController;
|
|
|
import com.doc.common.core.domain.AjaxResult;
|
|
|
import com.doc.common.core.page.TableDataInfo;
|
|
|
import com.doc.common.enums.BusinessType;
|
|
|
+import com.doc.common.enums.SpaceType;
|
|
|
+import com.doc.common.utils.FileContentUtils;
|
|
|
import com.doc.common.utils.SecurityUtils;
|
|
|
import com.doc.common.utils.poi.ExcelUtil;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.elasticsearch.NoSuchIndexException;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
@@ -24,8 +31,12 @@ import org.yaml.snakeyaml.util.UriEncoder;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.function.Function;
|
|
|
|
|
|
/**
|
|
|
* 文件基本信息表Controller
|
|
@@ -42,6 +53,10 @@ public class DocInfoController extends BaseController {
|
|
|
private IDocInfoService docInfoService;
|
|
|
@Resource
|
|
|
private IMongoService mongoService;
|
|
|
+ @Resource
|
|
|
+ private IEsDocInfoService esDocInfoService;
|
|
|
+ @Resource
|
|
|
+ private EsConfig esConfig;
|
|
|
|
|
|
/**
|
|
|
* 文件上传
|
|
@@ -51,9 +66,9 @@ public class DocInfoController extends BaseController {
|
|
|
*/
|
|
|
@ApiOperation("上传文件")
|
|
|
@PostMapping("/upload")
|
|
|
- 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) {
|
|
|
+ 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) {
|
|
|
try {
|
|
|
DocumentVO vo = mongoService.uploadFile(file);
|
|
|
DocInfo docInfo = new DocInfo();
|
|
@@ -64,9 +79,14 @@ public class DocInfoController extends BaseController {
|
|
|
docInfo.setFileSize(vo.getFileSize());
|
|
|
docInfo.setFileType(vo.getSuffix());
|
|
|
docInfo.setCreateBy(SecurityUtils.getUsername());
|
|
|
- return toAjax(docInfoService.insertDocInfo(docInfo));
|
|
|
+ docInfoService.insertDocInfo(docInfo);
|
|
|
+
|
|
|
+ DocumentVO documentVO = mongoService.downloadFile(vo.getFileId());
|
|
|
+ insertEs(documentVO.getData(), docInfo.getFileType(), docInfo.getDocId(), spaceId);
|
|
|
+
|
|
|
+ return success();
|
|
|
} catch (Exception e) {
|
|
|
- log.error("文件上传失败:" , e);
|
|
|
+ log.error("文件上传失败:", e);
|
|
|
return error(e.getMessage());
|
|
|
}
|
|
|
}
|
|
@@ -100,7 +120,7 @@ public class DocInfoController extends BaseController {
|
|
|
return ResponseEntity.ok()
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + UriEncoder.encode(mongoFileVo.getFileName()) + "\"")
|
|
|
.header(HttpHeaders.CONTENT_TYPE, mongoFileVo.getContentType())
|
|
|
- .header(HttpHeaders.CONTENT_LENGTH, mongoFileVo.getFileSize() + "").header("Connection" , "close")
|
|
|
+ .header(HttpHeaders.CONTENT_LENGTH, mongoFileVo.getFileSize() + "").header("Connection", "close")
|
|
|
.body(mongoFileVo.getData());
|
|
|
} else {
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
|
|
@@ -121,7 +141,7 @@ public class DocInfoController extends BaseController {
|
|
|
return ResponseEntity.ok()
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION, "filename=\"" + UriEncoder.encode(mongoFileVo.getFileName()) + "\"")
|
|
|
.header(HttpHeaders.CONTENT_TYPE, mongoFileVo.getContentType())
|
|
|
- .header(HttpHeaders.CONTENT_LENGTH, mongoFileVo.getFileSize() + "").header("Connection" , "close")
|
|
|
+ .header(HttpHeaders.CONTENT_LENGTH, mongoFileVo.getFileSize() + "").header("Connection", "close")
|
|
|
.body(mongoFileVo.getData());
|
|
|
} else {
|
|
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("file does not exist");
|
|
@@ -145,7 +165,7 @@ public class DocInfoController extends BaseController {
|
|
|
*/
|
|
|
@ApiOperation("导出文件基本信息表列表")
|
|
|
//@PreAuthorize("@ss.hasPermi('biz:info:export')")
|
|
|
- @Log(title = "文件基本信息表" , businessType = BusinessType.EXPORT)
|
|
|
+ @Log(title = "文件基本信息表", businessType = BusinessType.EXPORT)
|
|
|
@PostMapping("/export")
|
|
|
public void export(HttpServletResponse response, DocInfo docInfo) {
|
|
|
List<DocInfo> list = docInfoService.selectDocInfoList(docInfo);
|
|
@@ -168,7 +188,7 @@ public class DocInfoController extends BaseController {
|
|
|
*/
|
|
|
@ApiOperation("新增文件基本信息表")
|
|
|
//@PreAuthorize("@ss.hasPermi('biz:info:add')")
|
|
|
- @Log(title = "文件基本信息表" , businessType = BusinessType.INSERT)
|
|
|
+ @Log(title = "文件基本信息表", businessType = BusinessType.INSERT)
|
|
|
@PostMapping
|
|
|
public AjaxResult add(@RequestBody DocInfo docInfo) {
|
|
|
return toAjax(docInfoService.insertDocInfo(docInfo));
|
|
@@ -179,9 +199,10 @@ public class DocInfoController extends BaseController {
|
|
|
*/
|
|
|
@ApiOperation("修改文件基本信息表")
|
|
|
//@PreAuthorize("@ss.hasPermi('biz:info:edit')")
|
|
|
- @Log(title = "文件基本信息表" , businessType = BusinessType.UPDATE)
|
|
|
+ @Log(title = "文件基本信息表", businessType = BusinessType.UPDATE)
|
|
|
@PutMapping
|
|
|
public AjaxResult edit(@RequestBody DocInfo docInfo) {
|
|
|
+ //TODO 文件内容修改的ES处理
|
|
|
docInfo.setUpdateBy(SecurityUtils.getUsername());
|
|
|
return toAjax(docInfoService.updateDocInfo(docInfo));
|
|
|
}
|
|
@@ -191,13 +212,52 @@ public class DocInfoController extends BaseController {
|
|
|
*/
|
|
|
@ApiOperation("删除文件基本信息表")
|
|
|
//@PreAuthorize("@ss.hasPermi('biz:info:remove')")
|
|
|
- @Log(title = "文件基本信息表" , businessType = BusinessType.DELETE)
|
|
|
+ @Log(title = "文件基本信息表", businessType = BusinessType.DELETE)
|
|
|
@DeleteMapping("/{docIds}")
|
|
|
public AjaxResult remove(@PathVariable Long[] docIds) {
|
|
|
for (Long docId : docIds) {
|
|
|
DocInfo info = docInfoService.selectDocInfoByDocId(docId);
|
|
|
+ //删除mongo记录
|
|
|
mongoService.removeFile(info.getFileId());
|
|
|
+
|
|
|
+ try {
|
|
|
+ //删除ES记录
|
|
|
+ String indexName = "docs_" + info.getSpaceId();
|
|
|
+ esConfig.setIndexName(indexName);
|
|
|
+ esDocInfoService.deleteById(docId);
|
|
|
+ } catch (NoSuchIndexException e) {
|
|
|
+ //不用管,表示没这个索引
|
|
|
+ }
|
|
|
}
|
|
|
return toAjax(docInfoService.deleteDocInfoByDocIds(docIds));
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件内容入es库
|
|
|
+ *
|
|
|
+ * @param data 二进制文件内容
|
|
|
+ * @param ext 扩展名
|
|
|
+ * @param docId 文档ID
|
|
|
+ * @param spaceId 空间ID
|
|
|
+ */
|
|
|
+ private void insertEs(byte[] data, String ext, Long docId, Long spaceId) {
|
|
|
+ //组装ES索引名
|
|
|
+ String indexName = "docs_" + spaceId;
|
|
|
+
|
|
|
+ Map<String, Function<byte[], String>> handlerMap = new HashMap<>(16);
|
|
|
+ handlerMap.put(".docx", FileContentUtils::getContentDocx);
|
|
|
+ handlerMap.put(".doc", FileContentUtils::getContentDoc);
|
|
|
+ handlerMap.put(".wps", FileContentUtils::getContentWps);
|
|
|
+ handlerMap.put(".txt", FileContentUtils::getContentTxt);
|
|
|
+
|
|
|
+ Function<byte[], String> handler = handlerMap.get(ext);
|
|
|
+ if (handler != null) {
|
|
|
+ String content = handler.apply(data);
|
|
|
+ System.err.println(content);
|
|
|
+ EsDocInfo esDocInfo = new EsDocInfo(docId, content);
|
|
|
+ esConfig.setIndexName(indexName);
|
|
|
+ esDocInfoService.save(esDocInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|