优化:重构大量代码,修复异常
This commit is contained in:
@ -6,7 +6,6 @@ import cn.keking.service.cache.CacheService;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import javax.annotation.PostConstruct;
|
||||
@ -18,36 +17,44 @@ import java.util.concurrent.Executors;
|
||||
* Content :消费队列中的转换文件
|
||||
*/
|
||||
@Service
|
||||
public class FileConverQueueTask {
|
||||
public class FileConvertQueueTask {
|
||||
|
||||
Logger logger= LoggerFactory.getLogger(getClass());
|
||||
public static final String queueTaskName="FileConverQueueTask";
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
FilePreviewFactory previewFactory;
|
||||
private final FilePreviewFactory previewFactory;
|
||||
|
||||
@Autowired
|
||||
CacheService cacheService;
|
||||
private final CacheService cacheService;
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
private final FileUtils fileUtils;
|
||||
|
||||
public FileConvertQueueTask(FilePreviewFactory previewFactory,
|
||||
CacheService cacheService,
|
||||
FileUtils fileUtils) {
|
||||
this.previewFactory = previewFactory;
|
||||
this.cacheService = cacheService;
|
||||
this.fileUtils=fileUtils;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void startTask(){
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(3);
|
||||
executorService.submit(new ConverTask(previewFactory, cacheService, fileUtils));
|
||||
executorService.submit(new ConvertTask(previewFactory, cacheService, fileUtils));
|
||||
logger.info("队列处理文件转换任务启动完成 ");
|
||||
}
|
||||
|
||||
class ConverTask implements Runnable{
|
||||
class ConvertTask implements Runnable {
|
||||
|
||||
FilePreviewFactory previewFactory;
|
||||
private final Logger logger = LoggerFactory.getLogger(ConvertTask.class);
|
||||
|
||||
CacheService cacheService;
|
||||
private final FilePreviewFactory previewFactory;
|
||||
|
||||
FileUtils fileUtils;
|
||||
private final CacheService cacheService;
|
||||
|
||||
public ConverTask(FilePreviewFactory previewFactory, CacheService cacheService, FileUtils fileUtils) {
|
||||
private final FileUtils fileUtils;
|
||||
|
||||
public ConvertTask(FilePreviewFactory previewFactory,
|
||||
CacheService cacheService,
|
||||
FileUtils fileUtils) {
|
||||
this.previewFactory = previewFactory;
|
||||
this.cacheService = cacheService;
|
||||
this.fileUtils=fileUtils;
|
||||
@ -1,8 +1,6 @@
|
||||
package cn.keking.service;
|
||||
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -15,11 +13,11 @@ import java.util.Map;
|
||||
@Service
|
||||
public class FilePreviewFactory {
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
private final ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
public FilePreviewFactory(ApplicationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public FilePreview get(FileAttribute fileAttribute) {
|
||||
Map<String, FilePreview> filePreviewMap = context.getBeansOfType(FilePreview.class);
|
||||
|
||||
@ -4,19 +4,19 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @auther: chenjh
|
||||
* @time: 2019/4/2 16:45
|
||||
* @description
|
||||
* @author: chenjh
|
||||
* @since: 2019/4/2 16:45
|
||||
*/
|
||||
public interface CacheService {
|
||||
final String REDIS_FILE_PREVIEW_PDF_KEY = "converted-preview-pdf-file";
|
||||
final String REDIS_FILE_PREVIEW_IMGS_KEY = "converted-preview-imgs-file";//压缩包内图片文件集合
|
||||
final String REDIS_FILE_PREVIEW_PDF_IMGS_KEY = "converted-preview-pdfimgs-file";
|
||||
|
||||
String FILE_PREVIEW_PDF_KEY = "converted-preview-pdf-file";
|
||||
String FILE_PREVIEW_IMGS_KEY = "converted-preview-imgs-file";//压缩包内图片文件集合
|
||||
String FILE_PREVIEW_PDF_IMGS_KEY = "converted-preview-pdfimgs-file";
|
||||
String TASK_QUEUE_NAME = "convert-task";
|
||||
|
||||
final Integer DEFAULT_PDF_CAPACITY = 500000;
|
||||
final Integer DEFAULT_IMG_CAPACITY = 500000;
|
||||
final Integer DEFAULT_PDFIMG_CAPACITY = 500000;
|
||||
Integer DEFAULT_PDF_CAPACITY = 500000;
|
||||
Integer DEFAULT_IMG_CAPACITY = 500000;
|
||||
Integer DEFAULT_PDFIMG_CAPACITY = 500000;
|
||||
|
||||
void initPDFCachePool(Integer capacity);
|
||||
void initIMGCachePool(Integer capacity);
|
||||
@ -29,12 +29,8 @@ public interface CacheService {
|
||||
List<String> getImgCache(String key);
|
||||
Integer getPdfImageCache(String key);
|
||||
void putPdfImageCache(String pdfFilePath, int num);
|
||||
|
||||
void cleanCache();
|
||||
|
||||
void addQueueTask(String url);
|
||||
String takeQueueTask() throws InterruptedException;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -3,7 +3,6 @@ package cn.keking.service.cache.impl;
|
||||
import cn.keking.service.cache.CacheService;
|
||||
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
|
||||
import com.googlecode.concurrentlinkedhashmap.Weighers;
|
||||
import org.rocksdb.RocksDB;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -29,7 +28,7 @@ public class CacheServiceJDKImpl implements CacheService {
|
||||
|
||||
private static final int QUEUE_SIZE = 500000;
|
||||
|
||||
private BlockingQueue blockingQueue = new ArrayBlockingQueue(QUEUE_SIZE);
|
||||
private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(QUEUE_SIZE);
|
||||
|
||||
@Override
|
||||
public void initPDFCachePool(Integer capacity) {
|
||||
@ -130,6 +129,6 @@ public class CacheServiceJDKImpl implements CacheService {
|
||||
|
||||
@Override
|
||||
public String takeQueueTask() throws InterruptedException {
|
||||
return String.valueOf(blockingQueue.take());
|
||||
return blockingQueue.take();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,13 +1,11 @@
|
||||
package cn.keking.service.cache.impl;
|
||||
|
||||
import cn.keking.service.FileConverQueueTask;
|
||||
import cn.keking.service.cache.CacheService;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RBlockingQueue;
|
||||
import org.redisson.api.RMapCache;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -23,16 +21,12 @@ import java.util.Map;
|
||||
@Service
|
||||
public class CacheServiceRedisImpl implements CacheService {
|
||||
|
||||
private Config config;
|
||||
private final RedissonClient redissonClient;
|
||||
|
||||
@Autowired
|
||||
public CacheServiceRedisImpl(Config config) {
|
||||
this.config = config;
|
||||
this.redissonClient = Redisson.create(config);
|
||||
}
|
||||
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
@Override
|
||||
public void initPDFCachePool(Integer capacity) {
|
||||
|
||||
@ -50,47 +44,47 @@ public class CacheServiceRedisImpl implements CacheService {
|
||||
|
||||
@Override
|
||||
public void putPDFCache(String key, String value) {
|
||||
RMapCache<String, String> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY);
|
||||
RMapCache<String, String> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
|
||||
convertedList.fastPut(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putImgCache(String key, List<String> value) {
|
||||
RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY);
|
||||
RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
|
||||
convertedList.fastPut(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getPDFCache() {
|
||||
return redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY);
|
||||
return redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPDFCache(String key) {
|
||||
RMapCache<String, String> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY);
|
||||
RMapCache<String, String> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
|
||||
return convertedList.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getImgCache() {
|
||||
return redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY);
|
||||
return redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getImgCache(String key) {
|
||||
RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY);
|
||||
RMapCache<String, List<String>> convertedList = redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
|
||||
return convertedList.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPdfImageCache(String key) {
|
||||
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
return convertedList.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPdfImageCache(String pdfFilePath, int num) {
|
||||
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
RMapCache<String, Integer> convertedList = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
convertedList.fastPut(pdfFilePath, num);
|
||||
}
|
||||
|
||||
@ -103,28 +97,28 @@ public class CacheServiceRedisImpl implements CacheService {
|
||||
|
||||
@Override
|
||||
public void addQueueTask(String url) {
|
||||
RBlockingQueue<String> queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName);
|
||||
RBlockingQueue<String> queue = redissonClient.getBlockingQueue(TASK_QUEUE_NAME);
|
||||
queue.addAsync(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String takeQueueTask() throws InterruptedException {
|
||||
RBlockingQueue<String> queue = redissonClient.getBlockingQueue(FileConverQueueTask.queueTaskName);
|
||||
RBlockingQueue<String> queue = redissonClient.getBlockingQueue(TASK_QUEUE_NAME);
|
||||
return queue.take();
|
||||
}
|
||||
|
||||
private void cleanPdfCache() {
|
||||
RMapCache<String, String> pdfCache = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_KEY);
|
||||
RMapCache<String, String> pdfCache = redissonClient.getMapCache(FILE_PREVIEW_PDF_KEY);
|
||||
pdfCache.clear();
|
||||
}
|
||||
|
||||
private void cleanImgCache() {
|
||||
RMapCache<String, List<String>> imgCache = redissonClient.getMapCache(REDIS_FILE_PREVIEW_IMGS_KEY);
|
||||
RMapCache<String, List<String>> imgCache = redissonClient.getMapCache(FILE_PREVIEW_IMGS_KEY);
|
||||
imgCache.clear();
|
||||
}
|
||||
|
||||
private void cleanPdfImgCache() {
|
||||
RMapCache<String, Integer> pdfImg = redissonClient.getMapCache(REDIS_FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
RMapCache<String, Integer> pdfImg = redissonClient.getMapCache(FILE_PREVIEW_PDF_IMGS_KEY);
|
||||
pdfImg.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,24 +36,24 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CacheServiceRocksDBImpl.class);
|
||||
|
||||
private BlockingQueue blockingQueue = new ArrayBlockingQueue(QUEUE_SIZE);
|
||||
private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(QUEUE_SIZE);
|
||||
|
||||
private RocksDB db;
|
||||
|
||||
{
|
||||
try {
|
||||
db = RocksDB.open(DB_PATH);
|
||||
if (db.get(REDIS_FILE_PREVIEW_PDF_KEY.getBytes()) == null) {
|
||||
if (db.get(FILE_PREVIEW_PDF_KEY.getBytes()) == null) {
|
||||
Map<String, String> initPDFCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache));
|
||||
db.put(FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache));
|
||||
}
|
||||
if (db.get(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes()) == null) {
|
||||
if (db.get(FILE_PREVIEW_IMGS_KEY.getBytes()) == null) {
|
||||
Map<String, List<String>> initIMGCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache));
|
||||
db.put(FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache));
|
||||
}
|
||||
if (db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes()) == null) {
|
||||
if (db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()) == null) {
|
||||
Map<String, Integer> initPDFIMGCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
|
||||
db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
|
||||
}
|
||||
} catch (RocksDBException | IOException e) {
|
||||
LOGGER.error("Uable to init RocksDB" + e);
|
||||
@ -81,7 +81,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
try {
|
||||
Map<String, String> pdfCacheItem = getPDFCache();
|
||||
pdfCacheItem.put(key, value);
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(pdfCacheItem));
|
||||
db.put(FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(pdfCacheItem));
|
||||
} catch (RocksDBException | IOException e) {
|
||||
LOGGER.error("Put into RocksDB Exception" + e);
|
||||
}
|
||||
@ -92,7 +92,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
try {
|
||||
Map<String, List<String>> imgCacheItem = getImgCache();
|
||||
imgCacheItem.put(key, value);
|
||||
db.put(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(imgCacheItem));
|
||||
db.put(FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(imgCacheItem));
|
||||
} catch (RocksDBException | IOException e) {
|
||||
LOGGER.error("Put into RocksDB Exception" + e);
|
||||
}
|
||||
@ -102,7 +102,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
public Map<String, String> getPDFCache() {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
try{
|
||||
result = (Map<String, String>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_KEY.getBytes()));
|
||||
result = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes()));
|
||||
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
||||
LOGGER.error("Get from RocksDB Exception" + e);
|
||||
}
|
||||
@ -113,7 +113,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
public String getPDFCache(String key) {
|
||||
String result = "";
|
||||
try{
|
||||
Map<String, String> map = (Map<String, String>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_KEY.getBytes()));
|
||||
Map<String, String> map = (Map<String, String>) toObject(db.get(FILE_PREVIEW_PDF_KEY.getBytes()));
|
||||
result = map.get(key);
|
||||
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
||||
LOGGER.error("Get from RocksDB Exception" + e);
|
||||
@ -125,7 +125,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
public Map<String, List<String>> getImgCache() {
|
||||
Map<String, List<String>> result = new HashMap<>();
|
||||
try{
|
||||
result = (Map<String, List<String>>) toObject(db.get(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes()));
|
||||
result = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes()));
|
||||
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
||||
LOGGER.error("Get from RocksDB Exception" + e);
|
||||
}
|
||||
@ -137,7 +137,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
List<String> result = new ArrayList<>();
|
||||
Map<String, List<String>> map;
|
||||
try{
|
||||
map = (Map<String, List<String>>) toObject(db.get(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes()));
|
||||
map = (Map<String, List<String>>) toObject(db.get(FILE_PREVIEW_IMGS_KEY.getBytes()));
|
||||
result = map.get(key);
|
||||
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
||||
LOGGER.error("Get from RocksDB Exception" + e);
|
||||
@ -148,7 +148,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
public Map<String, Integer> getPdfImageCaches() {
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
try{
|
||||
map = (Map<String, Integer>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
|
||||
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);
|
||||
}
|
||||
@ -160,7 +160,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
Integer result = 0;
|
||||
Map<String, Integer> map;
|
||||
try{
|
||||
map = (Map<String, Integer>) toObject(db.get(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
|
||||
map = (Map<String, Integer>) toObject(db.get(FILE_PREVIEW_PDF_IMGS_KEY.getBytes()));
|
||||
result = map.get(key);
|
||||
} catch (RocksDBException | IOException | ClassNotFoundException e) {
|
||||
LOGGER.error("Get from RocksDB Exception" + e);
|
||||
@ -173,7 +173,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
try {
|
||||
Map<String, Integer> pdfImageCacheItem = getPdfImageCaches();
|
||||
pdfImageCacheItem.put(pdfFilePath, num);
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(pdfImageCacheItem));
|
||||
db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(pdfImageCacheItem));
|
||||
} catch (RocksDBException | IOException e) {
|
||||
LOGGER.error("Put into RocksDB Exception" + e);
|
||||
}
|
||||
@ -197,11 +197,11 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
|
||||
@Override
|
||||
public String takeQueueTask() throws InterruptedException {
|
||||
return String.valueOf(blockingQueue.take());
|
||||
return blockingQueue.take();
|
||||
}
|
||||
|
||||
private byte[] toByteArray (Object obj) throws IOException {
|
||||
byte[] bytes = null;
|
||||
byte[] bytes;
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
||||
oos.writeObject(obj);
|
||||
@ -213,7 +213,7 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
}
|
||||
|
||||
private Object toObject (byte[] bytes) throws IOException, ClassNotFoundException {
|
||||
Object obj = null;
|
||||
Object obj;
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream (bis);
|
||||
obj = ois.readObject();
|
||||
@ -224,16 +224,16 @@ public class CacheServiceRocksDBImpl implements CacheService {
|
||||
|
||||
private void cleanPdfCache() throws IOException, RocksDBException {
|
||||
Map<String, String> initPDFCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache));
|
||||
db.put(FILE_PREVIEW_PDF_KEY.getBytes(), toByteArray(initPDFCache));
|
||||
}
|
||||
|
||||
private void cleanImgCache() throws IOException, RocksDBException {
|
||||
Map<String, List<String>> initIMGCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache));
|
||||
db.put(FILE_PREVIEW_IMGS_KEY.getBytes(), toByteArray(initIMGCache));
|
||||
}
|
||||
|
||||
private void cleanPdfImgCache() throws IOException, RocksDBException {
|
||||
Map<String, Integer> initPDFIMGCache = new HashMap<>();
|
||||
db.put(REDIS_FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
|
||||
db.put(FILE_PREVIEW_PDF_IMGS_KEY.getBytes(), toByteArray(initPDFIMGCache));
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
|
||||
|
||||
/**
|
||||
* @author chenjh
|
||||
@ -52,7 +52,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
||||
String outFilePath = fileDir + 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);
|
||||
@ -74,19 +74,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
||||
}
|
||||
}
|
||||
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType))) {
|
||||
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
|
||||
if (imageUrls == null || imageUrls.size() < 1) {
|
||||
model.addAttribute("msg", "office转图片异常,请联系管理员");
|
||||
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||
return "fileNotSupported";
|
||||
}
|
||||
model.addAttribute("imgurls", imageUrls);
|
||||
model.addAttribute("currentUrl", imageUrls.get(0));
|
||||
if (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
||||
return "officePicture";
|
||||
} else {
|
||||
return "picture";
|
||||
}
|
||||
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
|
||||
}
|
||||
model.addAttribute("pdfUrl", pdfName);
|
||||
return "pdf";
|
||||
|
||||
@ -74,21 +74,25 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
||||
}
|
||||
}
|
||||
if (!isHtml && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALLIMAGES.equals(officePreviewType))) {
|
||||
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
|
||||
if (imageUrls == null || imageUrls.size() < 1) {
|
||||
model.addAttribute("msg", "office转图片异常,请联系管理员");
|
||||
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||
return "fileNotSupported";
|
||||
}
|
||||
model.addAttribute("imgurls", imageUrls);
|
||||
model.addAttribute("currentUrl", imageUrls.get(0));
|
||||
if (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
|
||||
return "officePicture";
|
||||
} else {
|
||||
return "picture";
|
||||
}
|
||||
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, pdfUtils, OFFICE_PREVIEW_TYPE_IMAGE);
|
||||
}
|
||||
model.addAttribute("pdfUrl", pdfName);
|
||||
return isHtml ? "html" : "pdf";
|
||||
}
|
||||
|
||||
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String baseUrl, String pdfName, String outFilePath, PdfUtils pdfUtils, String officePreviewTypeImage) {
|
||||
List<String> imageUrls = pdfUtils.pdf2jpg(outFilePath, pdfName, baseUrl);
|
||||
if (imageUrls == null || imageUrls.size() < 1) {
|
||||
model.addAttribute("msg", "office转图片异常,请联系管理员");
|
||||
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||
return "fileNotSupported";
|
||||
}
|
||||
model.addAttribute("imgurls", imageUrls);
|
||||
model.addAttribute("currentUrl", imageUrls.get(0));
|
||||
if (officePreviewTypeImage.equals(officePreviewType)) {
|
||||
return "officePicture";
|
||||
} else {
|
||||
return "picture";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package cn.keking.service.impl;
|
||||
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import cn.keking.service.FilePreview;
|
||||
@ -30,7 +29,7 @@ public class PictureFilePreviewImpl implements FilePreview {
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
|
||||
List imgUrls = Lists.newArrayList(url);
|
||||
List<String> imgUrls = Lists.newArrayList(url);
|
||||
try {
|
||||
imgUrls.clear();
|
||||
imgUrls.addAll(fileUtils.getImgCache(fileKey));
|
||||
|
||||
@ -3,9 +3,7 @@ package cn.keking.service.impl;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import cn.keking.service.FilePreview;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import cn.keking.utils.SimTextUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import cn.keking.utils.DownloadUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
|
||||
@ -18,19 +16,18 @@ import java.nio.file.Files;
|
||||
* Content :处理文本文件
|
||||
*/
|
||||
@Service
|
||||
public class SimTextFilePreviewImpl implements FilePreview{
|
||||
public class SimTextFilePreviewImpl implements FilePreview {
|
||||
|
||||
@Autowired
|
||||
SimTextUtil simTextUtil;
|
||||
private final DownloadUtils downloadUtils;
|
||||
|
||||
@Autowired
|
||||
FileUtils fileUtils;
|
||||
public SimTextFilePreviewImpl(DownloadUtils downloadUtils) {
|
||||
this.downloadUtils = downloadUtils;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute){
|
||||
String decodedUrl=fileAttribute.getDecodedUrl();
|
||||
String fileName=fileAttribute.getName();
|
||||
ReturnResponse<String> response = simTextUtil.readSimText(decodedUrl, fileName);
|
||||
String fileName = fileAttribute.getName();
|
||||
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
|
||||
if (0 != response.getCode()) {
|
||||
model.addAttribute("msg", response.getMsg());
|
||||
model.addAttribute("fileType",fileAttribute.getSuffix());
|
||||
|
||||
Reference in New Issue
Block a user