mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-09-18 17:48:08 +08:00
优化:重构代码
This commit is contained in:
@@ -45,7 +45,7 @@ public class ConfigConstants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Value("${cache.enabled:true}")
|
@Value("${cache.enabled:true}")
|
||||||
public static void setCacheEnabled(String cacheEnabled) {
|
public void setCacheEnabled(String cacheEnabled) {
|
||||||
setCacheEnabledValueValue(Boolean.parseBoolean(cacheEnabled));
|
setCacheEnabledValueValue(Boolean.parseBoolean(cacheEnabled));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -42,7 +42,7 @@ public class FileConvertQueueTask {
|
|||||||
logger.info("队列处理文件转换任务启动完成 ");
|
logger.info("队列处理文件转换任务启动完成 ");
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
@@ -99,6 +99,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public Map<String, String> getPDFCache() {
|
public Map<String, String> getPDFCache() {
|
||||||
Map<String, String> result = new HashMap<>();
|
Map<String, String> result = new HashMap<>();
|
||||||
try{
|
try{
|
||||||
@@ -110,6 +111,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public String getPDFCache(String key) {
|
public String getPDFCache(String key) {
|
||||||
String result = "";
|
String result = "";
|
||||||
try{
|
try{
|
||||||
@@ -122,6 +124,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public Map<String, List<String>> getImgCache() {
|
public Map<String, List<String>> getImgCache() {
|
||||||
Map<String, List<String>> result = new HashMap<>();
|
Map<String, List<String>> result = new HashMap<>();
|
||||||
try{
|
try{
|
||||||
@@ -133,6 +136,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public List<String> getImgCache(String key) {
|
public List<String> getImgCache(String key) {
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
Map<String, List<String>> map;
|
Map<String, List<String>> map;
|
||||||
@@ -145,17 +149,8 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Integer> getPdfImageCaches() {
|
|
||||||
Map<String, Integer> map = new HashMap<>();
|
|
||||||
try{
|
|
||||||
map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
|
|
||||||
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
|
||||||
LOGGER.error("Get from RocksDB Exception" + e);
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public Integer getPdfImageCache(String key) {
|
public Integer getPdfImageCache(String key) {
|
||||||
Integer result = 0;
|
Integer result = 0;
|
||||||
Map<String, Integer> map;
|
Map<String, Integer> map;
|
||||||
@@ -200,6 +195,18 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||||||
return blockingQueue.take();
|
return blockingQueue.take();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private Map<String, Integer> getPdfImageCaches() {
|
||||||
|
Map<String, Integer> map = new HashMap<>();
|
||||||
|
try{
|
||||||
|
map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
|
||||||
|
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
||||||
|
LOGGER.error("Get from RocksDB Exception" + e);
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private byte[] toByteArray (Object obj) throws IOException {
|
private byte[] toByteArray (Object obj) throws IOException {
|
||||||
byte[] bytes;
|
byte[] bytes;
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
@@ -4,12 +4,11 @@ import cn.keking.config.ConfigConstants;
|
|||||||
import cn.keking.model.FileAttribute;
|
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.CadToPdf;
|
import cn.keking.utils.CadUtils;
|
||||||
import cn.keking.utils.DownloadUtils;
|
import cn.keking.utils.DownloadUtils;
|
||||||
import cn.keking.utils.FileUtils;
|
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.beans.factory.annotation.Autowired;
|
|
||||||
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;
|
||||||
@@ -23,23 +22,29 @@ import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
|
|||||||
@Service
|
@Service
|
||||||
public class CadFilePreviewImpl implements FilePreview {
|
public class CadFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
private String fileDir = ConfigConstants.getFileDir();
|
private final FileUtils fileUtils;
|
||||||
|
|
||||||
@Autowired
|
private final DownloadUtils downloadUtils;
|
||||||
private FileUtils fileUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final CadUtils cadUtils;
|
||||||
private DownloadUtils downloadUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final PdfUtils pdfUtils;
|
||||||
private CadToPdf cadToPdf;
|
|
||||||
|
|
||||||
@Autowired
|
public CadFilePreviewImpl(FileUtils fileUtils,
|
||||||
private PdfUtils pdfUtils;
|
DownloadUtils downloadUtils,
|
||||||
|
CadUtils cadUtils,
|
||||||
|
PdfUtils pdfUtils) {
|
||||||
|
this.fileUtils = fileUtils;
|
||||||
|
this.downloadUtils = downloadUtils;
|
||||||
|
this.cadUtils = cadUtils;
|
||||||
|
this.pdfUtils = pdfUtils;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
||||||
|
private static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
|
||||||
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||||
|
|
||||||
public static final String OFFICE_PREVIEW_TYPE_PDF = "pdf";
|
|
||||||
public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
|
||||||
public static final String OFFICE_PREVIEW_TYPE_ALLIMAGES = "allImages";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
@@ -49,7 +54,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||||||
String suffix=fileAttribute.getSuffix();
|
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 = fileDir + pdfName;
|
String outFilePath = FILE_DIR + pdfName;
|
||||||
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
||||||
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||||
String filePath;
|
String filePath;
|
||||||
@@ -61,7 +66,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
filePath = response.getContent();
|
filePath = response.getContent();
|
||||||
if (StringUtils.hasText(outFilePath)) {
|
if (StringUtils.hasText(outFilePath)) {
|
||||||
boolean convertResult = cadToPdf.cadToPdf(filePath, outFilePath);
|
boolean convertResult = cadUtils.cadToPdf(filePath, outFilePath);
|
||||||
if (!convertResult) {
|
if (!convertResult) {
|
||||||
model.addAttribute("fileType", suffix);
|
model.addAttribute("fileType", suffix);
|
||||||
model.addAttribute("msg", "cad文件转换异常,请联系管理员");
|
model.addAttribute("msg", "cad文件转换异常,请联系管理员");
|
||||||
@@ -73,7 +78,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.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, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
|
||||||
}
|
}
|
||||||
model.addAttribute("pdfUrl", pdfName);
|
model.addAttribute("pdfUrl", pdfName);
|
||||||
|
@@ -7,7 +7,6 @@ import cn.keking.service.FilePreview;
|
|||||||
import cn.keking.utils.DownloadUtils;
|
import cn.keking.utils.DownloadUtils;
|
||||||
import cn.keking.utils.FileUtils;
|
import cn.keking.utils.FileUtils;
|
||||||
import cn.keking.utils.ZipReader;
|
import cn.keking.utils.ZipReader;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
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;
|
||||||
@@ -17,16 +16,21 @@ import org.springframework.util.StringUtils;
|
|||||||
* Content :处理压缩包文件
|
* Content :处理压缩包文件
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class CompressFilePreviewImpl implements FilePreview{
|
public class CompressFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
@Autowired
|
private final FileUtils fileUtils;
|
||||||
FileUtils fileUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final DownloadUtils downloadUtils;
|
||||||
DownloadUtils downloadUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final ZipReader zipReader;
|
||||||
ZipReader zipReader;
|
|
||||||
|
public CompressFilePreviewImpl(FileUtils fileUtils,
|
||||||
|
DownloadUtils downloadUtils,
|
||||||
|
ZipReader zipReader) {
|
||||||
|
this.fileUtils = fileUtils;
|
||||||
|
this.downloadUtils = downloadUtils;
|
||||||
|
this.zipReader = zipReader;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
|
@@ -2,8 +2,6 @@ package cn.keking.service.impl;
|
|||||||
|
|
||||||
import cn.keking.model.FileAttribute;
|
import cn.keking.model.FileAttribute;
|
||||||
import cn.keking.service.FilePreview;
|
import cn.keking.service.FilePreview;
|
||||||
import cn.keking.utils.FileUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
/**
|
/**
|
||||||
@@ -15,9 +13,6 @@ import org.springframework.ui.Model;
|
|||||||
@Service
|
@Service
|
||||||
public class MediaFilePreviewImpl implements FilePreview {
|
public class MediaFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
FileUtils fileUtils;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
model.addAttribute("mediaUrl", url);
|
model.addAttribute("mediaUrl", url);
|
||||||
|
@@ -9,7 +9,6 @@ import cn.keking.utils.FileUtils;
|
|||||||
import cn.keking.utils.OfficeToPdf;
|
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.beans.factory.annotation.Autowired;
|
|
||||||
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;
|
||||||
@@ -23,23 +22,27 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class OfficeFilePreviewImpl implements FilePreview {
|
public class OfficeFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
@Autowired
|
private final FileUtils fileUtils;
|
||||||
FileUtils fileUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final PdfUtils pdfUtils;
|
||||||
PdfUtils pdfUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final DownloadUtils downloadUtils;
|
||||||
DownloadUtils downloadUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final OfficeToPdf officeToPdf;
|
||||||
private OfficeToPdf officeToPdf;
|
|
||||||
|
|
||||||
String fileDir = ConfigConstants.getFileDir();
|
public OfficeFilePreviewImpl(FileUtils fileUtils,
|
||||||
|
PdfUtils pdfUtils,
|
||||||
|
DownloadUtils downloadUtils,
|
||||||
|
OfficeToPdf officeToPdf) {
|
||||||
|
this.fileUtils = fileUtils;
|
||||||
|
this.pdfUtils = pdfUtils;
|
||||||
|
this.downloadUtils = downloadUtils;
|
||||||
|
this.officeToPdf = officeToPdf;
|
||||||
|
}
|
||||||
|
|
||||||
public static final String OFFICE_PREVIEW_TYPE_PDF = "pdf";
|
|
||||||
public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
|
||||||
public static final String OFFICE_PREVIEW_TYPE_ALLIMAGES = "allImages";
|
public static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
|
||||||
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
@@ -50,10 +53,10 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||||||
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 = fileDir + pdfName;
|
String outFilePath = FILE_DIR + pdfName;
|
||||||
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
||||||
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||||
String filePath = fileDir + fileName;
|
String filePath;
|
||||||
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
|
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
|
||||||
if (0 != response.getCode()) {
|
if (0 != response.getCode()) {
|
||||||
model.addAttribute("fileType", suffix);
|
model.addAttribute("fileType", suffix);
|
||||||
@@ -73,7 +76,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.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, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
|
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
|
||||||
}
|
}
|
||||||
model.addAttribute("pdfUrl", pdfName);
|
model.addAttribute("pdfUrl", pdfName);
|
||||||
|
@@ -2,8 +2,6 @@ package cn.keking.service.impl;
|
|||||||
|
|
||||||
import cn.keking.model.FileAttribute;
|
import cn.keking.model.FileAttribute;
|
||||||
import cn.keking.service.FilePreview;
|
import cn.keking.service.FilePreview;
|
||||||
import cn.keking.utils.FileUtils;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
|
||||||
@@ -13,9 +11,6 @@ import org.springframework.ui.Model;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class OtherFilePreviewImpl implements FilePreview {
|
public class OtherFilePreviewImpl implements FilePreview {
|
||||||
@Autowired
|
|
||||||
FileUtils fileUtils;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
model.addAttribute("fileType",fileAttribute.getSuffix());
|
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||||
|
@@ -8,7 +8,6 @@ import cn.keking.utils.DownloadUtils;
|
|||||||
import cn.keking.utils.FileUtils;
|
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.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
|
||||||
@@ -19,19 +18,21 @@ import java.util.List;
|
|||||||
* Content :处理pdf文件
|
* Content :处理pdf文件
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class PdfFilePreviewImpl implements FilePreview{
|
public class PdfFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
|
private final FileUtils fileUtils;
|
||||||
|
|
||||||
@Autowired
|
private final PdfUtils pdfUtils;
|
||||||
FileUtils fileUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final DownloadUtils downloadUtils;
|
||||||
PdfUtils pdfUtils;
|
|
||||||
|
|
||||||
@Autowired
|
public PdfFilePreviewImpl(FileUtils fileUtils,
|
||||||
DownloadUtils downloadUtils;
|
PdfUtils pdfUtils,
|
||||||
|
DownloadUtils downloadUtils) {
|
||||||
String fileDir = ConfigConstants.getFileDir();
|
this.fileUtils = fileUtils;
|
||||||
|
this.pdfUtils = pdfUtils;
|
||||||
|
this.downloadUtils = downloadUtils;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
@@ -40,8 +41,8 @@ public class PdfFilePreviewImpl implements FilePreview{
|
|||||||
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 pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
||||||
String outFilePath = fileDir + pdfName;
|
String outFilePath;
|
||||||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType)) {
|
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
|
||||||
//当文件不存在时,就去下载
|
//当文件不存在时,就去下载
|
||||||
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
|
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
|
||||||
if (0 != response.getCode()) {
|
if (0 != response.getCode()) {
|
||||||
|
@@ -6,7 +6,6 @@ import cn.keking.service.FilePreview;
|
|||||||
import cn.keking.utils.DownloadUtils;
|
import cn.keking.utils.DownloadUtils;
|
||||||
import cn.keking.utils.FileUtils;
|
import cn.keking.utils.FileUtils;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
@@ -20,11 +19,15 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class PictureFilePreviewImpl implements FilePreview {
|
public class PictureFilePreviewImpl implements FilePreview {
|
||||||
|
|
||||||
@Autowired
|
private final FileUtils fileUtils;
|
||||||
FileUtils fileUtils;
|
|
||||||
|
|
||||||
@Autowired
|
private final DownloadUtils downloadUtils;
|
||||||
DownloadUtils downloadUtils;
|
|
||||||
|
public PictureFilePreviewImpl(FileUtils fileUtils,
|
||||||
|
DownloadUtils downloadUtils) {
|
||||||
|
this.fileUtils = fileUtils;
|
||||||
|
this.downloadUtils = downloadUtils;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||||
|
@@ -18,9 +18,9 @@ import java.io.OutputStream;
|
|||||||
* @since 2019/11/21 14:34
|
* @since 2019/11/21 14:34
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class CadToPdf {
|
public class CadUtils {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(CadToPdf.class);
|
private final Logger logger = LoggerFactory.getLogger(CadUtils.class);
|
||||||
|
|
||||||
public boolean cadToPdf(String inputFilePath, String outputFilePath) {
|
public boolean cadToPdf(String inputFilePath, String outputFilePath) {
|
||||||
com.aspose.cad.Image cadImage = com.aspose.cad.Image.load(inputFilePath);
|
com.aspose.cad.Image cadImage = com.aspose.cad.Image.load(inputFilePath);
|
@@ -59,7 +59,7 @@ public class FileController {
|
|||||||
if (!outFile.exists()) {
|
if (!outFile.exists()) {
|
||||||
outFile.mkdirs();
|
outFile.mkdirs();
|
||||||
}
|
}
|
||||||
logger.info("上传文件:{}", outFile.getAbsolutePath());
|
logger.info("上传文件:{}", fileDir + demoPath + fileName);
|
||||||
try(InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(fileDir + demoPath + fileName)) {
|
try(InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(fileDir + demoPath + fileName)) {
|
||||||
StreamUtils.copy(in, out);
|
StreamUtils.copy(in, out);
|
||||||
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null));
|
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null));
|
||||||
|
Reference in New Issue
Block a user