Added: GitHub picbed support

This commit is contained in:
Molunerfinn
2018-04-06 22:01:30 +08:00
parent 975927ac68
commit eecf98e452
11 changed files with 217 additions and 5 deletions

View File

@@ -0,0 +1,62 @@
import request from 'request-promise'
import * as img2Base64 from './img2base64'
import db from '../../datastore/index'
import { Notification } from 'electron'
const postOptions = (fileName, options, data) => {
const path = options.path || ''
const {token, repo} = options
return {
method: 'PUT',
url: `https://api.github.com/repos/${repo}/contents/${path}${encodeURI(fileName)}`,
headers: {
Authorization: `token ${token}`,
'User-Agent': 'PicGo'
},
body: data,
json: true
}
}
const githubUpload = async function (img, type, webContents) {
try {
webContents.send('uploadProgress', 0)
const imgList = await img2Base64[type](img)
const length = imgList.length
const githubOptions = db.read().get('picBed.github').value()
webContents.send('uploadProgress', 30)
for (let i in imgList) {
const data = {
message: 'Upload by PicGo',
branch: githubOptions.branch,
content: imgList[i].base64Image,
path: githubOptions.path + encodeURI(imgList[i].fileName)
}
const postConfig = postOptions(imgList[i].fileName, githubOptions, data)
const body = await request(postConfig)
if (body) {
delete imgList[i].base64Image
imgList[i]['imgUrl'] = body.content.download_url
imgList[i]['type'] = 'github'
if (i - length === -1) {
webContents.send('uploadProgress', 60)
}
} else {
webContents.send('uploadProgress', -1)
return new Error()
}
}
webContents.send('uploadProgress', 100)
return imgList
} catch (err) {
webContents.send('uploadProgress', -1)
const notification = new Notification({
title: '上传失败!',
body: '服务端出错,请重试'
})
notification.show()
throw new Error(err)
}
}
export default githubUpload

View File

@@ -2,6 +2,7 @@ import weiboUpload from './weiboUpload'
import qiniuUpload from './qiniuUpload'
import tcYunUpload from './tcYunUpload'
import upYunUpload from './upYunUpload'
import githubUpload from './githubUpload'
import db from '../../datastore/index'
const checkUploader = (type) => {
@@ -25,6 +26,8 @@ const uploader = (img, type, webContents) => {
return tcYunUpload(img, type, webContents)
case 'upyun':
return upYunUpload(img, type, webContents)
case 'github':
return githubUpload(img, type, webContents)
}
} else {
return false