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;
|
2020-05-15 18:09:19 +08:00
|
|
|
import cn.keking.utils.DownloadUtils;
|
2020-12-28 18:21:35 +08:00
|
|
|
import cn.keking.utils.KkFileUtils;
|
2021-01-09 15:38:21 +08:00
|
|
|
import jodd.io.FileUtil;
|
2020-12-29 18:32:24 +08:00
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2018-01-17 17:51:53 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.ui.Model;
|
2021-03-18 20:12:50 +08:00
|
|
|
import org.springframework.web.util.HtmlUtils;
|
2018-01-17 17:51:53 +08:00
|
|
|
|
2019-10-25 14:37:31 +08:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
2021-03-18 20:12:50 +08:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2019-10-25 14:37:31 +08:00
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
/**
|
|
|
|
|
* Created by kl on 2018/1/17.
|
|
|
|
|
* Content :处理文本文件
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
2020-05-15 18:09:19 +08:00
|
|
|
public class SimTextFilePreviewImpl implements FilePreview {
|
2018-01-17 17:51:53 +08:00
|
|
|
|
2020-12-27 14:47:28 +08:00
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
|
|
|
|
|
|
|
|
|
public SimTextFilePreviewImpl(OtherFilePreviewImpl otherFilePreview) {
|
|
|
|
|
this.otherFilePreview = otherFilePreview;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
@Override
|
2020-12-25 21:02:51 +08:00
|
|
|
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
|
2020-05-15 18:09:19 +08:00
|
|
|
String fileName = fileAttribute.getName();
|
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
|
|
|
}
|
2019-10-25 14:37:31 +08:00
|
|
|
try {
|
|
|
|
|
File originFile = new File(response.getContent());
|
2020-12-28 18:21:35 +08:00
|
|
|
String charset = KkFileUtils.getFileEncode(originFile);
|
2021-01-09 15:38:21 +08:00
|
|
|
String fileData = FileUtil.readString(originFile, charset);
|
2021-03-18 20:12:50 +08:00
|
|
|
fileData = HtmlUtils.htmlEscape(fileData, StandardCharsets.UTF_8.name());
|
2020-12-29 18:32:24 +08:00
|
|
|
model.addAttribute("textData", Base64.encodeBase64String(fileData.getBytes()));
|
2019-10-25 14:37:31 +08:00
|
|
|
} catch (IOException e) {
|
2020-12-27 14:47:28 +08:00
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage());
|
2019-10-25 14:37:31 +08:00
|
|
|
}
|
2020-12-27 15:14:32 +08:00
|
|
|
return TXT_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|