Browse Source

1、抽象整理FileType的获取逻辑

2、合理设置预览消费队列的线程数
encoding
chenkailing 4 years ago committed by kl
parent
commit
0f4f1d580b
  1. 1
      jodconverter-web/src/main/config/freemarker_implicit.ftl
  2. 41
      jodconverter-web/src/main/java/cn/keking/model/FileType.java
  3. 27
      jodconverter-web/src/main/java/cn/keking/service/FileConvertQueueTask.java
  4. 76
      jodconverter-web/src/main/java/cn/keking/service/FilePreviewCommonService.java
  5. 12
      jodconverter-web/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java
  6. 14
      jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java
  7. 10
      jodconverter-web/src/main/java/cn/keking/service/impl/MediaFilePreviewImpl.java
  8. 14
      jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java
  9. 18
      jodconverter-web/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java
  10. 12
      jodconverter-web/src/main/java/cn/keking/service/impl/PictureFilePreviewImpl.java
  11. 13
      jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java
  12. 11
      jodconverter-web/src/main/java/cn/keking/utils/PdfUtils.java
  13. 27
      jodconverter-web/src/main/java/cn/keking/utils/ZipReader.java
  14. 19
      jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java
  15. 2
      jodconverter-web/src/main/resources/web/picture.ftl
  16. 4
      jodconverter-web/src/main/resources/web/txt.ftl

1
jodconverter-web/src/main/config/freemarker_implicit.ftl

@ -1,5 +1,6 @@
[#ftl]
[#-- @implicitly included --]
[#-- @ftlvariable name="imgUrls" type="String" --]
[#-- @ftlvariable name="textData" type="java.lang.String" --]
[#-- @ftlvariable name="xmlContent" type="java.lang.String" --]
[#-- @ftlvariable name="textContent" type="java.lang.String" --]

41
jodconverter-web/src/main/java/cn/keking/model/FileType.java

@ -1,5 +1,10 @@
package cn.keking.model;
import cn.keking.config.ConfigConstants;
import java.util.HashMap;
import java.util.Map;
/**
* Created by kl on 2018/1/17.
* Content :文件类型文本office压缩包等等
@ -16,11 +21,43 @@ public enum FileType {
xml("xmlFilePreviewImpl"),
cad("cadFilePreviewImpl");
private static final String[] OFFICE_TYPES = {"docx", "doc", "xls", "xlsx", "ppt", "pptx"};
private static final String[] PICTURE_TYPES = {"jpg", "jpeg", "png", "gif", "bmp", "ico", "RAW"};
private static final String[] ARCHIVE_TYPES = {"rar", "zip", "jar", "7-zip", "tar", "gzip", "7z"};
private static final String[] SIMTEXT_TYPES = ConfigConstants.getSimText();
private static final String[] MEDIA_TYPES = ConfigConstants.getMedia();
private static final Map<String, FileType> FILE_TYPE_MAPPER = new HashMap<>();
static {
for (String office : OFFICE_TYPES) {
FILE_TYPE_MAPPER.put(office, FileType.office);
}
for (String picture : PICTURE_TYPES) {
FILE_TYPE_MAPPER.put(picture, FileType.picture);
}
for (String archive : ARCHIVE_TYPES) {
FILE_TYPE_MAPPER.put(archive, FileType.compress);
}
for (String text : SIMTEXT_TYPES) {
FILE_TYPE_MAPPER.put(text, FileType.simText);
}
for (String media : MEDIA_TYPES) {
FILE_TYPE_MAPPER.put(media, FileType.media);
}
FILE_TYPE_MAPPER.put("md", FileType.markdown);
FILE_TYPE_MAPPER.put("xml", FileType.xml);
FILE_TYPE_MAPPER.put("pdf", FileType.pdf);
FILE_TYPE_MAPPER.put("dwg", FileType.cad);
}
public static FileType to(String fileType){
return FILE_TYPE_MAPPER.getOrDefault(fileType,other);
}
private final String instanceName;
FileType(String instanceName){
this.instanceName=instanceName;
FileType(String instanceName) {
this.instanceName = instanceName;
}
public String getInstanceName() {

27
jodconverter-web/src/main/java/cn/keking/service/FileConvertQueueTask.java

@ -3,7 +3,6 @@ package cn.keking.service;
import cn.keking.model.FileAttribute;
import cn.keking.model.FileType;
import cn.keking.service.cache.CacheService;
import cn.keking.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@ -20,44 +19,36 @@ import java.util.concurrent.Executors;
public class FileConvertQueueTask {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final FilePreviewFactory previewFactory;
private final CacheService cacheService;
private final FilePreviewCommonService filePreviewCommonService;
private final FileUtils fileUtils;
public FileConvertQueueTask(FilePreviewFactory previewFactory,
CacheService cacheService,
FileUtils fileUtils) {
public FileConvertQueueTask(FilePreviewFactory previewFactory, CacheService cacheService, FilePreviewCommonService filePreviewCommonService) {
this.previewFactory = previewFactory;
this.cacheService = cacheService;
this.fileUtils=fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
}
@PostConstruct
public void startTask(){
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new ConvertTask(previewFactory, cacheService, fileUtils));
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(new ConvertTask(previewFactory, cacheService, filePreviewCommonService));
logger.info("队列处理文件转换任务启动完成 ");
}
static class ConvertTask implements Runnable {
private final Logger logger = LoggerFactory.getLogger(ConvertTask.class);
private final FilePreviewFactory previewFactory;
private final CacheService cacheService;
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
public ConvertTask(FilePreviewFactory previewFactory,
CacheService cacheService,
FileUtils fileUtils) {
FilePreviewCommonService filePreviewCommonService) {
this.previewFactory = previewFactory;
this.cacheService = cacheService;
this.fileUtils=fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
}
@Override
@ -67,7 +58,7 @@ public class FileConvertQueueTask {
try {
url = cacheService.takeQueueTask();
if(url != null){
FileAttribute fileAttribute = fileUtils.getFileAttribute(url,null);
FileAttribute fileAttribute = filePreviewCommonService.getFileAttribute(url,null);
FileType fileType = fileAttribute.getType();
logger.info("正在处理预览转换任务,url:{},预览类型:{}", url, fileType);
if(fileType.equals(FileType.compress) || fileType.equals(FileType.office) || fileType.equals(FileType.cad)) {

76
jodconverter-web/src/main/java/cn/keking/utils/FileUtils.java → jodconverter-web/src/main/java/cn/keking/service/FilePreviewCommonService.java

@ -1,4 +1,4 @@
package cn.keking.utils;
package cn.keking.service;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
@ -18,14 +18,14 @@ import java.util.*;
* @date 2017/11/13
*/
@Component
public class FileUtils {
public class FilePreviewCommonService {
private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
private final String fileDir = ConfigConstants.getFileDir();
private final CacheService cacheService;
public FileUtils(CacheService cacheService) {
public FilePreviewCommonService(CacheService cacheService) {
this.cacheService = cacheService;
}
@ -60,41 +60,12 @@ public class FileUtils {
public FileType typeFromUrl(String url) {
String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
return typeFromFileName(fileName);
return this.typeFromFileName(fileName);
}
private FileType typeFromFileName(String fileName) {
String[] simText = ConfigConstants.getSimText();
String[] media = ConfigConstants.getMedia();
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
if (listPictureTypes().contains(fileType.toLowerCase())) {
return FileType.picture;
}
if (listArchiveTypes().contains(fileType.toLowerCase())) {
return FileType.compress;
}
if (listOfficeTypes().contains(fileType.toLowerCase())) {
return FileType.office;
}
if ("md".equalsIgnoreCase(fileType)) {
return FileType.markdown;
}
if ("xml".equalsIgnoreCase(fileType)) {
return FileType.xml;
}
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
return FileType.simText;
}
if (Arrays.asList(media).contains(fileType.toLowerCase())) {
return FileType.media;
}
if ("pdf".equalsIgnoreCase(fileType)) {
return FileType.pdf;
}
if ("dwg".equalsIgnoreCase(fileType)) {
return FileType.cad;
}
return FileType.other;
return FileType.to(fileType);
}
/**
@ -120,41 +91,6 @@ public class FileUtils {
return path.substring(path.lastIndexOf(File.separator) + 1);
}
public List<String> listPictureTypes() {
List<String> list = new LinkedList<>();
list.add("jpg");
list.add("jpeg");
list.add("png");
list.add("gif");
list.add("bmp");
list.add("ico");
list.add("RAW");
return list;
}
public List<String> listArchiveTypes() {
List<String> list = new LinkedList<>();
list.add("rar");
list.add("zip");
list.add("jar");
list.add("7-zip");
list.add("tar");
list.add("gzip");
list.add("7z");
return list;
}
public List<String> listOfficeTypes() {
List<String> list = new LinkedList<>();
list.add("docx");
list.add("doc");
list.add("xls");
list.add("xlsx");
list.add("ppt");
list.add("pptx");
return list;
}
/**
* 获取相对路径
*
@ -341,7 +277,7 @@ public class FileUtils {
String fullFileName = this.getUrlParameterReg(url, "fullfilename");
if (StringUtils.hasText(fullFileName)) {
fileName = fullFileName;
type = typeFromFileName(fullFileName);
type = this.typeFromFileName(fullFileName);
suffix = suffixFromFileName(fullFileName);
} else {
fileName = getFileNameFromURL(url);

12
jodconverter-web/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java

@ -6,7 +6,7 @@ import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.CadUtils;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import cn.keking.service.FilePreviewCommonService;
import cn.keking.utils.PdfUtils;
import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service;
@ -22,7 +22,7 @@ import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
@Service
public class CadFilePreviewImpl implements FilePreview {
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
private final DownloadUtils downloadUtils;
@ -30,11 +30,11 @@ public class CadFilePreviewImpl implements FilePreview {
private final PdfUtils pdfUtils;
public CadFilePreviewImpl(FileUtils fileUtils,
public CadFilePreviewImpl(FilePreviewCommonService filePreviewCommonService,
DownloadUtils downloadUtils,
CadUtils cadUtils,
PdfUtils pdfUtils) {
this.fileUtils = fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
this.downloadUtils = downloadUtils;
this.cadUtils = cadUtils;
this.pdfUtils = pdfUtils;
@ -56,7 +56,7 @@ public class CadFilePreviewImpl implements FilePreview {
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
String outFilePath = FILE_DIR + pdfName;
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) {
@ -74,7 +74,7 @@ public class CadFilePreviewImpl implements FilePreview {
}
if (ConfigConstants.isCacheEnabled()) {
// 加入缓存
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath));
}
}
}

14
jodconverter-web/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java

@ -5,7 +5,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import cn.keking.service.FilePreviewCommonService;
import cn.keking.utils.ZipReader;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
@ -18,16 +18,16 @@ import org.springframework.util.StringUtils;
@Service
public class CompressFilePreviewImpl implements FilePreview {
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
private final DownloadUtils downloadUtils;
private final ZipReader zipReader;
public CompressFilePreviewImpl(FileUtils fileUtils,
public CompressFilePreviewImpl(FilePreviewCommonService filePreviewCommonService,
DownloadUtils downloadUtils,
ZipReader zipReader) {
this.fileUtils = fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
this.downloadUtils = downloadUtils;
this.zipReader = zipReader;
}
@ -38,7 +38,7 @@ public class CompressFilePreviewImpl implements FilePreview {
String suffix=fileAttribute.getSuffix();
String fileTree = null;
// 判断文件名是否存在(redis缓存读取)
if (!StringUtils.hasText(fileUtils.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
if (!StringUtils.hasText(filePreviewCommonService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
@ -54,10 +54,10 @@ public class CompressFilePreviewImpl implements FilePreview {
fileTree = zipReader.read7zFile(filePath, fileName);
}
if (fileTree != null && !"null".equals(fileTree) && ConfigConstants.isCacheEnabled()) {
fileUtils.addConvertedFile(fileName, fileTree);
filePreviewCommonService.addConvertedFile(fileName, fileTree);
}
} else {
fileTree = fileUtils.getConvertedFile(fileName);
fileTree = filePreviewCommonService.getConvertedFile(fileName);
}
if (fileTree != null && !"null".equals(fileTree)) {
model.addAttribute("fileTree", fileTree);

10
jodconverter-web/src/main/java/cn/keking/service/impl/MediaFilePreviewImpl.java

@ -4,7 +4,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import cn.keking.service.FilePreviewCommonService;
import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
@ -19,12 +19,12 @@ public class MediaFilePreviewImpl implements FilePreview {
private final DownloadUtils downloadUtils;
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
public MediaFilePreviewImpl(DownloadUtils downloadUtils,
FileUtils fileUtils) {
FilePreviewCommonService filePreviewCommonService) {
this.downloadUtils = downloadUtils;
this.fileUtils = fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
}
@Override
@ -37,7 +37,7 @@ public class MediaFilePreviewImpl implements FilePreview {
model.addAttribute("msg", response.getMsg());
return "fileNotSupported";
} else {
model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + fileUtils.getRelativePath(response.getContent()));
model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + filePreviewCommonService.getRelativePath(response.getContent()));
}
} else {
model.addAttribute("mediaUrl", url);

14
jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java

@ -5,7 +5,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import cn.keking.service.FilePreviewCommonService;
import cn.keking.service.OfficeToPdfService;
import cn.keking.utils.PdfUtils;
import cn.keking.web.filter.BaseUrlFilter;
@ -22,13 +22,13 @@ import java.util.List;
@Service
public class OfficeFilePreviewImpl implements FilePreview {
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
private final PdfUtils pdfUtils;
private final DownloadUtils downloadUtils;
private final OfficeToPdfService officeToPdfService;
public OfficeFilePreviewImpl(FileUtils fileUtils, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) {
this.fileUtils = fileUtils;
public OfficeFilePreviewImpl(FilePreviewCommonService filePreviewCommonService, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) {
this.filePreviewCommonService = filePreviewCommonService;
this.pdfUtils = pdfUtils;
this.downloadUtils = downloadUtils;
this.officeToPdfService = officeToPdfService;
@ -49,7 +49,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
String outFilePath = FILE_DIR + pdfName;
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) {
@ -62,11 +62,11 @@ public class OfficeFilePreviewImpl implements FilePreview {
officeToPdfService.openOfficeToPDF(filePath, outFilePath);
if (isHtml) {
// 对转换后的文件进行操作(改变编码方式)
fileUtils.doActionConvertedFile(outFilePath);
filePreviewCommonService.doActionConvertedFile(outFilePath);
}
if (ConfigConstants.isCacheEnabled()) {
// 加入缓存
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath));
}
}
}

18
jodconverter-web/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java

@ -5,7 +5,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import cn.keking.service.FilePreviewCommonService;
import cn.keking.utils.PdfUtils;
import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service;
@ -20,7 +20,7 @@ import java.util.List;
@Service
public class PdfFilePreviewImpl implements FilePreview {
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
private final PdfUtils pdfUtils;
@ -28,10 +28,10 @@ public class PdfFilePreviewImpl implements FilePreview {
private static final String FILE_DIR = ConfigConstants.getFileDir();
public PdfFilePreviewImpl(FileUtils fileUtils,
public PdfFilePreviewImpl(FilePreviewCommonService filePreviewCommonService,
PdfUtils pdfUtils,
DownloadUtils downloadUtils) {
this.fileUtils = fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
this.pdfUtils = pdfUtils;
this.downloadUtils = downloadUtils;
}
@ -46,7 +46,7 @@ public class PdfFilePreviewImpl implements FilePreview {
String outFilePath = FILE_DIR + pdfName;
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
//当文件不存在时,就去下载
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
@ -56,7 +56,7 @@ public class PdfFilePreviewImpl implements FilePreview {
outFilePath = response.getContent();
if (ConfigConstants.isCacheEnabled()) {
// 加入缓存
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath));
}
}
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
@ -75,17 +75,17 @@ public class PdfFilePreviewImpl implements FilePreview {
} else {
// 不是http开头,浏览器不能直接访问,需下载到本地
if (url != null && !url.toLowerCase().startsWith("http")) {
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
if (!filePreviewCommonService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, pdfName);
if (0 != response.getCode()) {
model.addAttribute("fileType", suffix);
model.addAttribute("msg", response.getMsg());
return "fileNotSupported";
}
model.addAttribute("pdfUrl", fileUtils.getRelativePath(response.getContent()));
model.addAttribute("pdfUrl", filePreviewCommonService.getRelativePath(response.getContent()));
if (ConfigConstants.isCacheEnabled()) {
// 加入缓存
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
filePreviewCommonService.addConvertedFile(pdfName, filePreviewCommonService.getRelativePath(outFilePath));
}
} else {
model.addAttribute("pdfUrl", pdfName);

12
jodconverter-web/src/main/java/cn/keking/service/impl/PictureFilePreviewImpl.java

@ -4,7 +4,7 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import cn.keking.service.FilePreviewCommonService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
@ -18,13 +18,13 @@ import java.util.List;
@Service
public class PictureFilePreviewImpl implements FilePreview {
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
private final DownloadUtils downloadUtils;
public PictureFilePreviewImpl(FileUtils fileUtils,
public PictureFilePreviewImpl(FilePreviewCommonService filePreviewCommonService,
DownloadUtils downloadUtils) {
this.fileUtils = fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
this.downloadUtils = downloadUtils;
}
@ -33,7 +33,7 @@ public class PictureFilePreviewImpl implements FilePreview {
List<String> imgUrls = new ArrayList<>();
imgUrls.add(url);
String fileKey = fileAttribute.getFileKey();
List<String> zipImgUrls = fileUtils.getImgCache(fileKey);
List<String> zipImgUrls = filePreviewCommonService.getImgCache(fileKey);
if (!CollectionUtils.isEmpty(zipImgUrls)) {
imgUrls.addAll(zipImgUrls);
}
@ -45,7 +45,7 @@ public class PictureFilePreviewImpl implements FilePreview {
model.addAttribute("msg", response.getMsg());
return "fileNotSupported";
} else {
String file = fileUtils.getRelativePath(response.getContent());
String file = filePreviewCommonService.getRelativePath(response.getContent());
imgUrls.clear();
imgUrls.add(file);
model.addAttribute("imgurls", imgUrls);

13
jodconverter-web/src/main/java/cn/keking/utils/DownloadUtils.java

@ -5,6 +5,7 @@ import cn.keking.hutool.URLUtil;
import cn.keking.model.FileAttribute;
import cn.keking.model.FileType;
import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreviewCommonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@ -24,10 +25,10 @@ public class DownloadUtils {
private final String fileDir = ConfigConstants.getFileDir();
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
public DownloadUtils(FileUtils fileUtils) {
this.fileUtils = fileUtils;
public DownloadUtils(FilePreviewCommonService filePreviewCommonService) {
this.filePreviewCommonService = filePreviewCommonService;
}
private static final String URL_PARAM_FTP_USERNAME = "ftp.username";
@ -61,9 +62,9 @@ public class DownloadUtils {
OutputStream os = new FileOutputStream(realPath);
saveBytesToOutStream(bytes, os);
} else if (url.getProtocol() != null && "ftp".equalsIgnoreCase(url.getProtocol())) {
String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
String ftpUsername = filePreviewCommonService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = filePreviewCommonService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
String ftpControlEncoding = filePreviewCommonService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
} else {
response.setCode(1);

11
jodconverter-web/src/main/java/cn/keking/utils/PdfUtils.java

@ -1,5 +1,6 @@
package cn.keking.utils;
import cn.keking.service.FilePreviewCommonService;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
@ -22,18 +23,18 @@ public class PdfUtils {
private final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
@Value("${server.tomcat.uri-encoding:UTF-8}")
private String uriEncoding;
public PdfUtils(FileUtils fileUtils) {
this.fileUtils = fileUtils;
public PdfUtils(FilePreviewCommonService filePreviewCommonService) {
this.filePreviewCommonService = filePreviewCommonService;
}
public List<String> pdf2jpg(String pdfFilePath, String pdfName, String baseUrl) {
List<String> imageUrls = new ArrayList<>();
Integer imageCount = fileUtils.getConvertedPdfImage(pdfFilePath);
Integer imageCount = filePreviewCommonService.getConvertedPdfImage(pdfFilePath);
String imageFileSuffix = ".jpg";
String pdfFolder = pdfName.substring(0, pdfName.length() - 4);
String urlPrefix = null;
@ -69,7 +70,7 @@ public class PdfUtils {
imageUrls.add(urlPrefix + "/" + pageIndex + imageFileSuffix);
}
doc.close();
fileUtils.addConvertedPdfImage(pdfFilePath, pageCount);
filePreviewCommonService.addConvertedPdfImage(pdfFilePath, pageCount);
} catch (IOException e) {
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
}

27
jodconverter-web/src/main/java/cn/keking/utils/ZipReader.java

@ -2,6 +2,7 @@ package cn.keking.utils;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileType;
import cn.keking.service.FilePreviewCommonService;
import cn.keking.web.filter.BaseUrlFilter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -33,14 +34,14 @@ import java.util.regex.Pattern;
public class ZipReader {
static Pattern pattern = Pattern.compile("^\\d+");
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
private final String fileDir = ConfigConstants.getFileDir();
private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public ZipReader(FileUtils fileUtils) {
this.fileUtils = fileUtils;
public ZipReader(FilePreviewCommonService filePreviewCommonService) {
this.filePreviewCommonService = filePreviewCommonService;
}
public String readZipFile(String filePath,String fileKey) {
@ -48,9 +49,9 @@ public class ZipReader {
Map<String, FileNode> appender = new HashMap<>();
List<String> imgUrls = new LinkedList<>();
String baseUrl = BaseUrlFilter.getBaseUrl();
String archiveFileName = fileUtils.getFileNameFromPath(filePath);
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath);
try {
ZipFile zipFile = new ZipFile(filePath, fileUtils.getFileEncodeUTFGBK(filePath));
ZipFile zipFile = new ZipFile(filePath, filePreviewCommonService.getFileEncodeUTFGBK(filePath));
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
// 排序
entries = sortZipEntries(entries);
@ -69,7 +70,7 @@ public class ZipReader {
}
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
parentName = (level-1) + "_" + parentName;
FileType type=fileUtils.typeFromUrl(childName);
FileType type= filePreviewCommonService.typeFromUrl(childName);
if (type.equals(FileType.picture)){//添加图片文件到图片列表
imgUrls.add(baseUrl+childName);
}
@ -79,7 +80,7 @@ public class ZipReader {
}
// 开启新的线程处理文件解压
executors.submit(new ZipExtractorWorker(entriesToBeExtracted, zipFile, filePath));
fileUtils.putImgCache(fileKey,imgUrls);
filePreviewCommonService.putImgCache(fileKey,imgUrls);
return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (IOException e) {
e.printStackTrace();
@ -104,7 +105,7 @@ public class ZipReader {
Archive archive = new Archive(new FileInputStream(new File(filePath)));
List<FileHeader> headers = archive.getFileHeaders();
headers = sortedHeaders(headers);
String archiveFileName = fileUtils.getFileNameFromPath(filePath);
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath);
List<Map<String, FileHeader>> headersToBeExtracted =new ArrayList<>();
for (FileHeader header : headers) {
String fullName;
@ -122,7 +123,7 @@ public class ZipReader {
headersToBeExtracted.add(Collections.singletonMap(childName, header));
}
String parentName = getLast2FileName(fullName, "\\", archiveFileName);
FileType type = fileUtils.typeFromUrl(childName);
FileType type = filePreviewCommonService.typeFromUrl(childName);
if (type.equals(FileType.picture)){//添加图片文件到图片列表
imgUrls.add(baseUrl+childName);
}
@ -131,7 +132,7 @@ public class ZipReader {
appender.put(childName, node);
}
executors.submit(new RarExtractorWorker(headersToBeExtracted, archive, filePath));
fileUtils.putImgCache(fileKey,imgUrls);
filePreviewCommonService.putImgCache(fileKey,imgUrls);
return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (RarException | IOException e) {
e.printStackTrace();
@ -144,7 +145,7 @@ public class ZipReader {
Map<String, FileNode> appender = new HashMap<>();
List<String> imgUrls = new ArrayList<>();
String baseUrl= BaseUrlFilter.getBaseUrl();
String archiveFileName = fileUtils.getFileNameFromPath(filePath);
String archiveFileName = filePreviewCommonService.getFileNameFromPath(filePath);
try {
SevenZFile zipFile = new SevenZFile(new File(filePath));
Iterable<SevenZArchiveEntry> entries = zipFile.getEntries();
@ -165,7 +166,7 @@ public class ZipReader {
}
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
parentName = (level-1) + "_" + parentName;
FileType type=fileUtils.typeFromUrl(childName);
FileType type= filePreviewCommonService.typeFromUrl(childName);
if (type.equals(FileType.picture)){//添加图片文件到图片列表
imgUrls.add(baseUrl+childName);
}
@ -175,7 +176,7 @@ public class ZipReader {
}
// 开启新的线程处理文件解压
executors.submit(new SevenZExtractorWorker(entriesToBeExtracted, filePath));
fileUtils.putImgCache(fileKey,imgUrls);
filePreviewCommonService.putImgCache(fileKey,imgUrls);
return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (IOException e) {
e.printStackTrace();

19
jodconverter-web/src/main/java/cn/keking/web/controller/OnlinePreviewController.java

@ -6,7 +6,7 @@ import cn.keking.service.FilePreviewFactory;
import cn.keking.service.cache.CacheService;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.FileUtils;
import cn.keking.service.FilePreviewCommonService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
@ -32,15 +32,12 @@ public class OnlinePreviewController {
private final FilePreviewFactory previewFactory;
private final CacheService cacheService;
private final FileUtils fileUtils;
private final FilePreviewCommonService filePreviewCommonService;
private final DownloadUtils downloadUtils;
public OnlinePreviewController(FilePreviewFactory filePreviewFactory,
FileUtils fileUtils,
CacheService cacheService,
DownloadUtils downloadUtils) {
public OnlinePreviewController(FilePreviewFactory filePreviewFactory, FilePreviewCommonService filePreviewCommonService, CacheService cacheService, DownloadUtils downloadUtils) {
this.previewFactory = filePreviewFactory;
this.fileUtils = fileUtils;
this.filePreviewCommonService = filePreviewCommonService;
this.cacheService = cacheService;
this.downloadUtils = downloadUtils;
}
@ -48,13 +45,13 @@ public class OnlinePreviewController {
@RequestMapping(value = "/onlinePreview")
public String onlinePreview(String url, Model model, HttpServletRequest req) {
FileAttribute fileAttribute = fileUtils.getFileAttribute(url,req);
FileAttribute fileAttribute = filePreviewCommonService.getFileAttribute(url,req);
FilePreview filePreview = previewFactory.get(fileAttribute);
logger.info("预览文件url:{},previewType:{}", url, fileAttribute.getType());
return filePreview.filePreviewHandle(url, model, fileAttribute);
}
@RequestMapping(value = "picturesPreview")
@RequestMapping(value = "/picturesPreview")
public String picturesPreview(Model model, HttpServletRequest req) throws UnsupportedEncodingException {
String urls = req.getParameter("urls");
String currentUrl = req.getParameter("currentUrl");
@ -64,8 +61,8 @@ public class OnlinePreviewController {
String decodedCurrentUrl = URLDecoder.decode(currentUrl, "utf-8");
// 抽取文件并返回文件列表
String[] imgs = decodedUrl.split("\\|");
List imgurls = Arrays.asList(imgs);
model.addAttribute("imgurls", imgurls);
List<String> imgUrls = Arrays.asList(imgs);
model.addAttribute("imgUrls", imgUrls);
model.addAttribute("currentUrl",decodedCurrentUrl);
return "picture";
}

2
jodconverter-web/src/main/resources/web/picture.ftl

@ -23,7 +23,7 @@
</head>
<body>
<ul id="dowebok">
<#list imgurls as img>
<#list imgUrls as img>
<#if img?contains("http://") || img?contains("https://")>
<#assign img="${img}">
<#else>

4
jodconverter-web/src/main/resources/web/txt.ftl

@ -6,8 +6,8 @@
<title>普通文本预览</title>
</head>
<body>
<input hidden id="textType" value="${textType}">
<input hidden id="textData" value="${textData}">
<input hidden id="textType" value="${textType}"/>
<input hidden id="textData" value="${textData}"/>
<div class="container">
<#if textType?? && textType == "markdown">

Loading…
Cancel
Save