Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 38429b476e |
@ -10,7 +10,6 @@ import java.util.Map;
|
|||||||
* Content :文件类型,文本,office,压缩包等等
|
* Content :文件类型,文本,office,压缩包等等
|
||||||
*/
|
*/
|
||||||
public enum FileType {
|
public enum FileType {
|
||||||
|
|
||||||
picture("pictureFilePreviewImpl"),
|
picture("pictureFilePreviewImpl"),
|
||||||
compress("compressFilePreviewImpl"),
|
compress("compressFilePreviewImpl"),
|
||||||
office("officeFilePreviewImpl"),
|
office("officeFilePreviewImpl"),
|
||||||
@ -20,13 +19,12 @@ public enum FileType {
|
|||||||
media("mediaFilePreviewImpl"),
|
media("mediaFilePreviewImpl"),
|
||||||
markdown("markdownFilePreviewImpl"),
|
markdown("markdownFilePreviewImpl"),
|
||||||
xml("xmlFilePreviewImpl"),
|
xml("xmlFilePreviewImpl"),
|
||||||
flv("flvFilePreviewImpl"),
|
|
||||||
cad("cadFilePreviewImpl");
|
cad("cadFilePreviewImpl");
|
||||||
|
|
||||||
private static final String[] OFFICE_TYPES = {"docx", "doc", "xls", "xlsx", "ppt", "pptx"};
|
private static final String[] OFFICE_TYPES = {"docx", "doc", "xls", "xlsx", "ppt", "pptx"};
|
||||||
private static final String[] PICTURE_TYPES = {"jpg", "jpeg", "png", "gif", "bmp", "ico", "RAW"};
|
private static final String[] PICTURE_TYPES = {"jpg", "jpeg", "png", "gif", "bmp", "ico", "RAW"};
|
||||||
private static final String[] ARCHIVE_TYPES = {"rar", "zip", "jar", "7-zip", "tar", "gzip", "7z"};
|
private static final String[] ARCHIVE_TYPES = {"rar", "zip", "jar", "7-zip", "tar", "gzip", "7z"};
|
||||||
private static final String[] SSIM_TEXT_TYPES = ConfigConstants.getSimText();
|
private static final String[] SIMTEXT_TYPES = ConfigConstants.getSimText();
|
||||||
private static final String[] MEDIA_TYPES = ConfigConstants.getMedia();
|
private static final String[] MEDIA_TYPES = ConfigConstants.getMedia();
|
||||||
private static final Map<String, FileType> FILE_TYPE_MAPPER = new HashMap<>();
|
private static final Map<String, FileType> FILE_TYPE_MAPPER = new HashMap<>();
|
||||||
|
|
||||||
@ -40,7 +38,7 @@ public enum FileType {
|
|||||||
for (String archive : ARCHIVE_TYPES) {
|
for (String archive : ARCHIVE_TYPES) {
|
||||||
FILE_TYPE_MAPPER.put(archive, FileType.compress);
|
FILE_TYPE_MAPPER.put(archive, FileType.compress);
|
||||||
}
|
}
|
||||||
for (String text : SSIM_TEXT_TYPES) {
|
for (String text : SIMTEXT_TYPES) {
|
||||||
FILE_TYPE_MAPPER.put(text, FileType.simText);
|
FILE_TYPE_MAPPER.put(text, FileType.simText);
|
||||||
}
|
}
|
||||||
for (String media : MEDIA_TYPES) {
|
for (String media : MEDIA_TYPES) {
|
||||||
@ -50,8 +48,6 @@ public enum FileType {
|
|||||||
FILE_TYPE_MAPPER.put("xml", FileType.xml);
|
FILE_TYPE_MAPPER.put("xml", FileType.xml);
|
||||||
FILE_TYPE_MAPPER.put("pdf", FileType.pdf);
|
FILE_TYPE_MAPPER.put("pdf", FileType.pdf);
|
||||||
FILE_TYPE_MAPPER.put("dwg", FileType.cad);
|
FILE_TYPE_MAPPER.put("dwg", FileType.cad);
|
||||||
FILE_TYPE_MAPPER.put("flv", FileType.flv);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static FileType to(String fileType){
|
private static FileType to(String fileType){
|
||||||
|
|||||||
@ -8,16 +8,5 @@ import org.springframework.ui.Model;
|
|||||||
* Content :
|
* Content :
|
||||||
*/
|
*/
|
||||||
public interface FilePreview {
|
public interface FilePreview {
|
||||||
|
|
||||||
String FLV_FILE_PREVIEW_PAGE = "flv";
|
|
||||||
String PDF_FILE_PREVIEW_PAGE = "pdf";
|
|
||||||
String COMPRESS_FILE_PREVIEW_PAGE = "compress";
|
|
||||||
String MEDIA_FILE_PREVIEW_PAGE = "media";
|
|
||||||
String PICTURE_FILE_PREVIEW_PAGE = "picture";
|
|
||||||
String OFFICE_PICTURE_FILE_PREVIEW_PAGE = "officePicture";
|
|
||||||
String TXT_FILE_PREVIEW_PAGE = "txt";
|
|
||||||
String EXEL_FILE_PREVIEW_PAGE = "html";
|
|
||||||
String NOT_SUPPORTED_FILE_PAGE = "fileNotSupported";
|
|
||||||
|
|
||||||
String filePreviewHandle(String url, Model model, FileAttribute fileAttribute);
|
String filePreviewHandle(String url, Model model, FileAttribute fileAttribute);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,11 +25,9 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||||||
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||||
|
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
|
||||||
|
|
||||||
public CadFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
public CadFilePreviewImpl(FileHandlerService fileHandlerService) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
this.otherFilePreview = otherFilePreview;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,6 +35,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||||||
// 预览Type,参数传了就取参数的,没传取系统默认
|
// 预览Type,参数传了就取参数的,没传取系统默认
|
||||||
String officePreviewType = model.asMap().get("officePreviewType") == null ? ConfigConstants.getOfficePreviewType() : model.asMap().get("officePreviewType").toString();
|
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 fileName = fileAttribute.getName();
|
String fileName = fileAttribute.getName();
|
||||||
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;
|
||||||
@ -45,13 +44,17 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||||||
String filePath;
|
String filePath;
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("fileType", suffix);
|
||||||
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
filePath = response.getContent();
|
filePath = response.getContent();
|
||||||
if (StringUtils.hasText(outFilePath)) {
|
if (StringUtils.hasText(outFilePath)) {
|
||||||
boolean convertResult = fileHandlerService.cadToPdf(filePath, outFilePath);
|
boolean convertResult = fileHandlerService.cadToPdf(filePath, outFilePath);
|
||||||
if (!convertResult) {
|
if (!convertResult) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "cad文件转换异常,请联系管理员");
|
model.addAttribute("fileType", suffix);
|
||||||
|
model.addAttribute("msg", "cad文件转换异常,请联系管理员");
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
if (ConfigConstants.isCacheEnabled()) {
|
if (ConfigConstants.isCacheEnabled()) {
|
||||||
// 加入缓存
|
// 加入缓存
|
||||||
@ -60,10 +63,10 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
||||||
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE,otherFilePreview);
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE);
|
||||||
}
|
}
|
||||||
model.addAttribute("pdfUrl", pdfName);
|
model.addAttribute("pdfUrl", pdfName);
|
||||||
return PDF_FILE_PREVIEW_PAGE;
|
return "pdf";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -20,12 +20,10 @@ public class CompressFilePreviewImpl implements FilePreview {
|
|||||||
|
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final CompressFileReader compressFileReader;
|
private final CompressFileReader compressFileReader;
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
|
||||||
|
|
||||||
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, CompressFileReader compressFileReader, OtherFilePreviewImpl otherFilePreview) {
|
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, CompressFileReader compressFileReader) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
this.compressFileReader = compressFileReader;
|
this.compressFileReader = compressFileReader;
|
||||||
this.otherFilePreview = otherFilePreview;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,7 +35,9 @@ public class CompressFilePreviewImpl implements FilePreview {
|
|||||||
if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
|
if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("fileType", suffix);
|
||||||
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
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)) {
|
||||||
@ -55,9 +55,11 @@ public class CompressFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
if (fileTree != null && !"null".equals(fileTree)) {
|
if (fileTree != null && !"null".equals(fileTree)) {
|
||||||
model.addAttribute("fileTree", fileTree);
|
model.addAttribute("fileTree", fileTree);
|
||||||
return COMPRESS_FILE_PREVIEW_PAGE;
|
return "compress";
|
||||||
} else {
|
} else {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "压缩文件类型不受支持,尝试在压缩的时候选择RAR4格式");
|
model.addAttribute("fileType", suffix);
|
||||||
|
model.addAttribute("msg", "压缩文件类型不受支持,尝试在压缩的时候选择RAR4格式");
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +0,0 @@
|
|||||||
package cn.keking.service.impl;
|
|
||||||
|
|
||||||
import cn.keking.model.FileAttribute;
|
|
||||||
import cn.keking.service.FilePreview;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.ui.Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author : kl
|
|
||||||
* create : 2020-12-27 2:50 下午
|
|
||||||
* flv文件预览处理实现
|
|
||||||
**/
|
|
||||||
@Service
|
|
||||||
public class FlvFilePreviewImpl implements FilePreview {
|
|
||||||
|
|
||||||
private final MediaFilePreviewImpl mediaFilePreview;
|
|
||||||
|
|
||||||
public FlvFilePreviewImpl(MediaFilePreviewImpl mediaFilePreview) {
|
|
||||||
this.mediaFilePreview = mediaFilePreview;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
|
||||||
mediaFilePreview.filePreviewHandle(url,model,fileAttribute);
|
|
||||||
return FLV_FILE_PREVIEW_PAGE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -19,11 +19,9 @@ import org.springframework.ui.Model;
|
|||||||
public class MediaFilePreviewImpl implements FilePreview {
|
public class MediaFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
|
||||||
|
|
||||||
public MediaFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
public MediaFilePreviewImpl(FileHandlerService fileHandlerService) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
this.otherFilePreview = otherFilePreview;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -31,8 +29,10 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||||||
// 不是http开头,浏览器不能直接访问,需下载到本地
|
// 不是http开头,浏览器不能直接访问,需下载到本地
|
||||||
if (url != null && !url.toLowerCase().startsWith("http")) {
|
if (url != null && !url.toLowerCase().startsWith("http")) {
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileAttribute.getName());
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileAttribute.getName());
|
||||||
if (response.isFailure()) {
|
if (!response.isSuccess()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("fileType", fileAttribute.getSuffix());
|
||||||
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
return "fileNotSupported";
|
||||||
} else {
|
} else {
|
||||||
model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + fileHandlerService.getRelativePath(response.getContent()));
|
model.addAttribute("mediaUrl", BaseUrlFilter.getBaseUrl() + fileHandlerService.getRelativePath(response.getContent()));
|
||||||
}
|
}
|
||||||
@ -40,7 +40,11 @@ public class MediaFilePreviewImpl implements FilePreview {
|
|||||||
model.addAttribute("mediaUrl", url);
|
model.addAttribute("mediaUrl", url);
|
||||||
}
|
}
|
||||||
model.addAttribute("mediaUrl", url);
|
model.addAttribute("mediaUrl", url);
|
||||||
return MEDIA_FILE_PREVIEW_PAGE;
|
String suffix = fileAttribute.getSuffix();
|
||||||
|
if ("flv".equalsIgnoreCase(suffix)) {
|
||||||
|
return "flv";
|
||||||
|
}
|
||||||
|
return "media";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -27,12 +27,10 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||||||
|
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final OfficeToPdfService officeToPdfService;
|
private final OfficeToPdfService officeToPdfService;
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
|
||||||
|
|
||||||
public OfficeFilePreviewImpl(FileHandlerService fileHandlerService, OfficeToPdfService officeToPdfService, OtherFilePreviewImpl otherFilePreview) {
|
public OfficeFilePreviewImpl(FileHandlerService fileHandlerService, OfficeToPdfService officeToPdfService) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
this.officeToPdfService = officeToPdfService;
|
this.officeToPdfService = officeToPdfService;
|
||||||
this.otherFilePreview = otherFilePreview;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -40,8 +38,8 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||||||
// 预览Type,参数传了就取参数的,没传取系统默认
|
// 预览Type,参数传了就取参数的,没传取系统默认
|
||||||
String officePreviewType = fileAttribute.getOfficePreviewType();
|
String officePreviewType = fileAttribute.getOfficePreviewType();
|
||||||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||||
String suffix = fileAttribute.getSuffix();
|
String suffix=fileAttribute.getSuffix();
|
||||||
String fileName = fileAttribute.getName();
|
String fileName=fileAttribute.getName();
|
||||||
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
|
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
|
||||||
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;
|
||||||
@ -50,7 +48,9 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||||||
String filePath;
|
String filePath;
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("fileType", suffix);
|
||||||
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
filePath = response.getContent();
|
filePath = response.getContent();
|
||||||
if (StringUtils.hasText(outFilePath)) {
|
if (StringUtils.hasText(outFilePath)) {
|
||||||
@ -66,23 +66,25 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
||||||
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE, otherFilePreview);
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE);
|
||||||
}
|
}
|
||||||
model.addAttribute("pdfUrl", pdfName);
|
model.addAttribute("pdfUrl", pdfName);
|
||||||
return isHtml ? EXEL_FILE_PREVIEW_PAGE : PDF_FILE_PREVIEW_PAGE;
|
return isHtml ? "html" : "pdf";
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String baseUrl, String pdfName, String outFilePath, FileHandlerService fileHandlerService, String officePreviewTypeImage, OtherFilePreviewImpl otherFilePreview) {
|
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String baseUrl, String pdfName, String outFilePath, FileHandlerService fileHandlerService, String officePreviewTypeImage) {
|
||||||
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl);
|
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl);
|
||||||
if (imageUrls == null || imageUrls.size() < 1) {
|
if (imageUrls == null || imageUrls.size() < 1) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
|
model.addAttribute("msg", "office转图片异常,请联系管理员");
|
||||||
|
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
model.addAttribute("imgurls", imageUrls);
|
model.addAttribute("imgurls", imageUrls);
|
||||||
model.addAttribute("currentUrl", imageUrls.get(0));
|
model.addAttribute("currentUrl", imageUrls.get(0));
|
||||||
if (officePreviewTypeImage.equals(officePreviewType)) {
|
if (officePreviewTypeImage.equals(officePreviewType)) {
|
||||||
return OFFICE_PICTURE_FILE_PREVIEW_PAGE;
|
return "officePicture";
|
||||||
} else {
|
} else {
|
||||||
return PICTURE_FILE_PREVIEW_PAGE;
|
return "picture";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,21 +11,10 @@ import org.springframework.ui.Model;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class OtherFilePreviewImpl implements FilePreview {
|
public class OtherFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
return this.notSupportedFile(model,fileAttribute,"系统还不支持该格式文件的在线预览");
|
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||||
}
|
model.addAttribute("msg", "系统还不支持该格式文件的在线预览");
|
||||||
|
return "fileNotSupported";
|
||||||
/**
|
|
||||||
* 通用的预览失败,导向到不支持的文件响应页面
|
|
||||||
*
|
|
||||||
* @return 页面
|
|
||||||
*/
|
|
||||||
public String notSupportedFile(Model model, FileAttribute fileAttribute, String errMsg) {
|
|
||||||
model.addAttribute("fileType", fileAttribute.getSuffix());
|
|
||||||
model.addAttribute("msg", errMsg);
|
|
||||||
return NOT_SUPPORTED_FILE_PAGE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,17 +20,16 @@ import java.util.List;
|
|||||||
public class PdfFilePreviewImpl implements FilePreview {
|
public class PdfFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
|
||||||
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||||
|
|
||||||
public PdfFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
public PdfFilePreviewImpl(FileHandlerService fileHandlerService) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
this.otherFilePreview = otherFilePreview;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 suffix=fileAttribute.getSuffix();
|
||||||
|
String fileName=fileAttribute.getName();
|
||||||
String officePreviewType = fileAttribute.getOfficePreviewType();
|
String officePreviewType = fileAttribute.getOfficePreviewType();
|
||||||
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";
|
||||||
@ -40,7 +39,9 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||||||
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("fileType", suffix);
|
||||||
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
outFilePath = response.getContent();
|
outFilePath = response.getContent();
|
||||||
if (ConfigConstants.isCacheEnabled()) {
|
if (ConfigConstants.isCacheEnabled()) {
|
||||||
@ -50,14 +51,16 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl);
|
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, baseUrl);
|
||||||
if (imageUrls == null || imageUrls.size() < 1) {
|
if (imageUrls == null || imageUrls.size() < 1) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
|
model.addAttribute("msg", "pdf转图片异常,请联系管理员");
|
||||||
|
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
model.addAttribute("imgurls", imageUrls);
|
model.addAttribute("imgurls", imageUrls);
|
||||||
model.addAttribute("currentUrl", imageUrls.get(0));
|
model.addAttribute("currentUrl", imageUrls.get(0));
|
||||||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
||||||
return OFFICE_PICTURE_FILE_PREVIEW_PAGE;
|
return "officePicture";
|
||||||
} else {
|
} else {
|
||||||
return PICTURE_FILE_PREVIEW_PAGE;
|
return "picture";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 不是http开头,浏览器不能直接访问,需下载到本地
|
// 不是http开头,浏览器不能直接访问,需下载到本地
|
||||||
@ -65,7 +68,9 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||||||
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("fileType", suffix);
|
||||||
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
model.addAttribute("pdfUrl", fileHandlerService.getRelativePath(response.getContent()));
|
model.addAttribute("pdfUrl", fileHandlerService.getRelativePath(response.getContent()));
|
||||||
if (ConfigConstants.isCacheEnabled()) {
|
if (ConfigConstants.isCacheEnabled()) {
|
||||||
@ -79,6 +84,6 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||||||
model.addAttribute("pdfUrl", url);
|
model.addAttribute("pdfUrl", url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return PDF_FILE_PREVIEW_PAGE;
|
return "pdf";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,11 +19,9 @@ import java.util.List;
|
|||||||
public class PictureFilePreviewImpl implements FilePreview {
|
public class PictureFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
|
||||||
|
|
||||||
public PictureFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
public PictureFilePreviewImpl(FileHandlerService fileHandlerService) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
this.otherFilePreview = otherFilePreview;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -39,7 +37,9 @@ public class PictureFilePreviewImpl implements FilePreview {
|
|||||||
if (url != null && !url.toLowerCase().startsWith("http")) {
|
if (url != null && !url.toLowerCase().startsWith("http")) {
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("fileType", fileAttribute.getSuffix());
|
||||||
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
return "fileNotSupported";
|
||||||
} else {
|
} else {
|
||||||
String file = fileHandlerService.getRelativePath(response.getContent());
|
String file = fileHandlerService.getRelativePath(response.getContent());
|
||||||
imgUrls.clear();
|
imgUrls.clear();
|
||||||
@ -51,6 +51,6 @@ public class PictureFilePreviewImpl implements FilePreview {
|
|||||||
model.addAttribute("imgurls", imgUrls);
|
model.addAttribute("imgurls", imgUrls);
|
||||||
model.addAttribute("currentUrl", url);
|
model.addAttribute("currentUrl", url);
|
||||||
}
|
}
|
||||||
return PICTURE_FILE_PREVIEW_PAGE;
|
return "picture";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,30 +23,28 @@ public class SimTextFilePreviewImpl implements FilePreview {
|
|||||||
public static final String TEXT_TYPE = "textType";
|
public static final String TEXT_TYPE = "textType";
|
||||||
public static final String DEFAULT_TEXT_TYPE = "simText";
|
public static final String DEFAULT_TEXT_TYPE = "simText";
|
||||||
|
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
|
||||||
|
|
||||||
public SimTextFilePreviewImpl(OtherFilePreviewImpl otherFilePreview) {
|
|
||||||
this.otherFilePreview = otherFilePreview;
|
|
||||||
}
|
|
||||||
|
|
||||||
@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 (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
model.addAttribute("msg", response.getMsg());
|
||||||
|
model.addAttribute("fileType", fileAttribute.getSuffix());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
File originFile = new File(response.getContent());
|
File originFile = new File(response.getContent());
|
||||||
String xmlString = FileUtils.readFileToString(originFile, StandardCharsets.UTF_8);
|
String xmlString = FileUtils.readFileToString(originFile, StandardCharsets.UTF_8);
|
||||||
model.addAttribute("textData", Base64Utils.encodeToString(xmlString.getBytes()));
|
model.addAttribute("textData", Base64Utils.encodeToString(xmlString.getBytes()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage());
|
model.addAttribute("msg", e.getLocalizedMessage());
|
||||||
|
model.addAttribute("fileType", fileAttribute.getSuffix());
|
||||||
|
return "fileNotSupported";
|
||||||
}
|
}
|
||||||
if (!model.containsAttribute(TEXT_TYPE)) {
|
if (!model.containsAttribute(TEXT_TYPE)) {
|
||||||
model.addAttribute(TEXT_TYPE, DEFAULT_TEXT_TYPE);
|
model.addAttribute(TEXT_TYPE, DEFAULT_TEXT_TYPE);
|
||||||
}
|
}
|
||||||
return TXT_FILE_PREVIEW_PAGE;
|
return "txt";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -20,6 +20,7 @@ public class XmlFilePreviewImpl implements FilePreview {
|
|||||||
this.simTextFilePreview = simTextFilePreview;
|
this.simTextFilePreview = simTextFilePreview;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
model.addAttribute(TEXT_TYPE,"xml");
|
model.addAttribute(TEXT_TYPE,"xml");
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import cn.keking.service.FilePreviewFactory;
|
|||||||
import cn.keking.service.cache.CacheService;
|
import cn.keking.service.cache.CacheService;
|
||||||
import cn.keking.utils.DownloadUtils;
|
import cn.keking.utils.DownloadUtils;
|
||||||
import cn.keking.service.FileHandlerService;
|
import cn.keking.service.FileHandlerService;
|
||||||
|
import com.thoughtworks.xstream.core.util.Base64JavaUtilCodec;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
@ -19,6 +20,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.net.URLDecoder;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -43,24 +45,25 @@ public class OnlinePreviewController {
|
|||||||
@RequestMapping(value = "/onlinePreview")
|
@RequestMapping(value = "/onlinePreview")
|
||||||
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
public String onlinePreview(String url, Model model, HttpServletRequest req) {
|
||||||
String fileUrl = new String(Base64Utils.decodeFromString(url));
|
String fileUrl = new String(Base64Utils.decodeFromString(url));
|
||||||
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req);
|
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl,req);
|
||||||
FilePreview filePreview = previewFactory.get(fileAttribute);
|
FilePreview filePreview = previewFactory.get(fileAttribute);
|
||||||
logger.info("预览文件url:{},previewType:{}", fileUrl, fileAttribute.getType());
|
logger.info("预览文件url:{},previewType:{}", fileUrl, fileAttribute.getType());
|
||||||
return filePreview.filePreviewHandle(fileUrl, model, fileAttribute);
|
return filePreview.filePreviewHandle(fileUrl, model, fileAttribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/picturesPreview")
|
@RequestMapping(value = "/picturesPreview")
|
||||||
public String picturesPreview(String urls, Model model, HttpServletRequest req) throws UnsupportedEncodingException {
|
public String picturesPreview(Model model, HttpServletRequest req) throws UnsupportedEncodingException {
|
||||||
String fileUrls = new String(Base64Utils.decodeFromString(urls));
|
String urls = req.getParameter("urls");
|
||||||
String currentUrl = req.getParameter("currentUrl");
|
String currentUrl = req.getParameter("currentUrl");
|
||||||
logger.info("预览文件url:{},urls:{}", fileUrls, urls);
|
logger.info("预览文件url:{},urls:{}", currentUrl, urls);
|
||||||
// 路径转码
|
// 路径转码
|
||||||
String decodedCurrentUrl = new String(Base64Utils.decodeFromString(currentUrl));
|
String decodedUrl = URLDecoder.decode(urls, "utf-8");
|
||||||
|
String decodedCurrentUrl = URLDecoder.decode(currentUrl, "utf-8");
|
||||||
// 抽取文件并返回文件列表
|
// 抽取文件并返回文件列表
|
||||||
String[] imgs = fileUrls.split("\\|");
|
String[] imgs = decodedUrl.split("\\|");
|
||||||
List<String> imgUrls = Arrays.asList(imgs);
|
List<String> imgUrls = Arrays.asList(imgs);
|
||||||
model.addAttribute("imgUrls", imgUrls);
|
model.addAttribute("imgUrls", imgUrls);
|
||||||
model.addAttribute("currentUrl", decodedCurrentUrl);
|
model.addAttribute("currentUrl",decodedCurrentUrl);
|
||||||
return "picture";
|
return "picture";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +71,7 @@ public class OnlinePreviewController {
|
|||||||
* 根据url获取文件内容
|
* 根据url获取文件内容
|
||||||
* 当pdfjs读取存在跨域问题的文件时将通过此接口读取
|
* 当pdfjs读取存在跨域问题的文件时将通过此接口读取
|
||||||
*
|
*
|
||||||
* @param urlPath url
|
* @param urlPath url
|
||||||
* @param response response
|
* @param response response
|
||||||
*/
|
*/
|
||||||
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
|
@RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
|
||||||
@ -84,7 +87,6 @@ public class OnlinePreviewController {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过api接口入队
|
* 通过api接口入队
|
||||||
*
|
|
||||||
* @param url 请编码后在入队
|
* @param url 请编码后在入队
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/addTask")
|
@RequestMapping("/addTask")
|
||||||
|
|||||||
Reference in New Issue
Block a user