Compare commits

..

1 Commits

Author SHA1 Message Date
kl
4af19dd646 新增markdown格式预览支持 2020-12-25 16:59:11 +08:00
4365 changed files with 84113 additions and 1237 deletions

5
.gitignore vendored
View File

@@ -27,6 +27,5 @@ nbdist/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
server/src/main/cache/ jodconverter-web/src/main/cache/
server/src/main/file/ jodconverter-web/src/main/file/
server/src/main/log

View File

@@ -1,6 +1,6 @@
FROM ubuntu:20.04 FROM ubuntu:20.04
MAINTAINER chenjh "842761733@qq.com" MAINTAINER chenjh "842761733@qq.com"
ADD server/target/kkFileView-*.tar.gz /opt/ ADD jodconverter-web/target/kkFileView-*.tar.gz /opt/
COPY fonts/* /usr/share/fonts/chienes/ COPY fonts/* /usr/share/fonts/chienes/
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" > /etc/apt/sources.list &&\ RUN echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse\ndeb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse\ndeb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" > /etc/apt/sources.list &&\
apt-get clean && apt-get update &&\ apt-get clean && apt-get update &&\

View File

@@ -4,7 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>cn.keking</groupId> <groupId>cn.keking</groupId>
<artifactId>office-plugin</artifactId> <artifactId>jodconverter-core</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>

View File

@@ -19,7 +19,6 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.artofsolving.jodconverter.util.ConfigUtils;
import org.artofsolving.jodconverter.util.PlatformUtils; import org.artofsolving.jodconverter.util.PlatformUtils;
import com.sun.star.beans.PropertyValue; import com.sun.star.beans.PropertyValue;
@@ -56,7 +55,7 @@ public class OfficeUtils {
Map<String,Object> subProperties = (Map<String,Object>) value; Map<String,Object> subProperties = (Map<String,Object>) value;
value = toUnoProperties(subProperties); value = toUnoProperties(subProperties);
} }
propertyValues[i++] = property(entry.getKey(), value); propertyValues[i++] = property((String) entry.getKey(), value);
} }
return propertyValues; return propertyValues;
} }
@@ -69,7 +68,7 @@ public class OfficeUtils {
public static File getDefaultOfficeHome() { public static File getDefaultOfficeHome() {
Properties properties = new Properties(); Properties properties = new Properties();
String customizedConfigPath = ConfigUtils.getCustomizedConfigPath(); String customizedConfigPath = getCustomizedConfigPath();
try { try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(customizedConfigPath)); BufferedReader bufferedReader = new BufferedReader(new FileReader(customizedConfigPath));
properties.load(bufferedReader); properties.load(bufferedReader);
@@ -81,7 +80,7 @@ public class OfficeUtils {
} }
if (PlatformUtils.isWindows()) { if (PlatformUtils.isWindows()) {
// %ProgramFiles(x86)% on 64-bit machines; %ProgramFiles% on 32-bit ones // %ProgramFiles(x86)% on 64-bit machines; %ProgramFiles% on 32-bit ones
String homePath = ConfigUtils.getHomePath(); String homePath = OfficeUtils.getHomePath();
String programFiles = System.getenv("ProgramFiles(x86)"); String programFiles = System.getenv("ProgramFiles(x86)");
if (programFiles == null) { if (programFiles == null) {
programFiles = System.getenv("ProgramFiles"); programFiles = System.getenv("ProgramFiles");
@@ -128,7 +127,30 @@ public class OfficeUtils {
} }
} }
public static String getHomePath() {
String userDir = System.getenv("KKFILEVIEW_BIN_FOLDER");
if (userDir == null) {
userDir = System.getProperty("user.dir");
}
if (userDir.endsWith("bin")) {
userDir = userDir.substring(0, userDir.length() - 4);
} else {
String separator = File.separator;
if (userDir.contains("jodconverter-web")) {
userDir = userDir + separator + "src" + separator + "main";
} else {
userDir = userDir + separator + "jodconverter-web" + separator + "src" + separator + "main";
}
}
return userDir;
}
public static String getCustomizedConfigPath() {
String homePath = OfficeUtils.getHomePath();
String separator = java.io.File.separator;
String configFilePath = homePath + separator + "config" + separator + "application.properties";
return configFilePath;
}
/** /**
* SpringBoot application.properties 支持从环境变量获取值 * SpringBoot application.properties 支持从环境变量获取值

View File

@@ -14,12 +14,12 @@
<artifactId>kkFileView</artifactId> <artifactId>kkFileView</artifactId>
<version>2.2.1</version> <version>2.2.1</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version> <java.version>1.8</java.version>
</properties> </properties>
<repositories> <repositories>
<repository> <repository>
<!-- required for org.hyperic:sigar --> <!-- required for org.hyperic:sigar -->
@@ -35,18 +35,12 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>net.logstash.logback</groupId>
<artifactId>spring-boot-starter-jetty</artifactId> <artifactId>logstash-logback-encoder</artifactId>
<version>4.11</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
@@ -54,7 +48,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.keking</groupId> <groupId>cn.keking</groupId>
<artifactId>office-plugin</artifactId> <artifactId>jodconverter-core</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
@@ -164,7 +158,12 @@
<dependency> <dependency>
<groupId>com.thoughtworks.xstream</groupId> <groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId> <artifactId>xstream</artifactId>
<version>1.4.15</version> <version>1.4.6</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.googlecode.concurrentlinkedhashmap</groupId> <groupId>com.googlecode.concurrentlinkedhashmap</groupId>
@@ -193,14 +192,6 @@
<scope>system</scope> <scope>system</scope>
<systemPath>${basedir}/lib/aspose-cad-19.9.jar</systemPath> <systemPath>${basedir}/lib/aspose-cad-19.9.jar</systemPath>
</dependency> </dependency>
<!-- 编码识别 -->
<dependency>
<groupId>cpdetector</groupId>
<artifactId>cpdetector</artifactId>
<version>1.04</version>
<scope>system</scope>
<systemPath>${basedir}/lib/cpdetector-1.04.jar</systemPath>
</dependency>
</dependencies> </dependencies>
<build> <build>
<resources> <resources>

View File

@@ -53,9 +53,6 @@ simText = ${KK_SIMTEXT:txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log
media = ${KK_MEDIA:mp3,wav,mp4,flv} media = ${KK_MEDIA:mp3,wav,mp4,flv}
#office类型文档(word ppt)样式默认为图片(image)可配置为pdf预览时也有按钮切换 #office类型文档(word ppt)样式默认为图片(image)可配置为pdf预览时也有按钮切换
office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:image} office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:image}
#是否关闭office预览切换开关默认为false可配置为true关闭
office.preview.switch.disabled = ${KK_OFFICE_PREVIEW_SWITCH_DISABLED:false}
#是否禁止下载转换生成的pdf文件 #是否禁止下载转换生成的pdf文件
pdf.download.disable = ${KK_PDF_DOWNLOAD_DISABLE:true} pdf.download.disable = ${KK_PDF_DOWNLOAD_DISABLE:true}

View File

@@ -1,13 +1,5 @@
[#ftl] [#ftl]
[#-- @implicitly included --] [#-- @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" --]
[#-- @ftlvariable name="textType" type="java.lang.String" --]
[#-- @ftlvariable name="markdown" type="String" --]
[#-- @ftlvariable name="xml" type="String" --]
[#-- @ftlvariable name="switchDisabled" type="String" --]
[#-- @ftlvariable name="imgurls" type="String" --] [#-- @ftlvariable name="imgurls" type="String" --]
[#-- @ftlvariable name="watermarkAngle" type="String" --] [#-- @ftlvariable name="watermarkAngle" type="String" --]
[#-- @ftlvariable name="watermarkHeight" type="String" --] [#-- @ftlvariable name="watermarkHeight" type="String" --]

View File

@@ -8,15 +8,8 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @SpringBootApplication
@EnableScheduling @EnableScheduling
@ComponentScan(value = "cn.keking.*") @ComponentScan(value = "cn.keking.*")
public class ServerMain { public class FilePreviewApplication {
public static void main(String[] args) { public static void main(String[] args) {
ServerMain.staticInitSystemProperty(); SpringApplication.run(FilePreviewApplication.class, args);
SpringApplication.run(ServerMain.class, args);
}
private static void staticInitSystemProperty(){
//pdfbox兼容低版本jdk
System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider");
} }
} }

View File

@@ -1,6 +1,6 @@
package cn.keking.config; package cn.keking.config;
import org.artofsolving.jodconverter.util.ConfigUtils; import org.artofsolving.jodconverter.office.OfficeUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -20,12 +20,11 @@ public class ConfigConstants {
private static String[] SIM_TEXT = {}; private static String[] SIM_TEXT = {};
private static String[] MEDIA = {}; private static String[] MEDIA = {};
private static String OFFICE_PREVIEW_TYPE; private static String OFFICE_PREVIEW_TYPE;
private static String OFFICE_PREVIEW_SWITCH_DISABLED;
private static String FTP_USERNAME; private static String FTP_USERNAME;
private static String FTP_PASSWORD; private static String FTP_PASSWORD;
private static String FTP_CONTROL_ENCODING; private static String FTP_CONTROL_ENCODING;
private static String BASE_URL; private static String BASE_URL;
private static String FILE_DIR = ConfigUtils.getHomePath() + File.separator + "file" + File.separator; private static String FILE_DIR = OfficeUtils.getHomePath() + File.separator + "file" + File.separator;
private static CopyOnWriteArraySet<String> TRUST_HOST_SET; private static CopyOnWriteArraySet<String> TRUST_HOST_SET;
private static String PDF_DOWNLOAD_DISABLE; private static String PDF_DOWNLOAD_DISABLE;
@@ -33,7 +32,6 @@ public class ConfigConstants {
public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd"; public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd";
public static final String DEFAULT_MEDIA_TYPE = "mp3,wav,mp4,flv"; public static final String DEFAULT_MEDIA_TYPE = "mp3,wav,mp4,flv";
public static final String DEFAULT_OFFICE_PREVIEW_TYPE = "image"; public static final String DEFAULT_OFFICE_PREVIEW_TYPE = "image";
public static final String DEFAULT_OFFICE_PREVIEW_SWITCH_DISABLED = "false";
public static final String DEFAULT_FTP_USERNAME = null; public static final String DEFAULT_FTP_USERNAME = null;
public static final String DEFAULT_FTP_PASSWORD = null; public static final String DEFAULT_FTP_PASSWORD = null;
public static final String DEFAULT_FTP_CONTROL_ENCODING = "UTF-8"; public static final String DEFAULT_FTP_CONTROL_ENCODING = "UTF-8";
@@ -158,7 +156,7 @@ public class ConfigConstants {
} }
public static void setFileDirValue(String fileDir) { public static void setFileDirValue(String fileDir) {
if (!DEFAULT_FILE_DIR_VALUE.equalsIgnoreCase(fileDir)) { if (!DEFAULT_FILE_DIR_VALUE.equals(fileDir.toLowerCase())) {
if (!fileDir.endsWith(File.separator)) { if (!fileDir.endsWith(File.separator)) {
fileDir = fileDir + File.separator; fileDir = fileDir + File.separator;
} }
@@ -173,7 +171,7 @@ public class ConfigConstants {
public static void setTrustHostValue(String trustHost) { public static void setTrustHostValue(String trustHost) {
CopyOnWriteArraySet<String> trustHostSet; CopyOnWriteArraySet<String> trustHostSet;
if (DEFAULT_TRUST_HOST.equalsIgnoreCase(trustHost)) { if (DEFAULT_TRUST_HOST.equals(trustHost.toLowerCase())) {
trustHostSet = new CopyOnWriteArraySet<>(); trustHostSet = new CopyOnWriteArraySet<>();
} else { } else {
String[] trustHostArray = trustHost.toLowerCase().split(","); String[] trustHostArray = trustHost.toLowerCase().split(",");
@@ -204,14 +202,4 @@ public class ConfigConstants {
PDF_DOWNLOAD_DISABLE = pdfDownloadDisable; PDF_DOWNLOAD_DISABLE = pdfDownloadDisable;
} }
public static String getOfficePreviewSwitchDisabled() {
return OFFICE_PREVIEW_SWITCH_DISABLED;
}
@Value("${office.preview.switch.disabled:true}")
public void setOfficePreviewSwitchDisabled(String officePreviewSwitchDisabled) {
OFFICE_PREVIEW_SWITCH_DISABLED = officePreviewSwitchDisabled;
}
public static void setOfficePreviewSwitchDisabledValue(String officePreviewSwitchDisabled) {
OFFICE_PREVIEW_SWITCH_DISABLED = officePreviewSwitchDisabled;
}
} }

View File

@@ -1,9 +1,6 @@
package cn.keking.service; package cn.keking.config;
import cn.keking.config.ConfigConstants;
import cn.keking.config.WatermarkConfigConstants;
import org.artofsolving.jodconverter.office.OfficeUtils; import org.artofsolving.jodconverter.office.OfficeUtils;
import org.artofsolving.jodconverter.util.ConfigUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -41,11 +38,10 @@ public class ConfigRefreshComponent {
String[] textArray; String[] textArray;
String[] mediaArray; String[] mediaArray;
String officePreviewType; String officePreviewType;
String officePreviewSwitchDisabled;
String ftpUsername; String ftpUsername;
String ftpPassword; String ftpPassword;
String ftpControlEncoding; String ftpControlEncoding;
String configFilePath = ConfigUtils.getCustomizedConfigPath(); String configFilePath = OfficeUtils.getCustomizedConfigPath();
String baseUrl; String baseUrl;
String trustHost; String trustHost;
String pdfDownloadDisable; String pdfDownloadDisable;
@@ -58,7 +54,6 @@ public class ConfigRefreshComponent {
text = properties.getProperty("simText", ConfigConstants.DEFAULT_TXT_TYPE); text = properties.getProperty("simText", ConfigConstants.DEFAULT_TXT_TYPE);
media = properties.getProperty("media", ConfigConstants.DEFAULT_MEDIA_TYPE); media = properties.getProperty("media", ConfigConstants.DEFAULT_MEDIA_TYPE);
officePreviewType = properties.getProperty("office.preview.type", ConfigConstants.DEFAULT_OFFICE_PREVIEW_TYPE); officePreviewType = properties.getProperty("office.preview.type", ConfigConstants.DEFAULT_OFFICE_PREVIEW_TYPE);
officePreviewSwitchDisabled = properties.getProperty("office.preview.switch.disabled", ConfigConstants.DEFAULT_OFFICE_PREVIEW_TYPE);
ftpUsername = properties.getProperty("ftp.username", ConfigConstants.DEFAULT_FTP_USERNAME); ftpUsername = properties.getProperty("ftp.username", ConfigConstants.DEFAULT_FTP_USERNAME);
ftpPassword = properties.getProperty("ftp.password", ConfigConstants.DEFAULT_FTP_PASSWORD); ftpPassword = properties.getProperty("ftp.password", ConfigConstants.DEFAULT_FTP_PASSWORD);
ftpControlEncoding = properties.getProperty("ftp.control.encoding", ConfigConstants.DEFAULT_FTP_CONTROL_ENCODING); ftpControlEncoding = properties.getProperty("ftp.control.encoding", ConfigConstants.DEFAULT_FTP_CONTROL_ENCODING);
@@ -76,7 +71,6 @@ public class ConfigRefreshComponent {
ConfigConstants.setFtpControlEncodingValue(ftpControlEncoding); ConfigConstants.setFtpControlEncodingValue(ftpControlEncoding);
ConfigConstants.setBaseUrlValue(baseUrl); ConfigConstants.setBaseUrlValue(baseUrl);
ConfigConstants.setTrustHostValue(trustHost); ConfigConstants.setTrustHostValue(trustHost);
ConfigConstants.setOfficePreviewSwitchDisabledValue(officePreviewSwitchDisabled);
ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable); ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable);
setWatermarkConfig(properties); setWatermarkConfig(properties);
bufferedReader.close(); bufferedReader.close();

View File

@@ -1,7 +1,5 @@
package cn.keking.model; package cn.keking.model;
import cn.keking.config.ConfigConstants;
/** /**
* Created by kl on 2018/1/17. * Created by kl on 2018/1/17.
* Content : * Content :
@@ -9,11 +7,12 @@ import cn.keking.config.ConfigConstants;
public class FileAttribute { public class FileAttribute {
private FileType type; private FileType type;
private String suffix; private String suffix;
private String name; private String name;
private String url; private String url;
private String fileKey;
private String officePreviewType = ConfigConstants.getOfficePreviewType();
public FileAttribute() { public FileAttribute() {
} }
@@ -25,30 +24,6 @@ public class FileAttribute {
this.url = url; this.url = url;
} }
public FileAttribute(FileType type, String suffix, String name, String url, String officePreviewType) {
this.type = type;
this.suffix = suffix;
this.name = name;
this.url = url;
this.officePreviewType = officePreviewType;
}
public String getFileKey() {
return fileKey;
}
public void setFileKey(String fileKey) {
this.fileKey = fileKey;
}
public String getOfficePreviewType() {
return officePreviewType;
}
public void setOfficePreviewType(String officePreviewType) {
this.officePreviewType = officePreviewType;
}
public FileType getType() { public FileType getType() {
return type; return type;
} }

View File

@@ -0,0 +1,29 @@
package cn.keking.model;
/**
* Created by kl on 2018/1/17.
* Content :文件类型文本office压缩包等等
*/
public enum FileType {
picture("picturefilepreviewimpl"),
compress("compressFilePreviewImpl"),
office("officeFilePreviewImpl"),
simText("simTextFilePreviewImpl"),
pdf("pdfFilePreviewImpl"),
other("otherFilePreviewImpl"),
media("mediaFilePreviewImpl"),
markdown("markdownFilePreviewImpl"),
cad("cadFilePreviewImpl");
private final String instanceName;
FileType(String instanceName){
this.instanceName=instanceName;
}
public String getInstanceName() {
return instanceName;
}
}

View File

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

View File

@@ -5,10 +5,7 @@ import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
import com.googlecode.concurrentlinkedhashmap.Weighers; import com.googlecode.concurrentlinkedhashmap.Weighers;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
@@ -24,78 +21,15 @@ import java.util.concurrent.BlockingQueue;
public class CacheServiceJDKImpl implements CacheService { public class CacheServiceJDKImpl implements CacheService {
private Map<String, String> pdfCache; private Map<String, String> pdfCache;
private Map<String, List<String>> imgCache; private Map<String, List<String>> imgCache;
private Map<String, Integer> pdfImagesCache; private Map<String, Integer> pdfImagesCache;
private static final int QUEUE_SIZE = 500000; private static final int QUEUE_SIZE = 500000;
private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(QUEUE_SIZE); private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(QUEUE_SIZE);
@PostConstruct
public void initCache(){
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
}
@Override
public void putPDFCache(String key, String value) {
pdfCache.put(key, value);
}
@Override
public void putImgCache(String key, List<String> value) {
imgCache.put(key, value);
}
@Override
public Map<String, String> getPDFCache() {
return pdfCache;
}
@Override
public String getPDFCache(String key) {
return pdfCache.get(key);
}
@Override
public Map<String, List<String>> getImgCache() {
return imgCache;
}
@Override
public List<String> getImgCache(String key) {
if(StringUtils.isEmpty(key)){
return new ArrayList<>();
}
return imgCache.get(key);
}
@Override
public Integer getPdfImageCache(String key) {
return pdfImagesCache.get(key);
}
@Override
public void putPdfImageCache(String pdfFilePath, int num) {
pdfImagesCache.put(pdfFilePath, num);
}
@Override
public void cleanCache() {
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
}
@Override
public void addQueueTask(String url) {
blockingQueue.add(url);
}
@Override
public String takeQueueTask() throws InterruptedException {
return blockingQueue.take();
}
@Override @Override
public void initPDFCachePool(Integer capacity) { public void initPDFCachePool(Integer capacity) {
pdfCache = new ConcurrentLinkedHashMap.Builder<String, String>() pdfCache = new ConcurrentLinkedHashMap.Builder<String, String>()
@@ -116,4 +50,85 @@ public class CacheServiceJDKImpl implements CacheService {
.maximumWeightedCapacity(capacity).weigher(Weighers.singleton()) .maximumWeightedCapacity(capacity).weigher(Weighers.singleton())
.build(); .build();
} }
@Override
public void putPDFCache(String key, String value) {
if (pdfCache == null) {
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
}
pdfCache.put(key, value);
}
@Override
public void putImgCache(String key, List<String> value) {
if (imgCache == null) {
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
}
imgCache.put(key, value);
}
@Override
public Map<String, String> getPDFCache() {
if (pdfCache == null) {
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
}
return pdfCache;
}
@Override
public String getPDFCache(String key) {
if (pdfCache == null) {
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
}
return pdfCache.get(key);
}
@Override
public Map<String, List<String>> getImgCache() {
if (imgCache == null) {
initPDFCachePool(CacheService.DEFAULT_IMG_CAPACITY);
}
return imgCache;
}
@Override
public List<String> getImgCache(String key) {
if (imgCache == null) {
initPDFCachePool(CacheService.DEFAULT_IMG_CAPACITY);
}
return imgCache.get(key);
}
@Override
public Integer getPdfImageCache(String key) {
if (pdfImagesCache == null) {
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
}
return pdfImagesCache.get(key);
}
@Override
public void putPdfImageCache(String pdfFilePath, int num) {
if (pdfImagesCache == null) {
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
}
pdfImagesCache.put(pdfFilePath, num);
}
@Override
public void cleanCache() {
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
}
@Override
public void addQueueTask(String url) {
blockingQueue.add(url);
}
@Override
public String takeQueueTask() throws InterruptedException {
return blockingQueue.take();
}
} }

View File

@@ -1,7 +1,7 @@
package cn.keking.service.cache.impl; package cn.keking.service.cache.impl;
import cn.keking.service.cache.CacheService; import cn.keking.service.cache.CacheService;
import org.artofsolving.jodconverter.util.ConfigUtils; import org.artofsolving.jodconverter.office.OfficeUtils;
import org.rocksdb.RocksDB; import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException; import org.rocksdb.RocksDBException;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -30,7 +30,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
RocksDB.loadLibrary(); RocksDB.loadLibrary();
} }
private static final String DB_PATH = ConfigUtils.getHomePath() + File.separator + "cache"; private static final String DB_PATH = OfficeUtils.getHomePath() + File.separator + "cache";
private static final int QUEUE_SIZE = 500000; private static final int QUEUE_SIZE = 500000;

View File

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

View File

@@ -5,8 +5,8 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FileHandlerService; import cn.keking.utils.FileUtils;
import cn.keking.service.CompressFileReader; import cn.keking.utils.ZipReader;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@@ -18,14 +18,18 @@ import org.springframework.util.StringUtils;
@Service @Service
public class CompressFilePreviewImpl implements FilePreview { public class CompressFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService; private final FileUtils fileUtils;
private final DownloadUtils downloadUtils;
private final CompressFileReader compressFileReader;
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, DownloadUtils downloadUtils, CompressFileReader compressFileReader) { private final DownloadUtils downloadUtils;
this.fileHandlerService = fileHandlerService;
private final ZipReader zipReader;
public CompressFilePreviewImpl(FileUtils fileUtils,
DownloadUtils downloadUtils,
ZipReader zipReader) {
this.fileUtils = fileUtils;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
this.compressFileReader = compressFileReader; this.zipReader = zipReader;
} }
@Override @Override
@@ -34,7 +38,7 @@ public class CompressFilePreviewImpl implements FilePreview {
String suffix=fileAttribute.getSuffix(); String suffix=fileAttribute.getSuffix();
String fileTree = null; String fileTree = null;
// 判断文件名是否存在(redis缓存读取) // 判断文件名是否存在(redis缓存读取)
if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) { if (!StringUtils.hasText(fileUtils.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) { if (0 != response.getCode()) {
model.addAttribute("fileType", suffix); model.addAttribute("fileType", suffix);
@@ -43,17 +47,17 @@ public class CompressFilePreviewImpl implements FilePreview {
} }
String filePath = response.getContent(); String filePath = response.getContent();
if ("zip".equalsIgnoreCase(suffix) || "jar".equalsIgnoreCase(suffix) || "gzip".equalsIgnoreCase(suffix)) { if ("zip".equalsIgnoreCase(suffix) || "jar".equalsIgnoreCase(suffix) || "gzip".equalsIgnoreCase(suffix)) {
fileTree = compressFileReader.readZipFile(filePath, fileName); fileTree = zipReader.readZipFile(filePath, fileName);
} else if ("rar".equalsIgnoreCase(suffix)) { } else if ("rar".equalsIgnoreCase(suffix)) {
fileTree = compressFileReader.unRar(filePath, fileName); fileTree = zipReader.unRar(filePath, fileName);
} else if ("7z".equalsIgnoreCase(suffix)) { } else if ("7z".equalsIgnoreCase(suffix)) {
fileTree = compressFileReader.read7zFile(filePath, fileName); fileTree = zipReader.read7zFile(filePath, fileName);
} }
if (fileTree != null && !"null".equals(fileTree) && ConfigConstants.isCacheEnabled()) { if (fileTree != null && !"null".equals(fileTree) && ConfigConstants.isCacheEnabled()) {
fileHandlerService.addConvertedFile(fileName, fileTree); fileUtils.addConvertedFile(fileName, fileTree);
} }
} else { } else {
fileTree = fileHandlerService.getConvertedFile(fileName); fileTree = fileUtils.getConvertedFile(fileName);
} }
if (fileTree != null && !"null".equals(fileTree)) { if (fileTree != null && !"null".equals(fileTree)) {
model.addAttribute("fileTree", fileTree); model.addAttribute("fileTree", fileTree);

View File

@@ -5,8 +5,6 @@ import cn.keking.service.FilePreview;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import static com.sun.glass.ui.Clipboard.TEXT_TYPE;
/** /**
* @author kl (http://kailing.pub) * @author kl (http://kailing.pub)
* @since 2020/12/25 * @since 2020/12/25
@@ -23,7 +21,7 @@ public class MarkdownFilePreviewImpl implements FilePreview {
@Override @Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
model.addAttribute(TEXT_TYPE,"markdown"); model.addAttribute("markdown","true");
return simTextFilePreview.filePreviewHandle(url, model, fileAttribute); return simTextFilePreview.filePreviewHandle(url, model, fileAttribute);
} }
} }

View File

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

View File

@@ -5,8 +5,8 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FileHandlerService; import cn.keking.utils.FileUtils;
import cn.keking.service.OfficeToPdfService; import cn.keking.utils.OfficeToPdf;
import cn.keking.utils.PdfUtils; import cn.keking.utils.PdfUtils;
import cn.keking.web.filter.BaseUrlFilter; import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -22,16 +22,22 @@ import java.util.List;
@Service @Service
public class OfficeFilePreviewImpl implements FilePreview { public class OfficeFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService; private final FileUtils fileUtils;
private final PdfUtils pdfUtils;
private final DownloadUtils downloadUtils;
private final OfficeToPdfService officeToPdfService;
public OfficeFilePreviewImpl(FileHandlerService fileHandlerService, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) { private final PdfUtils pdfUtils;
this.fileHandlerService = fileHandlerService;
private final DownloadUtils downloadUtils;
private final OfficeToPdf officeToPdf;
public OfficeFilePreviewImpl(FileUtils fileUtils,
PdfUtils pdfUtils,
DownloadUtils downloadUtils,
OfficeToPdf officeToPdf) {
this.fileUtils = fileUtils;
this.pdfUtils = pdfUtils; this.pdfUtils = pdfUtils;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
this.officeToPdfService = officeToPdfService; this.officeToPdf = officeToPdf;
} }
public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image"; public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
@@ -41,7 +47,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
@Override @Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
// 预览Type参数传了就取参数的没传取系统默认 // 预览Type参数传了就取参数的没传取系统默认
String officePreviewType = fileAttribute.getOfficePreviewType(); String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
String suffix=fileAttribute.getSuffix(); String suffix=fileAttribute.getSuffix();
String fileName=fileAttribute.getName(); String fileName=fileAttribute.getName();
@@ -49,7 +55,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf"); String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
String outFilePath = FILE_DIR + pdfName; String outFilePath = FILE_DIR + pdfName;
// 判断之前是否已转换过如果转换过直接返回否则执行转换 // 判断之前是否已转换过如果转换过直接返回否则执行转换
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) { if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath; String filePath;
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
if (0 != response.getCode()) { if (0 != response.getCode()) {
@@ -59,14 +65,14 @@ public class OfficeFilePreviewImpl implements FilePreview {
} }
filePath = response.getContent(); filePath = response.getContent();
if (StringUtils.hasText(outFilePath)) { if (StringUtils.hasText(outFilePath)) {
officeToPdfService.openOfficeToPDF(filePath, outFilePath); officeToPdf.openOfficeToPDF(filePath, outFilePath);
if (isHtml) { if (isHtml) {
// 对转换后的文件进行操作(改变编码方式) // 对转换后的文件进行操作(改变编码方式)
fileHandlerService.doActionConvertedFile(outFilePath); fileUtils.doActionConvertedFile(outFilePath);
} }
if (ConfigConstants.isCacheEnabled()) { if (ConfigConstants.isCacheEnabled()) {
// 加入缓存 // 加入缓存
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath)); fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
} }
} }
} }

View File

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

View File

@@ -4,11 +4,12 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import cn.keking.service.FileHandlerService; import cn.keking.utils.FileUtils;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils; import org.springframework.web.context.request.RequestContextHolder;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@@ -18,24 +19,25 @@ import java.util.List;
@Service @Service
public class PictureFilePreviewImpl implements FilePreview { public class PictureFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService; private final FileUtils fileUtils;
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
public PictureFilePreviewImpl(FileHandlerService fileHandlerService, public PictureFilePreviewImpl(FileUtils fileUtils,
DownloadUtils downloadUtils) { DownloadUtils downloadUtils) {
this.fileHandlerService = fileHandlerService; this.fileUtils = fileUtils;
this.downloadUtils = downloadUtils; this.downloadUtils = downloadUtils;
} }
@Override @Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
List<String> imgUrls = new ArrayList<>(); String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
imgUrls.add(url); List<String> imgUrls = Lists.newArrayList(url);
String fileKey = fileAttribute.getFileKey(); try {
List<String> zipImgUrls = fileHandlerService.getImgCache(fileKey); imgUrls.clear();
if (!CollectionUtils.isEmpty(zipImgUrls)) { imgUrls.addAll(fileUtils.getImgCache(fileKey));
imgUrls.addAll(zipImgUrls); } catch (Exception e){
imgUrls = Lists.newArrayList(url);
} }
// 不是http开头浏览器不能直接访问需下载到本地 // 不是http开头浏览器不能直接访问需下载到本地
if (url != null && !url.toLowerCase().startsWith("http")) { if (url != null && !url.toLowerCase().startsWith("http")) {
@@ -45,10 +47,8 @@ public class PictureFilePreviewImpl implements FilePreview {
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
return "fileNotSupported"; return "fileNotSupported";
} else { } else {
String file = fileHandlerService.getRelativePath(response.getContent()); String file = fileUtils.getRelativePath(response.getContent());
imgUrls.clear(); model.addAttribute("imgurls", Lists.newArrayList(file));
imgUrls.add(file);
model.addAttribute("imgurls", imgUrls);
model.addAttribute("currentUrl", file); model.addAttribute("currentUrl", file);
} }
} else { } else {

View File

@@ -4,14 +4,12 @@ import cn.keking.model.FileAttribute;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview; import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils; import cn.keking.utils.DownloadUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.Base64Utils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.file.Files;
/** /**
* Created by kl on 2018/1/17. * Created by kl on 2018/1/17.
@@ -20,9 +18,6 @@ import java.nio.charset.StandardCharsets;
@Service @Service
public class SimTextFilePreviewImpl implements FilePreview { public class SimTextFilePreviewImpl implements FilePreview {
public static final String TEXT_TYPE = "textType";
public static final String DEFAULT_TEXT_TYPE = "simText";
private final DownloadUtils downloadUtils; private final DownloadUtils downloadUtils;
public SimTextFilePreviewImpl(DownloadUtils downloadUtils) { public SimTextFilePreviewImpl(DownloadUtils downloadUtils) {
@@ -30,26 +25,27 @@ public class SimTextFilePreviewImpl implements FilePreview {
} }
@Override @Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) { public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute){
String fileName = fileAttribute.getName(); String fileName = fileAttribute.getName();
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName); ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) { if (0 != response.getCode()) {
model.addAttribute("msg", response.getMsg()); model.addAttribute("msg", response.getMsg());
model.addAttribute("fileType", fileAttribute.getSuffix()); model.addAttribute("fileType",fileAttribute.getSuffix());
return "fileNotSupported"; return "fileNotSupported";
} }
try { try {
File originFile = new File(response.getContent()); File originFile = new File(response.getContent());
String xmlString = FileUtils.readFileToString(originFile, StandardCharsets.UTF_8); File previewFile = new File(response.getContent() + ".txt");
model.addAttribute("textData", Base64Utils.encodeToString(xmlString.getBytes())); if (previewFile.exists()) {
previewFile.delete();
}
Files.copy(originFile.toPath(), previewFile.toPath());
} catch (IOException e) { } catch (IOException e) {
model.addAttribute("msg", e.getLocalizedMessage()); model.addAttribute("msg", e.getLocalizedMessage());
model.addAttribute("fileType", fileAttribute.getSuffix()); model.addAttribute("fileType",fileAttribute.getSuffix());
return "fileNotSupported"; return "fileNotSupported";
} }
if (!model.containsAttribute(TEXT_TYPE)) { model.addAttribute("ordinaryUrl", response.getMsg());
model.addAttribute(TEXT_TYPE, DEFAULT_TEXT_TYPE);
}
return "txt"; return "txt";
} }

View File

@@ -1,4 +1,4 @@
package cn.keking.service; package cn.keking.utils;
import com.sun.star.document.UpdateDocMode; import com.sun.star.document.UpdateDocMode;
import cn.keking.extend.ControlDocumentFormatRegistry; import cn.keking.extend.ControlDocumentFormatRegistry;
@@ -9,8 +9,6 @@ import org.artofsolving.jodconverter.office.OfficeManager;
import org.artofsolving.jodconverter.office.OfficeUtils; import org.artofsolving.jodconverter.office.OfficeUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@@ -31,23 +29,16 @@ import java.util.Properties;
* @date 2017/11/13 * @date 2017/11/13
*/ */
@Component @Component
@Order(Ordered.HIGHEST_PRECEDENCE) public class ConverterUtils {
public class OfficePluginManager {
private final Logger logger = LoggerFactory.getLogger(OfficePluginManager.class); private final Logger logger = LoggerFactory.getLogger(ConverterUtils.class);
private OfficeManager officeManager; private OfficeManager officeManager;
@PostConstruct @PostConstruct
public void initOfficeManager() { public void initOfficeManager() {
new Thread(this::startOfficeManager).start(); File officeHome;
} officeHome = OfficeUtils.getDefaultOfficeHome();
/**
* 启动Office组件进程
*/
private void startOfficeManager(){
File officeHome = OfficeUtils.getDefaultOfficeHome();
if (officeHome == null) { if (officeHome == null) {
throw new RuntimeException("找不到office组件请确认'office.home'配置是否有误"); throw new RuntimeException("找不到office组件请确认'office.home'配置是否有误");
} }

View File

@@ -1,18 +1,14 @@
package cn.keking.utils; package cn.keking.utils;
import cpdetector.CharsetPrinter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Objects; import java.util.Objects;
public class FileUtils { public class DeleteFileUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class); private static final Logger LOGGER = LoggerFactory.getLogger(DeleteFileUtil.class);
public static final String DEFAULT_FILE_ENCODING = "UTF-8";
/** /**
* 删除单个文件 * 删除单个文件
@@ -21,7 +17,7 @@ public class FileUtils {
* 要删除的文件的文件名 * 要删除的文件的文件名
* @return 单个文件删除成功返回true否则返回false * @return 单个文件删除成功返回true否则返回false
*/ */
public static boolean deleteFileByName(String fileName) { public static boolean deleteFile(String fileName) {
File file = new File(fileName); File file = new File(fileName);
// 如果文件路径所对应的文件存在并且是一个文件则直接删除 // 如果文件路径所对应的文件存在并且是一个文件则直接删除
if (file.exists() && file.isFile()) { if (file.exists() && file.isFile()) {
@@ -38,36 +34,6 @@ public class FileUtils {
} }
} }
/**
* 判断文件编码格式
*
* @param filePath 绝对路径
* @return 编码格式
*/
public static String getFileEncode(String filePath) {
File file = new File(filePath);
CharsetPrinter cp = new CharsetPrinter();
try {
String encoding = cp.guessEncoding(file);
LOGGER.info("检测到文件【{}】编码: {}", filePath, encoding);
return encoding;
} catch (IOException e) {
LOGGER.warn("文件编码获取失败采用默认的编码格式UTF-8", e);
return DEFAULT_FILE_ENCODING;
}
}
/**
* 根据文件路径删除文件
*
* @param filePath 绝对路径
*/
public static void deleteFileByPath(String filePath) {
File file = new File(filePath);
if (file.exists() && !file.delete()) {
LOGGER.warn("压缩包源文件删除失败:{}", filePath);
}
}
/** /**
* 删除目录及目录下的文件 * 删除目录及目录下的文件
@@ -93,20 +59,20 @@ public class FileUtils {
for (int i = 0; i < Objects.requireNonNull(files).length; i++) { for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
// 删除子文件 // 删除子文件
if (files[i].isFile()) { if (files[i].isFile()) {
flag = FileUtils.deleteFileByName(files[i].getAbsolutePath()); flag = DeleteFileUtil.deleteFile(files[i].getAbsolutePath());
if (!flag) { if (!flag) {
break; break;
} }
} else if (files[i].isDirectory()) { } else if (files[i].isDirectory()) {
// 删除子目录 // 删除子目录
flag = FileUtils.deleteDirectory(files[i].getAbsolutePath()); flag = DeleteFileUtil.deleteDirectory(files[i].getAbsolutePath());
if (!flag) { if (!flag) {
break; break;
} }
} }
} }
dirFile.delete();
if (!dirFile.delete() || !flag) { if (!flag) {
LOGGER.info("删除目录失败!"); LOGGER.info("删除目录失败!");
return false; return false;
} }

View File

@@ -5,7 +5,6 @@ import cn.keking.hutool.URLUtil;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import cn.keking.model.FileType; import cn.keking.model.FileType;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import cn.keking.service.FileHandlerService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -21,48 +20,50 @@ import java.util.UUID;
@Component @Component
public class DownloadUtils { public class DownloadUtils {
private final static Logger logger = LoggerFactory.getLogger(DownloadUtils.class); private final Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
private final String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
private final FileUtils fileUtils;
public DownloadUtils(FileUtils fileUtils) {
this.fileUtils = fileUtils;
}
private static final String URL_PARAM_FTP_USERNAME = "ftp.username"; private static final String URL_PARAM_FTP_USERNAME = "ftp.username";
private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; private static final String URL_PARAM_FTP_PASSWORD = "ftp.password";
private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding";
private final FileHandlerService fileHandlerService;
public DownloadUtils(FileHandlerService fileHandlerService) {
this.fileHandlerService = fileHandlerService;
}
/** /**
* @param fileAttribute fileAttribute * @param fileAttribute fileAttribute
* @param fileName 文件名 * @param fileName 文件名
* @return 本地文件绝对路径 * @return 本地文件绝对路径
*/ */
public ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) { public ReturnResponse<String> downLoad(FileAttribute fileAttribute, String fileName) {
String urlStr = fileAttribute.getUrl(); String urlStr = fileAttribute.getUrl();
String type = fileAttribute.getSuffix(); String type = fileAttribute.getSuffix();
ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", ""); ReturnResponse<String> response = new ReturnResponse<>(0, "下载成功!!!", "");
UUID uuid = UUID.randomUUID(); UUID uuid = UUID.randomUUID();
if (null == fileName) { if (null == fileName) {
fileName = uuid + "." + type; fileName = uuid+ "."+type;
} else { // 文件后缀不一致时以type为准(针对simText将类txt文件转为txt) } else { // 文件后缀不一致时以type为准(针对simText将类txt文件转为txt)
fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type); fileName = fileName.replace(fileName.substring(fileName.lastIndexOf(".") + 1), type);
} }
String realPath = fileDir + fileName; String realPath = fileDir + fileName;
File dirFile = new File(fileDir); File dirFile = new File(fileDir);
if (!dirFile.exists() && !dirFile.mkdirs()) { if (!dirFile.exists()) {
logger.error("创建目录【{}】失败,可能是权限不够,请检查", fileDir); dirFile.mkdirs();
} }
try { try {
URL url = new URL(urlStr); URL url = new URL(urlStr);
if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file") || url.getProtocol().toLowerCase().startsWith("http"))) { if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file")||url.getProtocol().toLowerCase().startsWith("http"))) {
byte[] bytes = getBytesFromUrl(urlStr); byte[] bytes = getBytesFromUrl(urlStr);
OutputStream os = new FileOutputStream(realPath); OutputStream os = new FileOutputStream(realPath);
saveBytesToOutStream(bytes, os); saveBytesToOutStream(bytes, os);
} else if (url.getProtocol() != null && "ftp".equalsIgnoreCase(url.getProtocol())) { } else if (url.getProtocol() != null && "ftp".equalsIgnoreCase(url.getProtocol())) {
String ftpUsername = fileHandlerService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = fileHandlerService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD); String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
String ftpControlEncoding = fileHandlerService.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING); String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding); FtpUtils.download(fileAttribute.getUrl(), realPath, ftpUsername, ftpPassword, ftpControlEncoding);
} else { } else {
response.setCode(1); response.setCode(1);
@@ -71,8 +72,8 @@ public class DownloadUtils {
} }
response.setContent(realPath); response.setContent(realPath);
response.setMsg(fileName); response.setMsg(fileName);
if (FileType.simText.equals(fileAttribute.getType())) { if(FileType.simText.equals(fileAttribute.getType())){
this.convertTextPlainFileCharsetToUtf8(realPath); convertTextPlainFileCharsetToUtf8(realPath);
} }
return response; return response;
} catch (IOException e) { } catch (IOException e) {
@@ -90,15 +91,17 @@ public class DownloadUtils {
public byte[] getBytesFromUrl(String urlStr) throws IOException { public byte[] getBytesFromUrl(String urlStr) throws IOException {
InputStream is = getInputStreamFromUrl(urlStr); InputStream is = getInputStreamFromUrl(urlStr);
if (is == null) { if (is != null) {
return getBytesFromStream(is);
} else {
urlStr = URLUtil.normalize(urlStr, true, true); urlStr = URLUtil.normalize(urlStr, true, true);
is = getInputStreamFromUrl(urlStr); is = getInputStreamFromUrl(urlStr);
if (is == null) { if (is == null) {
logger.error("文件下载异常url{}", urlStr); logger.error("文件下载异常url{}", urlStr);
throw new IOException("文件下载异常url" + urlStr); throw new IOException("文件下载异常url" + urlStr);
} }
return getBytesFromStream(is);
} }
return getBytesFromStream(is);
} }
public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException { public void saveBytesToOutStream(byte[] b, OutputStream os) throws IOException {
@@ -123,7 +126,7 @@ public class DownloadUtils {
private byte[] getBytesFromStream(InputStream is) throws IOException { private byte[] getBytesFromStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int len; int len = 0;
while ((len = is.read(buffer)) != -1) { while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len); baos.write(buffer, 0, len);
} }
@@ -133,37 +136,41 @@ public class DownloadUtils {
return b; return b;
} }
/** /**
* 转换文本文件编码为utf8 * 转换文本文件编码为utf8
* 探测源文件编码,探测到编码切不为utf8则进行转码 * 探测源文件编码,探测到编码切不为utf8则进行转码
* * @param filePath 文件路径
* @param filePath 文件路径 */
*/ private static void convertTextPlainFileCharsetToUtf8(String filePath) throws IOException {
private void convertTextPlainFileCharsetToUtf8(String filePath) throws IOException { File sourceFile = new File(filePath);
File sourceFile = new File(filePath); if(sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead()) {
if (sourceFile.exists() && sourceFile.isFile() && sourceFile.canRead()) { String encoding = null;
String encoding = FileUtils.getFileEncode(filePath); try {
if (!FileUtils.DEFAULT_FILE_ENCODING.equals(encoding)) { FileCharsetDetector.Observer observer = FileCharsetDetector.guessFileEncoding(sourceFile);
// 不为utf8,进行转 // 为准确探测到编码,不适用猜测的编
File tmpUtf8File = new File(filePath + ".utf8"); encoding = observer.isFound()?observer.getEncoding():null;
Writer writer = new OutputStreamWriter(new FileOutputStream(tmpUtf8File), StandardCharsets.UTF_8); // 为准确探测到编码,可以考虑使用GBK 大部分文件都是windows系统产生的
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile), encoding)); } catch (IOException e) {
char[] buf = new char[1024]; // 编码探测失败,
int read; e.printStackTrace();
while ((read = reader.read(buf)) > 0) { }
writer.write(buf, 0, read); if(encoding != null && !"UTF-8".equals(encoding)){
} // 不为utf8,进行转码
reader.close(); File tmpUtf8File = new File(filePath+".utf8");
writer.close(); Writer writer = new OutputStreamWriter(new FileOutputStream(tmpUtf8File), StandardCharsets.UTF_8);
// 删除源文件 Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFile),encoding));
if (!sourceFile.delete()) { char[] buf = new char[1024];
logger.error("源文件【{}】删除失败,请检查文件目录权限!", filePath); int read;
} while ((read = reader.read(buf)) > 0){
// 重命名 writer.write(buf, 0, read);
if (tmpUtf8File.renameTo(sourceFile)) {
logger.error("临时文件【{}】重命名失败,请检查文件路径权限!", tmpUtf8File.getPath());
}
}
} }
reader.close();
writer.close();
// 删除源文件
sourceFile.delete();
// 重命名
tmpUtf8File.renameTo(sourceFile);
}
} }
}
} }

View File

@@ -0,0 +1,157 @@
package cn.keking.utils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
/**
* 文本文件编码探测工具类
*
* @author HWliao
* @date 2017-12-24
*/
public class FileCharsetDetector {
/**
* 传入一个文件(File)对象,检查文件编码
*
* @param file File对象实例
* @return 文件编码若无则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public static Observer guessFileEncoding(File file)
throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector());
}
/**
* <pre>
* 获取文件的编码
* @param file
* File对象实例
* @param languageHint
* 语言提示区域代码 @see #nsPSMDetector ,取值如下:
* 1 : Japanese
* 2 : Chinese
* 3 : Simplified Chinese
* 4 : Traditional Chinese
* 5 : Korean
* 6 : Dont know(default)
* </pre>
*
* @return 文件编码egUTF-8,GBK,GB2312形式(不确定的时候,返回可能的字符编码序列)若无则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public static Observer guessFileEncoding(File file, int languageHint)
throws FileNotFoundException, IOException {
return guessFileEncoding(file, new nsDetector(languageHint));
}
/**
* 获取文件的编码
*
* @param file
* @param det
* @return
* @throws FileNotFoundException
* @throws IOException
*/
private static Observer guessFileEncoding(File file, nsDetector det)
throws FileNotFoundException, IOException {
// new Observer
Observer observer = new Observer();
// set Observer
// The Notify() will be called when a matching charset is found.
det.Init(observer);
BufferedInputStream imp = new BufferedInputStream(new FileInputStream(
file));
byte[] buf = new byte[1024];
int len;
boolean done = false;
boolean isAscii = false;
while ((len = imp.read(buf, 0, buf.length)) != -1) {
// Check if the stream is only ascii.
isAscii = det.isAscii(buf, len);
if (isAscii) {
break;
}
// DoIt if non-ascii and not done yet.
done = det.DoIt(buf, len, false);
if (done) {
break;
}
}
imp.close();
det.DataEnd();
if (isAscii) {
observer.encoding = "ASCII";
observer.found = true;
}
if (!observer.isFound()) {
String[] prob = det.getProbableCharsets();
// // 这里将可能的字符集组合起来返回
// for (int i = 0; i < prob.length; i++) {
// if (i == 0) {
// encoding = prob[i];
// } else {
// encoding += "," + prob[i];
// }
// }
if (prob.length > 0) {
// 在没有发现情况下,去第一个可能的编码
observer.encoding = prob[0];
} else {
observer.encoding = null;
}
}
return observer;
}
/**
* @author liaohongwei
* @Description: 文件字符编码观察者, 但判断出字符编码时候调用
* @date 2016年6月20日 下午2:27:06
*/
public static class Observer implements nsICharsetDetectionObserver {
/**
* @Fields encoding : 字符编码
*/
private String encoding = null;
/**
* @Fields found : 是否找到字符集
*/
private boolean found = false;
@Override
public void Notify(String charset) {
this.encoding = charset;
this.found = true;
}
public String getEncoding() {
return encoding;
}
public boolean isFound() {
return found;
}
@Override
public String toString() {
return "Observer [encoding=" + encoding + ", found=" + found + "]";
}
}
}

View File

@@ -1,31 +1,35 @@
package cn.keking.service; package cn.keking.utils;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute; import cn.keking.model.FileAttribute;
import cn.keking.model.FileType; import cn.keking.model.FileType;
import cn.keking.service.cache.CacheService; import cn.keking.service.cache.CacheService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.*; import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
*
* @author yudian-it * @author yudian-it
* @date 2017/11/13 * @date 2017/11/13
*/ */
@Component @Component
public class FileHandlerService { public class FileUtils {
private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding"); private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
private final String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
private final CacheService cacheService; private final CacheService cacheService;
public FileHandlerService(CacheService cacheService) { public FileUtils(CacheService cacheService) {
this.cacheService = cacheService; this.cacheService = cacheService;
} }
@@ -60,40 +64,99 @@ public class FileHandlerService {
public FileType typeFromUrl(String url) { public FileType typeFromUrl(String url) {
String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length()); String nonPramStr = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1); String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
return this.typeFromFileName(fileName); return typeFromFileName(fileName);
} }
private FileType typeFromFileName(String fileName) { private FileType typeFromFileName(String fileName) {
String[] simText = ConfigConstants.getSimText();
String[] media = ConfigConstants.getMedia();
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
return FileType.to(fileType); 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 (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;
} }
/** /**
* 从url中剥离出文件名 * 从url中剥离出文件名
* * @param url
* @param url 格式如http://www.com.cn/20171113164107_月度绩效表模板().xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k= * 格式如http://keking.ufile.ucloud.com.cn/20171113164107_月度绩效表模板().xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k=
* @return 文件名 * @return 文件名
*/ */
public String getFileNameFromURL(String url) { public String getFileNameFromURL(String url) {
// 因为url的参数中可能会存在/的情况所以直接url.lastIndexOf("/")会有问题 // 因为url的参数中可能会存在/的情况所以直接url.lastIndexOf("/")会有问题
// 所以先从处将url截断然后运用url.lastIndexOf("/")获取文件名 // 所以先从处将url截断然后运用url.lastIndexOf("/")获取文件名
String noQueryUrl = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length()); String noQueryUrl = url.substring(0, url.contains("?") ? url.indexOf("?"): url.length());
return noQueryUrl.substring(noQueryUrl.lastIndexOf("/") + 1); return noQueryUrl.substring(noQueryUrl.lastIndexOf("/") + 1);
} }
/** /**
* 从路径中获取文件负 * 从路径中获取文件负
* * @param path
* @param path 类似这种C:\Users\yudian-it\Downloads * 类似这种C:\Users\yudian-it\Downloads
* @return 文件名 * @return 文件名
*/ */
public String getFileNameFromPath(String path) { public String getFileNameFromPath(String path) {
return path.substring(path.lastIndexOf(File.separator) + 1); return path.substring(path.lastIndexOf(File.separator) + 1);
} }
public List<String> listPictureTypes(){
List<String> list = Lists.newArrayList();
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 = Lists.newArrayList();
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 = Lists.newArrayList();
list.add("docx");
list.add("doc");
list.add("xls");
list.add("xlsx");
list.add("ppt");
list.add("pptx");
return list;
}
/** /**
* 获取相对路径 * 获取相对路径
*
* @param absolutePath 绝对路径 * @param absolutePath 绝对路径
* @return 相对路径 * @return 相对路径
*/ */
@@ -103,55 +166,73 @@ public class FileHandlerService {
/** /**
* 添加转换后PDF缓存 * 添加转换后PDF缓存
*
* @param fileName pdf文件名 * @param fileName pdf文件名
* @param value 缓存相对路径 * @param value 缓存相对路径
*/ */
public void addConvertedFile(String fileName, String value) { public void addConvertedFile(String fileName, String value){
cacheService.putPDFCache(fileName, value); cacheService.putPDFCache(fileName, value);
} }
/** /**
* 添加转换后图片组缓存 * 添加转换后图片组缓存
*
* @param pdfFilePath pdf文件绝对路径 * @param pdfFilePath pdf文件绝对路径
* @param num 图片张数 * @param num 图片张数
*/ */
public void addConvertedPdfImage(String pdfFilePath, int num) { public void addConvertedPdfImage(String pdfFilePath, int num){
cacheService.putPdfImageCache(pdfFilePath, num); cacheService.putPdfImageCache(pdfFilePath, num);
} }
/** /**
* 获取redis中压缩包内图片文件 * 获取redis中压缩包内图片文件
*
* @param fileKey fileKey * @param fileKey fileKey
* @return 图片文件访问url列表 * @return 图片文件访问url列表
*/ */
public List<String> getImgCache(String fileKey) { public List<String> getImgCache(String fileKey){
return cacheService.getImgCache(fileKey); return cacheService.getImgCache(fileKey);
} }
/** /**
* 设置redis中压缩包内图片文件 * 设置redis中压缩包内图片文件
*
* @param fileKey fileKey * @param fileKey fileKey
* @param imgs 图片文件访问url列表 * @param imgs 图片文件访问url列表
*/ */
public void putImgCache(String fileKey, List<String> imgs) { public void putImgCache(String fileKey,List<String> imgs){
cacheService.putImgCache(fileKey, imgs); cacheService.putImgCache(fileKey, imgs);
} }
/**
* 判断文件编码格式
* @param path 绝对路径
* @return 编码格式
*/
public String getFileEncodeUTFGBK(String path){
String enc = Charset.forName("GBK").name();
File file = new File(path);
InputStream in;
try {
in = new FileInputStream(file);
byte[] b = new byte[3];
in.read(b);
in.close();
if (b[0] == -17 && b[1] == -69 && b[2] == -65) {
enc = StandardCharsets.UTF_8.name();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件编码格式为:" + enc);
return enc;
}
/** /**
* 对转换后的文件进行操作(改变编码方式) * 对转换后的文件进行操作(改变编码方式)
*
* @param outFilePath 文件绝对路径 * @param outFilePath 文件绝对路径
*/ */
public void doActionConvertedFile(String outFilePath) { public void doActionConvertedFile(String outFilePath) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
try (InputStream inputStream = new FileInputStream(outFilePath); try (InputStream inputStream = new FileInputStream(outFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CONVERTER_CHARSET))) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CONVERTER_CHARSET))){
String line; String line;
while (null != (line = reader.readLine())) { while(null != (line = reader.readLine())){
if (line.contains("charset=gb2312")) { if (line.contains("charset=gb2312")) {
line = line.replace("charset=gb2312", "charset=utf-8"); line = line.replace("charset=gb2312", "charset=utf-8");
} }
@@ -165,17 +246,15 @@ public class FileHandlerService {
e.printStackTrace(); e.printStackTrace();
} }
// 重新写入文件 // 重新写入文件
try (FileOutputStream fos = new FileOutputStream(outFilePath); try(FileOutputStream fos = new FileOutputStream(outFilePath);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
writer.write(sb.toString()); writer.write(sb.toString());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
/** /**
* 获取文件后缀 * 获取文件后缀
*
* @param url url * @param url url
* @return 文件后缀 * @return 文件后缀
*/ */
@@ -191,8 +270,7 @@ public class FileHandlerService {
/** /**
* 获取url中的参数 * 获取url中的参数
* * @param url url
* @param url url
* @param name 参数名 * @param name 参数名
* @return 参数值 * @return 参数值
*/ */
@@ -204,10 +282,10 @@ public class FileHandlerService {
} }
//每个键值为一组 //每个键值为一组
String[] arrSplit = strUrlParam.split("[&]"); String[] arrSplit = strUrlParam.split("[&]");
for (String strSplit : arrSplit) { for(String strSplit : arrSplit) {
String[] arrSplitEqual = strSplit.split("[=]"); String[] arrSplitEqual = strSplit.split("[=]");
//解析出键值 //解析出键值
if (arrSplitEqual.length > 1) { if(arrSplitEqual.length > 1) {
//正确解析 //正确解析
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]); mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
} else if (!arrSplitEqual[0].equals("")) { } else if (!arrSplitEqual[0].equals("")) {
@@ -220,7 +298,6 @@ public class FileHandlerService {
/** /**
* 去掉url中的路径留下请求参数部分 * 去掉url中的路径留下请求参数部分
*
* @param strURL url地址 * @param strURL url地址
* @return url请求参数部分 * @return url请求参数部分
*/ */
@@ -228,10 +305,10 @@ public class FileHandlerService {
String strAllParam = null; String strAllParam = null;
strURL = strURL.trim(); strURL = strURL.trim();
String[] arrSplit = strURL.split("[?]"); String[] arrSplit = strURL.split("[?]");
if (strURL.length() > 1) { if(strURL.length() > 1) {
if (arrSplit.length > 1) { if(arrSplit.length > 1) {
if (arrSplit[1] != null) { if(arrSplit[1] != null) {
strAllParam = arrSplit[1]; strAllParam=arrSplit[1];
} }
} }
} }
@@ -240,39 +317,23 @@ public class FileHandlerService {
/** /**
* 获取文件属性 * 获取文件属性
*
* @param url url * @param url url
* @return 文件属性 * @return 文件属性
*/ */
public FileAttribute getFileAttribute(String url, HttpServletRequest req) { public FileAttribute getFileAttribute(String url) {
FileAttribute attribute = new FileAttribute();
String suffix;
FileType type;
String fileName; String fileName;
String fullFileName = this.getUrlParameterReg(url, "fullfilename"); FileType type;
String suffix;
String fullFileName = getUrlParameterReg(url, "fullfilename");
if (StringUtils.hasText(fullFileName)) { if (StringUtils.hasText(fullFileName)) {
fileName = fullFileName; fileName = fullFileName;
type = this.typeFromFileName(fullFileName); type = typeFromFileName(fileName);
suffix = suffixFromFileName(fullFileName); suffix = suffixFromFileName(fileName);
} else { } else {
fileName = getFileNameFromURL(url); fileName = getFileNameFromURL(url);
type = typeFromUrl(url); type = typeFromUrl(url);
suffix = suffixFromUrl(url); suffix = suffixFromUrl(url);
} }
attribute.setType(type); return new FileAttribute(type,suffix,fileName,url);
attribute.setName(fileName);
attribute.setSuffix(suffix);
attribute.setUrl(url);
if (req != null) {
String officePreviewType = req.getParameter("officePreviewType");
String fileKey = req.getParameter("fileKey");
if (StringUtils.hasText(officePreviewType)) {
attribute.setOfficePreviewType(officePreviewType);
}
if (StringUtils.hasText(fileKey)) {
attribute.setFileKey(fileKey);
}
}
return attribute;
} }
} }

View File

@@ -1,4 +1,4 @@
package cn.keking.service; package cn.keking.utils;
import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -9,11 +9,11 @@ import java.io.File;
* @author yudian-it * @author yudian-it
*/ */
@Component @Component
public class OfficeToPdfService { public class OfficeToPdf {
private final OfficePluginManager officePluginManager; private final ConverterUtils converterUtils;
public OfficeToPdfService(OfficePluginManager officePluginManager) { public OfficeToPdf(ConverterUtils converterUtils) {
this.officePluginManager = officePluginManager; this.converterUtils = converterUtils;
} }
public void openOfficeToPDF(String inputFilePath, String outputFilePath) { public void openOfficeToPDF(String inputFilePath, String outputFilePath) {
@@ -33,7 +33,7 @@ public class OfficeToPdfService {
public void office2pdf(String inputFilePath, String outputFilePath) { public void office2pdf(String inputFilePath, String outputFilePath) {
OfficeDocumentConverter converter = officePluginManager.getDocumentConverter(); OfficeDocumentConverter converter = converterUtils.getDocumentConverter();
if (null != inputFilePath) { if (null != inputFilePath) {
File inputFile = new File(inputFilePath); File inputFile = new File(inputFilePath);
// 判断目标文件路径是否为空 // 判断目标文件路径是否为空

View File

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

View File

@@ -31,7 +31,7 @@ public class ShedulerClean {
public void clean() { public void clean() {
logger.info("Cache clean start"); logger.info("Cache clean start");
cacheService.cleanCache(); cacheService.cleanCache();
FileUtils.deleteDirectory(fileDir); DeleteFileUtil.deleteDirectory(fileDir);
logger.info("Cache clean end"); logger.info("Cache clean end");
} }
} }

View File

@@ -1,14 +1,15 @@
package cn.keking.service; package cn.keking.utils;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import cn.keking.model.FileType; import cn.keking.model.FileType;
import cn.keking.utils.FileUtils;
import cn.keking.web.filter.BaseUrlFilter; import cn.keking.web.filter.BaseUrlFilter;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.junrar.Archive; import com.github.junrar.Archive;
import com.github.junrar.exception.RarException; import com.github.junrar.exception.RarException;
import com.github.junrar.rarfile.FileHeader; import com.github.junrar.rarfile.FileHeader;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry; import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile; import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
@@ -26,34 +27,37 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
*
* @author yudian-it * @author yudian-it
* create 2017/11/27 * @date 2017/11/27
*/ */
@Component @Component
public class CompressFileReader { public class ZipReader {
static Pattern pattern = Pattern.compile("^\\d+");
private final FileUtils fileUtils;
private static final Pattern pattern = Pattern.compile("^\\d+");
private final FileHandlerService fileHandlerService;
private final String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); private final ExecutorService executors = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public CompressFileReader(FileHandlerService fileHandlerService) { public ZipReader(FileUtils fileUtils) {
this.fileHandlerService = fileHandlerService; this.fileUtils = fileUtils;
} }
public String readZipFile(String filePath, String fileKey) { public String readZipFile(String filePath,String fileKey) {
String archiveSeparator = "/"; String archiveSeparator = "/";
Map<String, FileNode> appender = new HashMap<>(); Map<String, FileNode> appender = Maps.newHashMap();
List<String> imgUrls = new LinkedList<>(); List<String> imgUrls = Lists.newArrayList();
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
String archiveFileName = fileHandlerService.getFileNameFromPath(filePath); String archiveFileName = fileUtils.getFileNameFromPath(filePath);
try { try {
ZipFile zipFile = new ZipFile(filePath, FileUtils.getFileEncode(filePath)); ZipFile zipFile = new ZipFile(filePath, fileUtils.getFileEncodeUTFGBK(filePath));
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
// 排序 // 排序
entries = sortZipEntries(entries); entries = sortZipEntries(entries);
List<Map<String, ZipArchiveEntry>> entriesToBeExtracted = new LinkedList<>(); List<Map<String, ZipArchiveEntry>> entriesToBeExtracted = Lists.newArrayList();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()){
ZipArchiveEntry entry = entries.nextElement(); ZipArchiveEntry entry = entries.nextElement();
String fullName = entry.getName(); String fullName = entry.getName();
int level = fullName.split(archiveSeparator).length; int level = fullName.split(archiveSeparator).length;
@@ -66,10 +70,10 @@ public class CompressFileReader {
entriesToBeExtracted.add(Collections.singletonMap(childName, entry)); entriesToBeExtracted.add(Collections.singletonMap(childName, entry));
} }
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
parentName = (level - 1) + "_" + parentName; parentName = (level-1) + "_" + parentName;
FileType type = fileHandlerService.typeFromUrl(childName); FileType type=fileUtils.typeFromUrl(childName);
if (type.equals(FileType.picture)) {//添加图片文件到图片列表 if (type.equals(FileType.picture)){//添加图片文件到图片列表
imgUrls.add(baseUrl + childName); imgUrls.add(baseUrl+childName);
} }
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
addNodes(appender, parentName, node); addNodes(appender, parentName, node);
@@ -77,7 +81,7 @@ public class CompressFileReader {
} }
// 开启新的线程处理文件解压 // 开启新的线程处理文件解压
executors.submit(new ZipExtractorWorker(entriesToBeExtracted, zipFile, filePath)); executors.submit(new ZipExtractorWorker(entriesToBeExtracted, zipFile, filePath));
fileHandlerService.putImgCache(fileKey, imgUrls); fileUtils.putImgCache(fileKey,imgUrls);
return new ObjectMapper().writeValueAsString(appender.get("")); return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -86,29 +90,29 @@ public class CompressFileReader {
} }
private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) { private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) {
List<ZipArchiveEntry> sortedEntries = new LinkedList<>(); List<ZipArchiveEntry> sortedEntries = Lists.newArrayList();
while (entries.hasMoreElements()) { while(entries.hasMoreElements()){
sortedEntries.add(entries.nextElement()); sortedEntries.add(entries.nextElement());
} }
sortedEntries.sort(Comparator.comparingInt(o -> o.getName().length())); sortedEntries.sort(Comparator.comparingInt(o -> o.getName().length()));
return Collections.enumeration(sortedEntries); return Collections.enumeration(sortedEntries);
} }
public String unRar(String filePath, String fileKey) { public String unRar(String filePath,String fileKey){
Map<String, FileNode> appender = new HashMap<>(); Map<String, FileNode> appender = Maps.newHashMap();
List<String> imgUrls = new ArrayList<>(); List<String> imgUrls = Lists.newArrayList();
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl = BaseUrlFilter.getBaseUrl();
try { try {
Archive archive = new Archive(new FileInputStream(filePath)); Archive archive = new Archive(new FileInputStream(new File(filePath)));
List<FileHeader> headers = archive.getFileHeaders(); List<FileHeader> headers = archive.getFileHeaders();
headers = sortedHeaders(headers); headers = sortedHeaders(headers);
String archiveFileName = fileHandlerService.getFileNameFromPath(filePath); String archiveFileName = fileUtils.getFileNameFromPath(filePath);
List<Map<String, FileHeader>> headersToBeExtracted = new ArrayList<>(); List<Map<String, FileHeader>> headersToBeExtracted = Lists.newArrayList();
for (FileHeader header : headers) { for (FileHeader header : headers) {
String fullName; String fullName;
if (header.isUnicode()) { if (header.isUnicode()) {
fullName = header.getFileNameW(); fullName = header.getFileNameW();
} else { }else {
fullName = header.getFileNameString(); fullName = header.getFileNameString();
} }
// 展示名 // 展示名
@@ -120,16 +124,16 @@ public class CompressFileReader {
headersToBeExtracted.add(Collections.singletonMap(childName, header)); headersToBeExtracted.add(Collections.singletonMap(childName, header));
} }
String parentName = getLast2FileName(fullName, "\\", archiveFileName); String parentName = getLast2FileName(fullName, "\\", archiveFileName);
FileType type = fileHandlerService.typeFromUrl(childName); FileType type = fileUtils.typeFromUrl(childName);
if (type.equals(FileType.picture)) {//添加图片文件到图片列表 if (type.equals(FileType.picture)){//添加图片文件到图片列表
imgUrls.add(baseUrl + childName); imgUrls.add(baseUrl+childName);
} }
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
addNodes(appender, parentName, node); addNodes(appender, parentName, node);
appender.put(childName, node); appender.put(childName, node);
} }
executors.submit(new RarExtractorWorker(headersToBeExtracted, archive, filePath)); executors.submit(new RarExtractorWorker(headersToBeExtracted, archive, filePath));
fileHandlerService.putImgCache(fileKey, imgUrls); fileUtils.putImgCache(fileKey,imgUrls);
return new ObjectMapper().writeValueAsString(appender.get("")); return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (RarException | IOException e) { } catch (RarException | IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -137,19 +141,19 @@ public class CompressFileReader {
return null; return null;
} }
public String read7zFile(String filePath, String fileKey) { public String read7zFile(String filePath,String fileKey) {
String archiveSeparator = "/"; String archiveSeparator = "/";
Map<String, FileNode> appender = new HashMap<>(); Map<String, FileNode> appender = Maps.newHashMap();
List<String> imgUrls = new ArrayList<>(); List<String> imgUrls = Lists.newArrayList();
String baseUrl = BaseUrlFilter.getBaseUrl(); String baseUrl= BaseUrlFilter.getBaseUrl();
String archiveFileName = fileHandlerService.getFileNameFromPath(filePath); String archiveFileName = fileUtils.getFileNameFromPath(filePath);
try { try {
SevenZFile zipFile = new SevenZFile(new File(filePath)); SevenZFile zipFile = new SevenZFile(new File(filePath));
Iterable<SevenZArchiveEntry> entries = zipFile.getEntries(); Iterable<SevenZArchiveEntry> entries = zipFile.getEntries();
// 排序 // 排序
Enumeration<SevenZArchiveEntry> newEntries = sortSevenZEntries(entries); Enumeration<SevenZArchiveEntry> newEntries = sortSevenZEntries(entries);
List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = new ArrayList<>(); List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = Lists.newArrayList();
while (newEntries.hasMoreElements()) { while (newEntries.hasMoreElements()){
SevenZArchiveEntry entry = newEntries.nextElement(); SevenZArchiveEntry entry = newEntries.nextElement();
String fullName = entry.getName(); String fullName = entry.getName();
int level = fullName.split(archiveSeparator).length; int level = fullName.split(archiveSeparator).length;
@@ -162,10 +166,10 @@ public class CompressFileReader {
entriesToBeExtracted.add(Collections.singletonMap(childName, entry)); entriesToBeExtracted.add(Collections.singletonMap(childName, entry));
} }
String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName); String parentName = getLast2FileName(fullName, archiveSeparator, archiveFileName);
parentName = (level - 1) + "_" + parentName; parentName = (level-1) + "_" + parentName;
FileType type = fileHandlerService.typeFromUrl(childName); FileType type=fileUtils.typeFromUrl(childName);
if (type.equals(FileType.picture)) {//添加图片文件到图片列表 if (type.equals(FileType.picture)){//添加图片文件到图片列表
imgUrls.add(baseUrl + childName); imgUrls.add(baseUrl+childName);
} }
FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey); FileNode node = new FileNode(originName, childName, parentName, new ArrayList<>(), directory, fileKey);
addNodes(appender, parentName, node); addNodes(appender, parentName, node);
@@ -173,7 +177,7 @@ public class CompressFileReader {
} }
// 开启新的线程处理文件解压 // 开启新的线程处理文件解压
executors.submit(new SevenZExtractorWorker(entriesToBeExtracted, filePath)); executors.submit(new SevenZExtractorWorker(entriesToBeExtracted, filePath));
fileHandlerService.putImgCache(fileKey, imgUrls); fileUtils.putImgCache(fileKey,imgUrls);
return new ObjectMapper().writeValueAsString(appender.get("")); return new ObjectMapper().writeValueAsString(appender.get(""));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -183,7 +187,7 @@ public class CompressFileReader {
private Enumeration<SevenZArchiveEntry> sortSevenZEntries(Iterable<SevenZArchiveEntry> entries) { private Enumeration<SevenZArchiveEntry> sortSevenZEntries(Iterable<SevenZArchiveEntry> entries) {
List<SevenZArchiveEntry> sortedEntries = new ArrayList<>(); List<SevenZArchiveEntry> sortedEntries = Lists.newArrayList();
for (SevenZArchiveEntry entry : entries) { for (SevenZArchiveEntry entry : entries) {
sortedEntries.add(entry); sortedEntries.add(entry);
} }
@@ -207,7 +211,7 @@ public class CompressFileReader {
List<FileHeader> sortedHeaders = new ArrayList<>(); List<FileHeader> sortedHeaders = new ArrayList<>();
Map<Integer, FileHeader> mapHeaders = new TreeMap<>(); Map<Integer, FileHeader> mapHeaders = new TreeMap<>();
headers.forEach(header -> mapHeaders.put(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length(), header)); headers.forEach(header -> mapHeaders.put(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length(), header));
for (Map.Entry<Integer, FileHeader> entry : mapHeaders.entrySet()) { for (Map.Entry<Integer, FileHeader> entry : mapHeaders.entrySet()){
for (FileHeader header : headers) { for (FileHeader header : headers) {
if (entry.getKey().equals(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length())) { if (entry.getKey().equals(new Integer(0).equals(header.getFileNameW().length()) ? header.getFileNameString().length() : header.getFileNameW().length())) {
sortedHeaders.add(header); sortedHeaders.add(header);
@@ -219,7 +223,7 @@ public class CompressFileReader {
private static String getLast2FileName(String fullName, String seperator, String rootName) { private static String getLast2FileName(String fullName, String seperator, String rootName) {
if (fullName.endsWith(seperator)) { if (fullName.endsWith(seperator)) {
fullName = fullName.substring(0, fullName.length() - 1); fullName = fullName.substring(0, fullName.length()-1);
} }
// 1.获取剩余部分 // 1.获取剩余部分
int endIndex = fullName.lastIndexOf(seperator); int endIndex = fullName.lastIndexOf(seperator);
@@ -234,7 +238,7 @@ public class CompressFileReader {
private static String getLastFileName(String fullName, String seperator) { private static String getLastFileName(String fullName, String seperator) {
if (fullName.endsWith(seperator)) { if (fullName.endsWith(seperator)) {
fullName = fullName.substring(0, fullName.length() - 1); fullName = fullName.substring(0, fullName.length()-1);
} }
String newName = fullName; String newName = fullName;
if (fullName.contains(seperator)) { if (fullName.contains(seperator)) {
@@ -245,11 +249,10 @@ public class CompressFileReader {
public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() { public static Comparator<FileNode> sortComparator = new Comparator<FileNode>() {
final Collator cmp = Collator.getInstance(Locale.US); final Collator cmp = Collator.getInstance(Locale.US);
@Override @Override
public int compare(FileNode o1, FileNode o2) { public int compare(FileNode o1, FileNode o2) {
// 判断两个对比对象是否是开头包含数字如果包含数字则获取数字并按数字真正大小进行排序 // 判断两个对比对象是否是开头包含数字如果包含数字则获取数字并按数字真正大小进行排序
BigDecimal num1, num2; BigDecimal num1,num2;
if (null != (num1 = isStartNumber(o1)) if (null != (num1 = isStartNumber(o1))
&& null != (num2 = isStartNumber(o2))) { && null != (num2 = isStartNumber(o2))) {
return num1.subtract(num2).intValue(); return num1.subtract(num2).intValue();
@@ -285,16 +288,14 @@ public class CompressFileReader {
this.childList = childList; this.childList = childList;
this.directory = directory; this.directory = directory;
} }
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory,String fileKey) {
public FileNode(String originName, String fileName, String parentFileName, List<FileNode> childList, boolean directory, String fileKey) {
this.originName = originName; this.originName = originName;
this.fileName = fileName; this.fileName = fileName;
this.parentFileName = parentFileName; this.parentFileName = parentFileName;
this.childList = childList; this.childList = childList;
this.directory = directory; this.directory = directory;
this.fileKey = fileKey; this.fileKey=fileKey;
} }
public String getFileKey() { public String getFileKey() {
return fileKey; return fileKey;
} }
@@ -382,15 +383,17 @@ public class CompressFileReader {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
FileUtils.deleteFileByPath(filePath); if (new File(filePath).exists()) {
new File(filePath).delete();
}
} }
private void extractZipFile(String childName, InputStream zipFile) { private void extractZipFile(String childName, InputStream zipFile) {
String outPath = fileDir + childName; String outPath = fileDir + childName;
try (OutputStream ot = new FileOutputStream(outPath)) { try (OutputStream ot = new FileOutputStream(outPath)){
byte[] inByte = new byte[1024]; byte[] inByte = new byte[1024];
int len; int len;
while ((-1 != (len = zipFile.read(inByte)))) { while ((-1 != (len = zipFile.read(inByte)))){
ot.write(inByte, 0, len); ot.write(inByte, 0, len);
} }
} catch (IOException e) { } catch (IOException e) {
@@ -439,7 +442,10 @@ public class CompressFileReader {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
FileUtils.deleteFileByPath(filePath);
if (new File(filePath).exists()) {
new File(filePath).delete();
}
} }
} }
@@ -468,12 +474,14 @@ public class CompressFileReader {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
FileUtils.deleteFileByPath(filePath); if (new File(filePath).exists()) {
new File(filePath).delete();
}
} }
private void extractRarFile(String childName, FileHeader header, Archive archive) { private void extractRarFile(String childName, FileHeader header, Archive archive) {
String outPath = fileDir + childName; String outPath = fileDir + childName;
try (OutputStream ot = new FileOutputStream(outPath)) { try(OutputStream ot = new FileOutputStream(outPath)) {
archive.extractFile(header, ot); archive.extractFile(header, ot);
} catch (IOException | RarException e) { } catch (IOException | RarException e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -3,7 +3,8 @@ package cn.keking.web.controller;
import cn.keking.config.ConfigConstants; import cn.keking.config.ConfigConstants;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import cn.keking.model.ReturnResponse; import cn.keking.model.ReturnResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -15,7 +16,10 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.util.*; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/** /**
* *
@@ -80,14 +84,10 @@ public class FileController {
@RequestMapping(value = "listFiles", method = RequestMethod.GET) @RequestMapping(value = "listFiles", method = RequestMethod.GET)
public String getFiles() throws JsonProcessingException { public String getFiles() throws JsonProcessingException {
List<Map<String, String>> list = new ArrayList<>(); List<Map<String, String>> list = Lists.newArrayList();
File file = new File(fileDir + demoPath); File file = new File(fileDir + demoPath);
if (file.exists()) { if (file.exists()) {
Arrays.stream(Objects.requireNonNull(file.listFiles())).forEach(file1 -> { Arrays.stream(Objects.requireNonNull(file.listFiles())).forEach(file1 -> list.add(ImmutableMap.of("fileName", demoDir + "/" + file1.getName())));
Map<String, String> fileName = new HashMap();
fileName.put("fileName", demoDir + "/" + file1.getName());
list.add(fileName);
});
} }
return new ObjectMapper().writeValueAsString(list); return new ObjectMapper().writeValueAsString(list);
} }

Some files were not shown because too many files have changed in this diff Show More