mirror of
https://github.com/kekingcn/kkFileView.git
synced 2026-04-20 01:08:37 +00:00
* 修复office下载方法中 错误 * 更新OFD解析效果,修复禁止trace请求无效问题,其他压缩包修复 * 修复压缩包 缓存BUG * 限制某些特殊文件上传 * 修复OFFICE文件密码检查关闭流 上传文件关闭流 检查PDF文件是否存在 * 特殊符号的支持 Co-authored-by: gaoxiongzaq <admin@cxcp.com>
87 lines
4.1 KiB
Java
87 lines
4.1 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 cn.keking.web.filter.BaseUrlFilter;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.ui.Model;
|
||
|
||
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 FILE_DIR = ConfigConstants.getFileDir();
|
||
|
||
public PdfFilePreviewImpl(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 officePreviewType = fileAttribute.getOfficePreviewType();
|
||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
||
String outFilePath = FILE_DIR + pdfName;
|
||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
|
||
//当文件不存在时,就去下载
|
||
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
||
if (response.isFailure()) {
|
||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
||
}
|
||
outFilePath = response.getContent();
|
||
if (ConfigConstants.isCacheEnabled()) {
|
||
// 加入缓存
|
||
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
||
}
|
||
}
|
||
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl);
|
||
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 {
|
||
pdfName = URLEncoder.encode(pdfName).replaceAll("\\+", "%20");
|
||
model.addAttribute("pdfUrl", pdfName);
|
||
}
|
||
} else {
|
||
model.addAttribute("pdfUrl", url);
|
||
}
|
||
}
|
||
return PDF_FILE_PREVIEW_PAGE;
|
||
}
|
||
}
|