Skip to content

Commit

Permalink
增加 chevereto 支持
Browse files Browse the repository at this point in the history
增加 onedrive 支持
  • Loading branch information
xiaou66 committed Aug 3, 2021
1 parent df86dfc commit 10a857e
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.DS_Store
node_modules
/dist
/vue
vue
# local env files
.env.local
.env.*.local
Expand All @@ -20,3 +20,4 @@ pnpm-debug.log*
*.njsproj
*.sln
*.sw?
yarn.lock
4 changes: 2 additions & 2 deletions pictureBed/plugin.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"pluginName": "图床",
"description": "图床",
"version": "1.1.5",
"version": "1.2.0",
"author": "xiaou",
"main": "vue/index.html",
"update": false,
"update": true,
"preload": "preload.js",
"logo": "logo.png",
"features": [
Expand Down
23 changes: 0 additions & 23 deletions picturebed-vue/.gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions picturebed-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"dependencies": {
"ali-oss": "^6.9.1",
"ant-design-vue": "^1.6.3",
"axios": "^0.21.1",
"core-js": "^3.6.5",
"cos-js-sdk-v5": "^0.5.27",
"node-cache": "^5.1.2",
"vue": "^2.6.11",
"vue-clipboard2": "^0.3.1",
"vue-router": "^3.2.0",
Expand All @@ -35,6 +37,7 @@
"sass": "^1.26.5",
"sass-loader": "^8.0.2",
"terser-webpack-plugin": "^3.0.6",
"utools-api-types": "^2.0.6",
"vue-template-compiler": "^2.6.11"
}
}
20 changes: 20 additions & 0 deletions picturebed-vue/src/api/chevereto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import store from '../store/index'
export default async (item, id) => {
const { token, url } = store.state.oss.chevereto
const fromData = new FormData()
fromData.append('key', token)
fromData.append('format', 'json')
fromData.append('source', item)
return await fetch(`${url}api/1/upload`, {
method: 'POST',
body: fromData
}).then(res => res.json())
.then(res => {
if (res.status_code !== 200) {
return { code: 403, message: res.error.message }
}
return { status: 200, url: res.image.display_url, id }
}).catch(() => {
return { code: 403, message: '上传失败' }
})
}
6 changes: 6 additions & 0 deletions picturebed-vue/src/api/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import GitHub from './GitHub'
import niupic from './niupic'
import smMs from './smMs'
import onji from '@/api/onji'
import onedrive from '@/api/onedrive'
import chevereto from '@/api/chevereto'

export const uploadImage = async (item, id, callback) => {
const type = store.state.image.selectFileMode
Expand All @@ -20,6 +22,10 @@ export const uploadImage = async (item, id, callback) => {
return await GitHub.uploadImage(item, id)
case 'imgUrlOrg':
return await imgUrlOrg.uploadImage(item, id)
case 'onedrive':
return await onedrive(item, id)
case 'chevereto':
return await chevereto(item, id)
}
if (type.includes('映画')) {
const nodeName = type.split('/')[1]
Expand Down
108 changes: 108 additions & 0 deletions picturebed-vue/src/api/onedrive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import axios from 'axios'
import store from '../store/index'
import Vue from 'vue'
import Utils from '@/js/Utils'
const service = axios.create()
service.interceptors.response.use((response) => response,
(error) => {
alert(error.response.statusText)
return Promise.reject(error)
})

export function uploadSmall (file, path, token) {
return service({
method: 'PUT',
url: `https://graph.microsoft.com/v1.0/me/drive/root:/${path}:/content`,
data: file,
headers: {
Authorization: `Bearer ${token}`
}
})
}

export function getUploadUrl (filePath, token) {
return service({
method: 'POST',
url: `https://graph.microsoft.com/v1.0/drive/root:/${filePath}:/createUploadSession`,
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
})
}
export function uploadLarge (data, url, size) {
return service({
method: 'PUT',
url,
data,
headers: {
'Content-Range': `bytes 0-${size - 1}/${size}`
}
})
}

export function getShareId (fileId, token) {
return axios({
method: 'POST',
url: `https://graph.microsoft.com/v1.0/me/drive/items/${fileId}/createLink`,
data: {
type: 'view',
scope: 'anonymous'
},
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
})
}

export function genShareUrl (shareId) {
return `https://api.onedrive.com/v1.0/shares/${shareId}/root/content`
}
async function getToken () {
// eslint-disable-next-line no-unused-expressions
const $Cache = Vue.prototype.$Cache
const token = $Cache.get('onedrive_token')
if (token) {
return token
}
return await service.post('https://xiaou.icu/music/microsoftGraph/auth/refresh_token', {
refresh_token: store.state.oss.onedrive.refreshToken
})
.then(res => {
const { status, data } = res.data
if (status === 200) {
store.state.oss.onedrive.refreshToken = data.refresh_token
$Cache.set('onedrive_token', data.access_token, 50 * 60)
return data.access_token
} else {
return ''
}
}).catch(e => {
return ''
})
}
export default async (item, id) => {
const directory = store.state.oss.onedrive.path
const uploadPath = directory ? Utils.getImageSavePath(directory, item.name) : 'pic' + item.name
const fullPath = uploadPath
const token = await getToken()
if (!token) {
return { status: 403, message: '绑定账号已经过期需要重新绑定' }
}
let uploadResponse
if (item.size > 4 * 1000 * 1000) {
// 大文件
const { data } = await getUploadUrl(fullPath, token)
uploadResponse = await uploadLarge(item, data.uploadUrl, item.size)
} else {
// 小文件
uploadResponse = await uploadSmall(item, fullPath, token)
}
if (!uploadResponse.data.id) {
return { status: 403, message: '上传失败' }
}
const { data } = await getShareId(uploadResponse.data.id, token)
const url = genShareUrl(data.shareId)
return { status: 200, url: url, id }
}
2 changes: 2 additions & 0 deletions picturebed-vue/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'ant-design-vue/dist/antd.css'
import VueClipboard from 'vue-clipboard2'
import AliOss from './js/AliOss'
import TencentOss from './js/TencentOss'
import NodeCache from 'node-cache'
VueClipboard.config.autoSetContainer = true
Vue.use(VueClipboard)
Vue.config.productionTip = false
Expand All @@ -22,6 +23,7 @@ if (process.env.NODE_ENV === 'development') {
Vue.use(Antd)
Vue.prototype.$aliOss = new AliOss()
Vue.prototype.$tencentOss = new TencentOss()
Vue.prototype.$Cache = new NodeCache()
new Vue({
router,
store,
Expand Down
11 changes: 8 additions & 3 deletions picturebed-vue/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,14 @@ const routes = [
component: () => import('../views/setting/smMs')
},
{
path: 'contributions',
name: 'contributions',
component: () => import('../views/setting/contributions')
path: 'onedrive',
name: 'onedrive',
component: () => import('../views/setting/onedrive')
},
{
path: 'chevereto',
name: 'chevereto',
component: () => import('../views/setting/chevereto')
}
]
}
Expand Down
8 changes: 8 additions & 0 deletions picturebed-vue/src/store/oss.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const oss = {
cdn: '',
branch: '',
at: false
},
onedrive: {
refreshToken: '',
path: 'pic'
},
chevereto: {
token: '',
url: ''
}
},
mutations: {
Expand Down
16 changes: 13 additions & 3 deletions picturebed-vue/src/views/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,10 @@ const defaultPictureBed = '猫盒'
export default {
data () {
return {
fileModeKey: ['阿里云OSS', '腾讯云OSS', 'GitHub',
fileModeKey: ['阿里云OSS', '腾讯云OSS', 'GitHub', 'onedrive', 'chevereto',
'猫盒', 'imgUrlOrg', '牛图网', 'smMs',
'映画/腾讯', '映画/京东', '映画/QQ', '映画/网易', '映画/头条', '映画/抖音',
'映画/阿里', '映画/美团', '映画/百度', '映画/携程', '映画/搜狐', '映画/快手',
'映画/百度', '映画/苏宁']
'映画/阿里', '映画/美团', '映画/百度', '映画/携程', '映画/搜狐', '映画/快手', '映画/苏宁']
}
},
computed: {
Expand Down Expand Up @@ -204,9 +203,20 @@ export default {
this.$message.warning('使用 「sm.ms」 需要在设置中需要配置 token')
this.image.selectFileMode = defaultPictureBed
}
} else if (value === 'onedrive') {
if (!this.$store.state.oss.onedrive.refreshToken) {
this.$message.warning('使用 「onedrive」 需要在设置中需要绑定账号')
this.image.selectFileMode = defaultPictureBed
}
} else if (value === 'chevereto') {
if (!this.$store.state.oss.chevereto.token) {
this.$message.warning('使用 「chevereto」 需要在设置中需要绑定账号')
this.image.selectFileMode = defaultPictureBed
}
}
},
uploadFilePath (path) {
// const prefixs = [{ key: 'chevereto', name: 'chevereto' }]
// github 必须有时间戳
const timeStamp = this.configure.timeStamp && this.selectFileMode !== 'GitHub'
const item = window.readFile(path, timeStamp)
Expand Down
5 changes: 3 additions & 2 deletions picturebed-vue/src/views/setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export default {
{ name: '腾讯OSS', router: { name: 'tencentOss' } },
{ name: 'GitHub', router: { name: 'GitHub' } },
{ name: 'sm.ms', router: { name: 'smMs' } },
{ name: '数据', router: { name: 'imageData' } },
{ name: '支持', router: { name: 'contributions' } }
{ name: 'onedrive', router: { name: 'onedrive' } },
{ name: 'chevereto', router: { name: 'chevereto' } },
{ name: '数据', router: { name: 'imageData' } }
],
currentTab: '阿里OSS'
}
Expand Down
7 changes: 6 additions & 1 deletion picturebed-vue/src/views/setting/aliOss.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
<a-input placeholder="bucket" v-model="oss.aliOss.bucket"></a-input>
</a-form-item>
<a-form-item label="目录">
<a-input placeholder="上传目录例如: abc/test/ {Y}:年 {M}:月 {D}:日 {H}:时 {m}:分 {s}:秒 {rand}:随机字符串 [可选]" v-model="oss.aliOss.uploadDirectory"></a-input>
<a-tooltip placement="topLeft">
<template #title>
{Y}:年 {M}:月 {D}:日 {H}:时 {m}:分 {s}:秒 {rand}:随机字符串 [可选]
</template>
<a-input placeholder="上传目录例如: abc/test/ {Y}:年 {M}:月 {D}:日 {H}:时 {m}:分 {s}:秒 {rand}:随机字符串 [可选]" v-model="oss.aliOss.uploadDirectory"></a-input>
</a-tooltip>
</a-form-item>
<a-form-item label="自定义域名">
<a-input placeholder="自定义域名例如:img.example.com [可选]" v-model="oss.aliOss.domain"></a-input>
Expand Down
47 changes: 47 additions & 0 deletions picturebed-vue/src/views/setting/chevereto.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<template>
<div style="min-height: 100%">
<a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 12 }">
<a-form-item label="token" style="margin-top: 20px;">
<a-tooltip placement="topLeft">
<template #title>
在「Dashboard > Settings > API」中
</template>
<a-input placeholder="请输入 API token" v-model="oss.chevereto.token"></a-input>
</a-tooltip>
</a-form-item>
<a-form-item label="图床地址">
<a-tooltip placement="topLeft">
<template #title>
结尾为 / 推荐游览器直接复制
</template>
<a-input placeholder="http://mysite.com/" v-model="oss.chevereto.url"></a-input>
</a-tooltip>
</a-form-item>
<!-- <a-form-item label="文件名前缀">-->
<!-- <a-tooltip placement="topLeft">-->
<!-- <template #title>-->
<!-- {Y}:年 {M}:月 {D}:日 {H}:时 {m}:分 {s}:秒 {rand}:随机字符串 [可选]-->
<!-- </template>-->
<!-- <a-input placeholder="{Y}:年 {M}:月 {D}:日 {H}:时 {m}:分 {s}:秒 {rand}:随机字符串 [可选]" v-model="oss.chevereto.prefix">-->
<!-- </a-input>-->
<!-- </a-tooltip>-->
<!-- </a-form-item>-->
</a-form>
<a-row type="flex" justify="center">
</a-row>
</div>
</template>

<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState(['oss'])
}
}
</script>

<style scoped>
</style>
17 changes: 0 additions & 17 deletions picturebed-vue/src/views/setting/contributions.vue

This file was deleted.

Loading

0 comments on commit 10a857e

Please sign in to comment.