mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-04-05 17:37:49 +08:00

2.修复PDF 带密码缓存问题 新增PDF带密码缓存方法 userToken 3.精简OFFICE 转换代码 4.精简TIF转换代码 新增TIF转换图片缓存 修复tif错误文件不自动释放内存 等待其他修复 5.修复下载方法错 特殊符号下载错误 6.调整文件名 统一方法在FileHandlerService 7.新增判断文件名是否被URL转义
100 lines
5.0 KiB
Java
100 lines
5.0 KiB
Java
package cn.keking.service.impl;
|
||
|
||
import cn.keking.config.ConfigConstants;
|
||
import cn.keking.model.FileAttribute;
|
||
import cn.keking.model.ReturnResponse;
|
||
import cn.keking.service.FilePreview;
|
||
import cn.keking.utils.DownloadUtils;
|
||
import cn.keking.service.FileHandlerService;
|
||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||
import org.apache.poi.EncryptedDocumentException;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.ui.Model;
|
||
|
||
import java.io.IOException;
|
||
import java.net.URLEncoder;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* Created by kl on 2018/1/17.
|
||
* Content :处理pdf文件
|
||
*/
|
||
@Service
|
||
public class PdfFilePreviewImpl implements FilePreview {
|
||
|
||
private final FileHandlerService fileHandlerService;
|
||
private final OtherFilePreviewImpl otherFilePreview;
|
||
private static final String PDF_PASSWORD_MSG = "password";
|
||
public PdfFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
||
this.fileHandlerService = fileHandlerService;
|
||
this.otherFilePreview = otherFilePreview;
|
||
}
|
||
@Override
|
||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||
String pdfName = fileAttribute.getName(); //获取原始文件名
|
||
String officePreviewType = fileAttribute.getOfficePreviewType(); //转换类型
|
||
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache(); //是否启用强制更新命令
|
||
String outFilePath = fileAttribute.getoutFilePath(); //生成的文件路径
|
||
String fileNameFilePath = fileAttribute.getfileNameFilePath(); //原始文件路径
|
||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
|
||
//当文件不存在时,就去下载
|
||
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
|
||
if (response.isFailure()) {
|
||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
||
}
|
||
fileNameFilePath = response.getContent();
|
||
if (ConfigConstants.isCacheEnabled()) {
|
||
// 加入缓存
|
||
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(fileNameFilePath));
|
||
}
|
||
}
|
||
List<String> imageUrls;
|
||
try {
|
||
imageUrls = fileHandlerService.pdf2jpg(fileNameFilePath,outFilePath, pdfName, fileAttribute);
|
||
} catch (Exception e) {
|
||
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
||
for (Throwable throwable : throwableArray) {
|
||
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
||
if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
|
||
model.addAttribute("needFilePassword", true);
|
||
return EXEL_FILE_PREVIEW_PAGE;
|
||
}
|
||
}
|
||
}
|
||
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
|
||
}
|
||
if (imageUrls == null || imageUrls.size() < 1) {
|
||
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
|
||
}
|
||
model.addAttribute("imgUrls", imageUrls);
|
||
model.addAttribute("currentUrl", imageUrls.get(0));
|
||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
||
return OFFICE_PICTURE_FILE_PREVIEW_PAGE;
|
||
} else {
|
||
return PICTURE_FILE_PREVIEW_PAGE;
|
||
}
|
||
} else {
|
||
// 不是http开头,浏览器不能直接访问,需下载到本地
|
||
if (url != null && !url.toLowerCase().startsWith("http")) {
|
||
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
|
||
if (response.isFailure()) {
|
||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
||
}
|
||
model.addAttribute("pdfUrl", fileHandlerService.getRelativePath(response.getContent()));
|
||
if (ConfigConstants.isCacheEnabled()) {
|
||
// 加入缓存
|
||
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
||
}
|
||
} else {
|
||
model.addAttribute("pdfUrl", pdfName);
|
||
}
|
||
} else {
|
||
model.addAttribute("pdfUrl", url);
|
||
}
|
||
}
|
||
return PDF_FILE_PREVIEW_PAGE;
|
||
}
|
||
}
|