2018-01-17 17:51:53 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
2019-08-22 15:22:05 +08:00
|
|
|
|
import cn.keking.config.ConfigConstants;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.model.FileAttribute;
|
2018-01-19 14:51:18 +08:00
|
|
|
|
import cn.keking.model.ReturnResponse;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
import cn.keking.service.FilePreview;
|
|
|
|
|
|
import cn.keking.utils.DownloadUtils;
|
2020-12-26 19:13:50 +08:00
|
|
|
|
import cn.keking.service.FileHandlerService;
|
|
|
|
|
|
import cn.keking.service.CompressFileReader;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
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 {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-12-26 19:13:50 +08:00
|
|
|
|
private final FileHandlerService fileHandlerService;
|
|
|
|
|
|
private final CompressFileReader compressFileReader;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
2020-12-27 01:43:50 +08:00
|
|
|
|
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, CompressFileReader compressFileReader) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
|
|
|
|
|
this.compressFileReader = compressFileReader;
|
2020-05-18 09:46:52 +08:00
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-06-17 14:21:16 +08:00
|
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
String fileName=fileAttribute.getName();
|
|
|
|
|
|
String suffix=fileAttribute.getSuffix();
|
|
|
|
|
|
String fileTree = null;
|
|
|
|
|
|
// 判断文件名是否存在(redis缓存读取)
|
2020-12-26 19:13:50 +08:00
|
|
|
|
if (!StringUtils.hasText(fileHandlerService.getConvertedFile(fileName)) || !ConfigConstants.isCacheEnabled()) {
|
2020-12-27 01:43:50 +08:00
|
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
2020-12-27 14:06:06 +08:00
|
|
|
|
if (response.isFailure()) {
|
2019-04-18 14:20:37 +08:00
|
|
|
|
model.addAttribute("fileType", suffix);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
model.addAttribute("msg", response.getMsg());
|
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
|
}
|
|
|
|
|
|
String filePath = response.getContent();
|
|
|
|
|
|
if ("zip".equalsIgnoreCase(suffix) || "jar".equalsIgnoreCase(suffix) || "gzip".equalsIgnoreCase(suffix)) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileTree = compressFileReader.readZipFile(filePath, fileName);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
} else if ("rar".equalsIgnoreCase(suffix)) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileTree = compressFileReader.unRar(filePath, fileName);
|
2019-05-16 17:44:34 +08:00
|
|
|
|
} else if ("7z".equalsIgnoreCase(suffix)) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileTree = compressFileReader.read7zFile(filePath, fileName);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2019-08-22 15:22:05 +08:00
|
|
|
|
if (fileTree != null && !"null".equals(fileTree) && ConfigConstants.isCacheEnabled()) {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileHandlerService.addConvertedFile(fileName, fileTree);
|
2019-04-18 14:20:37 +08:00
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
} else {
|
2020-12-26 19:13:50 +08:00
|
|
|
|
fileTree = fileHandlerService.getConvertedFile(fileName);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2019-04-18 14:20:37 +08:00
|
|
|
|
if (fileTree != null && !"null".equals(fileTree)) {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
model.addAttribute("fileTree", fileTree);
|
|
|
|
|
|
return "compress";
|
|
|
|
|
|
} else {
|
2019-04-18 14:20:37 +08:00
|
|
|
|
model.addAttribute("fileType", suffix);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
model.addAttribute("msg", "压缩文件类型不受支持,尝试在压缩的时候选择RAR4格式");
|
|
|
|
|
|
return "fileNotSupported";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|