Commit a77e4da9 by huangfusuper

资源拉取压缩

parent 245f6ebe
package com.byit.node.impl; package com.byit.node.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.ZipUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.byit.dto.executor.ScriptDto; import com.byit.dto.executor.ScriptDto;
import com.byit.dto.executor.ScriptParamAndPlaceholderDto; import com.byit.dto.executor.ScriptParamAndPlaceholderDto;
import com.byit.enums.PlaceholderEnum; import com.byit.enums.PlaceholderEnum;
import com.byit.enums.SourceType;
import com.byit.exceptions.ExecutorException; import com.byit.exceptions.ExecutorException;
import com.byit.filesystem.FileSystem; import com.byit.filesystem.FileSystem;
import com.byit.job.utils.DateUtil; import com.byit.job.utils.DateUtil;
...@@ -15,13 +20,9 @@ import org.apache.commons.lang3.StringUtils; ...@@ -15,13 +20,9 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.File; import java.io.*;
import java.io.FileOutputStream; import java.nio.charset.Charset;
import java.io.IOException; import java.util.*;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
/** /**
* 2.命令处理节点,主要做命令的参数替换操作 * 2.命令处理节点,主要做命令的参数替换操作
...@@ -53,12 +54,15 @@ public class CommandAndScriptProcessingMachine implements ProcessingMachine { ...@@ -53,12 +54,15 @@ public class CommandAndScriptProcessingMachine implements ProcessingMachine {
} }
//判断是否需要拉取文件服务器的文件,包含biz_file的条件下会对这个字段进行替换 //判断是否需要拉取文件服务器的文件,包含biz_file的条件下会对这个字段进行替换
if (command.contains(PlaceholderEnum.BIZ_SCRIPT_FILE.getName())) { if (command.contains(PlaceholderEnum.BIZ_SCRIPT_FILE.getName())) {
String scriptPath = byteArrayToFile(scriptDto.getRemotePath(),paramAndPlaceholderDto); //这里需要解压压缩包 然后将
log.debug("-----远程地址的文件被拉取到:[{}]------------",scriptPath); String scriptDirPath = byteArrayToSource(scriptDto.getRemotePath());
log.debug("-----远程地址的文件被拉取到:[{}]------------",scriptDirPath);
//初始化命令信息 //初始化命令信息
command = PlaceholderUtils.initCommand(command,scriptPath); command = PlaceholderUtils.initCommand(command,scriptDirPath);
log.debug("-----执行命令初始化完成,命令为:[{}]------------",command); log.debug("-----执行命令初始化完成,命令为:[{}]------------",command);
resourceKeywordReplacement(scriptDirPath,paramAndPlaceholderDto);
scriptDto.setCommand(command); scriptDto.setCommand(command);
scriptDto.setTempDir(scriptDirPath);
} }
if (paramAndPlaceholderDto != null) { if (paramAndPlaceholderDto != null) {
//执行命令参数的替换 //执行命令参数的替换
...@@ -78,6 +82,81 @@ public class CommandAndScriptProcessingMachine implements ProcessingMachine { ...@@ -78,6 +82,81 @@ public class CommandAndScriptProcessingMachine implements ProcessingMachine {
} }
/** /**
* 资源脚本替换
* @param dirPath
* @return
*/
private void resourceKeywordReplacement(String dirPath, ScriptParamAndPlaceholderDto scriptParamAndPlaceholderDto){
File baseFile = new File(dirPath);
List<File> files = FileUtil.loopFiles(baseFile);
if(scriptParamAndPlaceholderDto != null){
files.forEach(file ->{
String fileName = file.getName();
if(SourceType.match(fileName)){
FileInputStream fileInputStream = IoUtil.toStream(file);
byte[] bytes = IoUtil.readBytes(fileInputStream);
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
bytes = PlaceholderUtils.resolvePlaceholders(bytes,scriptParamAndPlaceholderDto.getPlaceholder());
FileUtil.writeBytes(bytes,file);
}
});
}
}
/**
* 获取内部所有文件
* @param baseFile 文件
* @param files 所有文件
*/
public void getFile(File baseFile, List<File> files){
if (baseFile.isDirectory()) {
File[] fileList = baseFile.listFiles();
for (File file : fileList) {
if(file.isDirectory()){
getFile(file,files);
}else{
files.add(file);
}
}
}
}
/**
* 拉取文件,解压文件
* @param remotePath 文件路径
* @return 加压后的文件夹路径
*/
private String byteArrayToSource(String remotePath){
//创建目录
File rootPathMkdir = new File(rootScriptPath, DateUtil.dateFormat(new Date(),"yyyyMMdd"));
if(!rootPathMkdir.exists()){
rootPathMkdir.mkdirs();
}
try {
//下载脚本压缩包
byte[] scriptByteArray = fileSystem.downloaderFile(remotePath);
//获取文件元信息
Map<String, String> fileMate = fileSystem.getFileMate(remotePath);
String fileName = fileMate.get("filename");
fileName = UUID.randomUUID().toString().replace("-","")+fileName;
File file = new File(rootPathMkdir,fileName);
FileWriter.create(file).write(scriptByteArray, 0, scriptByteArray.length);
//解压后的文件夹路径
File unzip = ZipUtil.unzip(file);
//删除这个压缩包
file.deleteOnExit();
return unzip.getPath();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 将脚本字节转换成文件 * 将脚本字节转换成文件
* @return 生成文件的本地路径 * @return 生成文件的本地路径
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment