2018-01-17 17:51:53 +08:00
|
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
|
|
|
* Content :处理压缩包文件
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
|
|
|
|
public class CompressFilePreviewImpl implements FilePreview{
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
FileUtils fileUtils;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
DownloadUtils downloadUtils;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
ZipReader zipReader;
|
|
|
|
|
|
|
|
|
|
@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缓存读取)
|
|
|
|
|
if (!StringUtils.hasText(fileUtils.getConvertedFile(fileName))) {
|
2019-06-19 14:18:09 +08:00
|
|
|
|
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
if (0 != response.getCode()) {
|
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)) {
|
|
|
|
|
fileTree = zipReader.readZipFile(filePath, fileName);
|
|
|
|
|
} else if ("rar".equalsIgnoreCase(suffix)) {
|
|
|
|
|
fileTree = zipReader.unRar(filePath, fileName);
|
2019-05-16 17:44:34 +08:00
|
|
|
|
} else if ("7z".equalsIgnoreCase(suffix)) {
|
|
|
|
|
fileTree = zipReader.read7zFile(filePath, fileName);
|
2018-01-17 17:51:53 +08:00
|
|
|
|
}
|
2019-04-18 14:20:37 +08:00
|
|
|
|
if (fileTree != null && !"null".equals(fileTree)) {
|
|
|
|
|
fileUtils.addConvertedFile(fileName, fileTree);
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
|
} else {
|
|
|
|
|
fileTree = fileUtils.getConvertedFile(fileName);
|
|
|
|
|
}
|
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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|