优化文本类型预览逻辑

encoding
kl 4 years ago committed by kl
parent da1553920b
commit d4b11a4056

@ -1,5 +1,6 @@
[#ftl]
[#-- @implicitly included --]
[#-- @ftlvariable name="textData" type="java.lang.String" --]
[#-- @ftlvariable name="xmlContent" type="java.lang.String" --]
[#-- @ftlvariable name="textContent" type="java.lang.String" --]
[#-- @ftlvariable name="textType" type="java.lang.String" --]

@ -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";
}

@ -7,6 +7,7 @@
</head>
<body>
<input hidden id="textType" value="${textType}">
<input hidden id="textData" value="${textData}">
<div class="container">
<#if textType?? && textType == "markdown">
@ -16,7 +17,6 @@
</p>
<div id="markdown" style="padding: 18px;"></div>
<#elseif textType?? && textType == "xml" >
<input hidden id="xmlContent" value="${xmlContent}">
<div id="xml" style="padding: 18px;"></div>
<#else>
<div id="text"></div>
@ -42,6 +42,9 @@
$("#markdown_btn").hide()
initWaterMark();
fetchData();
loadText();
loadXmlData()
loadMarkdown();
}
/**
@ -73,17 +76,8 @@
*
*/
function fetchData() {
$.ajax({
type: 'GET',
url: '${ordinaryUrl}',
success: function (data) {
window.textData = data;
window.textPreData = "<pre>" + data + "</pre>";
loadText();
loadXmlData()
loadMarkdown();
}
});
window.textData = Base64.decode($("#textData").val())
window.textPreData = "<pre>" + window.textData + "</pre>";
}
/**
@ -108,8 +102,7 @@
*/
function loadXmlData() {
if ($("#textType").val() === "xml") {
var xmlStr = Base64.decode($("#xmlContent").val());
var xmlNode = xmlTreeViewer.parseXML(xmlStr);
var xmlNode = xmlTreeViewer.parseXML(window.textData);
var retNode = xmlTreeViewer.getXMLViewerNode(xmlNode.xml);
$("#xml").html(retNode);
}

Loading…
Cancel
Save