diff --git a/src/datastore/pic-bed-handler.js b/src/datastore/pic-bed-handler.js index 2ccc2dd..9aa6cf2 100644 --- a/src/datastore/pic-bed-handler.js +++ b/src/datastore/pic-bed-handler.js @@ -5,6 +5,7 @@ import upYunUpload from '../main/utils/upYunUpload' import githubUpload from '../main/utils/githubUpload' import smmsUpload from '../main/utils/smmsUpload' import aliYunUpload from '../main/utils/aliYunUpload' +import imgurUpload from '../main/utils/imgurUpload' const picBedHandler = { weibo: weiboUpload, @@ -13,7 +14,8 @@ const picBedHandler = { upyun: upYunUpload, github: githubUpload, smms: smmsUpload, - aliyun: aliYunUpload + aliyun: aliYunUpload, + imgur: imgurUpload } export default picBedHandler diff --git a/src/datastore/pic-bed.js b/src/datastore/pic-bed.js index 23d5310..2df60ed 100644 --- a/src/datastore/pic-bed.js +++ b/src/datastore/pic-bed.js @@ -35,6 +35,11 @@ let picBed = [ type: 'aliyun', name: '阿里云OSS', visible: true + }, + { + type: 'imgur', + name: 'Imgur图床', + visible: true } ] diff --git a/src/main/utils/imgurUpload.js b/src/main/utils/imgurUpload.js new file mode 100644 index 0000000..8da68c2 --- /dev/null +++ b/src/main/utils/imgurUpload.js @@ -0,0 +1,65 @@ +import request from 'request-promise' +import * as img2Base64 from './img2base64' +import db from '../../datastore/index' +import { Notification, clipboard } from 'electron' + +const postOptions = (fileName, imgBase64) => { + const options = db.read().get('picBed.imgur').value() + const clientId = options.clientId + let obj = { + method: 'POST', + url: `https://api.imgur.com/3/image`, + headers: { + Authorization: 'Client-ID ' + clientId, + 'content-type': 'multipart/form-data', + 'User-Agent': 'PicGo' + }, + formData: { + image: imgBase64, + type: 'base64', + name: fileName + } + } + if (options.proxy) { + obj.proxy = options.proxy + } + return obj +} + +const imgurUpload = async (img, type, webContents) => { + try { + webContents.send('uploadProgress', 0) + const imgList = await img2Base64[type](img) + webContents.send('uploadProgress', 30) + const length = imgList.length + for (let i in imgList) { + const options = postOptions(imgList[i].fileName, imgList[i].base64Image) + let body = await request(options) + body = JSON.parse(body) + if (body.success) { + delete imgList[i].base64Image + imgList[i]['imgUrl'] = `${body.data.link}` + imgList[i]['type'] = 'imgur' + 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() + clipboard.writeText('http://docs.imgur.com/api/errno/') + throw new Error(err) + } +} + +export default imgurUpload diff --git a/src/renderer/components/SettingView/Imgur.vue b/src/renderer/components/SettingView/Imgur.vue new file mode 100644 index 0000000..ba2b2d1 --- /dev/null +++ b/src/renderer/components/SettingView/Imgur.vue @@ -0,0 +1,122 @@ + + + \ No newline at end of file diff --git a/src/renderer/router/index.js b/src/renderer/router/index.js index e915a19..f1df17f 100644 --- a/src/renderer/router/index.js +++ b/src/renderer/router/index.js @@ -60,6 +60,11 @@ export default new Router({ component: require('@/components/SettingView/AliYun').default, name: 'aliyun' }, + { + path: 'imgur', + component: require('@/components/SettingView/Imgur').default, + name: 'imgur' + }, { path: 'gallery', component: require('@/components/SettingView/Gallery').default,