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;
|
2022-12-16 23:58:26 +08:00
|
|
|
import cn.keking.service.FileHandlerService;
|
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;
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
import cn.keking.utils.KkFileUtils;
|
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;
|
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
|
|
|
|
2022-12-16 23:58:26 +08:00
|
|
|
private final FileHandlerService fileHandlerService;
|
2020-12-27 14:47:28 +08:00
|
|
|
private final OtherFilePreviewImpl otherFilePreview;
|
|
|
|
|
|
2022-12-16 23:58:26 +08:00
|
|
|
public SimTextFilePreviewImpl(FileHandlerService fileHandlerService,OtherFilePreviewImpl otherFilePreview) {
|
|
|
|
|
this.fileHandlerService = fileHandlerService;
|
2020-12-27 14:47:28 +08:00
|
|
|
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();
|
2023-04-23 11:22:29 +08:00
|
|
|
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
2022-12-12 10:30:13 +08:00
|
|
|
String filePath = FILE_DIR + fileName;
|
2023-04-23 11:22:29 +08:00
|
|
|
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(fileName) || !ConfigConstants.isCacheEnabled()) {
|
2022-12-16 23:58:26 +08:00
|
|
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
|
|
|
|
if (response.isFailure()) {
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
|
|
|
|
}
|
|
|
|
|
filePath = response.getContent();
|
|
|
|
|
if (ConfigConstants.isCacheEnabled()) {
|
|
|
|
|
fileHandlerService.addConvertedFile(fileName, filePath); //加入缓存
|
|
|
|
|
}
|
|
|
|
|
try {
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
String fileData = HtmlUtils.htmlEscape(textData(filePath,fileName));
|
2022-12-16 23:58:26 +08:00
|
|
|
model.addAttribute("textData", Base64.encodeBase64String(fileData.getBytes()));
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
return otherFilePreview.notSupportedFile(model, fileAttribute, e.getLocalizedMessage());
|
|
|
|
|
}
|
|
|
|
|
return TXT_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|
2022-12-16 23:58:26 +08:00
|
|
|
String fileData = null;
|
2019-10-25 14:37:31 +08:00
|
|
|
try {
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
fileData = HtmlUtils.htmlEscape(textData(filePath,fileName));
|
2019-10-25 14:37:31 +08:00
|
|
|
} catch (IOException e) {
|
2022-12-16 23:58:26 +08:00
|
|
|
e.printStackTrace();
|
2019-10-25 14:37:31 +08:00
|
|
|
}
|
2022-12-16 23:58:26 +08:00
|
|
|
model.addAttribute("textData", Base64.encodeBase64String(fileData.getBytes()));
|
2020-12-27 15:14:32 +08:00
|
|
|
return TXT_FILE_PREVIEW_PAGE;
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|
|
|
|
|
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
private String textData(String filePath,String fileName) throws IOException {
|
2022-12-12 10:30:13 +08:00
|
|
|
File file = new File(filePath);
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
if (KkFileUtils.isIllegalFileName(fileName)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-12-12 10:30:13 +08:00
|
|
|
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();
|
|
|
|
|
}
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(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");
|
|
|
|
|
}
|
1,优化URL报错,2,更新OFD组件 3,美化Excel 4,文本方法关闭字节流 5,新增多种类型文件预览 (#419)
1,优化URL报错
2,更新OFD组件
3,美化Excel
4,文本方法关闭字节流
5,新增xmind、eml、epub、"obj", "3ds", "stl", "ply", "off", "3dm", "fbx", "dae", "wrl", "3mf", "ifc","glb","o3dv","gltf","stp","bim","fcstd","step","iges","brep"格式
Co-authored-by: gaoxiongzaq <admin@cxcp.com>
2022-12-28 10:17:06 +08:00
|
|
|
br.close();
|
2021-04-15 17:46:44 +08:00
|
|
|
return result.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-17 17:51:53 +08:00
|
|
|
}
|