2018-01-17 17:51:53 +08:00
|
|
|
package cn.keking.service.impl;
|
|
|
|
|
|
2021-04-15 17:46:44 +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;
|
2020-05-15 18:09:19 +08:00
|
|
|
import cn.keking.utils.DownloadUtils;
|
2021-06-17 18:10:37 +08:00
|
|
|
import cn.keking.utils.EncodingDetects;
|
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
|
|
|
|
2021-04-15 17:46:44 +08:00
|
|
|
import java.io.*;
|
2022-12-12 10:30:13 +08:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Paths;
|
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;
|
|
|
|
|
}
|
2021-04-15 17:46:44 +08:00
|
|
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
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();
|
2022-12-12 10:30:13 +08:00
|
|
|
String filePath = FILE_DIR + fileName;
|
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 {
|
2022-12-12 10:30:13 +08:00
|
|
|
String fileData = HtmlUtils.htmlEscape(textData(filePath));
|
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
|
|
|
}
|
|
|
|
|
|
2022-12-12 10:30:13 +08:00
|
|
|
private String textData(String filePath) throws IOException {
|
|
|
|
|
File file = new File(filePath);
|
|
|
|
|
if (!file.exists() || file.length() == 0) {
|
2022-07-25 17:26:02 +08:00
|
|
|
return "";
|
2022-12-12 10:30:13 +08:00
|
|
|
} else {
|
|
|
|
|
String charset = EncodingDetects.getJavaEncode(filePath);
|
|
|
|
|
if ("ASCII".equals(charset)) {
|
|
|
|
|
charset = StandardCharsets.US_ASCII.name();
|
|
|
|
|
}
|
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(filePath)), charset));
|
2021-04-15 17:46:44 +08:00
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
|
result.append(line).append("\r\n");
|
|
|
|
|
}
|
|
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|