新增markdown格式预览支持

This commit is contained in:
kl 2020-12-25 16:59:11 +08:00
parent cb21952155
commit 4af19dd646
9 changed files with 132 additions and 26 deletions

View File

@ -0,0 +1,14 @@
[#ftl]
[#-- @implicitly included --]
[#-- @ftlvariable name="imgurls" type="String" --]
[#-- @ftlvariable name="watermarkAngle" type="String" --]
[#-- @ftlvariable name="watermarkHeight" type="String" --]
[#-- @ftlvariable name="watermarkWidth" type="String" --]
[#-- @ftlvariable name="watermarkAlpha" type="String" --]
[#-- @ftlvariable name="watermarkColor" type="String" --]
[#-- @ftlvariable name="watermarkFontsize" type="String" --]
[#-- @ftlvariable name="watermarkFont" type="String" --]
[#-- @ftlvariable name="watermarkYSpace" type="String" --]
[#-- @ftlvariable name="watermarkXSpace" type="String" --]
[#-- @ftlvariable name="watermarkTxt" type="String" --]
[#-- @ftlvariable name="ordinaryUrl" type="String" --]

View File

@ -5,15 +5,17 @@ package cn.keking.model;
* Content :文件类型文本office压缩包等等
*/
public enum FileType {
picture("pictureFilePreviewImpl"),
picture("picturefilepreviewimpl"),
compress("compressFilePreviewImpl"),
office("officeFilePreviewImpl"),
simText("simTextFilePreviewImpl"),
pdf("pdfFilePreviewImpl"),
other("otherFilePreviewImpl"),
media("mediaFilePreviewImpl"),
markdown("markdownFilePreviewImpl"),
cad("cadFilePreviewImpl");
private final String instanceName;
FileType(String instanceName){

View File

@ -0,0 +1,27 @@
package cn.keking.service.impl;
import cn.keking.model.FileAttribute;
import cn.keking.service.FilePreview;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
/**
* @author kl (http://kailing.pub)
* @since 2020/12/25
*/
@Service
public class MarkdownFilePreviewImpl implements FilePreview {
private final SimTextFilePreviewImpl simTextFilePreview;
public MarkdownFilePreviewImpl(SimTextFilePreviewImpl simTextFilePreview) {
this.simTextFilePreview = simTextFilePreview;
}
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
model.addAttribute("markdown","true");
return simTextFilePreview.filePreviewHandle(url, model, fileAttribute);
}
}

View File

@ -58,9 +58,9 @@ public class DownloadUtils {
URL url = new URL(urlStr);
if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file")||url.getProtocol().toLowerCase().startsWith("http"))) {
byte[] bytes = getBytesFromUrl(urlStr);
OutputStream os = new FileOutputStream(new File(realPath));
OutputStream os = new FileOutputStream(realPath);
saveBytesToOutStream(bytes, os);
} else if (url.getProtocol() != null && "ftp".equals(url.getProtocol().toLowerCase())) {
} else if (url.getProtocol() != null && "ftp".equalsIgnoreCase(url.getProtocol())) {
String ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);

View File

@ -27,7 +27,6 @@ public class FileUtils {
private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
private final String fileDir = ConfigConstants.getFileDir();
private final CacheService cacheService;
public FileUtils(CacheService cacheService) {
@ -81,6 +80,9 @@ public class FileUtils {
if (listOfficeTypes().contains(fileType.toLowerCase())) {
return FileType.office;
}
if("md".equalsIgnoreCase(fileType)){
return FileType.markdown;
}
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
return FileType.simText;
}
@ -323,7 +325,7 @@ public class FileUtils {
FileType type;
String suffix;
String fullFileName = getUrlParameterReg(url, "fullfilename");
if (!StringUtils.isEmpty(fullFileName)) {
if (StringUtils.hasText(fullFileName)) {
fileName = fullFileName;
type = typeFromFileName(fileName);
suffix = suffixFromFileName(fileName);

View File

@ -32,11 +32,8 @@ public class OnlinePreviewController {
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
private final FilePreviewFactory previewFactory;
private final CacheService cacheService;
private final FileUtils fileUtils;
private final DownloadUtils downloadUtils;
public OnlinePreviewController(FilePreviewFactory filePreviewFactory,

File diff suppressed because one or more lines are too long

View File

@ -63,12 +63,12 @@
window.onscroll = throttle(checkImgs);
function changePreviewType(previewType) {
var url = window.location.href;
if (url.indexOf("officePreviewType=image") != -1) {
if (url.indexOf("officePreviewType=image") !== -1) {
url = url.replace("officePreviewType=image", "officePreviewType="+previewType);
} else {
url = url + "&officePreviewType="+previewType;
}
if ('allImages' == previewType) {
if ('allImages' === previewType) {
window.open(url)
} else {
window.location.href = url;

View File

@ -1,28 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
<title>普通文本预览</title>
<style>
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div id = "text"></div>
<div class="container" >
<#if markdown??>
<p>
<button id="markdown_btn" type="button" class="btn btn-primary">切换markdown</button>
<button id="text_btn" type="button" class="btn btn-primary">切换text</button>
</p>
<div id="markdown" style="padding: 18px;"></div>
<#else>
<div id="text"></div>
</#if>
</div>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
<script src="js/jquery-3.0.0.min.js" type="text/javascript"></script>
<script src="js/jquery.form.min.js" type="text/javascript"></script>
<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="js/watermark.js" type="text/javascript"></script>
<script src="js/marked.min.js" type="text/javascript"></script>
<script>
/*初始化水印*/
window.onload = function() {
var watermarkTxt = '${watermarkTxt}';
window.onload = function () {
$("#markdown_btn").hide()
initWaterMark();
fetchData();
}
/**
* 初始化水印
*/
function initWaterMark() {
let watermarkTxt = '${watermarkTxt}';
if (watermarkTxt !== '') {
watermark.init({
watermark_txt: '${watermarkTxt}',
@ -34,23 +50,65 @@
watermark_y_space: ${watermarkYSpace},
watermark_font: '${watermarkFont}',
watermark_fontsize: '${watermarkFontsize}',
watermark_color:'${watermarkColor}',
watermark_color: '${watermarkColor}',
watermark_alpha: ${watermarkAlpha},
watermark_width: ${watermarkWidth},
watermark_height: ${watermarkHeight},
watermark_angle: ${watermarkAngle},
});
}
}
/**
* 获取文本数据
*/
function fetchData() {
$.ajax({
type: 'GET',
url: '${ordinaryUrl}',
success: function (data) {
$("#text").html("<pre>" + data + "</pre>");
window.textData = data;
window.textPreData = "<pre>" + data + "</pre>";
window.textMarkdownData = marked(window.textData);
$("#text").html(window.textPreData);
$("#markdown").html(window.textMarkdownData);
}
});
}
$(function () {
$("#markdown_btn").click(function () {
$("#markdown").html(window.textMarkdownData);
$("#text_btn").show()
$("#markdown_btn").hide()
});
$("#text_btn").click(function () {
$("#markdown_btn").show()
$("#text_btn").hide();
$("#markdown").html(window.textPreData);
});
});
</script>
<style>
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
width: 100%;
}
#markdown {
height: 97%;
max-height: 97%;
border: 1px solid #eee;
overflow-y: scroll;
width: 100%;
}
</style>
</body>
</html>