2020-05-01 21:30:25 +08:00
|
|
|
import fetch from './fetch';
|
2020-08-31 20:48:22 +08:00
|
|
|
import {
|
|
|
|
v4 as uuidv4
|
|
|
|
} from 'uuid';
|
2020-08-29 11:55:16 +08:00
|
|
|
const fileUploadConfig = {
|
|
|
|
username: 'filess',
|
|
|
|
repo: 'images',
|
2020-08-29 14:42:02 +08:00
|
|
|
accessToken: [
|
|
|
|
'7715d7ca67b5d3837cfdoocsmde8c38421815aa423510af',
|
|
|
|
'c411415bf95dbe39625doocsmd5047ba9b7a2a6c9642abe',
|
|
|
|
'2821cd8819fa345c053doocsmdca86ac653f8bc20db1f1b',
|
|
|
|
'445f0dae46ef1f2a4d6doocsmdc797301e94797b4750a4c',
|
|
|
|
'cc1d0c1426d0fd0902bdoocsmdd2d7184b14da61b86ec46',
|
|
|
|
'b67e9d15cb6f910492fdoocsmdac6b44d379c953bb19eff',
|
|
|
|
'618c4dc2244ccbbc088doocsmd125d17fd31b7d06a50cf3',
|
|
|
|
'a4b581732e1c1507458doocsmdc5b223b27dae5e2e16a55'
|
2020-08-29 11:55:16 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:48:22 +08:00
|
|
|
function getConfiguration() {
|
2020-08-30 20:21:15 +08:00
|
|
|
const imgHost = localStorage.getItem("ImgHost") || 'default'
|
|
|
|
|
2020-08-31 20:48:22 +08:00
|
|
|
// default
|
|
|
|
let token = fileUploadConfig.accessToken[Math.floor(Math.random() * fileUploadConfig.accessToken.length)].replace('doocsmd', '')
|
|
|
|
let username = fileUploadConfig.username
|
|
|
|
let repo = fileUploadConfig.repo
|
2020-08-30 20:21:15 +08:00
|
|
|
|
2020-08-31 20:48:22 +08:00
|
|
|
// GitHub
|
2020-08-30 20:21:15 +08:00
|
|
|
if (imgHost === 'github' && localStorage.getItem("GitHubConfig")) {
|
2020-08-31 20:48:22 +08:00
|
|
|
const githubConfg = JSON.parse(localStorage.getItem("GitHubConfig"))
|
|
|
|
const repoUrl = githubConfg.repo.replace("https://github.com/", "").replace("http://github.com/", "").replace("github.com/", "").split("/")
|
2020-08-30 20:21:15 +08:00
|
|
|
token = githubConfg.accessToken
|
2020-08-31 20:48:22 +08:00
|
|
|
username = repoUrl[0]
|
|
|
|
repo = repoUrl[1]
|
2020-08-30 20:21:15 +08:00
|
|
|
}
|
2020-08-31 20:48:22 +08:00
|
|
|
return {
|
|
|
|
username,
|
|
|
|
repo,
|
|
|
|
token
|
|
|
|
}
|
|
|
|
}
|
2020-08-30 20:21:15 +08:00
|
|
|
|
2020-08-31 20:48:22 +08:00
|
|
|
|
|
|
|
function fileUpload(content, filename) {
|
|
|
|
const date = new Date();
|
|
|
|
const dir = date.getFullYear() + '/' + (date.getMonth() + 1).toString().padStart(2, '0') + '/' + date.getDate().toString().padStart(2, '0');
|
|
|
|
const uuid = uuidv4();
|
|
|
|
const dateFilename = new Date().getTime() + '-' + uuid + '.' + filename.split('.')[1];
|
|
|
|
const {
|
|
|
|
username,
|
|
|
|
repo,
|
|
|
|
token
|
|
|
|
} = getConfiguration()
|
|
|
|
const url = `https://api.github.com/repos/${username}/${repo}/contents/${dir}/${dateFilename}`
|
2020-05-01 21:30:25 +08:00
|
|
|
|
|
|
|
return fetch({
|
2020-08-29 11:55:16 +08:00
|
|
|
url,
|
|
|
|
method: 'put',
|
|
|
|
headers: {
|
|
|
|
'Authorization': 'token ' + token
|
|
|
|
},
|
|
|
|
data: {
|
2020-08-29 14:42:02 +08:00
|
|
|
message: 'Upload image by https://doocs.github.io/md',
|
2020-08-29 11:55:16 +08:00
|
|
|
content: content
|
|
|
|
}
|
2020-05-01 21:30:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
fileUpload
|
2020-08-31 20:48:22 +08:00
|
|
|
};
|