Merge pull request #39 from biuaxia/master

Feat add {b64Url} extension to the preview engine
This commit is contained in:
eyeblue
2021-11-26 17:39:34 +08:00
committed by GitHub
2 changed files with 25 additions and 3 deletions
@@ -10,6 +10,7 @@ export default class PreviewEngine extends Base {
* 此预览引擎的url
* {originUrl} 文件原始的url地址。eg: https://tank.eyeblue.cn/api/alien/download/2a0ceee1-744c-4c82-4215-69c382597a50/abstract-free-photo-2210x1473.jpg
* {url} 对于公有文件publicUrl=originUrl,对于私有文件publicUrl是originUrl带上downloadToken。 eg: https://tank.eyeblue.cn/api/alien/download/2a0ceee1-744c-4c82-4215-69c382597a50/abstract-free-photo-2210x1473.jpg?downloadTokenUuid=6bdba52f-af6b-49ae-5a5d-fd80bfb01d3b
* {b64Url} 文件原始地址
*/
url: string = "";
+24 -3
View File
@@ -11,6 +11,26 @@ import DownloadToken from "../../../common/model/download/token/DownloadToken";
* 文件预览帮助器
*/
export default class PreviewerHelper {
static b64EncodeUnicode(str: string) {
// first we use encodeURIComponent to get percent-encoded UTF-8,
// then we convert the percent encodings into raw bytes which
// can be fed into btoa.
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
// function toSolidBytes(match, p1) {
(match, p1) => {
// console.debug('match: ' + match);
return String.fromCharCode(("0x" + p1) as any);
}));
}
static b64DecodeUnicode(str: string) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
/**
* 准备好一个文件url
*/
@@ -76,8 +96,9 @@ export default class PreviewerHelper {
const engine: PreviewEngine = previewEngine;
if(previewUrl) {
if (previewUrl) {
targetUrl = targetUrl.replace("{originUrl}", previewUrl);
targetUrl = targetUrl.replace("{b64Url}", previewUrl);
targetUrl = targetUrl.replace("{url}", previewUrl);
if (engine.previewInSite) {
BrowserPreviewer.show(fileName, targetUrl, matter.size);
@@ -85,12 +106,12 @@ export default class PreviewerHelper {
window.open(targetUrl);
}
} else {
const needToken = matter.privacy && targetUrl.indexOf("{url}") !== -1;
const needToken = matter.privacy && (targetUrl.indexOf("{url}") !== -1||targetUrl.indexOf("{b64Url}") !== -1)
PreviewerHelper.prepareMatterUrl(matter, needToken, function (url) {
targetUrl = targetUrl.replace("{originUrl}", matter.getPreviewUrl());
targetUrl = targetUrl.replace("{b64Url}", encodeURIComponent(PreviewerHelper.b64EncodeUnicode(url)));
targetUrl = targetUrl.replace("{url}", encodeURIComponent(url));
if (engine.previewInSite) {
BrowserPreviewer.show(fileName, targetUrl, matter.size);
} else {