新增markdown格式预览支持
parent
cb21952155
commit
cf1f833d60
|
@ -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" --]
|
|
@ -5,15 +5,17 @@ package cn.keking.model;
|
||||||
* Content :文件类型,文本,office,压缩包等等
|
* Content :文件类型,文本,office,压缩包等等
|
||||||
*/
|
*/
|
||||||
public enum FileType {
|
public enum FileType {
|
||||||
picture("pictureFilePreviewImpl"),
|
picture("picturefilepreviewimpl"),
|
||||||
compress("compressFilePreviewImpl"),
|
compress("compressFilePreviewImpl"),
|
||||||
office("officeFilePreviewImpl"),
|
office("officeFilePreviewImpl"),
|
||||||
simText("simTextFilePreviewImpl"),
|
simText("simTextFilePreviewImpl"),
|
||||||
pdf("pdfFilePreviewImpl"),
|
pdf("pdfFilePreviewImpl"),
|
||||||
other("otherFilePreviewImpl"),
|
other("otherFilePreviewImpl"),
|
||||||
media("mediaFilePreviewImpl"),
|
media("mediaFilePreviewImpl"),
|
||||||
|
markdown("markdownFilePreviewImpl"),
|
||||||
cad("cadFilePreviewImpl");
|
cad("cadFilePreviewImpl");
|
||||||
|
|
||||||
|
|
||||||
private final String instanceName;
|
private final String instanceName;
|
||||||
|
|
||||||
FileType(String instanceName){
|
FileType(String instanceName){
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -58,9 +58,9 @@ public class DownloadUtils {
|
||||||
URL url = new URL(urlStr);
|
URL url = new URL(urlStr);
|
||||||
if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file")||url.getProtocol().toLowerCase().startsWith("http"))) {
|
if (url.getProtocol() != null && (url.getProtocol().toLowerCase().startsWith("file")||url.getProtocol().toLowerCase().startsWith("http"))) {
|
||||||
byte[] bytes = getBytesFromUrl(urlStr);
|
byte[] bytes = getBytesFromUrl(urlStr);
|
||||||
OutputStream os = new FileOutputStream(new File(realPath));
|
OutputStream os = new FileOutputStream(realPath);
|
||||||
saveBytesToOutStream(bytes, os);
|
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 ftpUsername = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME);
|
||||||
String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
|
String ftpPassword = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);
|
||||||
String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
|
String ftpControlEncoding = fileUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_CONTROL_ENCODING);
|
||||||
|
|
|
@ -27,7 +27,6 @@ public class FileUtils {
|
||||||
private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
|
private static final String DEFAULT_CONVERTER_CHARSET = System.getProperty("sun.jnu.encoding");
|
||||||
|
|
||||||
private final String fileDir = ConfigConstants.getFileDir();
|
private final String fileDir = ConfigConstants.getFileDir();
|
||||||
|
|
||||||
private final CacheService cacheService;
|
private final CacheService cacheService;
|
||||||
|
|
||||||
public FileUtils(CacheService cacheService) {
|
public FileUtils(CacheService cacheService) {
|
||||||
|
@ -81,6 +80,9 @@ public class FileUtils {
|
||||||
if (listOfficeTypes().contains(fileType.toLowerCase())) {
|
if (listOfficeTypes().contains(fileType.toLowerCase())) {
|
||||||
return FileType.office;
|
return FileType.office;
|
||||||
}
|
}
|
||||||
|
if("md".equalsIgnoreCase(fileType)){
|
||||||
|
return FileType.markdown;
|
||||||
|
}
|
||||||
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
|
if (Arrays.asList(simText).contains(fileType.toLowerCase())) {
|
||||||
return FileType.simText;
|
return FileType.simText;
|
||||||
}
|
}
|
||||||
|
@ -323,7 +325,7 @@ public class FileUtils {
|
||||||
FileType type;
|
FileType type;
|
||||||
String suffix;
|
String suffix;
|
||||||
String fullFileName = getUrlParameterReg(url, "fullfilename");
|
String fullFileName = getUrlParameterReg(url, "fullfilename");
|
||||||
if (!StringUtils.isEmpty(fullFileName)) {
|
if (StringUtils.hasText(fullFileName)) {
|
||||||
fileName = fullFileName;
|
fileName = fullFileName;
|
||||||
type = typeFromFileName(fileName);
|
type = typeFromFileName(fileName);
|
||||||
suffix = suffixFromFileName(fileName);
|
suffix = suffixFromFileName(fileName);
|
||||||
|
|
|
@ -32,11 +32,8 @@ public class OnlinePreviewController {
|
||||||
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
|
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
|
||||||
|
|
||||||
private final FilePreviewFactory previewFactory;
|
private final FilePreviewFactory previewFactory;
|
||||||
|
|
||||||
private final CacheService cacheService;
|
private final CacheService cacheService;
|
||||||
|
|
||||||
private final FileUtils fileUtils;
|
private final FileUtils fileUtils;
|
||||||
|
|
||||||
private final DownloadUtils downloadUtils;
|
private final DownloadUtils downloadUtils;
|
||||||
|
|
||||||
public OnlinePreviewController(FilePreviewFactory filePreviewFactory,
|
public OnlinePreviewController(FilePreviewFactory filePreviewFactory,
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -63,12 +63,12 @@
|
||||||
window.onscroll = throttle(checkImgs);
|
window.onscroll = throttle(checkImgs);
|
||||||
function changePreviewType(previewType) {
|
function changePreviewType(previewType) {
|
||||||
var url = window.location.href;
|
var url = window.location.href;
|
||||||
if (url.indexOf("officePreviewType=image") != -1) {
|
if (url.indexOf("officePreviewType=image") !== -1) {
|
||||||
url = url.replace("officePreviewType=image", "officePreviewType="+previewType);
|
url = url.replace("officePreviewType=image", "officePreviewType="+previewType);
|
||||||
} else {
|
} else {
|
||||||
url = url + "&officePreviewType="+previewType;
|
url = url + "&officePreviewType="+previewType;
|
||||||
}
|
}
|
||||||
if ('allImages' == previewType) {
|
if ('allImages' === previewType) {
|
||||||
window.open(url)
|
window.open(url)
|
||||||
} else {
|
} else {
|
||||||
window.location.href = url;
|
window.location.href = url;
|
||||||
|
|
|
@ -1,28 +1,44 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8"/>
|
||||||
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
|
||||||
<title>普通文本预览</title>
|
<title>普通文本预览</title>
|
||||||
<style>
|
|
||||||
* {
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
html, body {
|
|
||||||
height: 100%;
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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-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/watermark.js" type="text/javascript"></script>
|
||||||
|
<script src="js/marked.min.js" type="text/javascript"></script>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
/*初始化水印*/
|
/*初始化水印*/
|
||||||
window.onload = function() {
|
window.onload = function () {
|
||||||
var watermarkTxt = '${watermarkTxt}';
|
$("#markdown_btn").hide()
|
||||||
|
initWaterMark();
|
||||||
|
fetchData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化水印
|
||||||
|
*/
|
||||||
|
function initWaterMark() {
|
||||||
|
let watermarkTxt = '${watermarkTxt}';
|
||||||
if (watermarkTxt !== '') {
|
if (watermarkTxt !== '') {
|
||||||
watermark.init({
|
watermark.init({
|
||||||
watermark_txt: '${watermarkTxt}',
|
watermark_txt: '${watermarkTxt}',
|
||||||
|
@ -34,23 +50,65 @@
|
||||||
watermark_y_space: ${watermarkYSpace},
|
watermark_y_space: ${watermarkYSpace},
|
||||||
watermark_font: '${watermarkFont}',
|
watermark_font: '${watermarkFont}',
|
||||||
watermark_fontsize: '${watermarkFontsize}',
|
watermark_fontsize: '${watermarkFontsize}',
|
||||||
watermark_color:'${watermarkColor}',
|
watermark_color: '${watermarkColor}',
|
||||||
watermark_alpha: ${watermarkAlpha},
|
watermark_alpha: ${watermarkAlpha},
|
||||||
watermark_width: ${watermarkWidth},
|
watermark_width: ${watermarkWidth},
|
||||||
watermark_height: ${watermarkHeight},
|
watermark_height: ${watermarkHeight},
|
||||||
watermark_angle: ${watermarkAngle},
|
watermark_angle: ${watermarkAngle},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文本数据
|
||||||
|
*/
|
||||||
|
function fetchData() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
type: 'GET',
|
type: 'GET',
|
||||||
url: '${ordinaryUrl}',
|
url: '${ordinaryUrl}',
|
||||||
success: function (data) {
|
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>
|
</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>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue