Skip to content

Commit

Permalink
增加 GitHub 上传后自定义路径
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaou66 committed Aug 20, 2022
1 parent 2543cd1 commit 16d920c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
45 changes: 27 additions & 18 deletions picturebed-vue/src/api/GitHub.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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: '上传失败' })
Expand Down
8 changes: 8 additions & 0 deletions picturebed-vue/src/js/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('压缩')
Expand Down
1 change: 1 addition & 0 deletions picturebed-vue/src/store/oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const oss = {
project: '',
cdn: '',
branch: '',
formatPath: '',
at: false
},
onedrive: {
Expand Down
3 changes: 2 additions & 1 deletion picturebed-vue/src/views/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions picturebed-vue/src/views/setting/GitHub.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
<a-input placeholder="[留空>>使用仓库默认分支(通常是 master)]" v-model="oss.GitHub.branch"></a-input>
</a-tooltip>
</a-form-item>
<a-form-item label="格式化路径">
<a-tooltip placement="topLeft">
<template #title>
高级设置 {branch} 分支 {uploadPath} 上传后路径 {project} 仓库名称
例子: https://fastly.jsdelivr.net/gh/{project}@{branch}/{uploadPath}
</template>
<a-input placeholder="" v-model="oss.GitHub.formatPath" />
</a-tooltip>
</a-form-item>
</a-form>
<a-row type="flex" justify="center" style="margin-bottom: 10px">
<a-button @click="fixHostHandler">修复</a-button>
Expand Down

0 comments on commit 16d920c

Please sign in to comment.