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;
|
2023-04-21 09:56:46 +08:00
|
|
|
import cn.keking.utils.KkFileUtils;
|
2023-04-08 05:45:17 +00:00
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
|
import org.apache.poi.EncryptedDocumentException;
|
2018-01-17 17:51:53 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.ui.Model;
|
2023-04-03 07:08:04 +00:00
|
|
|
import org.springframework.util.ObjectUtils;
|
2018-01-17 17:51:53 +08:00
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
2023-04-08 05:45:17 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2020-12-27 14:47:28 +08:00
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
2023-04-08 05:45:17 +00:00
|
|
|
private static final String Rar_PASSWORD_MSG = "password";
|
2020-12-27 14:47:28 +08:00
|
|
|
public CompressFilePreviewImpl(FileHandlerService fileHandlerService, CompressFileReader compressFileReader, OtherFilePreviewImpl otherFilePreview) {
|
2020-12-26 19:13:50 +08:00
|
|
|
this.fileHandlerService = fileHandlerService;
|
|
|
|
|
this.compressFileReader = compressFileReader;
|
2020-12-27 14:47:28 +08:00
|
|
|
this.otherFilePreview = otherFilePreview;
|
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();
|
2023-04-03 07:08:04 +00:00
|
|
|
String filePassword = fileAttribute.getFilePassword();
|
2023-04-23 11:22:29 +08:00
|
|
|
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
2023-04-08 05:45:17 +00:00
|
|
|
String fileTree = null;
|
2018-01-17 17:51:53 +08:00
|
|
|
// 判断文件名是否存在(redis缓存读取)
|
2023-04-23 11:22:29 +08:00
|
|
|
if (forceUpdatedCache || !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()) {
|
2020-12-27 14:47:28 +08:00
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|
|
|
|
|
String filePath = response.getContent();
|
2023-04-08 05:45:17 +00:00
|
|
|
try {
|
|
|
|
|
fileTree = compressFileReader.unRar(filePath, filePassword,fileName);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
|
|
|
|
for (Throwable throwable : throwableArray) {
|
|
|
|
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
|
|
|
|
if (e.getMessage().toLowerCase().contains(Rar_PASSWORD_MSG)) {
|
|
|
|
|
model.addAttribute("needFilePassword", true);
|
|
|
|
|
return EXEL_FILE_PREVIEW_PAGE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-03 07:08:04 +00:00
|
|
|
}
|
|
|
|
|
if (!ObjectUtils.isEmpty(fileTree)) {
|
2023-05-06 16:51:06 +08:00
|
|
|
//是否保留压缩包源文件
|
|
|
|
|
if (ConfigConstants.getDeleteSourceFile()) {
|
2023-04-21 09:56:46 +08:00
|
|
|
KkFileUtils.deleteFileByPath(filePath);
|
|
|
|
|
}
|
2023-01-11 13:26:18 +08:00
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
// 加入缓存
|
|
|
|
|
fileHandlerService.addConvertedFile(fileName, fileTree);
|
|
|
|
|
}
|
2023-04-08 05:45:17 +00:00
|
|
|
}else {
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "压缩文件密码错误! 压缩文件损坏! 压缩文件类型不受支持!");
|
2023-01-11 13:26:18 +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
|
|
|
}
|
2023-04-03 07:08:04 +00:00
|
|
|
model.addAttribute("fileName", fileName);
|
2018-01-17 17:51:53 +08:00
|
|
|
model.addAttribute("fileTree", fileTree);
|
2020-12-27 15:14:32 +08:00
|
|
|
return COMPRESS_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|
|
|
|
|
}
|