|
@@ -2,6 +2,7 @@ package com.pavis.backend.slim.project.system.service.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -9,6 +10,7 @@ import com.github.pagehelper.PageHelper;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
import com.pavis.backend.slim.common.constant.Hodgepodge;
|
|
|
import com.pavis.backend.slim.common.utils.SecurityUtils;
|
|
|
+import com.pavis.backend.slim.framework.config.MinioConfig;
|
|
|
import com.pavis.backend.slim.project.system.domain.SysFile;
|
|
|
import com.pavis.backend.slim.project.system.domain.SysKb;
|
|
|
import com.pavis.backend.slim.project.system.domain.SysKbFile;
|
|
@@ -16,11 +18,26 @@ import com.pavis.backend.slim.project.system.domain.front.FileKey;
|
|
|
import com.pavis.backend.slim.project.system.mapper.SysFileMapper;
|
|
|
import com.pavis.backend.slim.project.system.mapper.SysKbFileMapper;
|
|
|
import com.pavis.backend.slim.project.system.mapper.SysKbMapper;
|
|
|
+import com.pavis.backend.slim.project.system.service.SysFileService;
|
|
|
import com.pavis.backend.slim.project.system.service.SysKbService;
|
|
|
+import io.minio.MinioClient;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.net.URL;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Calendar;
|
|
@@ -43,6 +60,17 @@ public class SysKbServiceImpl extends ServiceImpl<SysKbMapper, SysKb> implements
|
|
|
@Autowired
|
|
|
private SysKbMapper sysKbMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysFileService sysFileService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MinioConfig minioFileUtil;
|
|
|
+
|
|
|
+ @Value("${pavis.profile}")
|
|
|
+ private String urlLocal;
|
|
|
+ @Value("${minio.bucketName}")
|
|
|
+ private String bucketName;
|
|
|
+
|
|
|
@Override
|
|
|
public SysKb create(SysKb kb) {
|
|
|
kb.setCreateBy(SecurityUtils.getUsername());
|
|
@@ -137,6 +165,109 @@ public class SysKbServiceImpl extends ServiceImpl<SysKbMapper, SysKb> implements
|
|
|
}
|
|
|
//删除知识库
|
|
|
sysKbMapper.deleteById(kbId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void previewFile(String fileId, HttpServletResponse response, HttpServletRequest request) throws Exception {
|
|
|
+ //根据fileId,获取文件详情
|
|
|
+ SysFile sysFile = sysFileMapper.selectById(fileId);
|
|
|
+ //从minio将要预览的文件下载到本地
|
|
|
+ MinioClient client = minioFileUtil.client();
|
|
|
+ minioFileUtil.downloadPath(urlLocal, bucketName, sysFile.getObjectKey(), sysFile.getName());
|
|
|
+ //预览文件
|
|
|
+ try {
|
|
|
+ log.info("开始预览");
|
|
|
+ // response.setCharacterEncoding("utf-8");
|
|
|
+ // response.setContentType("application/octet-stream");
|
|
|
+
|
|
|
+ // 读文件输入流
|
|
|
+ String filePath = urlLocal + sysFile.getName();
|
|
|
+ InputStream inputStream = new FileInputStream(filePath);
|
|
|
+
|
|
|
+ // 复制文件流
|
|
|
+ StreamUtils.copy(inputStream, response.getOutputStream());
|
|
|
+
|
|
|
+ // 关闭输入流
|
|
|
+ inputStream.close();
|
|
|
+
|
|
|
+ log.info("预览结束");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存修改后的文件
|
|
|
+ *
|
|
|
+ * @param response
|
|
|
+ * @param request
|
|
|
+ * @param name 修改文件的本地路径
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void saveFile(HttpServletResponse response, HttpServletRequest request, String name) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 获得response信息
|
|
|
+ // PrintWriter writer = response.getWriter();
|
|
|
+
|
|
|
+ // 获取数据文件信息
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ StreamUtils.copy(request.getInputStream(), byteArrayOutputStream);
|
|
|
+ String fileInfoStr = byteArrayOutputStream.toString();
|
|
|
+ byteArrayOutputStream.close();
|
|
|
+
|
|
|
+ // if (fileInfoStr.isEmpty()) {
|
|
|
+ // writer.write("request的输入流为空");
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+
|
|
|
+ System.out.println(fileInfoStr);
|
|
|
+
|
|
|
+ // 转化字符串对象转化为JSON对象
|
|
|
+ JSONObject fileInfoObj = JSON.parseObject(fileInfoStr);
|
|
|
+
|
|
|
+ // 获取编辑文件的状态
|
|
|
|
|
|
+ int status = (Integer) fileInfoObj.get("status");
|
|
|
+ int saved = 0;
|
|
|
+
|
|
|
+ // 关闭正在编辑的文档10秒钟后,会收到该消息
|
|
|
+ if (status == 2 || status == 3) {
|
|
|
+ // 获取文件数据
|
|
|
+ try {
|
|
|
+ URL url = new URL((String) fileInfoObj.get("url"));
|
|
|
+ java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
|
|
|
+ InputStream inputStream = connection.getInputStream();
|
|
|
+
|
|
|
+ if (inputStream == null) {
|
|
|
+ throw new Exception("Stream is null");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存编辑后的数据
|
|
|
+ String path = urlLocal + name;
|
|
|
+ File file = new File(path);
|
|
|
+
|
|
|
+ // 复制数据流
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(file);
|
|
|
+ StreamUtils.copy(inputStream, outputStream);
|
|
|
+
|
|
|
+ // 关闭数据资源
|
|
|
+ outputStream.close();
|
|
|
+ inputStream.close();
|
|
|
+ connection.disconnect();
|
|
|
+ } catch (Exception ex) {
|
|
|
+ saved = 1;
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ System.out.println("保存文件成功");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // writer.write("{\"error\":" + saved + "}");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
}
|