修复图片预览bug,移除guava
parent
9b0f381c06
commit
5a559aa868
|
@ -160,11 +160,6 @@
|
|||
<artifactId>xstream</artifactId>
|
||||
<version>1.4.15</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>19.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.concurrentlinkedhashmap</groupId>
|
||||
<artifactId>concurrentlinkedhashmap-lru</artifactId>
|
||||
|
|
|
@ -9,13 +9,10 @@ import cn.keking.config.ConfigConstants;
|
|||
public class FileAttribute {
|
||||
|
||||
private FileType type;
|
||||
|
||||
private String suffix;
|
||||
|
||||
private String name;
|
||||
|
||||
private String url;
|
||||
|
||||
private String fileKey;
|
||||
private String officePreviewType = ConfigConstants.getOfficePreviewType();
|
||||
|
||||
public FileAttribute() {
|
||||
|
@ -36,6 +33,14 @@ public class FileAttribute {
|
|||
this.officePreviewType = officePreviewType;
|
||||
}
|
||||
|
||||
public String getFileKey() {
|
||||
return fileKey;
|
||||
}
|
||||
|
||||
public void setFileKey(String fileKey) {
|
||||
this.fileKey = fileKey;
|
||||
}
|
||||
|
||||
public String getOfficePreviewType() {
|
||||
return officePreviewType;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ package cn.keking.model;
|
|||
* Content :文件类型,文本,office,压缩包等等
|
||||
*/
|
||||
public enum FileType {
|
||||
picture("picturefilepreviewimpl"),
|
||||
picture("pictureFilePreviewImpl"),
|
||||
compress("compressFilePreviewImpl"),
|
||||
office("officeFilePreviewImpl"),
|
||||
simText("simTextFilePreviewImpl"),
|
||||
|
|
|
@ -5,7 +5,10 @@ import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
|
|||
import com.googlecode.concurrentlinkedhashmap.Weighers;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
|
@ -21,15 +24,78 @@ import java.util.concurrent.BlockingQueue;
|
|||
public class CacheServiceJDKImpl implements CacheService {
|
||||
|
||||
private Map<String, String> pdfCache;
|
||||
|
||||
private Map<String, List<String>> imgCache;
|
||||
|
||||
private Map<String, Integer> pdfImagesCache;
|
||||
|
||||
private static final int QUEUE_SIZE = 500000;
|
||||
|
||||
private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(QUEUE_SIZE);
|
||||
|
||||
@PostConstruct
|
||||
public void initCache(){
|
||||
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
|
||||
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
|
||||
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPDFCache(String key, String value) {
|
||||
pdfCache.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putImgCache(String key, List<String> value) {
|
||||
imgCache.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getPDFCache() {
|
||||
return pdfCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPDFCache(String key) {
|
||||
return pdfCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getImgCache() {
|
||||
return imgCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getImgCache(String key) {
|
||||
if(StringUtils.isEmpty(key)){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return imgCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPdfImageCache(String key) {
|
||||
return pdfImagesCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPdfImageCache(String pdfFilePath, int num) {
|
||||
pdfImagesCache.put(pdfFilePath, num);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache() {
|
||||
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
|
||||
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
|
||||
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addQueueTask(String url) {
|
||||
blockingQueue.add(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String takeQueueTask() throws InterruptedException {
|
||||
return blockingQueue.take();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPDFCachePool(Integer capacity) {
|
||||
pdfCache = new ConcurrentLinkedHashMap.Builder<String, String>()
|
||||
|
@ -50,85 +116,4 @@ public class CacheServiceJDKImpl implements CacheService {
|
|||
.maximumWeightedCapacity(capacity).weigher(Weighers.singleton())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPDFCache(String key, String value) {
|
||||
if (pdfCache == null) {
|
||||
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
|
||||
}
|
||||
pdfCache.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putImgCache(String key, List<String> value) {
|
||||
if (imgCache == null) {
|
||||
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
|
||||
}
|
||||
imgCache.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getPDFCache() {
|
||||
if (pdfCache == null) {
|
||||
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
|
||||
}
|
||||
return pdfCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPDFCache(String key) {
|
||||
if (pdfCache == null) {
|
||||
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
|
||||
}
|
||||
return pdfCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, List<String>> getImgCache() {
|
||||
if (imgCache == null) {
|
||||
initPDFCachePool(CacheService.DEFAULT_IMG_CAPACITY);
|
||||
}
|
||||
return imgCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getImgCache(String key) {
|
||||
if (imgCache == null) {
|
||||
initPDFCachePool(CacheService.DEFAULT_IMG_CAPACITY);
|
||||
}
|
||||
return imgCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPdfImageCache(String key) {
|
||||
if (pdfImagesCache == null) {
|
||||
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
|
||||
}
|
||||
return pdfImagesCache.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putPdfImageCache(String pdfFilePath, int num) {
|
||||
if (pdfImagesCache == null) {
|
||||
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
|
||||
}
|
||||
pdfImagesCache.put(pdfFilePath, num);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache() {
|
||||
initPDFCachePool(CacheService.DEFAULT_PDF_CAPACITY);
|
||||
initIMGCachePool(CacheService.DEFAULT_IMG_CAPACITY);
|
||||
initPdfImagesCachePool(CacheService.DEFAULT_PDFIMG_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addQueueTask(String url) {
|
||||
blockingQueue.add(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String takeQueueTask() throws InterruptedException {
|
||||
return blockingQueue.take();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,6 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
|
||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + (isHtml ? "html" : "pdf");
|
||||
String outFilePath = FILE_DIR + pdfName;
|
||||
model.addAttribute("switchDisabled", ConfigConstants.getOfficePreviewSwitchDisabled());
|
||||
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
||||
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||
String filePath;
|
||||
|
|
|
@ -44,7 +44,6 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
|
||||
String outFilePath = FILE_DIR + pdfName;
|
||||
model.addAttribute("switchDisabled", ConfigConstants.getOfficePreviewSwitchDisabled());
|
||||
if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
|
||||
//当文件不存在时,就去下载
|
||||
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
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;
|
||||
import cn.keking.utils.DownloadUtils;
|
||||
import cn.keking.utils.FileUtils;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -32,14 +30,12 @@ public class PictureFilePreviewImpl implements FilePreview {
|
|||
|
||||
@Override
|
||||
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
||||
String fileKey = (String) RequestContextHolder.currentRequestAttributes().getAttribute("fileKey",0);
|
||||
List<String> imgUrls = Lists.newArrayList(url);
|
||||
model.addAttribute("switchDisabled", ConfigConstants.getOfficePreviewSwitchDisabled());
|
||||
try {
|
||||
imgUrls.clear();
|
||||
imgUrls.addAll(fileUtils.getImgCache(fileKey));
|
||||
} catch (Exception e){
|
||||
imgUrls = Lists.newArrayList(url);
|
||||
List<String> imgUrls = new ArrayList<>();
|
||||
imgUrls.add(url);
|
||||
String fileKey = fileAttribute.getFileKey();
|
||||
List<String> zipImgUrls = fileUtils.getImgCache(fileKey);
|
||||
if (!CollectionUtils.isEmpty(zipImgUrls)) {
|
||||
imgUrls.addAll(zipImgUrls);
|
||||
}
|
||||
// 不是http开头,浏览器不能直接访问,需下载到本地
|
||||
if (url != null && !url.toLowerCase().startsWith("http")) {
|
||||
|
@ -50,7 +46,9 @@ public class PictureFilePreviewImpl implements FilePreview {
|
|||
return "fileNotSupported";
|
||||
} else {
|
||||
String file = fileUtils.getRelativePath(response.getContent());
|
||||
model.addAttribute("imgurls", Lists.newArrayList(file));
|
||||
imgUrls.clear();
|
||||
imgUrls.add(file);
|
||||
model.addAttribute("imgurls", imgUrls);
|
||||
model.addAttribute("currentUrl", file);
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -4,7 +4,6 @@ import cn.keking.config.ConfigConstants;
|
|||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.FileType;
|
||||
import cn.keking.service.cache.CacheService;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
@ -12,10 +11,7 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author yudian-it
|
||||
|
@ -125,7 +121,7 @@ public class FileUtils {
|
|||
}
|
||||
|
||||
public List<String> listPictureTypes() {
|
||||
List<String> list = Lists.newArrayList();
|
||||
List<String> list = new LinkedList<>();
|
||||
list.add("jpg");
|
||||
list.add("jpeg");
|
||||
list.add("png");
|
||||
|
@ -137,7 +133,7 @@ public class FileUtils {
|
|||
}
|
||||
|
||||
public List<String> listArchiveTypes() {
|
||||
List<String> list = Lists.newArrayList();
|
||||
List<String> list = new LinkedList<>();
|
||||
list.add("rar");
|
||||
list.add("zip");
|
||||
list.add("jar");
|
||||
|
@ -149,7 +145,7 @@ public class FileUtils {
|
|||
}
|
||||
|
||||
public List<String> listOfficeTypes() {
|
||||
List<String> list = Lists.newArrayList();
|
||||
List<String> list = new LinkedList<>();
|
||||
list.add("docx");
|
||||
list.add("doc");
|
||||
list.add("xls");
|
||||
|
@ -358,9 +354,13 @@ public class FileUtils {
|
|||
attribute.setUrl(url);
|
||||
if (req != null) {
|
||||
String officePreviewType = req.getParameter("officePreviewType");
|
||||
String fileKey = req.getParameter("fileKey");
|
||||
if(StringUtils.hasText(officePreviewType)){
|
||||
attribute.setOfficePreviewType(officePreviewType);
|
||||
}
|
||||
if(StringUtils.hasText(fileKey)){
|
||||
attribute.setFileKey(fileKey);
|
||||
}
|
||||
}
|
||||
return attribute;
|
||||
}
|
||||
|
|
|
@ -8,8 +8,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.github.junrar.Archive;
|
||||
import com.github.junrar.exception.RarException;
|
||||
import com.github.junrar.rarfile.FileHeader;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
||||
|
@ -47,8 +45,8 @@ public class ZipReader {
|
|||
|
||||
public String readZipFile(String filePath,String fileKey) {
|
||||
String archiveSeparator = "/";
|
||||
Map<String, FileNode> appender = Maps.newHashMap();
|
||||
List<String> imgUrls = Lists.newArrayList();
|
||||
Map<String, FileNode> appender = new HashMap<>();
|
||||
List<String> imgUrls = new LinkedList<>();
|
||||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||
String archiveFileName = fileUtils.getFileNameFromPath(filePath);
|
||||
try {
|
||||
|
@ -56,7 +54,7 @@ public class ZipReader {
|
|||
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
|
||||
// 排序
|
||||
entries = sortZipEntries(entries);
|
||||
List<Map<String, ZipArchiveEntry>> entriesToBeExtracted = Lists.newArrayList();
|
||||
List<Map<String, ZipArchiveEntry>> entriesToBeExtracted = new LinkedList<>();
|
||||
while (entries.hasMoreElements()){
|
||||
ZipArchiveEntry entry = entries.nextElement();
|
||||
String fullName = entry.getName();
|
||||
|
@ -90,7 +88,7 @@ public class ZipReader {
|
|||
}
|
||||
|
||||
private Enumeration<ZipArchiveEntry> sortZipEntries(Enumeration<ZipArchiveEntry> entries) {
|
||||
List<ZipArchiveEntry> sortedEntries = Lists.newArrayList();
|
||||
List<ZipArchiveEntry> sortedEntries = new LinkedList<>();
|
||||
while(entries.hasMoreElements()){
|
||||
sortedEntries.add(entries.nextElement());
|
||||
}
|
||||
|
@ -99,15 +97,15 @@ public class ZipReader {
|
|||
}
|
||||
|
||||
public String unRar(String filePath,String fileKey){
|
||||
Map<String, FileNode> appender = Maps.newHashMap();
|
||||
List<String> imgUrls = Lists.newArrayList();
|
||||
Map<String, FileNode> appender = new HashMap<>();
|
||||
List<String> imgUrls = new ArrayList<>();
|
||||
String baseUrl = BaseUrlFilter.getBaseUrl();
|
||||
try {
|
||||
Archive archive = new Archive(new FileInputStream(new File(filePath)));
|
||||
List<FileHeader> headers = archive.getFileHeaders();
|
||||
headers = sortedHeaders(headers);
|
||||
String archiveFileName = fileUtils.getFileNameFromPath(filePath);
|
||||
List<Map<String, FileHeader>> headersToBeExtracted = Lists.newArrayList();
|
||||
List<Map<String, FileHeader>> headersToBeExtracted =new ArrayList<>();
|
||||
for (FileHeader header : headers) {
|
||||
String fullName;
|
||||
if (header.isUnicode()) {
|
||||
|
@ -143,8 +141,8 @@ public class ZipReader {
|
|||
|
||||
public String read7zFile(String filePath,String fileKey) {
|
||||
String archiveSeparator = "/";
|
||||
Map<String, FileNode> appender = Maps.newHashMap();
|
||||
List<String> imgUrls = Lists.newArrayList();
|
||||
Map<String, FileNode> appender = new HashMap<>();
|
||||
List<String> imgUrls = new ArrayList<>();
|
||||
String baseUrl= BaseUrlFilter.getBaseUrl();
|
||||
String archiveFileName = fileUtils.getFileNameFromPath(filePath);
|
||||
try {
|
||||
|
@ -152,7 +150,7 @@ public class ZipReader {
|
|||
Iterable<SevenZArchiveEntry> entries = zipFile.getEntries();
|
||||
// 排序
|
||||
Enumeration<SevenZArchiveEntry> newEntries = sortSevenZEntries(entries);
|
||||
List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = Lists.newArrayList();
|
||||
List<Map<String, SevenZArchiveEntry>> entriesToBeExtracted = new ArrayList<>();
|
||||
while (newEntries.hasMoreElements()){
|
||||
SevenZArchiveEntry entry = newEntries.nextElement();
|
||||
String fullName = entry.getName();
|
||||
|
@ -187,7 +185,7 @@ public class ZipReader {
|
|||
|
||||
|
||||
private Enumeration<SevenZArchiveEntry> sortSevenZEntries(Iterable<SevenZArchiveEntry> entries) {
|
||||
List<SevenZArchiveEntry> sortedEntries = Lists.newArrayList();
|
||||
List<SevenZArchiveEntry> sortedEntries = new ArrayList<>();
|
||||
for (SevenZArchiveEntry entry : entries) {
|
||||
sortedEntries.add(entry);
|
||||
}
|
||||
|
|
|
@ -3,8 +3,7 @@ package cn.keking.web.controller;
|
|||
import cn.keking.config.ConfigConstants;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -16,10 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -84,10 +80,14 @@ public class FileController {
|
|||
|
||||
@RequestMapping(value = "listFiles", method = RequestMethod.GET)
|
||||
public String getFiles() throws JsonProcessingException {
|
||||
List<Map<String, String>> list = Lists.newArrayList();
|
||||
List<Map<String, String>> list = new ArrayList<>();
|
||||
File file = new File(fileDir + demoPath);
|
||||
if (file.exists()) {
|
||||
Arrays.stream(Objects.requireNonNull(file.listFiles())).forEach(file1 -> list.add(ImmutableMap.of("fileName", demoDir + "/" + file1.getName())));
|
||||
Arrays.stream(Objects.requireNonNull(file.listFiles())).forEach(file1 -> {
|
||||
Map<String, String> fileName = new HashMap();
|
||||
fileName.put("fileName", demoDir + "/" + file1.getName());
|
||||
list.add(fileName);
|
||||
});
|
||||
}
|
||||
return new ObjectMapper().writeValueAsString(list);
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@ public class AttributeSetFilter implements Filter {
|
|||
HttpServletRequest httpRequest = (HttpServletRequest)request;
|
||||
request.setAttribute("pdfDownloadDisable", ConfigConstants.getPdfDownloadDisable());
|
||||
request.setAttribute("fileKey", httpRequest.getParameter("fileKey"));
|
||||
request.setAttribute("switchDisabled", ConfigConstants.getOfficePreviewSwitchDisabled());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue