md/src/api/file.js

41 lines
1.3 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',
access_tokens: [
2020-08-29 14:33:24 +08:00
'cc16734fc2d92c5b6d90155f0da9b5c43701b386',
'f5c1b69cac9e2d53213adb1adda927ae7b3ca762',
'5533628a92d69d2271d6d1fdd5a9170c7de65bc8',
'7dc129821137b52d9fb897ba4a96d16224b63845',
'90669202e6277399ec0ea81649953b8f60793c6a',
'a1900917f80c8c1b2ad73327e7c35b47106025dd'
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:33:24 +08:00
const token = fileUploadConfig.access_tokens[Math.round(fileUploadConfig.access_tokens.length * Math.random())];
2020-08-29 11:55:16 +08:00
const dateFilename = new Date().getTime() + '-' + uuid + '.' + fileName.split('.')[1];
const url = `https://api.github.com/repos/${fileUploadConfig.username}/${fileUploadConfig.repo}/contents/${dir}/${dateFilename}?access_token=${token}`;
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: {
message: 'Upload image by https://github.com/doocs/md',
content: content
}
2020-05-01 21:30:25 +08:00
})
}
export default {
fileUpload
};