优化:重构代码
parent
f620c00785
commit
1edf4d83f1
|
@ -45,7 +45,7 @@ public class ConfigConstants {
|
|||
}
|
||||
|
||||
@Value("${cache.enabled:true}")
|
||||
public static void setCacheEnabled(String cacheEnabled) {
|
||||
public void setCacheEnabled(String cacheEnabled) {
|
||||
setCacheEnabledValueValue(Boolean.parseBoolean(cacheEnabled));
|
||||
}
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ public class FileConvertQueueTask {
|
|||
logger.info("队列处理文件转换任务启动完成 ");
|
||||
}
|
||||
|
||||
class ConvertTask implements Runnable {
|
||||
static class ConvertTask implements Runnable {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ConvertTask.class);
|
||||
|
||||
|
|
|
@ -99,6 +99,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, String> getPDFCache() {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
try{
|
||||
|
@ -110,6 +111,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public String getPDFCache(String key) {
|
||||
String result = "";
|
||||
try{
|
||||
|
@ -122,6 +124,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, List<String>> getImgCache() {
|
||||
Map<String, List<String>> result = new HashMap<>();
|
||||
try{
|
||||
|
@ -133,6 +136,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getImgCache(String key) {
|
||||
List<String> result = new ArrayList<>();
|
||||
Map<String, List<String>> map;
|
||||
|
@ -145,17 +149,8 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||
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
|
||||
@SuppressWarnings("unchecked")
|
||||
public Integer getPdfImageCache(String key) {
|
||||
Integer result = 0;
|
||||
Map<String, Integer> map;
|
||||
|
@ -200,6 +195,18 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
|||
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 {
|
||||
byte[] bytes;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
|
|
|
@ -4,12 +4,11 @@ import cn.keking.config.ConfigConstants;
|
|||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import cn.keking.service.FilePreview;
|
||||
import cn.keking.utils.CadToPdf;
|
||||
import cn.keking.utils.CadUtils;
|
||||
import cn.keking.utils.DownloadUtils;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import cn.keking.utils.PdfUtils;
|
||||
import cn.keking.web.filter.BaseUrlFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
@ -23,23 +22,29 @@ import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
|
|||
@Service
|
||||
public class CadFilePreviewImpl implements FilePreview {
|
||||
|
||||
private String fileDir = ConfigConstants.getFileDir();
|
||||
private final FileUtils fileUtils;
|
||||
|
||||
@Autowired
|
||||
private FileUtils fileUtils;
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
@Autowired
|
||||
private DownloadUtils downloadUtils;
|
||||
private final CadUtils cadUtils;
|
||||
|
||||
@Autowired
|
||||
private CadToPdf cadToPdf;
|
||||
private final PdfUtils pdfUtils;
|
||||
|
||||
@Autowired
|
||||
private PdfUtils pdfUtils;
|
||||
public CadFilePreviewImpl(FileUtils fileUtils,
|
||||
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
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
|
@ -49,7 +54,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||
String suffix=fileAttribute.getSuffix();
|
||||
String fileName=fileAttribute.getName();
|
||||
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()) {
|
||||
String filePath;
|
||||
|
@ -61,7 +66,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
|||
}
|
||||
filePath = response.getContent();
|
||||
if (StringUtils.hasText(outFilePath)) {
|
||||
boolean convertResult = cadToPdf.cadToPdf(filePath, outFilePath);
|
||||
boolean convertResult = cadUtils.cadToPdf(filePath, outFilePath);
|
||||
if (!convertResult) {
|
||||
model.addAttribute("fileType", suffix);
|
||||
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);
|
||||
}
|
||||
model.addAttribute("pdfUrl", pdfName);
|
||||
|
|
|
@ -7,7 +7,6 @@ import cn.keking.service.FilePreview;
|
|||
import cn.keking.utils.DownloadUtils;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import cn.keking.utils.ZipReader;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
@ -17,16 +16,21 @@ import org.springframework.util.StringUtils;
|
|||
* Content :处理压缩包文件
|
||||
*/
|
||||
@Service
|
||||
public class CompressFilePreviewImpl implements FilePreview{
|
||||
public class CompressFilePreviewImpl implements FilePreview {
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
private final FileUtils fileUtils;
|
||||
|
||||
@Autowired
|
||||
DownloadUtils downloadUtils;
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
@Autowired
|
||||
ZipReader zipReader;
|
||||
private final ZipReader zipReader;
|
||||
|
||||
public CompressFilePreviewImpl(FileUtils fileUtils,
|
||||
DownloadUtils downloadUtils,
|
||||
ZipReader zipReader) {
|
||||
this.fileUtils = fileUtils;
|
||||
this.downloadUtils = downloadUtils;
|
||||
this.zipReader = zipReader;
|
||||
}
|
||||
|
||||
@Override
|
||||
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.service.FilePreview;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
/**
|
||||
|
@ -15,9 +13,6 @@ import org.springframework.ui.Model;
|
|||
@Service
|
||||
public class MediaFilePreviewImpl implements FilePreview {
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
model.addAttribute("mediaUrl", url);
|
||||
|
|
|
@ -9,7 +9,6 @@ import cn.keking.utils.FileUtils;
|
|||
import cn.keking.utils.OfficeToPdf;
|
||||
import cn.keking.utils.PdfUtils;
|
||||
import cn.keking.web.filter.BaseUrlFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
@ -23,23 +22,27 @@ import java.util.List;
|
|||
@Service
|
||||
public class OfficeFilePreviewImpl implements FilePreview {
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
private final FileUtils fileUtils;
|
||||
|
||||
@Autowired
|
||||
PdfUtils pdfUtils;
|
||||
private final PdfUtils pdfUtils;
|
||||
|
||||
@Autowired
|
||||
DownloadUtils downloadUtils;
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
@Autowired
|
||||
private OfficeToPdf officeToPdf;
|
||||
private final 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_ALLIMAGES = "allImages";
|
||||
public static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
|
||||
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
|
@ -50,10 +53,10 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||
String fileName=fileAttribute.getName();
|
||||
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
|
||||
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()) {
|
||||
String filePath = fileDir + fileName;
|
||||
String filePath;
|
||||
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
|
||||
if (0 != response.getCode()) {
|
||||
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);
|
||||
}
|
||||
model.addAttribute("pdfUrl", pdfName);
|
||||
|
|
|
@ -2,8 +2,6 @@ package cn.keking.service.impl;
|
|||
|
||||
import cn.keking.model.FileAttribute;
|
||||
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.ui.Model;
|
||||
|
||||
|
@ -13,9 +11,6 @@ import org.springframework.ui.Model;
|
|||
*/
|
||||
@Service
|
||||
public class OtherFilePreviewImpl implements FilePreview {
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||
|
|
|
@ -8,7 +8,6 @@ import cn.keking.utils.DownloadUtils;
|
|||
import cn.keking.utils.FileUtils;
|
||||
import cn.keking.utils.PdfUtils;
|
||||
import cn.keking.web.filter.BaseUrlFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
|
@ -19,19 +18,21 @@ import java.util.List;
|
|||
* Content :处理pdf文件
|
||||
*/
|
||||
@Service
|
||||
public class PdfFilePreviewImpl implements FilePreview{
|
||||
public class PdfFilePreviewImpl implements FilePreview {
|
||||
|
||||
private final FileUtils fileUtils;
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
private final PdfUtils pdfUtils;
|
||||
|
||||
@Autowired
|
||||
PdfUtils pdfUtils;
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
@Autowired
|
||||
DownloadUtils downloadUtils;
|
||||
|
||||
String fileDir = ConfigConstants.getFileDir();
|
||||
public PdfFilePreviewImpl(FileUtils fileUtils,
|
||||
PdfUtils pdfUtils,
|
||||
DownloadUtils downloadUtils) {
|
||||
this.fileUtils = fileUtils;
|
||||
this.pdfUtils = pdfUtils;
|
||||
this.downloadUtils = downloadUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
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 baseUrl = BaseUrlFilter.getBaseUrl();
|
||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
||||
String outFilePath = fileDir + pdfName;
|
||||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType)) {
|
||||
String outFilePath;
|
||||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
|
||||
//当文件不存在时,就去下载
|
||||
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
|
||||
if (0 != response.getCode()) {
|
||||
|
|
|
@ -6,7 +6,6 @@ import cn.keking.service.FilePreview;
|
|||
import cn.keking.utils.DownloadUtils;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
|
@ -20,11 +19,15 @@ import java.util.List;
|
|||
@Service
|
||||
public class PictureFilePreviewImpl implements FilePreview {
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
private final FileUtils fileUtils;
|
||||
|
||||
@Autowired
|
||||
DownloadUtils downloadUtils;
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
public PictureFilePreviewImpl(FileUtils fileUtils,
|
||||
DownloadUtils downloadUtils) {
|
||||
this.fileUtils = fileUtils;
|
||||
this.downloadUtils = downloadUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
|
|
|
@ -18,9 +18,9 @@ import java.io.OutputStream;
|
|||
* @since 2019/11/21 14:34
|
||||
*/
|
||||
@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) {
|
||||
com.aspose.cad.Image cadImage = com.aspose.cad.Image.load(inputFilePath);
|
|
@ -59,7 +59,7 @@ public class FileController {
|
|||
if (!outFile.exists()) {
|
||||
outFile.mkdirs();
|
||||
}
|
||||
logger.info("上传文件:{}", outFile.getAbsolutePath());
|
||||
logger.info("上传文件:{}", fileDir + demoPath + fileName);
|
||||
try(InputStream in = file.getInputStream(); OutputStream out = new FileOutputStream(fileDir + demoPath + fileName)) {
|
||||
StreamUtils.copy(in, out);
|
||||
return new ObjectMapper().writeValueAsString(new ReturnResponse<String>(0, "SUCCESS", null));
|
||||
|
|
Loading…
Reference in New Issue