From 16d920cc2d0da36de477f5df06dd8770998aa3e7 Mon Sep 17 00:00:00 2001 From: xiaou66 <2630316030@qq.com> Date: Sat, 20 Aug 2022 11:19:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20GitHub=20=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=90=8E=E8=87=AA=E5=AE=9A=E4=B9=89=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- picturebed-vue/src/api/GitHub.js | 45 ++++++++++++--------- picturebed-vue/src/js/Utils.js | 8 ++++ picturebed-vue/src/store/oss.js | 1 + picturebed-vue/src/views/index.vue | 3 +- picturebed-vue/src/views/setting/GitHub.vue | 9 +++++ 5 files changed, 47 insertions(+), 19 deletions(-) diff --git a/picturebed-vue/src/api/GitHub.js b/picturebed-vue/src/api/GitHub.js index 0291fa2..9aea37f 100644 --- a/picturebed-vue/src/api/GitHub.js +++ b/picturebed-vue/src/api/GitHub.js @@ -1,11 +1,12 @@ import store from '../store/index' +// eslint-disable-next-line import/no-duplicates import Utils from '@/js/Utils' const branchReg = /ref=(.*)/ function GitHub () {} const Path = require('path') GitHub.uploadImage = (item, id) => { return new Promise((resolve, reject) => { - const { path, project, token, branch } = store.state.oss.GitHub + const { path, project, token, branch, formatPath = '' } = store.state.oss.GitHub const fileName = Utils.getImageSavePath(path, item.name, { timestamp: true }) const fr = new FileReader() fr.readAsDataURL(item) @@ -30,25 +31,33 @@ GitHub.uploadImage = (item, id) => { }) .then(res => res.json()) .then(res => { - if (res.content && res.content.download_url) { - // https://raw.githubusercontent.com/xiaou66/picture/master/1597318943475-logo.png - // https://cdn.jsdelivr.net/gh/xiaou66/picture/master/1597318943475-logo.png - // 超过 20 MB 不使用 jsdelivr cdn - if (item.size >= 20 * 1024 * 1024) { - resolve({ status: 200, url: res.content.download_url, id }) - } else { - debugger - let baseUrl = '' - const { path, url } = res.content - branchReg.test(url) - console.log(RegExp.$1) - baseUrl = `${project}@${RegExp.$1}/${path}` - // /xiaou66/pic/image/1609054282923-86467220_p0.png - const jsdelivrUrl = `https://fastly.jsdelivr.net/gh/${baseUrl}` - resolve({ status: 200, url: jsdelivrUrl, id }) + const { path, url } = res.content + branchReg.test(url) + if (formatPath) { + const params = { + branch: RegExp.$1, + uploadPath: path, + project } + console.log('params', params) + console.log(formatPath) + const uploadPath = Utils.formatPath(formatPath, params) + resolve({ status: 200, url: uploadPath, id }) } else { - resolve({ status: 403, message: '上传失败' }) + // region 默认逻辑 >= 20MB 文件使用 GitHub 直连 < 20MB 使用 jsdelivr CDN 加速 + if (res.content && res.content.download_url) { + if (item.size >= 20 * 1024 * 1024) { + resolve({ status: 200, url: res.content.download_url, id }) + } else { + let baseUrl = '' + baseUrl = `${project}@${RegExp.$1}/${path}` + // /xiaou66/pic/image/1609054282923-86467220_p0.png + const jsdelivrUrl = `https://fastly.jsdelivr.net/gh/${baseUrl}` + resolve({ status: 200, url: jsdelivrUrl, id }) + } + } else { + resolve({ status: 403, message: '上传失败' }) + } } }).catch(() => { resolve({ status: 403, message: '上传失败' }) diff --git a/picturebed-vue/src/js/Utils.js b/picturebed-vue/src/js/Utils.js index 7458e48..be8508b 100644 --- a/picturebed-vue/src/js/Utils.js +++ b/picturebed-vue/src/js/Utils.js @@ -70,6 +70,14 @@ Utils.getImageSavePath = (formatPath, fileName, { timestamp = false } = {}) => { // }) // }) // } +Utils.formatPath = (formatPath, params) => { + for (const key in params) { + if (params[key]) { + formatPath = formatPath.replace(new RegExp('\\{' + key + '\\}', 'g'), params[key]) + } + } + return formatPath +} Utils.compressImage = async (file, maxSizeMB) => { try { console.log('压缩') diff --git a/picturebed-vue/src/store/oss.js b/picturebed-vue/src/store/oss.js index 7d0f7e1..4b7212c 100644 --- a/picturebed-vue/src/store/oss.js +++ b/picturebed-vue/src/store/oss.js @@ -28,6 +28,7 @@ const oss = { project: '', cdn: '', branch: '', + formatPath: '', at: false }, onedrive: { diff --git a/picturebed-vue/src/views/index.vue b/picturebed-vue/src/views/index.vue index 48cc2f7..5d3dba7 100644 --- a/picturebed-vue/src/views/index.vue +++ b/picturebed-vue/src/views/index.vue @@ -101,7 +101,8 @@ export default { uploadTaskQueue: new Bobolink({ scheduleMode: Bobolink.SCHEDULE_MODE_FREQUENCY, countPerTimeScale: 1, - concurrency: 1 + concurrency: 1, + timeout: 10 * 1000 * 60 }), picturePreview: { visible: false, diff --git a/picturebed-vue/src/views/setting/GitHub.vue b/picturebed-vue/src/views/setting/GitHub.vue index a904bb4..f684246 100644 --- a/picturebed-vue/src/views/setting/GitHub.vue +++ b/picturebed-vue/src/views/setting/GitHub.vue @@ -29,6 +29,15 @@ + + + + + + 修复