From 656ebeb3e91f1ef89e7af2220a68f59f32026888 Mon Sep 17 00:00:00 2001 From: wangxiaoshuang <825034831@qq.com> Date: Tue, 4 Jun 2024 11:00:54 +0800 Subject: [PATCH] =?UTF-8?q?oss=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.js | 14 ++++++++------ src/utils/oss.js | 15 ++++++++++++--- static/pdfjs/web/viewer.js | 11 ++++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main.js b/src/main.js index b16b28d..5f7d827 100644 --- a/src/main.js +++ b/src/main.js @@ -103,12 +103,14 @@ Vue.prototype.selectDictLabel = selectDictLabel Vue.prototype.handleTree = handleTree Vue.prototype.$getObjectName = (url) => { - console.log(url) - var value = url - var str = value.split("//"); //https://进行分割, - var index = str[1].indexOf("/") + 1; //indexOf 获取第一个斜杠的索引, - console.log(str[1].substring(index)); //substring 截取, - return '/' + str[1].substring(index) + // console.log(url,'url') + // console.log(Vue.prototype.OSSclientConfig.viewEndpoint,'url') + var value = url; + let basePath = Vue.prototype.OSSclientConfig.viewEndpoint; + if (Vue.prototype.OSSclientConfig.ObjectStoreUse === 'AliyunOSS') { + basePath = Vue.prototype.OSSclientConfig.endPoint.split('https://').length > 1 ? Vue.prototype.OSSclientConfig.endPoint.split('https://')[1] : Vue.prototype.OSSclientConfig.endPoint; + } + return value.split(basePath)[1]; } Vue.prototype.getGuid = (text) => { text = md5(text) diff --git a/src/utils/oss.js b/src/utils/oss.js index e1eae40..e1340e1 100644 --- a/src/utils/oss.js +++ b/src/utils/oss.js @@ -19,6 +19,7 @@ function blobToBuffer(blob, fileName) { async function ossGenerateSTS() { let res = await GetObjectStoreToken() Vue.prototype.OSSclientConfig = { ...res.Result[res.Result.ObjectStoreUse] } + Vue.prototype.OSSclientConfig.ObjectStoreUse = res.Result.ObjectStoreUse; Vue.prototype.OSSclientConfig.basePath = Vue.prototype.OSSclientConfig.viewEndpoint switch (res.Result.ObjectStoreUse) { case 'AliyunOSS': @@ -39,7 +40,7 @@ async function ossGenerateSTS() { if (res && res.url) { resolve({ name: objectName, - url: Vue.prototype.OSSclientConfig.viewEndpoint + objectName + url: res.url }) } else { reject() @@ -77,7 +78,7 @@ async function ossGenerateSTS() { } else { resolve({ name: objectName, - url: Vue.prototype.OSSclientConfig.viewEndpoint + objectName + url: Vue.prototype.OSSclientConfig.viewEndpoint + decodeUtf8(objectName) }) } }) @@ -94,6 +95,14 @@ async function ossGenerateSTS() { return } - +function decodeUtf8(bytes) { + let str = bytes.split('?'); + let str2 = str[0].split('/'); + let name = str2[str2.length - 1]; + name = encodeURIComponent(name); + str.shift(); + str2.pop(); + return str2.join("/") + '/' + name; +} export const OSSclient = ossGenerateSTS diff --git a/static/pdfjs/web/viewer.js b/static/pdfjs/web/viewer.js index 62b83ae..54bf426 100644 --- a/static/pdfjs/web/viewer.js +++ b/static/pdfjs/web/viewer.js @@ -977,7 +977,15 @@ const PDFViewerApplication = { await Promise.all(promises); }, - + decodeUtf8(bytes) { + let str = bytes.split('?'); + let str2 = str[0].split('/'); + let name = str2[str2.length-1]; + name = encodeURIComponent(name); + str.shift(); + str2.pop(); + return str2.join("/")+'/'+name+'?'+str.join("?"); + }, async open(file, args) { if (this.pdfLoadingTask) { await this.close(); @@ -992,6 +1000,7 @@ const PDFViewerApplication = { const parameters = Object.create(null); if (typeof file === "string") { + file = this.decodeUtf8(file); this.setTitleUsingUrl(file, file); parameters.url = file; } else if (file && "byteLength" in file) {