Partly finished: qiniu upload

This commit is contained in:
Molunerfinn 2017-11-29 23:13:35 +08:00
parent d0fdb79821
commit ec12ba4e0e
12 changed files with 126 additions and 22 deletions

View File

@ -60,6 +60,7 @@
"element-ui": "^2.0.5",
"fs-extra": "^4.0.2",
"image-size": "^0.6.1",
"lodash-id": "^0.14.0",
"lowdb": "^1.0.0",
"qiniu": "^7.1.1",
"request": "^2.83.0",

View File

@ -1,4 +1,5 @@
import Datastore from 'lowdb'
import LodashId from 'lodash-id'
import FileSync from 'lowdb/adapters/FileSync'
import path from 'path'
import { remote, app } from 'electron'
@ -9,6 +10,7 @@ const STORE_PATH = APP.getPath('userData')
const adapter = new FileSync(path.join(STORE_PATH, '/data.json'))
const db = Datastore(adapter)
db._.mixin(LodashId)
if (!db.has('uploaded').value()) {
db.set('uploaded', []).write()

View File

@ -2,6 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<meta name="referrer" content="never">
<title>PicGo</title>
<% if (htmlWebpackPlugin.options.nodeModules) { %>
<!-- Add `node_modules/` to global paths so `require` works properly in development -->

View File

@ -1,6 +1,6 @@
'use strict'
import { weiboUpload } from './utils/weiboUpload.js'
import uploader from './utils/uploader.js'
import { app, BrowserWindow, Tray, Menu, Notification, clipboard, ipcMain } from 'electron'
import db from '../datastore'
import pasteTemplate from './utils/pasteTemplate'
@ -73,10 +73,12 @@ function createTray () {
let img = clipboard.readImage()
let obj = []
if (!img.isEmpty()) {
// 从剪贴板来的图片默认转为png
const imgUrl = 'data:image/png;base64,' + Buffer.from(img.toPNG(), 'binary').toString('base64')
obj.push({
width: img.getSize().width,
height: img.getSize().height,
imgUrl: img.toDataURL()
imgUrl
})
}
toggleWindow()
@ -94,8 +96,8 @@ function createTray () {
})
tray.on('drop-files', async (event, files) => {
const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown'
const imgs = await weiboUpload(files, 'imgFromPath', window.webContents)
const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown'
const imgs = await uploader(files, 'imgFromPath', window.webContents)
for (let i in imgs) {
clipboard.writeText(pasteTemplate(pasteStyle, imgs[i].imgUrl))
const notification = new Notification({
@ -193,8 +195,8 @@ const showWindow = () => {
}
ipcMain.on('uploadClipboardFiles', async (evt, file) => {
const img = await weiboUpload(file, 'imgFromClipboard', window.webContents)
const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown'
const img = await uploader(file, 'imgFromClipboard', window.webContents)
const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown'
clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl))
const notification = new Notification({
title: '上传成功',
@ -202,14 +204,13 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => {
icon: file[0]
})
notification.show()
clipboard.clear()
window.webContents.send('clipboardFiles', [])
window.webContents.send('uploadFiles', img)
})
ipcMain.on('uploadChoosedFiles', async (evt, files) => {
const imgs = await weiboUpload(files, 'imgFromUploader', settingWindow.webContents)
const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown'
const imgs = await uploader(files, 'imgFromUploader', settingWindow.webContents)
const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown'
let pasteText = ''
for (let i in imgs) {
pasteText += pasteTemplate(pasteStyle, imgs[i].imgUrl) + '\r\n'

View File

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

View 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

View 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

View File

@ -70,6 +70,4 @@ const weiboUpload = async function (img, type, webContents) {
}
}
export {
weiboUpload
}
export default weiboUpload

View File

@ -109,10 +109,10 @@ export default {
this.$electron.ipcRenderer.send('uploadChoosedFiles', sendFiles)
},
getPasteStyle () {
this.pasteStyle = this.$db.get('picBed.pasteStyle').value() || 'markdown'
this.pasteStyle = this.$db.read().get('picBed.pasteStyle').value() || 'markdown'
},
handlePasteStyleChange (val) {
this.$db.set('picBed.pasteStyle', val)
this.$db.read().set('picBed.pasteStyle', val)
.write()
}
}

View File

@ -56,7 +56,7 @@ export default {
}
},
created () {
const config = this.$db.get('picBed.weibo').value()
const config = this.$db.read().get('picBed.weibo').value()
if (config) {
this.form.username = config.username
this.form.password = config.password
@ -67,7 +67,7 @@ export default {
confirm (formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
this.$db.set('picBed.weibo', {
this.$db.read().set('picBed.weibo', {
username: this.form.username,
password: this.form.password,
quality: this.quality

View File

@ -46,15 +46,19 @@
this.disableDragFile()
this.getData()
this.$electron.ipcRenderer.on('dragFiles', (event, files) => {
this.$db.get('uploaded').push(...files).write()
this.files = this.$db.get('uploaded').slice().reverse().slice(0, 5).value()
files.forEach(item => {
this.$db.read().get('uploaded').insert(item).write()
})
this.files = this.$db.read().get('uploaded').slice().reverse().slice(0, 5).value()
})
this.$electron.ipcRenderer.on('clipboardFiles', (event, files) => {
this.clipboardFiles = files
})
this.$electron.ipcRenderer.on('uploadFiles', (event, files) => {
this.$db.get('uploaded').push(...files).write()
this.files = this.$db.get('uploaded').slice().reverse().slice(0, 5).value()
files.forEach(item => {
this.$db.read().get('uploaded').insert(item).write()
})
this.files = this.$db.read().get('uploaded').slice().reverse().slice(0, 5).value()
})
},
beforeDestroy () {
@ -64,7 +68,7 @@
},
methods: {
getData () {
this.files = this.$db.get('uploaded').slice().reverse().slice(0, 5).value()
this.files = this.$db.read().get('uploaded').slice().reverse().slice(0, 5).value()
},
copyTheLink (item) {
this.notification.body = item.imgUrl

View File

@ -4939,6 +4939,10 @@ locate-path@^2.0.0:
p-locate "^2.0.0"
path-exists "^3.0.0"
lodash-id@^0.14.0:
version "0.14.0"
resolved "https://registry.yarnpkg.com/lodash-id/-/lodash-id-0.14.0.tgz#baf48934e543a1b5d6346f8c84698b1a8c803896"
lodash._arraycopy@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._arraycopy/-/lodash._arraycopy-3.0.0.tgz#76e7b7c1f1fb92547374878a562ed06a3e50f6e1"