2018-07-11 03:28:56 -04:00
|
|
|
import request from 'request-promise'
|
2018-06-26 11:50:16 -04:00
|
|
|
import * as img2Base64 from './img2base64'
|
|
|
|
import db from '../../datastore/index'
|
2018-09-07 05:32:11 -04:00
|
|
|
import { Notification } from 'electron'
|
2018-07-11 03:28:56 -04:00
|
|
|
import crypto from 'crypto'
|
2018-07-18 03:34:35 -04:00
|
|
|
import mime from 'mime-types'
|
2018-06-26 11:50:16 -04:00
|
|
|
|
2018-07-11 03:28:56 -04:00
|
|
|
// generate OSS signature
|
|
|
|
const generateSignature = (fileName) => {
|
|
|
|
const options = db.read().get('picBed.aliyun').value()
|
|
|
|
const date = new Date().toGMTString()
|
2018-07-18 03:34:35 -04:00
|
|
|
const signString = `PUT\n\n${mime.lookup(fileName)}\n${date}\n/${options.bucket}/${options.path}${fileName}`
|
2018-07-11 03:28:56 -04:00
|
|
|
|
|
|
|
const signature = crypto.createHmac('sha1', options.accessKeySecret).update(signString).digest('base64')
|
|
|
|
return `OSS ${options.accessKeyId}:${signature}`
|
|
|
|
}
|
2018-06-26 11:50:16 -04:00
|
|
|
|
2018-07-11 03:28:56 -04:00
|
|
|
const postOptions = (fileName, signature, imgBase64) => {
|
2018-06-26 11:50:16 -04:00
|
|
|
const options = db.read().get('picBed.aliyun').value()
|
2018-07-11 03:28:56 -04:00
|
|
|
return {
|
|
|
|
method: 'PUT',
|
|
|
|
url: `https://${options.bucket}.${options.area}.aliyuncs.com/${encodeURI(options.path)}${encodeURI(fileName)}`,
|
|
|
|
headers: {
|
|
|
|
Host: `${options.bucket}.${options.area}.aliyuncs.com`,
|
|
|
|
Authorization: signature,
|
2018-07-18 03:34:35 -04:00
|
|
|
Date: new Date().toGMTString(),
|
|
|
|
'content-type': mime.lookup(fileName)
|
2018-07-11 03:28:56 -04:00
|
|
|
},
|
|
|
|
body: Buffer.from(imgBase64, 'base64'),
|
|
|
|
resolveWithFullResponse: true
|
|
|
|
}
|
2018-06-26 11:50:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
for (let i in imgList) {
|
2018-07-11 03:28:56 -04:00
|
|
|
const signature = generateSignature(imgList[i].fileName)
|
|
|
|
const options = postOptions(imgList[i].fileName, signature, imgList[i].base64Image)
|
|
|
|
let body = await request(options)
|
|
|
|
if (body.statusCode === 200) {
|
2018-06-26 11:50:16 -04:00
|
|
|
delete imgList[i].base64Image
|
|
|
|
if (customUrl) {
|
|
|
|
imgList[i]['imgUrl'] = `${customUrl}/${path}${imgList[i].fileName}`
|
|
|
|
} else {
|
2018-09-26 11:11:21 -04:00
|
|
|
imgList[i]['imgUrl'] = `https://${aliYunOptions.bucket}.${aliYunOptions.area}.aliyuncs.com/${path}${imgList[i].fileName}`
|
2018-06-26 11:50:16 -04:00
|
|
|
}
|
|
|
|
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: '上传失败!',
|
2018-07-06 12:31:53 -04:00
|
|
|
body: `请检查你的配置项是否正确`
|
2018-06-26 11:50:16 -04:00
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
throw new Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default aliYunUpload
|