|
@@ -1,12 +1,24 @@
|
|
|
package com.caimei.service.data.impl;
|
|
|
|
|
|
import com.caimei.mapper.cmMapper.FileMapper;
|
|
|
+import com.caimei.model.ResponseJson;
|
|
|
import com.caimei.model.vo.FileTreeVo;
|
|
|
import com.caimei.service.data.DatabaseService;
|
|
|
+import com.caimei.utils.FileZipUtils;
|
|
|
+import com.caimei.utils.OSSUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
/**
|
|
|
* @author zzj
|
|
@@ -18,10 +30,101 @@ public class DatabaseServiceImpl implements DatabaseService {
|
|
|
@Resource
|
|
|
private FileMapper fileMapper;
|
|
|
|
|
|
+ @Value("${spring.profiles.active}")
|
|
|
+ private String active;
|
|
|
+
|
|
|
@Override
|
|
|
- public FileTreeVo getFileById(Integer fileId) {
|
|
|
- FileTreeVo fileTree=fileMapper.findFileById(fileId);
|
|
|
+ public ResponseJson<FileTreeVo> getFileById(Integer fileId) {
|
|
|
+ FileTreeVo fileTree = fileMapper.findFileById(fileId);
|
|
|
fileTree.setChildList(fileMapper.findFileChild(fileId));
|
|
|
- return fileTree;
|
|
|
+ return ResponseJson.success(fileTree);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson createPackage(Integer fileId, String packageName) {
|
|
|
+ fileMapper.creatPackage(fileId, packageName);
|
|
|
+ return ResponseJson.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void downLoadZip(Integer fileId, String packageName, HttpServletResponse response) {
|
|
|
+ //1.查下一级所有文件
|
|
|
+ List<FileTreeVo> sonFiles = fileMapper.findSonFiles(fileId);
|
|
|
+ //2.有package则在服务器固定路径new file.mkdir
|
|
|
+ StringBuilder filePath = new StringBuilder("/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/");
|
|
|
+ filePath = filePath.append(packageName + "/");
|
|
|
+ if ("dev".equals(active)) {
|
|
|
+ filePath = new StringBuilder("D:\\caimei-workSpace\\file\\");
|
|
|
+ filePath.append(packageName);
|
|
|
+ }
|
|
|
+ String zipPath = filePath.toString();
|
|
|
+ File fil = new File(filePath.toString());
|
|
|
+ fil.mkdirs();
|
|
|
+ recursion(fileId, filePath);
|
|
|
+ //压缩
|
|
|
+ FileZipUtils.compress(zipPath, "", true);
|
|
|
+ String s = zipPath + ".zip";
|
|
|
+ String fileName=packageName+".zip";
|
|
|
+ File file = new File(s);
|
|
|
+ try {
|
|
|
+ if(file.exists()){
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
+ ServletOutputStream outputStream = response.getOutputStream();
|
|
|
+ // 设置下载文件的mineType,告诉浏览器下载文件类型
|
|
|
+ // int i = fileName.lastIndexOf(".");
|
|
|
+ // String substring = fileName.substring(i + 1, filePath.length());
|
|
|
+ // response.setContentType(substring);
|
|
|
+ // 设置一个响应头,无论是否被浏览器解析,都下载
|
|
|
+ response.setHeader("Content-disposition", "attachment; filename=" + fileName);
|
|
|
+ byte[] bytes = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = fileInputStream.read(bytes)) != -1) {
|
|
|
+ outputStream.write(bytes, 0, len);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //todo 一级包会重复,recursion中filepath会重复
|
|
|
+ private void recursion(Integer fileId, StringBuilder filePath) {
|
|
|
+ //1.如果当前目录存在普通文件,下载当前目录下的文件到文件夹中
|
|
|
+ List<FileTreeVo> commonFile = existsPackageOrCommonFile(fileId, 2);
|
|
|
+ if (null != commonFile && commonFile.size() > 0) {
|
|
|
+ commonFile.forEach(c -> {
|
|
|
+ String fileName = "";
|
|
|
+ if ("dev".equals(active)) {
|
|
|
+ fileName = filePath + "\\" + c.getFileName();
|
|
|
+ } else {
|
|
|
+ fileName = filePath + "/" + c.getFileName();
|
|
|
+ }
|
|
|
+ OSSUtils.downFileByFilePath("authFile/", c.getOssName(), fileName);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //2.如果存在文件夹循环文件夹,递归本方法
|
|
|
+ List<FileTreeVo> packages = existsPackageOrCommonFile(fileId, 1);
|
|
|
+ if (null != packages && packages.size() > 0) {
|
|
|
+ for (FileTreeVo aPackage : packages) {
|
|
|
+ if ("dev".equals(active)) {
|
|
|
+ filePath.append("\\" + aPackage.getFileName());
|
|
|
+ } else {
|
|
|
+ filePath.append(aPackage.getFileName() + "/");
|
|
|
+ }
|
|
|
+ File fil = new File(filePath.toString());
|
|
|
+ fil.mkdirs();
|
|
|
+ recursion(aPackage.getId(), filePath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * selectFor 1文件夹package,2普通文件
|
|
|
+ *
|
|
|
+ * @param fileId
|
|
|
+ * @param selectFor
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<FileTreeVo> existsPackageOrCommonFile(Integer fileId, Integer selectFor) {
|
|
|
+ return fileMapper.findSonPackage(fileId, selectFor);
|
|
|
}
|
|
|
}
|