md/src/api/file.js

43 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-05-01 21:30:25 +08:00
import fetch from './fetch';
2020-08-29 11:55:16 +08:00
import { v4 as uuidv4 } from 'uuid';
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-05-01 21:30:25 +08:00
2020-08-29 11:55:16 +08:00
function fileUpload(content, fileName) {
const date = new Date();
const dir = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
const uuid = uuidv4();
2020-08-29 14:42:02 +08:00
const token = fileUploadConfig.accessToken[Math.floor(Math.random() * fileUploadConfig.accessToken.length)].replace('doocsmd', '');
2020-08-29 11:55:16 +08:00
const dateFilename = new Date().getTime() + '-' + uuid + '.' + fileName.split('.')[1];
2020-08-29 14:42:02 +08:00
const url = `https://api.github.com/repos/${fileUploadConfig.username}/${fileUploadConfig.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
};