mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-27 19:20:24 +08:00
Partly finished: qiniu upload
This commit is contained in:
@@ -21,8 +21,10 @@ const imgFromPath = async (imgPath) => {
|
||||
|
||||
const imgFromClipboard = (file) => {
|
||||
let result = []
|
||||
const today = new Date().toLocaleString()
|
||||
result.push({
|
||||
base64Image: file.imgUrl.replace(/^data\S+,/, ''),
|
||||
fileName: `${today}.png`,
|
||||
width: file.width,
|
||||
height: file.height
|
||||
})
|
||||
@@ -30,7 +32,6 @@ const imgFromClipboard = (file) => {
|
||||
}
|
||||
|
||||
const imgFromUploader = async (files) => {
|
||||
console.log(files)
|
||||
let results = []
|
||||
await Promise.all(files.map(async item => {
|
||||
let buffer = await fs.readFile(item.path)
|
||||
|
||||
78
src/main/utils/qiniuUpload.js
Normal file
78
src/main/utils/qiniuUpload.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import request from 'request-promise'
|
||||
import * as img2Base64 from './img2base64'
|
||||
import db from '../../datastore/index'
|
||||
import * as qiniu from 'qiniu'
|
||||
import { Notification } from 'electron'
|
||||
|
||||
function postOptions (fileName, token, imgBase64) {
|
||||
const area = selectArea(db.read().get('picBed.qiniu.area').value() || 'z0')
|
||||
const base64FileName = Buffer.from(fileName).toString('base64')
|
||||
return {
|
||||
method: 'POST',
|
||||
url: `http://upload${area}.qiniu.com/putb64/-1/key/${base64FileName}`,
|
||||
headers: {
|
||||
Authorization: `UpToken ${token}`,
|
||||
contentType: 'application/octet-stream'
|
||||
},
|
||||
body: imgBase64
|
||||
}
|
||||
}
|
||||
|
||||
function selectArea (area) {
|
||||
return area === 'z0' ? '' : '-' + area
|
||||
}
|
||||
|
||||
function getToken () {
|
||||
const accessKey = db.read().get('picBed.qiniu.accessKey').value()
|
||||
const secretKey = db.read().get('picBed.qiniu.secretKey').value()
|
||||
const mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
|
||||
const options = {
|
||||
scope: db.read().get('picBed.qiniu.bucket').value()
|
||||
}
|
||||
const putPolicy = new qiniu.rs.PutPolicy(options)
|
||||
return putPolicy.uploadToken(mac)
|
||||
}
|
||||
|
||||
const qiniuUpload = async function (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, getToken(), imgList[i].base64Image)
|
||||
const res = await request(options)
|
||||
const body = JSON.parse(res)
|
||||
if (body.key) {
|
||||
delete imgList[i].base64Image
|
||||
const baseUrl = db.get('picBed.qiniu.url').value()
|
||||
const options = db.get('picBed.qiniu.options').value()
|
||||
imgList[i]['imgUrl'] = `${baseUrl}/${body.key}${options}`
|
||||
imgList[i]['type'] = 'qiniu'
|
||||
if (i - length === -1) {
|
||||
webContents.send('uploadProgress', 60)
|
||||
}
|
||||
} else {
|
||||
webContents.send('uploadProgress', -1)
|
||||
const notification = new Notification({
|
||||
title: '上传失败!',
|
||||
body: res.body.msg
|
||||
})
|
||||
notification.show()
|
||||
}
|
||||
}
|
||||
webContents.send('uploadProgress', 100)
|
||||
return imgList
|
||||
} catch (err) {
|
||||
webContents.send('uploadProgress', -1)
|
||||
const error = JSON.parse(err.response.body)
|
||||
const notification = new Notification({
|
||||
title: '上传失败!',
|
||||
body: error.error
|
||||
})
|
||||
notification.show()
|
||||
throw new Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
export default qiniuUpload
|
||||
14
src/main/utils/uploader.js
Normal file
14
src/main/utils/uploader.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import weiboUpload from './weiboUpload'
|
||||
import qiniuUpload from './qiniuUpload'
|
||||
import db from '../../datastore/index'
|
||||
const uploader = (img, type, webContents) => {
|
||||
const uploadType = db.read().get('picBed.current').value()
|
||||
switch (uploadType) {
|
||||
case 'weibo':
|
||||
return weiboUpload(img, type, webContents)
|
||||
case 'qiniu':
|
||||
return qiniuUpload(img, type, webContents)
|
||||
}
|
||||
}
|
||||
|
||||
export default uploader
|
||||
@@ -70,6 +70,4 @@ const weiboUpload = async function (img, type, webContents) {
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
weiboUpload
|
||||
}
|
||||
export default weiboUpload
|
||||
|
||||
Reference in New Issue
Block a user