-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加 onedrive 支持
- Loading branch information
Showing
17 changed files
with
308 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '上传失败' } | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.