预览逻辑重构
parent
d4b11a4056
commit
c1802b2487
|
@ -1,5 +1,7 @@
|
|||
package cn.keking.model;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
|
||||
/**
|
||||
* Created by kl on 2018/1/17.
|
||||
* Content :
|
||||
|
@ -14,6 +16,8 @@ public class FileAttribute {
|
|||
|
||||
private String url;
|
||||
|
||||
private String officePreviewType = ConfigConstants.getOfficePreviewType();
|
||||
|
||||
public FileAttribute() {
|
||||
}
|
||||
|
||||
|
@ -24,6 +28,22 @@ public class FileAttribute {
|
|||
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 getOfficePreviewType() {
|
||||
return officePreviewType;
|
||||
}
|
||||
|
||||
public void setOfficePreviewType(String officePreviewType) {
|
||||
this.officePreviewType = officePreviewType;
|
||||
}
|
||||
|
||||
public FileType getType() {
|
||||
return type;
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class FileConvertQueueTask {
|
|||
try {
|
||||
url = cacheService.takeQueueTask();
|
||||
if(url != null){
|
||||
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
|
||||
FileAttribute fileAttribute = fileUtils.getFileAttribute(url,null);
|
||||
FileType fileType = fileAttribute.getType();
|
||||
logger.info("正在处理预览转换任务,url:{},预览类型:{}", url, fileType);
|
||||
if(fileType.equals(FileType.compress) || fileType.equals(FileType.office) || fileType.equals(FileType.cad)) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.keking.service;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
|
@ -8,5 +9,9 @@ import org.springframework.ui.Model;
|
|||
* Content :
|
||||
*/
|
||||
public interface FilePreview {
|
||||
|
||||
String TEXT_TYPE = "textType";
|
||||
String DEFAULT_TEXT_TYPE = "simText";
|
||||
|
||||
String filePreviewHandle(String url, Model model, FileAttribute fileAttribute);
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ public class MarkdownFilePreviewImpl implements FilePreview {
|
|||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
model.addAttribute("textType","markdown");
|
||||
model.addAttribute(TEXT_TYPE,"markdown");
|
||||
return simTextFilePreview.filePreviewHandle(url, model, fileAttribute);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
String suffix=fileAttribute.getSuffix();
|
||||
String fileName=fileAttribute.getName();
|
||||
String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
|
||||
String officePreviewType = fileAttribute.getOfficePreviewType();
|
||||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
||||
String outFilePath = FILE_DIR + pdfName;
|
||||
|
|
|
@ -24,6 +24,7 @@ public class SimTextFilePreviewImpl implements FilePreview {
|
|||
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
|
||||
public SimTextFilePreviewImpl(DownloadUtils downloadUtils) {
|
||||
this.downloadUtils = downloadUtils;
|
||||
}
|
||||
|
@ -46,6 +47,9 @@ public class SimTextFilePreviewImpl implements FilePreview {
|
|||
model.addAttribute("fileType", fileAttribute.getSuffix());
|
||||
return "fileNotSupported";
|
||||
}
|
||||
if (!model.containsAttribute(TEXT_TYPE)) {
|
||||
model.addAttribute(TEXT_TYPE, DEFAULT_TEXT_TYPE);
|
||||
}
|
||||
return "txt";
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ public class XmlFilePreviewImpl implements FilePreview {
|
|||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
model.addAttribute("textType","xml");
|
||||
model.addAttribute(TEXT_TYPE,"xml");
|
||||
return simTextFilePreview.filePreviewHandle(url, model, fileAttribute);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.google.common.collect.Lists;
|
|||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -17,7 +18,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author yudian-it
|
||||
* @date 2017/11/13
|
||||
*/
|
||||
|
@ -80,10 +80,10 @@ public class FileUtils {
|
|||
if (listOfficeTypes().contains(fileType.toLowerCase())) {
|
||||
return FileType.office;
|
||||
}
|
||||
if("md".equalsIgnoreCase(fileType)){
|
||||
if ("md".equalsIgnoreCase(fileType)) {
|
||||
return FileType.markdown;
|
||||
}
|
||||
if("xml".equalsIgnoreCase(fileType)){
|
||||
if ("xml".equalsIgnoreCase(fileType)) {
|
||||
return FileType.xml;
|
||||
}
|
||||
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
|
||||
|
@ -100,30 +100,31 @@ public class FileUtils {
|
|||
}
|
||||
return FileType.other;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从url中剥离出文件名
|
||||
* @param url
|
||||
* 格式如:http://keking.ufile.ucloud.com.cn/20171113164107_月度绩效表模板(新).xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k=
|
||||
*
|
||||
* @param url 格式如:http://keking.ufile.ucloud.com.cn/20171113164107_月度绩效表模板(新).xls?UCloudPublicKey=ucloudtangshd@weifenf.com14355492830001993909323&Expires=&Signature=I D1NOFtAJSPT16E6imv6JWuq0k=
|
||||
* @return 文件名
|
||||
*/
|
||||
public String getFileNameFromURL(String url) {
|
||||
// 因为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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从路径中获取文件负
|
||||
* @param path
|
||||
* 类似这种:C:\Users\yudian-it\Downloads
|
||||
*
|
||||
* @param path 类似这种:C:\Users\yudian-it\Downloads
|
||||
* @return 文件名
|
||||
*/
|
||||
public String getFileNameFromPath(String path) {
|
||||
return path.substring(path.lastIndexOf(File.separator) + 1);
|
||||
}
|
||||
|
||||
public List<String> listPictureTypes(){
|
||||
public List<String> listPictureTypes() {
|
||||
List<String> list = Lists.newArrayList();
|
||||
list.add("jpg");
|
||||
list.add("jpeg");
|
||||
|
@ -135,7 +136,7 @@ public class FileUtils {
|
|||
return list;
|
||||
}
|
||||
|
||||
public List<String> listArchiveTypes(){
|
||||
public List<String> listArchiveTypes() {
|
||||
List<String> list = Lists.newArrayList();
|
||||
list.add("rar");
|
||||
list.add("zip");
|
||||
|
@ -160,6 +161,7 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* 获取相对路径
|
||||
*
|
||||
* @param absolutePath 绝对路径
|
||||
* @return 相对路径
|
||||
*/
|
||||
|
@ -169,45 +171,51 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* 添加转换后PDF缓存
|
||||
*
|
||||
* @param fileName pdf文件名
|
||||
* @param value 缓存相对路径
|
||||
*/
|
||||
public void addConvertedFile(String fileName, String value){
|
||||
public void addConvertedFile(String fileName, String value) {
|
||||
cacheService.putPDFCache(fileName, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加转换后图片组缓存
|
||||
*
|
||||
* @param pdfFilePath pdf文件绝对路径
|
||||
* @param num 图片张数
|
||||
*/
|
||||
public void addConvertedPdfImage(String pdfFilePath, int num){
|
||||
public void addConvertedPdfImage(String pdfFilePath, int num) {
|
||||
cacheService.putPdfImageCache(pdfFilePath, num);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取redis中压缩包内图片文件
|
||||
*
|
||||
* @param fileKey fileKey
|
||||
* @return 图片文件访问url列表
|
||||
*/
|
||||
public List<String> getImgCache(String fileKey){
|
||||
public List<String> getImgCache(String fileKey) {
|
||||
return cacheService.getImgCache(fileKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置redis中压缩包内图片文件
|
||||
*
|
||||
* @param fileKey fileKey
|
||||
* @param imgs 图片文件访问url列表
|
||||
*/
|
||||
public void putImgCache(String fileKey,List<String> imgs){
|
||||
public void putImgCache(String fileKey, List<String> imgs) {
|
||||
cacheService.putImgCache(fileKey, imgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件编码格式
|
||||
*
|
||||
* @param path 绝对路径
|
||||
* @return 编码格式
|
||||
*/
|
||||
public String getFileEncodeUTFGBK(String path){
|
||||
public String getFileEncodeUTFGBK(String path) {
|
||||
String enc = Charset.forName("GBK").name();
|
||||
File file = new File(path);
|
||||
InputStream in;
|
||||
|
@ -228,14 +236,15 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* 对转换后的文件进行操作(改变编码方式)
|
||||
*
|
||||
* @param outFilePath 文件绝对路径
|
||||
*/
|
||||
public void doActionConvertedFile(String outFilePath) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
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;
|
||||
while(null != (line = reader.readLine())){
|
||||
while (null != (line = reader.readLine())) {
|
||||
if (line.contains("charset=gb2312")) {
|
||||
line = line.replace("charset=gb2312", "charset=utf-8");
|
||||
}
|
||||
|
@ -249,15 +258,17 @@ public class FileUtils {
|
|||
e.printStackTrace();
|
||||
}
|
||||
// 重新写入文件
|
||||
try(FileOutputStream fos = new FileOutputStream(outFilePath);
|
||||
try (FileOutputStream fos = new FileOutputStream(outFilePath);
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
|
||||
writer.write(sb.toString());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件后缀
|
||||
*
|
||||
* @param url url
|
||||
* @return 文件后缀
|
||||
*/
|
||||
|
@ -273,6 +284,7 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* 获取url中的参数
|
||||
*
|
||||
* @param url url
|
||||
* @param name 参数名
|
||||
* @return 参数值
|
||||
|
@ -285,10 +297,10 @@ public class FileUtils {
|
|||
}
|
||||
//每个键值为一组
|
||||
String[] arrSplit = strUrlParam.split("[&]");
|
||||
for(String strSplit : arrSplit) {
|
||||
for (String strSplit : arrSplit) {
|
||||
String[] arrSplitEqual = strSplit.split("[=]");
|
||||
//解析出键值
|
||||
if(arrSplitEqual.length > 1) {
|
||||
if (arrSplitEqual.length > 1) {
|
||||
//正确解析
|
||||
mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
|
||||
} else if (!arrSplitEqual[0].equals("")) {
|
||||
|
@ -301,6 +313,7 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* 去掉url中的路径,留下请求参数部分
|
||||
*
|
||||
* @param strURL url地址
|
||||
* @return url请求参数部分
|
||||
*/
|
||||
|
@ -308,10 +321,10 @@ public class FileUtils {
|
|||
String strAllParam = null;
|
||||
strURL = strURL.trim();
|
||||
String[] arrSplit = strURL.split("[?]");
|
||||
if(strURL.length() > 1) {
|
||||
if(arrSplit.length > 1) {
|
||||
if(arrSplit[1] != null) {
|
||||
strAllParam=arrSplit[1];
|
||||
if (strURL.length() > 1) {
|
||||
if (arrSplit.length > 1) {
|
||||
if (arrSplit[1] != null) {
|
||||
strAllParam = arrSplit[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -320,23 +333,33 @@ public class FileUtils {
|
|||
|
||||
/**
|
||||
* 获取文件属性
|
||||
*
|
||||
* @param url url
|
||||
* @return 文件属性
|
||||
*/
|
||||
public FileAttribute getFileAttribute(String url) {
|
||||
String fileName;
|
||||
FileType type;
|
||||
public FileAttribute getFileAttribute(String url, HttpServletRequest req) {
|
||||
FileAttribute attribute = new FileAttribute();
|
||||
String suffix;
|
||||
String fullFileName = getUrlParameterReg(url, "fullfilename");
|
||||
if (StringUtils.hasText(fullFileName)) {
|
||||
fileName = fullFileName;
|
||||
type = typeFromFileName(fileName);
|
||||
suffix = suffixFromFileName(fileName);
|
||||
attribute.setName(fullFileName);
|
||||
FileType type = typeFromFileName(fullFileName);
|
||||
attribute.setType(type);
|
||||
suffix = suffixFromFileName(fullFileName);
|
||||
} else {
|
||||
fileName = getFileNameFromURL(url);
|
||||
type = typeFromUrl(url);
|
||||
|
||||
String fileName = getFileNameFromURL(url);
|
||||
FileType type = typeFromUrl(url);
|
||||
attribute.setName(fileName);
|
||||
attribute.setType(type);
|
||||
suffix = suffixFromUrl(url);
|
||||
}
|
||||
return new FileAttribute(type,suffix,fileName,url);
|
||||
attribute.setSuffix(suffix);
|
||||
if (req != null) {
|
||||
String officePreviewType = req.getParameter("officePreviewType");
|
||||
|
||||
attribute.setOfficePreviewType(officePreviewType);
|
||||
}
|
||||
return attribute;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package cn.keking.web.controller;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.service.FilePreview;
|
||||
import cn.keking.service.FilePreviewFactory;
|
||||
|
@ -49,10 +48,7 @@ public class OnlinePreviewController {
|
|||
|
||||
@RequestMapping(value = "/onlinePreview")
|
||||
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
||||
FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
|
||||
req.setAttribute("fileKey", req.getParameter("fileKey"));
|
||||
model.addAttribute("pdfDownloadDisable", ConfigConstants.getPdfDownloadDisable());
|
||||
model.addAttribute("officePreviewType", req.getParameter("officePreviewType"));
|
||||
FileAttribute fileAttribute = fileUtils.getFileAttribute(url,req);
|
||||
FilePreview filePreview = previewFactory.get(fileAttribute);
|
||||
logger.info("预览文件url:{},previewType:{}", url, fileAttribute.getType());
|
||||
return filePreview.filePreviewHandle(url, model, fileAttribute);
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
package cn.keking.web.filter;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.config.WatermarkConfigConstants;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author chenjh
|
||||
* @since 2020/5/13 18:34
|
||||
*/
|
||||
public class WatermarkConfigFilter implements Filter {
|
||||
public class AttributeSetFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) {
|
||||
|
@ -18,6 +20,26 @@ public class WatermarkConfigFilter implements Filter {
|
|||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
|
||||
this.setWatermarkAttribute(request);
|
||||
this.setFileAttribute(request);
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置办公文具预览逻辑需要的属性
|
||||
* @param request request
|
||||
*/
|
||||
private void setFileAttribute(ServletRequest request){
|
||||
HttpServletRequest httpRequest = (HttpServletRequest)request;
|
||||
request.setAttribute("pdfDownloadDisable", ConfigConstants.getPdfDownloadDisable());
|
||||
request.setAttribute("fileKey", httpRequest.getParameter("fileKey"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置水印属性
|
||||
* @param request request
|
||||
*/
|
||||
private void setWatermarkAttribute(ServletRequest request) {
|
||||
String watermarkTxt = request.getParameter("watermarkTxt");
|
||||
request.setAttribute("watermarkTxt", watermarkTxt != null ? watermarkTxt : WatermarkConfigConstants.getWatermarkTxt());
|
||||
request.setAttribute("watermarkXSpace", WatermarkConfigConstants.getWatermarkXSpace());
|
||||
|
@ -29,7 +51,6 @@ public class WatermarkConfigFilter implements Filter {
|
|||
request.setAttribute("watermarkWidth", WatermarkConfigConstants.getWatermarkWidth());
|
||||
request.setAttribute("watermarkHeight", WatermarkConfigConstants.getWatermarkHeight());
|
||||
request.setAttribute("watermarkAngle", WatermarkConfigConstants.getWatermarkAngle());
|
||||
filterChain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
|
@ -57,7 +57,7 @@ public class FilterConfiguration {
|
|||
Set<String> filterUri = new HashSet<>();
|
||||
filterUri.add("/onlinePreview");
|
||||
filterUri.add("/picturesPreview");
|
||||
WatermarkConfigFilter filter = new WatermarkConfigFilter();
|
||||
AttributeSetFilter filter = new AttributeSetFilter();
|
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
||||
registrationBean.setFilter(filter);
|
||||
registrationBean.setUrlPatterns(filterUri);
|
||||
|
|
Loading…
Reference in New Issue