parent
a54cd75469
commit
8ac8cd8487
|
@ -12,7 +12,8 @@ public class FileAttribute {
|
|||
private String suffix;
|
||||
private String name;
|
||||
private String url;
|
||||
private String fileKey;
|
||||
private boolean isCompressFile = false;
|
||||
private String compressFileKey;
|
||||
private String filePassword;
|
||||
private boolean usePasswordCache;
|
||||
private String officePreviewType = ConfigConstants.getOfficePreviewType();
|
||||
|
@ -23,7 +24,7 @@ public class FileAttribute {
|
|||
private String outFilePath;
|
||||
private String originFilePath;
|
||||
private String cacheListName;
|
||||
private boolean isHtml;
|
||||
private boolean isHtmlView = false;
|
||||
|
||||
/**
|
||||
* 代理请求到文件服务器的认证请求头,格式如下:
|
||||
|
@ -50,12 +51,12 @@ public class FileAttribute {
|
|||
this.officePreviewType = officePreviewType;
|
||||
}
|
||||
|
||||
public String getFileKey() {
|
||||
return fileKey;
|
||||
public boolean isCompressFile() {
|
||||
return isCompressFile;
|
||||
}
|
||||
|
||||
public void setFileKey(String fileKey) {
|
||||
this.fileKey = fileKey;
|
||||
public void setCompressFile(boolean compressFile) {
|
||||
isCompressFile = compressFile;
|
||||
}
|
||||
|
||||
public String getFilePassword() {
|
||||
|
@ -98,6 +99,14 @@ public class FileAttribute {
|
|||
this.suffix = suffix;
|
||||
}
|
||||
|
||||
public String getCompressFileKey() {
|
||||
return compressFileKey;
|
||||
}
|
||||
|
||||
public void setCompressFileKey(String compressFileKey) {
|
||||
this.compressFileKey = compressFileKey;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -113,8 +122,8 @@ public class FileAttribute {
|
|||
public String getOriginFilePath() {
|
||||
return originFilePath;
|
||||
}
|
||||
public boolean getIsHtml() {
|
||||
return isHtml;
|
||||
public boolean isHtmlView() {
|
||||
return isHtmlView;
|
||||
}
|
||||
|
||||
public void setCacheName(String cacheName) {
|
||||
|
@ -129,8 +138,8 @@ public class FileAttribute {
|
|||
public void setOriginFilePath(String originFilePath) {
|
||||
this.originFilePath = originFilePath;
|
||||
}
|
||||
public void setIsHtml(boolean isHtml) {
|
||||
this.isHtml = isHtml;
|
||||
public void setHtmlView(boolean isHtmlView) {
|
||||
this.isHtmlView = isHtmlView;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cn.keking.service;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.FileType;
|
||||
import cn.keking.utils.RarUtils;
|
||||
import cn.keking.web.filter.BaseUrlFilter;
|
||||
|
@ -28,23 +29,25 @@ import java.util.List;
|
|||
public class CompressFileReader {
|
||||
private final FileHandlerService fileHandlerService;
|
||||
private static final String fileDir = ConfigConstants.getFileDir();
|
||||
|
||||
public CompressFileReader(FileHandlerService fileHandlerService) {
|
||||
this.fileHandlerService = fileHandlerService;
|
||||
}
|
||||
public String unRar(String filePath, String filePassword, String fileName, String fileKey) throws Exception {
|
||||
|
||||
public String unRar(String filePath, String filePassword, String fileName, FileAttribute fileAttribute) throws Exception {
|
||||
List<String> imgUrls = new ArrayList<>();
|
||||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||
String packagePath = "_"; //防止文件名重复 压缩包统一生成文件添加_符号
|
||||
String folderName = filePath.replace(fileDir, ""); //修复压缩包 多重目录获取路径错误
|
||||
if (!ObjectUtils.isEmpty(fileKey)) { //压缩包文件 直接赋予路径 不予下载
|
||||
folderName = "_decompression"+folderName; //重新修改多重压缩包 生成文件路径
|
||||
String folderName = filePath.replace(fileDir, ""); //修复压缩包 多重目录获取路径错误
|
||||
if (fileAttribute.isCompressFile()) { //压缩包文件 直接赋予路径 不予下载
|
||||
folderName = "_decompression" + folderName; //重新修改多重压缩包 生成文件路径
|
||||
}
|
||||
RandomAccessFile randomAccessFile = null;
|
||||
IInArchive inArchive = null;
|
||||
try {
|
||||
randomAccessFile = new RandomAccessFile(filePath, "r");
|
||||
inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
|
||||
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
|
||||
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
|
||||
final String[] str = {null};
|
||||
for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
|
||||
if (!item.isFolder()) {
|
||||
|
@ -53,16 +56,16 @@ public class CompressFileReader {
|
|||
result = item.extractSlow(data -> {
|
||||
try {
|
||||
str[0] = RarUtils.getUtf8String(item.getPath());
|
||||
if (RarUtils.isMessyCode(str[0])){
|
||||
if (RarUtils.isMessyCode(str[0])) {
|
||||
str[0] = new String(item.getPath().getBytes(StandardCharsets.ISO_8859_1), "gbk");
|
||||
}
|
||||
str[0] = str[0].replace("\\", File.separator); //Linux 下路径错误
|
||||
String str1 = str[0].substring(0, str[0].lastIndexOf(File.separator)+ 1);
|
||||
str[0] = str[0].replace("\\", File.separator); //Linux 下路径错误
|
||||
String str1 = str[0].substring(0, str[0].lastIndexOf(File.separator) + 1);
|
||||
File file = new File(fileDir, finalFolderName + packagePath + File.separator + str1);
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
OutputStream out = new FileOutputStream( fileDir+ finalFolderName + packagePath + File.separator + str[0], true);
|
||||
OutputStream out = new FileOutputStream(fileDir + finalFolderName + packagePath + File.separator + str[0], true);
|
||||
IOUtils.write(data, out);
|
||||
out.close();
|
||||
} catch (Exception e) {
|
||||
|
@ -74,9 +77,9 @@ public class CompressFileReader {
|
|||
if (result == ExtractOperationResult.OK) {
|
||||
FileType type = FileType.typeFromUrl(str[0]);
|
||||
if (type.equals(FileType.PICTURE)) {
|
||||
imgUrls.add(baseUrl +folderName + packagePath +"/" + str[0].replace("\\", "/"));
|
||||
imgUrls.add(baseUrl + folderName + packagePath + "/" + str[0].replace("\\", "/"));
|
||||
}
|
||||
fileHandlerService.putImgCache(fileName+ packagePath, imgUrls);
|
||||
fileHandlerService.putImgCache(fileName + packagePath, imgUrls);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -135,11 +135,11 @@ public class FileHandlerService implements InitializingBean {
|
|||
/**
|
||||
* 获取redis中压缩包内图片文件
|
||||
*
|
||||
* @param fileKey fileKey
|
||||
* @param compressFileKey compressFileKey
|
||||
* @return 图片文件访问url列表
|
||||
*/
|
||||
public List<String> getImgCache(String fileKey) {
|
||||
return cacheService.getImgCache(fileKey);
|
||||
public List<String> getImgCache(String compressFileKey) {
|
||||
return cacheService.getImgCache(compressFileKey);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -153,13 +153,15 @@ public class FileHandlerService implements InitializingBean {
|
|||
}
|
||||
|
||||
/**
|
||||
cad定义线程池
|
||||
* cad定义线程池
|
||||
*/
|
||||
private ExecutorService pool = null;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
pool = Executors.newFixedThreadPool(ConfigConstants.getCadThread());
|
||||
}
|
||||
|
||||
/**
|
||||
* 对转换后的文件进行操作(改变编码方式)
|
||||
*
|
||||
|
@ -168,8 +170,7 @@ public class FileHandlerService implements InitializingBean {
|
|||
public void doActionConvertedFile(String outFilePath) {
|
||||
String charset = EncodingDetects.getJavaEncode(outFilePath);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try (InputStream inputStream = new FileInputStream(outFilePath);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
|
||||
try (InputStream inputStream = new FileInputStream(outFilePath); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
|
||||
String line;
|
||||
while (null != (line = reader.readLine())) {
|
||||
if (line.contains("charset=gb2312")) {
|
||||
|
@ -185,8 +186,7 @@ public class FileHandlerService implements InitializingBean {
|
|||
e.printStackTrace();
|
||||
}
|
||||
// 重新写入文件
|
||||
try (FileOutputStream fos = new FileOutputStream(outFilePath);
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
|
||||
try (FileOutputStream fos = new FileOutputStream(outFilePath); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
|
||||
writer.write(sb.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -195,8 +195,9 @@ public class FileHandlerService implements InitializingBean {
|
|||
|
||||
/**
|
||||
* 获取本地 pdf 转 image 后的 web 访问地址
|
||||
*
|
||||
* @param pdfFilePath pdf文件名
|
||||
* @param index 图片索引
|
||||
* @param index 图片索引
|
||||
* @return 图片访问地址
|
||||
*/
|
||||
private String getPdf2jpgUrl(String pdfFilePath, int index) {
|
||||
|
@ -215,11 +216,11 @@ public class FileHandlerService implements InitializingBean {
|
|||
|
||||
/**
|
||||
* 获取缓存中的 pdf 转换成 jpg 图片集
|
||||
*
|
||||
* @param pdfFilePath pdf文件路径
|
||||
* @param pdfName pdf文件名称
|
||||
* @return 图片访问集合
|
||||
*/
|
||||
private List<String> loadPdf2jpgCache(String pdfFilePath, String pdfName, String fileKey) {
|
||||
private List<String> loadPdf2jpgCache(String pdfFilePath) {
|
||||
List<String> imageUrls = new ArrayList<>();
|
||||
Integer imageCount = this.getPdf2jpgCache(pdfFilePath);
|
||||
if (Objects.isNull(imageCount)) {
|
||||
|
@ -237,18 +238,17 @@ public class FileHandlerService implements InitializingBean {
|
|||
* fileNameFilePath pdf文件路径
|
||||
* pdfFilePath pdf输出文件路径
|
||||
* pdfName pdf文件名称
|
||||
* loadPdf2jpgCache 图片访问集合
|
||||
* loadPdf2jpgCache 图片访问集合
|
||||
*/
|
||||
public List<String> pdf2jpg(String fileNameFilePath,String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception {
|
||||
public List<String> pdf2jpg(String fileNameFilePath, String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception {
|
||||
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
||||
boolean usePasswordCache = fileAttribute.getUsePasswordCache();
|
||||
String filePassword = fileAttribute.getFilePassword();
|
||||
String fileKey = fileAttribute.getFileKey();
|
||||
String pdfPassword = null;
|
||||
PDDocument doc = null;
|
||||
PdfReader pdfReader = null;
|
||||
if (!forceUpdatedCache) {
|
||||
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath, pdfName,fileKey);
|
||||
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath);
|
||||
if (!CollectionUtils.isEmpty(cacheResult)) {
|
||||
return cacheResult;
|
||||
}
|
||||
|
@ -259,7 +259,7 @@ public class FileHandlerService implements InitializingBean {
|
|||
if (!pdfFile.exists()) {
|
||||
return null;
|
||||
}
|
||||
doc = PDDocument.load(pdfFile,filePassword);
|
||||
doc = PDDocument.load(pdfFile, filePassword);
|
||||
doc.setResourceCache(new NotResourceCache());
|
||||
int pageCount = doc.getNumberOfPages();
|
||||
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
||||
|
@ -278,8 +278,8 @@ public class FileHandlerService implements InitializingBean {
|
|||
imageUrls.add(imageUrl);
|
||||
}
|
||||
try {
|
||||
if (!ObjectUtils.isEmpty(filePassword)){ //获取到密码 判断是否是加密文件
|
||||
pdfReader = new PdfReader(fileNameFilePath); //读取PDF文件 通过异常获取该文件是否有密码字符
|
||||
if (!ObjectUtils.isEmpty(filePassword)) { //获取到密码 判断是否是加密文件
|
||||
pdfReader = new PdfReader(fileNameFilePath); //读取PDF文件 通过异常获取该文件是否有密码字符
|
||||
}
|
||||
} catch (Exception e) { //获取异常方法 判断是否有加密字符串
|
||||
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
||||
|
@ -304,7 +304,7 @@ public class FileHandlerService implements InitializingBean {
|
|||
this.addPdf2jpgCache(pdfFilePath, pageCount);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (!e.getMessage().contains(PDF_PASSWORD_MSG) ) {
|
||||
if (!e.getMessage().contains(PDF_PASSWORD_MSG)) {
|
||||
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
|
||||
}
|
||||
throw new Exception(e);
|
||||
|
@ -323,9 +323,9 @@ public class FileHandlerService implements InitializingBean {
|
|||
* @param outputFilePath pdf输出文件路径
|
||||
* @return 转换是否成功
|
||||
*/
|
||||
public String cadToPdf(String inputFilePath, String outputFilePath ,String cadPreviewType ,String fileKey) throws Exception {
|
||||
public String cadToPdf(String inputFilePath, String outputFilePath, String cadPreviewType, FileAttribute fileAttribute) throws Exception {
|
||||
final InterruptionTokenSource source = new InterruptionTokenSource();//CAD延时
|
||||
if (!ObjectUtils.isEmpty(fileKey)) { //判断 是压缩包的创建新的目录
|
||||
if (fileAttribute.isCompressFile()) { //判断 是压缩包的创建新的目录
|
||||
int index = outputFilePath.lastIndexOf("/"); //截取最后一个斜杠的前面的内容
|
||||
String folder = outputFilePath.substring(0, index);
|
||||
File path = new File(folder);
|
||||
|
@ -348,7 +348,7 @@ public class FileHandlerService implements InitializingBean {
|
|||
cadRasterizationOptions.setDrawType(1);
|
||||
SvgOptions SvgOptions = null;
|
||||
PdfOptions pdfOptions = null;
|
||||
TiffOptions TiffOptions = null;
|
||||
TiffOptions TiffOptions = null;
|
||||
switch (cadPreviewType) { //新增格式方法
|
||||
case "svg":
|
||||
SvgOptions = new SvgOptions();
|
||||
|
@ -408,17 +408,16 @@ public class FileHandlerService implements InitializingBean {
|
|||
}
|
||||
return "true";
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param str 原字符串(待截取原串)
|
||||
* @param str 原字符串(待截取原串)
|
||||
* @param posStr 指定字符串
|
||||
* @return 截取截取指定字符串之后的数据
|
||||
*/
|
||||
public static String getSubString(String str, String posStr){
|
||||
public static String getSubString(String str, String posStr) {
|
||||
return str.substring(str.indexOf(posStr) + posStr.length());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件属性
|
||||
*
|
||||
|
@ -429,16 +428,13 @@ public class FileHandlerService implements InitializingBean {
|
|||
FileAttribute attribute = new FileAttribute();
|
||||
String suffix;
|
||||
FileType type;
|
||||
String fileName; //原始文件名
|
||||
String cacheName; //缓存文件名
|
||||
String cachePrefixName; //缓存文件名统一去除文件后缀名
|
||||
String cacheListName; //缓存列表文件名称
|
||||
String originFileName; //原始文件名
|
||||
String outFilePath; //生成文件的路径
|
||||
String originFilePath; //原始文件路径
|
||||
String fullFileName = WebUtils.getUrlParameterReg(url, "fullfilename");
|
||||
String fileKey = WebUtils.getUrlParameterReg(url, "kkCompressfileKey"); //压缩包指定特殊符号
|
||||
String compressFileKey = WebUtils.getUrlParameterReg(url, "kkCompressfileKey"); //压缩包指定特殊符号
|
||||
if (StringUtils.hasText(fullFileName)) {
|
||||
fileName = fullFileName;
|
||||
originFileName = fullFileName;
|
||||
type = FileType.typeFromFileName(fullFileName);
|
||||
suffix = KkFileUtils.suffixFromFileName(fullFileName);
|
||||
// 移除fullfilename参数
|
||||
|
@ -448,61 +444,44 @@ public class FileHandlerService implements InitializingBean {
|
|||
url = url.replace("fullfilename=" + fullFileName, "");
|
||||
}
|
||||
} else {
|
||||
fileName = WebUtils.getFileNameFromURL(url);
|
||||
originFileName = WebUtils.getFileNameFromURL(url);
|
||||
type = FileType.typeFromUrl(url);
|
||||
suffix = WebUtils.suffixFromUrl(url);
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(fileKey)) { //判断是否使用特定压缩包符号
|
||||
boolean isCompressFile = !ObjectUtils.isEmpty(compressFileKey);
|
||||
if (isCompressFile) { //判断是否使用特定压缩包符号
|
||||
try {
|
||||
// http://127.0.0.1:8012/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_
|
||||
// http://127.0.0.1:8012/preview/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_ 获取路径就会错误 需要下面的方法
|
||||
URL urll = new URL(url);
|
||||
String _Path = urll.getPath(); //获取url路径
|
||||
String urlStrr = getSubString(_Path, fileKey); //反代情况下添加前缀,只获取有压缩包字符的路径
|
||||
fileName = fileKey + urlStrr.trim(); //拼接完整路径
|
||||
String urlStrr = getSubString(_Path, compressFileKey); //反代情况下添加前缀,只获取有压缩包字符的路径
|
||||
originFileName = compressFileKey + urlStrr.trim(); //拼接完整路径
|
||||
attribute.setSkipDownLoad(true);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
url = WebUtils.encodeUrlFileName(url);
|
||||
if(UrlEncoderUtils.hasUrlEncoded(fileName)){ //判断文件名是否转义
|
||||
if (UrlEncoderUtils.hasUrlEncoded(originFileName)) { //判断文件名是否转义
|
||||
try {
|
||||
fileName = URLDecoder.decode(fileName, uriEncoding).replaceAll("\\+", "%20");
|
||||
originFileName = URLDecoder.decode(originFileName, uriEncoding).replaceAll("\\+", "%20");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
fileName = KkFileUtils.htmlEscape(fileName); //文件名处理
|
||||
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx") || suffix.equalsIgnoreCase("csv") || suffix.equalsIgnoreCase("xlsm") || suffix.equalsIgnoreCase("xlt") || suffix.equalsIgnoreCase("xltm") || suffix.equalsIgnoreCase("et") || suffix.equalsIgnoreCase("ett") || suffix.equalsIgnoreCase("xlam");
|
||||
cachePrefixName = fileName.substring(0, fileName.lastIndexOf(".") ) + suffix+"."; //这里统一文件名处理 下面更具类型 各自添加后缀
|
||||
if(type.equals(FileType.OFFICE)){
|
||||
cacheName = cachePrefixName +(isHtml ? "html" : "pdf"); //生成文件添加类型后缀 防止同名文件
|
||||
}else if(type.equals(FileType.PDF)){
|
||||
cacheName = fileName;
|
||||
}else if(type.equals(FileType.MEDIACONVERT)){
|
||||
cacheName = cachePrefixName +"mp4" ;
|
||||
}else if(type.equals(FileType.CAD)){
|
||||
String cadPreviewType = ConfigConstants.getCadPreviewType();
|
||||
cacheName = cachePrefixName + cadPreviewType ; //生成文件添加类型后缀 防止同名文件
|
||||
}else if(type.equals(FileType.COMPRESS)){
|
||||
cacheName = fileName;
|
||||
}else if(type.equals(FileType.TIFF)){
|
||||
cacheName = cachePrefixName + ConfigConstants.getTifPreviewType();
|
||||
}else {
|
||||
cacheName = fileName;
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(fileKey)) { //判断是否使用特定压缩包符号
|
||||
cacheName = "_decompression"+ cacheName;
|
||||
}
|
||||
outFilePath = fileDir + cacheName;
|
||||
originFilePath = fileDir + fileName;
|
||||
cacheListName = cachePrefixName+"ListName"; //文件列表缓存文件名
|
||||
originFileName = KkFileUtils.htmlEscape(originFileName); //文件名处理
|
||||
boolean isHtmlView = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx") || suffix.equalsIgnoreCase("csv") || suffix.equalsIgnoreCase("xlsm") || suffix.equalsIgnoreCase("xlt") || suffix.equalsIgnoreCase("xltm") || suffix.equalsIgnoreCase("et") || suffix.equalsIgnoreCase("ett") || suffix.equalsIgnoreCase("xlam");
|
||||
String cacheFilePrefixName = originFileName.substring(0, originFileName.lastIndexOf(".")) + suffix + "."; //这里统一文件名处理 下面更具类型 各自添加后缀
|
||||
String cacheFileName = this.getCacheFileName(type, originFileName, cacheFilePrefixName, isHtmlView, isCompressFile);
|
||||
outFilePath = fileDir + cacheFileName;
|
||||
originFilePath = fileDir + originFileName;
|
||||
String cacheListName = cacheFilePrefixName + "ListName"; //文件列表缓存文件名
|
||||
attribute.setType(type);
|
||||
attribute.setName(fileName);
|
||||
attribute.setCacheName(cacheName);
|
||||
attribute.setName(originFileName);
|
||||
attribute.setCacheName(cacheFileName);
|
||||
attribute.setCacheListName(cacheListName);
|
||||
attribute.setIsHtml(isHtml);
|
||||
attribute.setHtmlView(isHtmlView);
|
||||
attribute.setOutFilePath(outFilePath);
|
||||
attribute.setOriginFilePath(originFilePath);
|
||||
attribute.setSuffix(suffix);
|
||||
|
@ -510,12 +489,13 @@ public class FileHandlerService implements InitializingBean {
|
|||
if (req != null) {
|
||||
String officePreviewType = req.getParameter("officePreviewType");
|
||||
String forceUpdatedCache = req.getParameter("forceUpdatedCache");
|
||||
String usePasswordCache =req.getParameter("usePasswordCache");
|
||||
String usePasswordCache = req.getParameter("usePasswordCache");
|
||||
if (StringUtils.hasText(officePreviewType)) {
|
||||
attribute.setOfficePreviewType(officePreviewType);
|
||||
}
|
||||
if (StringUtils.hasText(fileKey)) {
|
||||
attribute.setFileKey(fileKey);
|
||||
if (StringUtils.hasText(compressFileKey)) {
|
||||
attribute.setCompressFile(isCompressFile);
|
||||
attribute.setCompressFileKey(compressFileKey);
|
||||
}
|
||||
if ("true".equalsIgnoreCase(forceUpdatedCache)) {
|
||||
attribute.setForceUpdatedCache(true);
|
||||
|
@ -533,7 +513,7 @@ public class FileHandlerService implements InitializingBean {
|
|||
if ("true".equalsIgnoreCase(usePasswordCache)) {
|
||||
attribute.setUsePasswordCache(true);
|
||||
}
|
||||
String kkProxyAuthorization = req.getHeader( "kk-proxy-authorization");
|
||||
String kkProxyAuthorization = req.getHeader("kk-proxy-authorization");
|
||||
attribute.setKkProxyAuthorization(kkProxyAuthorization);
|
||||
|
||||
}
|
||||
|
@ -541,6 +521,35 @@ public class FileHandlerService implements InitializingBean {
|
|||
return attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存的文件名
|
||||
*
|
||||
* @return 文件名
|
||||
*/
|
||||
private String getCacheFileName(FileType type, String originFileName, String cacheFilePrefixName, boolean isHtmlView, boolean isCompressFile) {
|
||||
String cacheFileName;
|
||||
if (type.equals(FileType.OFFICE)) {
|
||||
cacheFileName = cacheFilePrefixName + (isHtmlView ? "html" : "pdf"); //生成文件添加类型后缀 防止同名文件
|
||||
} else if (type.equals(FileType.PDF)) {
|
||||
cacheFileName = originFileName;
|
||||
} else if (type.equals(FileType.MEDIACONVERT)) {
|
||||
cacheFileName = cacheFilePrefixName + "mp4";
|
||||
} else if (type.equals(FileType.CAD)) {
|
||||
String cadPreviewType = ConfigConstants.getCadPreviewType();
|
||||
cacheFileName = cacheFilePrefixName + cadPreviewType; //生成文件添加类型后缀 防止同名文件
|
||||
} else if (type.equals(FileType.COMPRESS)) {
|
||||
cacheFileName = originFileName;
|
||||
} else if (type.equals(FileType.TIFF)) {
|
||||
cacheFileName = cacheFilePrefixName + ConfigConstants.getTifPreviewType();
|
||||
} else {
|
||||
cacheFileName = originFileName;
|
||||
}
|
||||
if (isCompressFile) { //判断是否使用特定压缩包符号
|
||||
cacheFileName = "_decompression" + cacheFileName;
|
||||
}
|
||||
return cacheFileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 已转换过的视频文件集合(缓存)
|
||||
*/
|
||||
|
|
|
@ -38,12 +38,11 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||
// 预览Type,参数传了就取参数的,没传取系统默认
|
||||
String officePreviewType = fileAttribute.getOfficePreviewType() == null ? ConfigConstants.getOfficePreviewType() : fileAttribute.getOfficePreviewType();
|
||||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
||||
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
||||
String fileName = fileAttribute.getName();
|
||||
String cadPreviewType = ConfigConstants.getCadPreviewType();
|
||||
String cacheName = fileAttribute.getCacheName();
|
||||
String cacheName = fileAttribute.getCacheName();
|
||||
String outFilePath = fileAttribute.getOutFilePath();
|
||||
String fileKey = fileAttribute.getFileKey(); //判断是否压缩包
|
||||
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
||||
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(cacheName) || !ConfigConstants.isCacheEnabled()) {
|
||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
||||
|
@ -54,15 +53,15 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||
String imageUrls = null;
|
||||
if (StringUtils.hasText(outFilePath)) {
|
||||
try {
|
||||
imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath,cadPreviewType,fileKey);
|
||||
imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath, cadPreviewType, fileAttribute);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (imageUrls == null ) {
|
||||
if (imageUrls == null) {
|
||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
|
||||
}
|
||||
//是否保留CAD源文件
|
||||
if(ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
|
||||
if (!fileAttribute.isCompressFile() && ConfigConstants.getDeleteSourceFile()) {
|
||||
KkFileUtils.deleteFileByPath(filePath);
|
||||
}
|
||||
if (ConfigConstants.isCacheEnabled()) {
|
||||
|
@ -71,15 +70,15 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
}
|
||||
}
|
||||
if("tif".equalsIgnoreCase(cadPreviewType)){
|
||||
if ("tif".equalsIgnoreCase(cadPreviewType)) {
|
||||
model.addAttribute("currentUrl", cacheName);
|
||||
return TIFF_FILE_PREVIEW_PAGE;
|
||||
}else if("svg".equalsIgnoreCase(cadPreviewType)){
|
||||
} else if ("svg".equalsIgnoreCase(cadPreviewType)) {
|
||||
model.addAttribute("currentUrl", cacheName);
|
||||
return SVG_FILE_PREVIEW_PAGE;
|
||||
}
|
||||
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
||||
return getPreviewType(model, fileAttribute, officePreviewType, cacheName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE,otherFilePreview);
|
||||
return getPreviewType(model, fileAttribute, officePreviewType, cacheName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE, otherFilePreview);
|
||||
}
|
||||
model.addAttribute("pdfUrl", cacheName);
|
||||
return PDF_FILE_PREVIEW_PAGE;
|
||||
|
|
|
@ -39,7 +39,6 @@ public class CompressFilePreviewImpl implements FilePreview {
|
|||
String fileName=fileAttribute.getName();
|
||||
String filePassword = fileAttribute.getFilePassword();
|
||||
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
||||
String fileKey = fileAttribute.getFileKey(); //判断是否压缩包
|
||||
String fileTree = null;
|
||||
// 判断文件名是否存在(redis缓存读取)
|
||||
if (forceUpdatedCache || !StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
|
||||
|
@ -49,7 +48,7 @@ public class CompressFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
String filePath = response.getContent();
|
||||
try {
|
||||
fileTree = compressFileReader.unRar(filePath, filePassword,fileName,fileKey);
|
||||
fileTree = compressFileReader.unRar(filePath, filePassword,fileName, fileAttribute);
|
||||
} catch (Exception e) {
|
||||
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
||||
for (Throwable throwable : throwableArray) {
|
||||
|
@ -63,7 +62,7 @@ public class CompressFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
if (!ObjectUtils.isEmpty(fileTree)) {
|
||||
//是否保留压缩包源文件
|
||||
if (ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
|
||||
if (!fileAttribute.isCompressFile() && ConfigConstants.getDeleteSourceFile()) {
|
||||
KkFileUtils.deleteFileByPath(filePath);
|
||||
}
|
||||
if (ConfigConstants.isCacheEnabled()) {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
package cn.keking.service.impl;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.FileType;
|
||||
|
@ -28,28 +29,29 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||
private final FileHandlerService fileHandlerService;
|
||||
private final OtherFilePreviewImpl otherFilePreview;
|
||||
private static final String mp4 = "mp4";
|
||||
|
||||
public MediaFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
||||
this.fileHandlerService = fileHandlerService;
|
||||
this.otherFilePreview = otherFilePreview;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
String fileName = fileAttribute.getName();
|
||||
String suffix = fileAttribute.getSuffix();
|
||||
String cacheName = fileAttribute.getCacheName();
|
||||
String cacheName = fileAttribute.getCacheName();
|
||||
String outFilePath = fileAttribute.getOutFilePath();
|
||||
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
||||
String fileKey = fileAttribute.getFileKey();
|
||||
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
||||
FileType type = fileAttribute.getType();
|
||||
String[] mediaTypesConvert = FileType.MEDIA_CONVERT_TYPES; //获取支持的转换格式
|
||||
boolean mediaTypes = false;
|
||||
for(String temp : mediaTypesConvert){
|
||||
boolean mediaTypes = false;
|
||||
for (String temp : mediaTypesConvert) {
|
||||
if (suffix.equals(temp)) {
|
||||
mediaTypes = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!url.toLowerCase().startsWith("http") || checkNeedConvert(mediaTypes)){ //不是http协议的 // 开启转换方式并是支持转换格式的
|
||||
if (!url.toLowerCase().startsWith("http") || checkNeedConvert(mediaTypes)) { //不是http协议的 // 开启转换方式并是支持转换格式的
|
||||
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(cacheName) || !ConfigConstants.isCacheEnabled()) { //查询是否开启缓存
|
||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
||||
if (response.isFailure()) {
|
||||
|
@ -58,15 +60,15 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||
String filePath = response.getContent();
|
||||
String convertedUrl = null;
|
||||
try {
|
||||
if(mediaTypes){
|
||||
convertedUrl=convertToMp4(filePath,outFilePath,fileKey);
|
||||
}else {
|
||||
convertedUrl =outFilePath; //其他协议的 不需要转换方式的文件 直接输出
|
||||
if (mediaTypes) {
|
||||
convertedUrl = convertToMp4(filePath, outFilePath, fileAttribute);
|
||||
} else {
|
||||
convertedUrl = outFilePath; //其他协议的 不需要转换方式的文件 直接输出
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (convertedUrl == null ) {
|
||||
if (convertedUrl == null) {
|
||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "视频转换异常,请联系管理员");
|
||||
}
|
||||
if (ConfigConstants.isCacheEnabled()) {
|
||||
|
@ -74,39 +76,42 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||
fileHandlerService.addConvertedFile(cacheName, fileHandlerService.getRelativePath(outFilePath));
|
||||
}
|
||||
model.addAttribute("mediaUrl", fileHandlerService.getRelativePath(outFilePath));
|
||||
}else{
|
||||
} else {
|
||||
model.addAttribute("mediaUrl", fileHandlerService.listConvertedFiles().get(cacheName));
|
||||
}
|
||||
return MEDIA_FILE_PREVIEW_PAGE;
|
||||
}
|
||||
if(type.equals(FileType.MEDIA)){ // 支持输出 只限默认格式
|
||||
if (type.equals(FileType.MEDIA)) { // 支持输出 只限默认格式
|
||||
model.addAttribute("mediaUrl", url);
|
||||
return MEDIA_FILE_PREVIEW_PAGE;
|
||||
}
|
||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "系统还不支持该格式文件的在线预览");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查视频文件转换是否已开启,以及当前文件是否需要转换
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private boolean checkNeedConvert(boolean mediaTypes) {
|
||||
//1.检查开关是否开启
|
||||
if("true".equals(ConfigConstants.getMediaConvertDisable())){
|
||||
if ("true".equals(ConfigConstants.getMediaConvertDisable())) {
|
||||
return mediaTypes;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
private static String convertToMp4(String filePath,String outFilePath,String fileKey)throws Exception {
|
||||
|
||||
private static String convertToMp4(String filePath, String outFilePath, FileAttribute fileAttribute) throws Exception {
|
||||
FFmpegFrameGrabber frameGrabber = FFmpegFrameGrabber.createDefault(filePath);
|
||||
Frame captured_frame;
|
||||
FFmpegFrameRecorder recorder = null;
|
||||
try {
|
||||
File desFile=new File(outFilePath);
|
||||
File desFile = new File(outFilePath);
|
||||
//判断一下防止重复转换
|
||||
if(desFile.exists()){
|
||||
if (desFile.exists()) {
|
||||
return outFilePath;
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(fileKey)) { //判断 是压缩包的创建新的目录
|
||||
if (fileAttribute.isCompressFile()) { //判断 是压缩包的创建新的目录
|
||||
int index = outFilePath.lastIndexOf("/"); //截取最后一个斜杠的前面的内容
|
||||
String folder = outFilePath.substring(0, index);
|
||||
File path = new File(folder);
|
||||
|
@ -137,7 +142,7 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||
while (true) {
|
||||
captured_frame = frameGrabber.grabFrame();
|
||||
if (captured_frame == null) {
|
||||
System.out.println("转码完成:"+filePath);
|
||||
System.out.println("转码完成:" + filePath);
|
||||
break;
|
||||
}
|
||||
recorder.record(captured_frame);
|
||||
|
@ -145,7 +150,7 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}finally {
|
||||
} finally {
|
||||
if (recorder != null) { //关闭
|
||||
recorder.stop();
|
||||
recorder.close();
|
||||
|
|
|
@ -52,10 +52,9 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||
String fileName = fileAttribute.getName(); //获取文件原始名称
|
||||
String filePassword = fileAttribute.getFilePassword(); //获取密码
|
||||
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache(); //是否启用强制更新命令
|
||||
boolean isHtml =fileAttribute.getIsHtml(); //xlsx 转换成html
|
||||
boolean isHtmlView = fileAttribute.isHtmlView(); //xlsx 转换成html
|
||||
String cacheName = fileAttribute.getCacheName(); //转换后的文件名
|
||||
String outFilePath = fileAttribute.getOutFilePath(); //转换后生成文件的路径
|
||||
String fileKey = fileAttribute.getFileKey(); //判断是否压缩包
|
||||
if (!officePreviewType.equalsIgnoreCase("html")) {
|
||||
if (ConfigConstants.getOfficeTypeWeb() .equalsIgnoreCase("web")) {
|
||||
if (suffix.equalsIgnoreCase("xlsx")) {
|
||||
|
@ -93,12 +92,12 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "抱歉,该文件版本不兼容,文件版本错误。");
|
||||
}
|
||||
if (isHtml) {
|
||||
if (isHtmlView) {
|
||||
// 对转换后的文件进行操作(改变编码方式)
|
||||
fileHandlerService.doActionConvertedFile(outFilePath);
|
||||
}
|
||||
//是否保留OFFICE源文件
|
||||
if (ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
|
||||
if (!fileAttribute.isCompressFile() && ConfigConstants.getDeleteSourceFile()) {
|
||||
KkFileUtils.deleteFileByPath(filePath);
|
||||
}
|
||||
if (userToken || !isPwdProtectedOffice) {
|
||||
|
@ -109,11 +108,11 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
|
||||
}
|
||||
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
||||
if (!isHtmlView && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
||||
return getPreviewType(model, fileAttribute, officePreviewType, cacheName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE, otherFilePreview);
|
||||
}
|
||||
model.addAttribute("pdfUrl", cacheName);
|
||||
return isHtml ? EXEL_FILE_PREVIEW_PAGE : PDF_FILE_PREVIEW_PAGE;
|
||||
return isHtmlView ? EXEL_FILE_PREVIEW_PAGE : PDF_FILE_PREVIEW_PAGE;
|
||||
}
|
||||
|
||||
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String pdfName, String outFilePath, FileHandlerService fileHandlerService, String officePreviewTypeImage, OtherFilePreviewImpl otherFilePreview) {
|
||||
|
|
|
@ -29,8 +29,8 @@ public class PictureFilePreviewImpl extends CommonPreviewImpl {
|
|||
url= KkFileUtils.htmlEscape(url);
|
||||
List<String> imgUrls = new ArrayList<>();
|
||||
imgUrls.add(url);
|
||||
String fileKey = fileAttribute.getFileKey();
|
||||
List<String> zipImgUrls = fileHandlerService.getImgCache(fileKey);
|
||||
String compressFileKey = fileAttribute.getCompressFileKey();
|
||||
List<String> zipImgUrls = fileHandlerService.getImgCache(compressFileKey);
|
||||
if (!CollectionUtils.isEmpty(zipImgUrls)) {
|
||||
imgUrls.addAll(zipImgUrls);
|
||||
}
|
||||
|
|
|
@ -36,7 +36,6 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||
String tifPreviewType = ConfigConstants.getTifPreviewType();
|
||||
String cacheName = fileAttribute.getCacheName();
|
||||
String outFilePath = fileAttribute.getOutFilePath();
|
||||
String fileKey = fileAttribute.getFileKey(); //判断是否压缩包
|
||||
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
||||
if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
|
||||
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(cacheName) || !ConfigConstants.isCacheEnabled()) {
|
||||
|
@ -58,7 +57,7 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
}
|
||||
//是否保留TIFF源文件
|
||||
if (ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
|
||||
if (!fileAttribute.isCompressFile() && ConfigConstants.getDeleteSourceFile()) {
|
||||
// KkFileUtils.deleteFileByPath(filePath);
|
||||
}
|
||||
if (ConfigConstants.isCacheEnabled()) {
|
||||
|
@ -82,7 +81,7 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
}
|
||||
//是否保留源文件,转换失败保留源文件,转换成功删除源文件
|
||||
if(ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
|
||||
if(!fileAttribute.isCompressFile() && ConfigConstants.getDeleteSourceFile()) {
|
||||
KkFileUtils.deleteFileByPath(filePath);
|
||||
}
|
||||
if (ConfigConstants.isCacheEnabled()) {
|
||||
|
|
|
@ -50,7 +50,6 @@ public class DownloadUtils {
|
|||
* @return 本地文件绝对路径
|
||||
*/
|
||||
public static ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
|
||||
String fileKey = fileAttribute.getFileKey();
|
||||
// 忽略ssl证书
|
||||
String urlStr = null;
|
||||
try {
|
||||
|
@ -75,7 +74,7 @@ public class DownloadUtils {
|
|||
response.setMsg("下载失败:不支持的类型!" + urlStr);
|
||||
return response;
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(fileKey)) { //压缩包文件 直接赋予路径 不予下载
|
||||
if (fileAttribute.isCompressFile()) { //压缩包文件 直接赋予路径 不予下载
|
||||
response.setContent(fileDir + fileName);
|
||||
response.setMsg(fileName);
|
||||
return response;
|
||||
|
|
|
@ -38,7 +38,6 @@ public class AttributeSetFilter implements Filter {
|
|||
request.setAttribute("pdfDownloadDisable", ConfigConstants.getPdfDownloadDisable());
|
||||
request.setAttribute("pdfBookmarkDisable", ConfigConstants.getPdfBookmarkDisable());
|
||||
request.setAttribute("pdfDisableEditing", ConfigConstants.getPdfDisableEditing());
|
||||
request.setAttribute("fileKey", httpRequest.getParameter("fileKey"));
|
||||
request.setAttribute("switchDisabled", ConfigConstants.getOfficePreviewSwitchDisabled());
|
||||
request.setAttribute("fileUploadDisable", ConfigConstants.getFileUploadDisable());
|
||||
request.setAttribute("beian", ConfigConstants.getBeian());
|
||||
|
|
Loading…
Reference in New Issue