优化文本类型预览逻辑

This commit is contained in:
kl
2020-12-25 21:02:51 +08:00
committed by kl
parent da1553920b
commit d4b11a4056
3 changed files with 13 additions and 28 deletions

View File

@ -29,32 +29,23 @@ public class SimTextFilePreviewImpl implements FilePreview {
}
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute){
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
String fileName = fileAttribute.getName();
ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, fileName);
if (0 != response.getCode()) {
model.addAttribute("msg", response.getMsg());
model.addAttribute("fileType",fileAttribute.getSuffix());
model.addAttribute("fileType", fileAttribute.getSuffix());
return "fileNotSupported";
}
try {
File originFile = new File(response.getContent());
File previewFile = new File(response.getContent() + ".txt");
if (previewFile.exists()) {
previewFile.delete();
}
Files.copy(originFile.toPath(), previewFile.toPath());
if(fileAttribute.getType().equals(FileType.xml)){
String xmlString = FileUtils.readFileToString(previewFile, StandardCharsets.UTF_8);
model.addAttribute("xmlContent", Base64Utils.encodeToString(xmlString.getBytes()));
}
String xmlString = FileUtils.readFileToString(originFile, StandardCharsets.UTF_8);
model.addAttribute("textData", Base64Utils.encodeToString(xmlString.getBytes()));
} catch (IOException e) {
model.addAttribute("msg", e.getLocalizedMessage());
model.addAttribute("fileType",fileAttribute.getSuffix());
model.addAttribute("fileType", fileAttribute.getSuffix());
return "fileNotSupported";
}
model.addAttribute("ordinaryUrl", response.getMsg());
return "txt";
}