Files
file-online-preview/server/src/main/java/cn/keking/service/impl/CompressFilePreviewImpl.java

60 lines
2.4 KiB
Java
Raw Normal View History

package cn.keking.service.impl;
2019-08-22 15:22:05 +08:00
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.service.FileHandlerService;
import cn.keking.service.CompressFileReader;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
/**
* Created by kl on 2018/1/17.
* Content :处理压缩包文件
*/
@Service
2020-05-18 09:46:52 +08:00
public class CompressFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService;
private final CompressFileReader compressFileReader;
private final OtherFilePreviewImpl otherFilePreview;
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, CompressFileReader compressFileReader, OtherFilePreviewImpl otherFilePreview) {
this.fileHandlerService = fileHandlerService;
this.compressFileReader = compressFileReader;
this.otherFilePreview = otherFilePreview;
2020-05-18 09:46:52 +08:00
}
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
String fileName=fileAttribute.getName();
String fileTree;
// 判断文件名是否存在(redis缓存读取)
if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
if (response.isFailure()) {
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
}
String filePath = response.getContent();
fileTree = compressFileReader.unRar(filePath, fileName);
if (fileTree != null && !"null".equals(fileTree)) {
if (ConfigConstants.isCacheEnabled()) {
// 加入缓存
fileHandlerService.addConvertedFile(fileName, fileTree);
}
}
} else {
fileTree = fileHandlerService.getConvertedFile(fileName);
}
2019-04-18 14:20:37 +08:00
if (fileTree != null && !"null".equals(fileTree)) {
model.addAttribute("fileTree", fileTree);
2020-12-27 15:14:32 +08:00
return COMPRESS_FILE_PREVIEW_PAGE;
} else {
return otherFilePreview.notSupportedFile(model, fileAttribute, "压缩文件类型不受支持");
}
}
}