2020-12-26 16:44:05 +08:00
|
|
|
|
package cn.keking.service;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2019-04-11 14:45:22 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.model.FileAttribute;
|
|
|
|
|
import cn.keking.model.FileType;
|
2019-04-08 17:50:13 +08:00
|
|
|
|
import cn.keking.service.cache.CacheService;
|
2023-02-18 15:27:35 +08:00
|
|
|
|
import cn.keking.service.cache.NotResourceCache;
|
2023-03-15 10:40:32 +08:00
|
|
|
|
import cn.keking.utils.EncodingDetects;
|
2020-12-28 18:21:35 +08:00
|
|
|
|
import cn.keking.utils.KkFileUtils;
|
2020-12-27 01:43:50 +08:00
|
|
|
|
import cn.keking.utils.WebUtils;
|
2023-04-27 16:27:04 +08:00
|
|
|
|
import cn.keking.web.filter.BaseUrlFilter;
|
2023-01-17 22:56:51 +08:00
|
|
|
|
import com.aspose.cad.CodePages;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
import com.aspose.cad.Color;
|
2023-01-17 22:56:51 +08:00
|
|
|
|
import com.aspose.cad.Image;
|
|
|
|
|
import com.aspose.cad.LoadOptions;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
import com.aspose.cad.imageoptions.CadRasterizationOptions;
|
|
|
|
|
import com.aspose.cad.imageoptions.PdfOptions;
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
|
import org.apache.pdfbox.rendering.ImageType;
|
|
|
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
|
|
|
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import org.springframework.stereotype.Component;
|
2023-04-24 10:08:18 +08:00
|
|
|
|
import org.springframework.util.CollectionUtils;
|
2019-06-17 14:21:16 +08:00
|
|
|
|
import org.springframework.util.StringUtils;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
2020-12-25 22:47:30 +08:00
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
import java.awt.image.BufferedImage;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
import java.io.*;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
import java.net.URLEncoder;
|
2020-05-15 18:09:19 +08:00
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
import java.util.ArrayList;
|
2020-12-26 19:13:50 +08:00
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
2023-04-24 10:08:18 +08:00
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.stream.IntStream;
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author yudian-it
|
|
|
|
|
* @date 2017/11/13
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
2020-12-26 19:13:50 +08:00
|
|
|
|
public class FileHandlerService {
|
2019-10-25 13:59:29 +08:00
|
|
|
|
|
2020-12-27 14:06:06 +08:00
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(FileHandlerService.class);
|
2020-05-19 11:04:53 +08:00
|
|
|
|
private final String fileDir = ConfigConstants.getFileDir();
|
2023-04-24 10:08:18 +08:00
|
|
|
|
private final static String pdf2jpg_image_format = ".jpg";
|
2020-05-15 18:09:19 +08:00
|
|
|
|
private final CacheService cacheService;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-12-27 14:06:06 +08:00
|
|
|
|
@Value("${server.tomcat.uri-encoding:UTF-8}")
|
|
|
|
|
private String uriEncoding;
|
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
|
public FileHandlerService(CacheService cacheService) {
|
2020-05-15 18:09:19 +08:00
|
|
|
|
this.cacheService = cacheService;
|
|
|
|
|
}
|
2019-04-08 17:50:13 +08:00
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 已转换过的文件集合(缓存)
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public Map<String, String> listConvertedFiles() {
|
2019-04-08 17:50:13 +08:00
|
|
|
|
return cacheService.getPDFCache();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 已转换过的文件,根据文件名获取
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getConvertedFile(String key) {
|
2019-04-08 17:50:13 +08:00
|
|
|
|
return cacheService.getPDFCache(key);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 18:39:58 +08:00
|
|
|
|
/**
|
|
|
|
|
* @param key pdf本地路径
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 已将pdf转换成图片的图片本地相对路径
|
2019-04-25 18:39:58 +08:00
|
|
|
|
*/
|
2023-04-24 10:08:18 +08:00
|
|
|
|
public Integer getPdf2jpgCache(String key) {
|
2019-04-25 18:39:58 +08:00
|
|
|
|
return cacheService.getPdfImageCache(key);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* 从路径中获取文件负
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
|
|
|
|
* @param path 类似这种:C:\Users\yudian-it\Downloads
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @return 文件名
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getFileNameFromPath(String path) {
|
|
|
|
|
return path.substring(path.lastIndexOf(File.separator) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取相对路径
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param absolutePath 绝对路径
|
|
|
|
|
* @return 相对路径
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public String getRelativePath(String absolutePath) {
|
|
|
|
|
return absolutePath.substring(fileDir.length());
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
/**
|
|
|
|
|
* 添加转换后PDF缓存
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param fileName pdf文件名
|
2020-12-25 22:47:30 +08:00
|
|
|
|
* @param value 缓存相对路径
|
2020-05-15 18:09:19 +08:00
|
|
|
|
*/
|
2020-12-25 22:47:30 +08:00
|
|
|
|
public void addConvertedFile(String fileName, String value) {
|
2019-04-08 17:50:13 +08:00
|
|
|
|
cacheService.putPDFCache(fileName, value);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 18:39:58 +08:00
|
|
|
|
/**
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* 添加转换后图片组缓存
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param pdfFilePath pdf文件绝对路径
|
2020-12-25 22:47:30 +08:00
|
|
|
|
* @param num 图片张数
|
2019-04-25 18:39:58 +08:00
|
|
|
|
*/
|
2023-04-24 10:08:18 +08:00
|
|
|
|
public void addPdf2jpgCache(String pdfFilePath, int num) {
|
2019-04-25 18:39:58 +08:00
|
|
|
|
cacheService.putPdfImageCache(pdfFilePath, num);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取redis中压缩包内图片文件
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param fileKey fileKey
|
|
|
|
|
* @return 图片文件访问url列表
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
2020-12-25 22:47:30 +08:00
|
|
|
|
public List<String> getImgCache(String fileKey) {
|
2019-04-08 17:50:13 +08:00
|
|
|
|
return cacheService.getImgCache(fileKey);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置redis中压缩包内图片文件
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param fileKey fileKey
|
2020-12-25 22:47:30 +08:00
|
|
|
|
* @param imgs 图片文件访问url列表
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
2020-12-25 22:47:30 +08:00
|
|
|
|
public void putImgCache(String fileKey, List<String> imgs) {
|
2019-04-08 17:50:13 +08:00
|
|
|
|
cacheService.putImgCache(fileKey, imgs);
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|
2020-12-25 22:47:30 +08:00
|
|
|
|
|
2018-01-17 14:10:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* 对转换后的文件进行操作(改变编码方式)
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param outFilePath 文件绝对路径
|
2018-01-17 14:10:40 +08:00
|
|
|
|
*/
|
|
|
|
|
public void doActionConvertedFile(String outFilePath) {
|
2023-03-15 10:40:32 +08:00
|
|
|
|
String charset = EncodingDetects.getJavaEncode(outFilePath);
|
2020-05-15 18:09:19 +08:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2018-01-17 14:10:40 +08:00
|
|
|
|
try (InputStream inputStream = new FileInputStream(outFilePath);
|
2023-03-15 10:34:52 +08:00
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
|
2018-01-17 14:10:40 +08:00
|
|
|
|
String line;
|
2020-12-25 22:47:30 +08:00
|
|
|
|
while (null != (line = reader.readLine())) {
|
2018-01-17 14:10:40 +08:00
|
|
|
|
if (line.contains("charset=gb2312")) {
|
|
|
|
|
line = line.replace("charset=gb2312", "charset=utf-8");
|
|
|
|
|
}
|
|
|
|
|
sb.append(line);
|
|
|
|
|
}
|
|
|
|
|
// 添加sheet控制头
|
2022-12-13 15:53:19 +08:00
|
|
|
|
sb.append("<script src=\"js/jquery-3.6.1.min.js\" type=\"text/javascript\"></script>");
|
2018-01-17 14:10:40 +08:00
|
|
|
|
sb.append("<script src=\"js/excel.header.js\" type=\"text/javascript\"></script>");
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
|
sb.append("<link rel=\"stylesheet\" href=\"bootstrap/css/xlsx.css\">");
|
2018-01-17 14:10:40 +08:00
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
// 重新写入文件
|
2020-12-25 22:47:30 +08:00
|
|
|
|
try (FileOutputStream fos = new FileOutputStream(outFilePath);
|
|
|
|
|
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
|
2018-01-17 14:10:40 +08:00
|
|
|
|
writer.write(sb.toString());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-25 22:47:30 +08:00
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
|
/**
|
2023-04-24 10:08:18 +08:00
|
|
|
|
* 获取本地 pdf 转 image 后的 web 访问地址
|
|
|
|
|
* @param pdfName pdf文件名
|
|
|
|
|
* @param index 图片索引
|
|
|
|
|
* @return 图片访问地址
|
2018-01-17 17:51:53 +08:00
|
|
|
|
*/
|
2023-04-24 10:08:18 +08:00
|
|
|
|
private String getPdf2jpgUrl(String pdfName, int index) {
|
2023-04-25 10:59:41 +08:00
|
|
|
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
2020-12-27 14:06:06 +08:00
|
|
|
|
String pdfFolder = pdfName.substring(0, pdfName.length() - 4);
|
|
|
|
|
String urlPrefix;
|
2023-04-24 10:08:18 +08:00
|
|
|
|
|
2020-12-27 14:06:06 +08:00
|
|
|
|
try {
|
2021-01-22 16:05:34 +08:00
|
|
|
|
urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20");
|
2020-12-27 14:06:06 +08:00
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
logger.error("UnsupportedEncodingException", e);
|
|
|
|
|
urlPrefix = baseUrl + pdfFolder;
|
|
|
|
|
}
|
2023-04-24 10:08:18 +08:00
|
|
|
|
return urlPrefix + "/" + index + pdf2jpg_image_format;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取缓存中的 pdf 转换成 jpg 图片集
|
|
|
|
|
* @param pdfFilePath pdf文件路径
|
|
|
|
|
* @param pdfName pdf文件名称
|
|
|
|
|
* @return 图片访问集合
|
|
|
|
|
*/
|
|
|
|
|
private List<String> loadPdf2jpgCache(String pdfFilePath, String pdfName) {
|
|
|
|
|
List<String> imageUrls = new ArrayList<>();
|
|
|
|
|
Integer imageCount = this.getPdf2jpgCache(pdfFilePath);
|
|
|
|
|
if (Objects.isNull(imageCount)) {
|
2020-12-27 14:06:06 +08:00
|
|
|
|
return imageUrls;
|
|
|
|
|
}
|
2023-04-24 10:08:18 +08:00
|
|
|
|
IntStream.range(0, imageCount).forEach(i -> {
|
|
|
|
|
String imageUrl = this.getPdf2jpgUrl(pdfName, i);
|
|
|
|
|
imageUrls.add(imageUrl);
|
|
|
|
|
});
|
|
|
|
|
return imageUrls;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* pdf文件转换成jpg图片集
|
|
|
|
|
*
|
|
|
|
|
* @param pdfFilePath pdf文件路径
|
|
|
|
|
* @param pdfName pdf文件名称
|
|
|
|
|
* @return 图片访问集合
|
|
|
|
|
*/
|
|
|
|
|
public List<String> pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) {
|
|
|
|
|
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
|
|
|
|
if (!forceUpdatedCache) {
|
|
|
|
|
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath, pdfName);
|
|
|
|
|
if (!CollectionUtils.isEmpty(cacheResult)) {
|
|
|
|
|
return cacheResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
List<String> imageUrls = new ArrayList<>();
|
2020-12-27 14:06:06 +08:00
|
|
|
|
try {
|
|
|
|
|
File pdfFile = new File(pdfFilePath);
|
2023-01-11 13:26:18 +08:00
|
|
|
|
if (!pdfFile.exists()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-23 13:54:21 +08:00
|
|
|
|
PDDocument doc = PDDocument.load(pdfFile);
|
2023-02-18 15:27:35 +08:00
|
|
|
|
doc.setResourceCache(new NotResourceCache());
|
2020-12-27 14:06:06 +08:00
|
|
|
|
int pageCount = doc.getNumberOfPages();
|
|
|
|
|
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
|
|
|
|
|
|
|
|
|
int index = pdfFilePath.lastIndexOf(".");
|
|
|
|
|
String folder = pdfFilePath.substring(0, index);
|
|
|
|
|
|
|
|
|
|
File path = new File(folder);
|
|
|
|
|
if (!path.exists() && !path.mkdirs()) {
|
|
|
|
|
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
|
|
|
|
|
}
|
|
|
|
|
String imageFilePath;
|
|
|
|
|
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
|
2023-04-24 10:08:18 +08:00
|
|
|
|
imageFilePath = folder + File.separator + pageIndex + pdf2jpg_image_format;
|
2023-04-27 16:27:04 +08:00
|
|
|
|
BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getpdf2JpgDpi(), ImageType.RGB);
|
|
|
|
|
ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getpdf2JpgDpi());
|
2023-04-24 10:08:18 +08:00
|
|
|
|
String imageUrl = this.getPdf2jpgUrl(pdfName, pageIndex);
|
|
|
|
|
imageUrls.add(imageUrl);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
}
|
|
|
|
|
doc.close();
|
2023-04-24 10:08:18 +08:00
|
|
|
|
this.addPdf2jpgCache(pdfFilePath, pageCount);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
|
|
|
|
|
}
|
|
|
|
|
return imageUrls;
|
2019-06-17 14:21:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-27 14:06:06 +08:00
|
|
|
|
/**
|
|
|
|
|
* cad文件转pdf
|
2023-04-24 10:08:18 +08:00
|
|
|
|
*
|
|
|
|
|
* @param inputFilePath cad文件路径
|
2020-12-27 14:06:06 +08:00
|
|
|
|
* @param outputFilePath pdf输出文件路径
|
|
|
|
|
* @return 转换是否成功
|
|
|
|
|
*/
|
2023-04-24 10:08:18 +08:00
|
|
|
|
public boolean cadToPdf(String inputFilePath, String outputFilePath) {
|
2023-01-17 22:56:51 +08:00
|
|
|
|
File outputFile = new File(outputFilePath);
|
|
|
|
|
LoadOptions opts = new LoadOptions();
|
|
|
|
|
opts.setSpecifiedEncoding(CodePages.SimpChinese);
|
|
|
|
|
com.aspose.cad.Image cadImage = Image.load(inputFilePath, opts);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
|
|
|
|
|
cadRasterizationOptions.setBackgroundColor(Color.getWhite());
|
2023-01-17 22:56:51 +08:00
|
|
|
|
cadRasterizationOptions.setPageWidth(1400);
|
|
|
|
|
cadRasterizationOptions.setPageHeight(650);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
|
2023-04-24 10:08:18 +08:00
|
|
|
|
cadRasterizationOptions.setNoScaling(false);
|
2023-01-17 22:56:51 +08:00
|
|
|
|
cadRasterizationOptions.setDrawType(1);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
PdfOptions pdfOptions = new PdfOptions();
|
|
|
|
|
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
|
|
|
|
|
OutputStream stream;
|
|
|
|
|
try {
|
|
|
|
|
stream = new FileOutputStream(outputFile);
|
|
|
|
|
cadImage.save(stream, pdfOptions);
|
2023-01-17 22:56:51 +08:00
|
|
|
|
stream.close();
|
2020-12-27 14:06:06 +08:00
|
|
|
|
cadImage.close();
|
|
|
|
|
return true;
|
2023-01-17 22:56:51 +08:00
|
|
|
|
} catch (IOException e) {
|
2020-12-27 14:06:06 +08:00
|
|
|
|
logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e);
|
2023-04-24 10:08:18 +08:00
|
|
|
|
} finally {
|
|
|
|
|
if (cadImage != null) { //关闭
|
2023-01-17 22:56:51 +08:00
|
|
|
|
cadImage.close();
|
|
|
|
|
}
|
2020-12-27 14:06:06 +08:00
|
|
|
|
}
|
2023-01-17 22:56:51 +08:00
|
|
|
|
return false;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2023-04-27 16:27:04 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param str 原字符串(待截取原串)
|
|
|
|
|
* @param posStr 指定字符串
|
|
|
|
|
* @return 截取截取指定字符串之后的数据
|
|
|
|
|
*/
|
|
|
|
|
public static String getSubString(String str, String posStr){
|
|
|
|
|
return str.substring(str.indexOf(posStr) + posStr.length());
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-05-15 18:09:19 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取文件属性
|
2020-12-25 22:47:30 +08:00
|
|
|
|
*
|
2020-05-15 18:09:19 +08:00
|
|
|
|
* @param url url
|
|
|
|
|
* @return 文件属性
|
|
|
|
|
*/
|
2020-12-25 22:47:30 +08:00
|
|
|
|
public FileAttribute getFileAttribute(String url, HttpServletRequest req) {
|
|
|
|
|
FileAttribute attribute = new FileAttribute();
|
2019-06-17 14:21:16 +08:00
|
|
|
|
String suffix;
|
2020-12-26 00:56:48 +08:00
|
|
|
|
FileType type;
|
|
|
|
|
String fileName;
|
2020-12-27 01:43:50 +08:00
|
|
|
|
String fullFileName = WebUtils.getUrlParameterReg(url, "fullfilename");
|
2020-12-25 16:59:11 +08:00
|
|
|
|
if (StringUtils.hasText(fullFileName)) {
|
2020-12-26 00:56:48 +08:00
|
|
|
|
fileName = fullFileName;
|
2020-12-27 14:06:06 +08:00
|
|
|
|
type = FileType.typeFromFileName(fullFileName);
|
2020-12-28 18:21:35 +08:00
|
|
|
|
suffix = KkFileUtils.suffixFromFileName(fullFileName);
|
2019-06-17 14:21:16 +08:00
|
|
|
|
} else {
|
2020-12-27 14:06:06 +08:00
|
|
|
|
fileName = WebUtils.getFileNameFromURL(url);
|
|
|
|
|
type = FileType.typeFromUrl(url);
|
|
|
|
|
suffix = WebUtils.suffixFromUrl(url);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2021-12-17 09:23:32 +08:00
|
|
|
|
if (url.contains("?fileKey=")) {
|
2023-04-27 11:06:18 +08:00
|
|
|
|
String[] strs = url.split("="); //处理解压后有反代情况下 文件的路径
|
|
|
|
|
String urlStrr = getSubString(url, strs[1]);
|
|
|
|
|
urlStrr = urlStrr.substring(0,urlStrr.lastIndexOf("?"));
|
|
|
|
|
fileName = strs[1] + urlStrr.trim();
|
2021-12-17 09:23:32 +08:00
|
|
|
|
attribute.setSkipDownLoad(true);
|
|
|
|
|
}
|
2023-04-03 15:06:46 +08:00
|
|
|
|
// System.out.println(fileName);
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
|
url = WebUtils.encodeUrlFileName(url);
|
2023-04-24 10:08:18 +08:00
|
|
|
|
fileName = KkFileUtils.htmlEscape(fileName); //文件名处理
|
2020-12-26 00:56:48 +08:00
|
|
|
|
attribute.setType(type);
|
|
|
|
|
attribute.setName(fileName);
|
2020-12-25 22:47:30 +08:00
|
|
|
|
attribute.setSuffix(suffix);
|
2020-12-26 00:56:48 +08:00
|
|
|
|
attribute.setUrl(url);
|
2020-12-25 22:47:30 +08:00
|
|
|
|
if (req != null) {
|
2020-12-26 00:56:48 +08:00
|
|
|
|
String officePreviewType = req.getParameter("officePreviewType");
|
2023-04-23 12:02:27 +08:00
|
|
|
|
String forceUpdatedCache = req.getParameter("forceUpdatedCache");
|
2023-04-24 10:08:18 +08:00
|
|
|
|
String fileKey = WebUtils.getUrlParameterReg(url, "fileKey");
|
2020-12-26 19:13:50 +08:00
|
|
|
|
if (StringUtils.hasText(officePreviewType)) {
|
2020-12-26 00:56:48 +08:00
|
|
|
|
attribute.setOfficePreviewType(officePreviewType);
|
|
|
|
|
}
|
2020-12-26 19:13:50 +08:00
|
|
|
|
if (StringUtils.hasText(fileKey)) {
|
2020-12-26 01:52:52 +08:00
|
|
|
|
attribute.setFileKey(fileKey);
|
|
|
|
|
}
|
2023-04-24 10:08:18 +08:00
|
|
|
|
if ("true".equalsIgnoreCase(forceUpdatedCache)) {
|
2023-04-23 11:22:29 +08:00
|
|
|
|
attribute.setforceUpdatedCache(true);
|
|
|
|
|
}
|
2021-12-09 19:10:23 +08:00
|
|
|
|
|
2021-12-10 15:36:25 +08:00
|
|
|
|
String tifPreviewType = req.getParameter("tifPreviewType");
|
|
|
|
|
if (StringUtils.hasText(tifPreviewType)) {
|
|
|
|
|
attribute.setTifPreviewType(tifPreviewType);
|
2021-12-09 19:10:23 +08:00
|
|
|
|
}
|
2022-07-21 11:19:46 +08:00
|
|
|
|
|
|
|
|
|
String filePassword = req.getParameter("filePassword");
|
|
|
|
|
if (StringUtils.hasText(filePassword)) {
|
|
|
|
|
attribute.setFilePassword(filePassword);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String userToken = req.getParameter("userToken");
|
|
|
|
|
if (StringUtils.hasText(userToken)) {
|
|
|
|
|
attribute.setUserToken(userToken);
|
|
|
|
|
}
|
2020-12-25 22:47:30 +08:00
|
|
|
|
}
|
2022-07-21 11:19:46 +08:00
|
|
|
|
|
2020-12-25 22:47:30 +08:00
|
|
|
|
return attribute;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2021-04-18 12:40:59 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return 已转换过的视频文件集合(缓存)
|
|
|
|
|
*/
|
|
|
|
|
public Map<String, String> listConvertedMedias() {
|
|
|
|
|
return cacheService.getMediaConvertCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加转换后的视频文件缓存
|
2023-04-24 10:08:18 +08:00
|
|
|
|
*
|
2021-04-18 12:40:59 +08:00
|
|
|
|
* @param fileName
|
|
|
|
|
* @param value
|
|
|
|
|
*/
|
|
|
|
|
public void addConvertedMedias(String fileName, String value) {
|
|
|
|
|
cacheService.putMediaConvertCache(fileName, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return 已转换视频文件缓存,根据文件名获取
|
|
|
|
|
*/
|
|
|
|
|
public String getConvertedMedias(String key) {
|
|
|
|
|
return cacheService.getMediaConvertCache(key);
|
|
|
|
|
}
|
2018-01-17 14:10:40 +08:00
|
|
|
|
}
|