mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-07-10 15:12:08 +08:00
Added: aliYun OSS support
This commit is contained in:
@@ -4,6 +4,7 @@ import tcYunUpload from '../main/utils/tcYunUpload'
|
||||
import upYunUpload from '../main/utils/upYunUpload'
|
||||
import githubUpload from '../main/utils/githubUpload'
|
||||
import smmsUpload from '../main/utils/smmsUpload'
|
||||
import aliYunUpload from '../main/utils/aliYunUpload'
|
||||
|
||||
const picBedHandler = {
|
||||
weibo: weiboUpload,
|
||||
@@ -11,7 +12,8 @@ const picBedHandler = {
|
||||
tcyun: tcYunUpload,
|
||||
upyun: upYunUpload,
|
||||
github: githubUpload,
|
||||
smms: smmsUpload
|
||||
smms: smmsUpload,
|
||||
aliyun: aliYunUpload
|
||||
}
|
||||
|
||||
export default picBedHandler
|
||||
|
||||
@@ -30,6 +30,11 @@ let picBed = [
|
||||
type: 'smms',
|
||||
name: 'SM.MS图床',
|
||||
visible: true
|
||||
},
|
||||
{
|
||||
type: 'aliyun',
|
||||
name: '阿里云OSS',
|
||||
visible: true
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
76
src/main/utils/aliYunUpload.js
Normal file
76
src/main/utils/aliYunUpload.js
Normal file
@@ -0,0 +1,76 @@
|
||||
import * as img2Base64 from './img2base64'
|
||||
import db from '../../datastore/index'
|
||||
import { Notification, clipboard } from 'electron'
|
||||
import { Wrapper as OSS } from 'ali-oss'
|
||||
|
||||
let client
|
||||
|
||||
// generate OSS Options
|
||||
const generateOSSOptions = () => {
|
||||
const options = db.read().get('picBed.aliyun').value()
|
||||
let obj = {
|
||||
region: `${options.area}`,
|
||||
accessKeyId: `${options.accessKeyId}`,
|
||||
accessKeySecret: `${options.accessKeySecret}`,
|
||||
bucket: `${options.bucket}`,
|
||||
secure: true
|
||||
}
|
||||
client = new OSS({
|
||||
region: `${options.area}`,
|
||||
accessKeyId: `${options.accessKeyId}`,
|
||||
accessKeySecret: `${options.accessKeySecret}`,
|
||||
bucket: `${options.bucket}`,
|
||||
secure: true
|
||||
})
|
||||
console.log(obj)
|
||||
return client
|
||||
}
|
||||
|
||||
const aliYunUpload = async (img, type, webContents) => {
|
||||
try {
|
||||
webContents.send('uploadProgress', 0)
|
||||
const imgList = await img2Base64[type](img)
|
||||
webContents.send('uploadProgress', 30)
|
||||
const aliYunOptions = db.read().get('picBed.aliyun').value()
|
||||
const customUrl = aliYunOptions.customUrl
|
||||
const path = aliYunOptions.path
|
||||
const length = imgList.length
|
||||
generateOSSOptions()
|
||||
for (let i in imgList) {
|
||||
let body = await client.put(`${path}${imgList[i].fileName}`, Buffer.from(imgList[i].base64Image, 'base64'))
|
||||
if (body.res.status === 200) {
|
||||
delete imgList[i].base64Image
|
||||
if (customUrl) {
|
||||
imgList[i]['imgUrl'] = `${customUrl}/${path}${imgList[i].fileName}`
|
||||
} else {
|
||||
imgList[i]['imgUrl'] = body.url
|
||||
}
|
||||
imgList[i]['type'] = 'aliyun'
|
||||
if (i - length === -1) {
|
||||
webContents.send('uploadProgress', 60)
|
||||
}
|
||||
} else {
|
||||
webContents.send('uploadProgress', -1)
|
||||
const notification = new Notification({
|
||||
title: '上传失败!',
|
||||
body: '上传失败!'
|
||||
})
|
||||
notification.show()
|
||||
return false
|
||||
}
|
||||
}
|
||||
webContents.send('uploadProgress', 100)
|
||||
return imgList
|
||||
} catch (err) {
|
||||
webContents.send('uploadProgress', -1)
|
||||
const notification = new Notification({
|
||||
title: '上传失败!',
|
||||
body: `请检查你的配置`
|
||||
})
|
||||
notification.show()
|
||||
clipboard.writeText('https://cloud.tencent.com/document/product/436/8432')
|
||||
throw new Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export default aliYunUpload
|
||||
154
src/renderer/components/SettingView/AliYun.vue
Normal file
154
src/renderer/components/SettingView/AliYun.vue
Normal file
@@ -0,0 +1,154 @@
|
||||
<template>
|
||||
<div id="aliyun-view">
|
||||
<el-row :gutter="16">
|
||||
<el-col :span="16" :offset="4">
|
||||
<div class="view-title">
|
||||
阿里云OSS设置
|
||||
</div>
|
||||
<el-form
|
||||
ref="aliyun"
|
||||
label-position="right"
|
||||
label-width="120px"
|
||||
:model="form"
|
||||
size="mini">
|
||||
<el-form-item
|
||||
label="设定KeyId"
|
||||
prop="accessKeyId"
|
||||
:rules="{
|
||||
required: true, message: 'AccessKeyId不能为空', trigger: 'blur'
|
||||
}">
|
||||
<el-input v-model="form.accessKeyId" placeholder="AccessKeyId" @keyup.native.enter="confirm"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
label="设定KeySecret"
|
||||
prop="accessKeySecret"
|
||||
:rules="{
|
||||
required: true, message: 'AccessKeySecret不能为空', trigger: 'blur'
|
||||
}">
|
||||
<el-input v-model="form.accessKeySecret" type="password" @keyup.native.enter="confirm" placeholder="AccessKeySecret"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
label="设定存储空间名"
|
||||
prop="bucket"
|
||||
:rules="{
|
||||
required: true, message: 'Bucket不能为空', trigger: 'blur'
|
||||
}">
|
||||
<el-input v-model="form.bucket" @keyup.native.enter="confirm" placeholder="Bucket"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
label="确认存储区域"
|
||||
prop="area"
|
||||
:rules="{
|
||||
required: true, message: '区域代码不能为空', trigger: 'blur'
|
||||
}">
|
||||
<el-input v-model="form.area" @keyup.native.enter="confirm" placeholder="例如oss-cn-beijing"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
label="指定存储路径"
|
||||
>
|
||||
<el-input v-model="form.path" @keyup.native.enter="confirm" placeholder="例如img/"></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
label="设定自定义域名"
|
||||
>
|
||||
<el-input v-model="form.customUrl" @keyup.native.enter="confirm" placeholder="例如https://xxxx.com"></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('aliyun')" round :disabled="defaultPicBed === 'aliyun'">设为默认图床</el-button>
|
||||
</el-button-group>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</div>
|
||||
</template>
|
||||
<script>
|
||||
import mixin from '../mixin'
|
||||
export default {
|
||||
name: 'aliyun',
|
||||
mixins: [mixin],
|
||||
data () {
|
||||
return {
|
||||
form: {
|
||||
accessKeyId: '',
|
||||
accessKeySecret: '',
|
||||
bucket: '',
|
||||
area: '',
|
||||
path: '',
|
||||
customUrl: ''
|
||||
}
|
||||
}
|
||||
},
|
||||
created () {
|
||||
const config = this.$db.get('picBed.aliyun').value()
|
||||
if (config) {
|
||||
for (let i in config) {
|
||||
this.form[i] = config[i]
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
confirm () {
|
||||
this.$refs.aliyun.validate((valid) => {
|
||||
if (valid) {
|
||||
this.$db.set('picBed.aliyun', 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
|
||||
#aliyun-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>
|
||||
@@ -55,6 +55,11 @@ export default new Router({
|
||||
component: require('@/components/SettingView/SMMS').default,
|
||||
name: 'smms'
|
||||
},
|
||||
{
|
||||
path: 'aliyun',
|
||||
component: require('@/components/SettingView/AliYun').default,
|
||||
name: 'aliyun'
|
||||
},
|
||||
{
|
||||
path: 'gallery',
|
||||
component: require('@/components/SettingView/Gallery').default,
|
||||
|
||||
Reference in New Issue
Block a user