Added: imgur support

This commit is contained in:
Molunerfinn 2018-06-27 22:57:15 +08:00
parent 6aed2d2a97
commit 61a2a047c7
5 changed files with 200 additions and 1 deletions

View File

@ -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

View File

@ -35,6 +35,11 @@ let picBed = [
type: 'aliyun',
name: '阿里云OSS',
visible: true
},
{
type: 'imgur',
name: 'Imgur图床',
visible: true
}
]

View File

@ -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

View File

@ -0,0 +1,122 @@
<template>
<div id="imgur-view">
<el-row :gutter="16">
<el-col :span="16" :offset="4">
<div class="view-title">
Imgur图床设置
</div>
<el-form
ref="imgur"
label-position="right"
label-width="120px"
:model="form"
size="mini">
<el-form-item
label="设定ClientId"
prop="clientId"
:rules="{
required: true, message: 'ClientId不能为空', trigger: 'blur'
}">
<el-input v-model="form.clientId" placeholder="ClientId" @keyup.native.enter="confirm"></el-input>
</el-form-item>
<el-form-item
label="设定代理"
prop="proxy"
>
<el-input v-model="form.proxy" placeholder="例如http://127.0.0.1:1080" @keyup.native.enter="confirm"></el-input>
</el-form-item>
<el-form-item>
<el-button-group>
<el-button type="primary" @click="confirm" round>确定</el-button>
<el-button type="success" @click="setDefaultPicBed('imgur')" round :disabled="defaultPicBed === 'imgur'">设为默认图床</el-button>
</el-button-group>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
</template>
<script>
import mixin from '../mixin'
export default {
name: 'imgur',
mixins: [mixin],
data () {
return {
form: {
clientId: '',
proxy: ''
}
}
},
created () {
const config = this.$db.get('picBed.imgur').value()
if (config) {
for (let i in config) {
this.form[i] = config[i]
}
}
},
methods: {
confirm () {
this.$refs.imgur.validate((valid) => {
if (valid) {
this.$db.set('picBed.imgur', this.form).write()
const successNotification = new window.Notification('设置结果', {
body: '设置成功'
})
successNotification.onclick = () => {
return true
}
} else {
return false
}
})
}
}
}
</script>
<style lang='stylus'>
.view-title
color #eee
font-size 20px
text-align center
margin 20px auto
#imgur-view
.el-form
label
line-height 22px
padding-bottom 0
color #eee
.el-input__inner
border-radius 19px
&-item
margin-bottom 10.5px
.el-radio-group
width 100%
label
width 25%
.el-radio-button__inner
width 100%
.el-radio-button:first-child
.el-radio-button__inner
border-left none
border-radius 14px 0 0 14px
.el-radio-button:last-child
.el-radio-button__inner
border-left none
border-radius 0 14px 14px 0
.el-switch__label
color #eee
&.is-active
color #409EFF
.el-icon-question
font-size 20px
float right
margin-top 9px
color #eee
cursor pointer
transition .2s color ease-in-out
&:hover
color #409EFF
</style>

View File

@ -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,