|
@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.crypto.digest.MD5;
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.pavis.backend.slim.common.constant.Constant;
|
|
|
import com.pavis.backend.slim.common.exception.ServiceException;
|
|
@@ -16,12 +17,31 @@ import com.pavis.backend.slim.project.system.mapper.SysFileMapper;
|
|
|
import com.pavis.backend.slim.project.system.service.SysFileService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.compress.utils.IOUtils;
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
+import org.apache.commons.fileupload.FileItemFactory;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItem;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
+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.OutputStream;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.net.URL;
|
|
|
import java.security.InvalidKeyException;
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.*;
|
|
@@ -32,6 +52,7 @@ import java.util.stream.Collectors;
|
|
|
* @create 2023-04-24 14:08
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
|
|
|
|
|
|
@Autowired
|
|
@@ -47,6 +68,8 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
|
|
|
@Override
|
|
|
public SysFile uploadFile(MultipartFile file, String icon, String path, boolean isDir) {
|
|
|
System.out.println("icon:" + icon);
|
|
|
+ log.info("Constant.DIR_SEP:{}", Constant.DIR_SEP);
|
|
|
+ System.out.println("path:" + path);
|
|
|
SysFile sysFile = new SysFile();
|
|
|
// 获取上传文件的用户id
|
|
|
sysFile.setUserId(SecurityUtils.getUserId());
|
|
@@ -175,6 +198,15 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
|
|
|
public List<TreeFile> getTreeFiles(List<SysFile> sysFileList) {
|
|
|
List<TreeFile> treeFiles = new ArrayList<>();
|
|
|
for (SysFile sysFile : sysFileList) {
|
|
|
+ log.info("ghj file:{}", sysFile.getObjectKey());
|
|
|
+ if (null != sysFile.getObjectKey()) {
|
|
|
+ // 获取完整minio链接,在onlyoffice预览文件。
|
|
|
+ String preview = storage.preview(sysFile.getObjectKey());
|
|
|
+ sysFile.setTotalUrl(preview);
|
|
|
+ // 文件key需要唯一,todo 后期此处仍需再修改,虽目前已可以预览文件。
|
|
|
+ sysFile.setFileKey(MD5.create().digestHex16(String.valueOf(System.currentTimeMillis())));
|
|
|
+ log.info("to preview:{}", preview);
|
|
|
+ }
|
|
|
treeFiles.add(getTreeFile(sysFile, sysFileList));
|
|
|
}
|
|
|
System.out.println("1-tree-files:" + JSON.toJSONString(treeFiles));
|
|
@@ -239,4 +271,129 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> impl
|
|
|
return flag;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onlyOfficeToMinio(String fileId, String path, HttpServletResponse response, HttpServletRequest request) {
|
|
|
+ if (null != fileId && StringUtils.isNotEmpty(fileId)) {
|
|
|
+ SysFile selSysFile = baseMapper.selectById(fileId);
|
|
|
+ if (null != selSysFile) {
|
|
|
+ uploadToMinio(path, selSysFile, response, request);
|
|
|
+ } else {
|
|
|
+ throw new ServiceException("文件不存在!");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new ServiceException("文件id不能为空!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户编辑的文件上传至minio中。
|
|
|
+ * @param path
|
|
|
+ * @param selSysFile
|
|
|
+ * @param response
|
|
|
+ * @param request
|
|
|
+ */
|
|
|
+ public void uploadToMinio(String path, SysFile selSysFile, HttpServletResponse response, HttpServletRequest request) {
|
|
|
+ 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("输入流为空");
|
|
|
+ log.info("输入流为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("get onlyOffice fileInfoStr:{}", 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 ServiceException("Stream is null");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 文件名:
|
|
|
+ String key = fileInfoObj.get("key").toString();
|
|
|
+ String filetype = fileInfoObj.get("filetype").toString();
|
|
|
+ String fileName = FileUtils.genFileName(key) + "-" + key + "." + filetype;
|
|
|
+ // 本地先保存编辑后的数据
|
|
|
+ String filePath = "E:\\pvs\\" + fileName;
|
|
|
+ File file = new File(filePath);
|
|
|
+
|
|
|
+ // 复制数据流
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(file);
|
|
|
+ StreamUtils.copy(inputStream, outputStream);
|
|
|
+ // 关闭数据资源
|
|
|
+ outputStream.close();
|
|
|
+ inputStream.close();
|
|
|
+ connection.disconnect();
|
|
|
+ log.info("file size:{}", file.length());
|
|
|
+
|
|
|
+ // 更新sysFile值。
|
|
|
+ selSysFile.setName(fileName);
|
|
|
+ log.info("fileName:{},key:{}",fileName,selSysFile.getName());
|
|
|
+ // 生成新的key。http://192.168.1.141:8989/cache/files/data/181ba63c8d60452c_2481/output.docx/output.docx?md5=z3NfV5Oj4OhFo6Yo6mk2jw&expires=1692692984&filename=output.docx
|
|
|
+ String objKey = storage.genKeyWithPath(selSysFile.getUserId(), selSysFile.getName(), path);
|
|
|
+ selSysFile.setObjectKey(objKey);
|
|
|
+ // 根据文件key值生成文件url
|
|
|
+ selSysFile.setUrl(storage.getObjectUrl(objKey));
|
|
|
+ // 设置文件所属路径。
|
|
|
+ selSysFile.setPath(path + selSysFile.getName());// 保存编辑后的数据
|
|
|
+ // 获取文件大小
|
|
|
+ selSysFile.setSize(file.length());
|
|
|
+ // 获取文件后缀
|
|
|
+ selSysFile.setSuffix(StringUtils.substringAfterLast(selSysFile.getOriginalName(), "."));
|
|
|
+ // 获取文件类型
|
|
|
+ selSysFile.setType(FileUtils.getFileType(selSysFile.getSuffix()));
|
|
|
+
|
|
|
+ // 存储到minio
|
|
|
+ try {
|
|
|
+ InputStream ins = new FileInputStream(file);
|
|
|
+ selSysFile.setIdentifier(MD5.create().digestHex16(file));
|
|
|
+ storage.putMinioByStream(selSysFile.getObjectKey(), ins, file.length());
|
|
|
+ log.info("---->selSysFile:{}", JSON.toJSONString(selSysFile));
|
|
|
+ baseMapper.updateById(selSysFile);
|
|
|
+ if (file.exists()) {
|
|
|
+ boolean deleteFlag = deleteFile(file.getPath());
|
|
|
+ log.info("file del path:{},deleteFlag:{}",file.getPath(),deleteFlag);
|
|
|
+ boolean delete = file.delete();
|
|
|
+ log.info("delete res:{}",delete);
|
|
|
+ if (delete) {
|
|
|
+ log.info("本地临时写入的文件已删除:{}", filePath);
|
|
|
+ }
|
|
|
+ log.info("本地临时写入的文件删除失败");
|
|
|
+ }
|
|
|
+ } catch (IOException | NoSuchAlgorithmException e) {
|
|
|
+ log.info("上传至minio ex:{}", e);
|
|
|
+ throw new ServiceException(e.getMessage());
|
|
|
+ } catch (InvalidKeyException e) {
|
|
|
+ log.info("上传至minio key无效:{}", e);
|
|
|
+ throw new ServiceException("文件的key无效!");
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ saved = 1;
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ log.info("文件编辑成功");
|
|
|
+ }
|
|
|
+ writer.write("{\"error\":" + saved + "}");
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|