From 4dd6586cb4a11ec7932f6e67e2f66d1f529c9448 Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Tue, 28 Nov 2017 23:56:15 +0800 Subject: [PATCH] Partly finished: weibo picbed --- src/main/index.js | 67 ++++++-- src/main/utils/img2base64.js | 21 ++- src/main/utils/pasteTemplate.js | 9 + src/main/utils/weiboUpload.js | 25 ++- src/renderer/App.vue | 3 + .../components/SettingView/Upload.vue | 156 +++++++++++++++++- src/renderer/components/SettingView/Weibo.vue | 54 +++--- src/renderer/components/TrayPage.vue | 8 +- src/renderer/components/mixin.js | 25 +++ 9 files changed, 318 insertions(+), 50 deletions(-) create mode 100644 src/main/utils/pasteTemplate.js create mode 100644 src/renderer/components/mixin.js diff --git a/src/main/index.js b/src/main/index.js index 7be48d9..d296e22 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -2,7 +2,8 @@ import { weiboUpload } from './utils/weiboUpload.js' import { app, BrowserWindow, Tray, Menu, Notification, clipboard, ipcMain } from 'electron' -import db from '../datastore/index' +import db from '../datastore' +import pasteTemplate from './utils/pasteTemplate' /** * Set `__static` path to static files in production * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html @@ -29,7 +30,7 @@ function createTray () { label: 'Quit' }, { - label: 'Open setting Window', + label: '打开详细窗口', click () { if (settingWindow === null) { createSettingWindow() @@ -38,6 +39,26 @@ function createTray () { settingWindow.focus() } } + }, + { + label: '选择默认图床', + type: 'submenu', + submenu: [ + { + label: '微博图床', + type: 'radio', + checked: db.read().get('picBed.current').value() === 'weibo' + }, + { + label: '七牛图床', + type: 'radio', + checked: db.read().get('picBed.current').value() === 'qiniu', + click () { + db.read().set('picBed.current', 'qiniu') + .write() + } + } + ] } ]) tray.on('right-click', () => { @@ -69,9 +90,10 @@ function createTray () { }) tray.on('drop-files', async (event, files) => { - const imgs = await weiboUpload(files, 'imgFromPath') + const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown' + const imgs = await weiboUpload(files, 'imgFromPath', window.webContents) for (let i in imgs) { - clipboard.writeText(imgs[i].imgUrl) + clipboard.writeText(pasteTemplate(pasteStyle, imgs[i].imgUrl)) const notification = new Notification({ title: '上传成功', body: imgs[i].imgUrl, @@ -81,7 +103,6 @@ function createTray () { notification.show() }, i * 100) } - console.log('drag-files') window.webContents.send('dragFiles', imgs) }) toggleWindow() @@ -160,15 +181,6 @@ const toggleWindow = () => { } } -// const toggleSettingWindow = () => { -// if (settingWindow.isVisible()) { -// settingWindow.hide() -// } else { -// settingWindow.show() -// settingWindow.focus() -// } -// } - const showWindow = () => { const position = getWindowPosition() window.setPosition(position.x, position.y, false) @@ -177,8 +189,9 @@ const showWindow = () => { } ipcMain.on('uploadClipboardFiles', async (evt, file) => { - const img = await weiboUpload(file, 'imgFromClipboard') - clipboard.writeText(img[0].imgUrl) + const img = await weiboUpload(file, 'imgFromClipboard', window.webContents) + const pasteStyle = db.read().get('picBed.pasteStyle') || 'markdown' + clipboard.writeText(pasteTemplate(pasteStyle, img[0].imgUrl)) const notification = new Notification({ title: '上传成功', body: img[0].imgUrl, @@ -187,8 +200,26 @@ ipcMain.on('uploadClipboardFiles', async (evt, file) => { notification.show() clipboard.clear() window.webContents.send('clipboardFiles', []) - window.webContents.send('uploadClipboardFiles', img) - console.log('clipboard-upload') + 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' + let pasteText = '' + for (let i in imgs) { + pasteText += pasteTemplate(pasteStyle, imgs[i].imgUrl) + '\r\n' + const notification = new Notification({ + title: '上传成功', + body: imgs[i].imgUrl, + icon: files[i].path + }) + setTimeout(() => { + notification.show() + }, i * 100) + } + clipboard.writeText(pasteText) + window.webContents.send('uploadFiles', imgs) }) app.on('ready', () => { diff --git a/src/main/utils/img2base64.js b/src/main/utils/img2base64.js index 8237792..e41e588 100644 --- a/src/main/utils/img2base64.js +++ b/src/main/utils/img2base64.js @@ -29,7 +29,26 @@ const imgFromClipboard = (file) => { return result } +const imgFromUploader = async (files) => { + console.log(files) + let results = [] + await Promise.all(files.map(async item => { + let buffer = await fs.readFile(item.path) + let base64Image = Buffer.from(buffer, 'binary').toString('base64') + let fileName = item.name + let imgSize = sizeOf(item.path) + results.push({ + base64Image, + fileName, + width: imgSize.width, + height: imgSize.height + }) + })) + return results +} + export { imgFromPath, - imgFromClipboard + imgFromClipboard, + imgFromUploader } diff --git a/src/main/utils/pasteTemplate.js b/src/main/utils/pasteTemplate.js new file mode 100644 index 0000000..9572ee2 --- /dev/null +++ b/src/main/utils/pasteTemplate.js @@ -0,0 +1,9 @@ +export default (style, url) => { + const tpl = { + 'markdown': `![](${url})`, + 'HTML': ``, + 'URL': url, + 'UBB': `[IMG]${url}[/IMG]` + } + return tpl[style] +} diff --git a/src/main/utils/weiboUpload.js b/src/main/utils/weiboUpload.js index 01569d7..cfffc61 100644 --- a/src/main/utils/weiboUpload.js +++ b/src/main/utils/weiboUpload.js @@ -20,35 +20,39 @@ function postOptions (formData) { } } -const weiboUpload = async function (img, type) { +const weiboUpload = async function (img, type, webContents) { try { + webContents.send('uploadProgress', 0) const formData = { username: db.read().get('picBed.weibo.username').value(), password: db.read().get('picBed.weibo.password').value() } + const quality = db.read().get('picBed.weibo.quality').value() const options = postOptions(formData) const res = await rp(options) + webContents.send('uploadProgress', 30) if (res.body.retcode === 20000000) { for (let i in res.body.data.crossdomainlist) { await rp.get(res.body.data.crossdomainlist[i]) } + webContents.send('uploadProgress', 60) const imgList = await img2Base64[type](img) - let resText = [] for (let i in imgList) { let result = await rp.post(UPLOAD_URL, { formData: { b64_data: imgList[i].base64Image } }) - resText.push(result.replace(/<.*?\/>/, '').replace(/<(\w+).*?>.*?<\/\1>/, '')) - } - for (let i in imgList) { - const resTextJson = JSON.parse(resText[i]) - imgList[i]['imgUrl'] = `https://ws1.sinaimg.cn/large/${resTextJson.data.pics.pic_1.pid}` + result = result.replace(/<.*?\/>/, '').replace(/<(\w+).*?>.*?<\/\1>/, '') delete imgList[i].base64Image + const resTextJson = JSON.parse(result) + imgList[i]['imgUrl'] = `https://ws1.sinaimg.cn/${quality}/${resTextJson.data.pics.pic_1.pid}` + imgList[i]['type'] = 'weibo' } + webContents.send('uploadProgress', 100) return imgList } else { + webContents.send('uploadProgress', -1) const notification = new Notification({ title: '上传失败!', body: res.body.msg @@ -56,7 +60,12 @@ const weiboUpload = async function (img, type) { notification.show() } } catch (err) { - console.log('This is error', err, err.name === 'RequestError') + webContents.send('uploadProgress', -1) + const notification = new Notification({ + title: '上传失败!', + body: '服务端出错,请重试' + }) + notification.show() throw new Error(err) } } diff --git a/src/renderer/App.vue b/src/renderer/App.vue index c1d8c3a..bab3111 100644 --- a/src/renderer/App.vue +++ b/src/renderer/App.vue @@ -17,4 +17,7 @@ margin 0 height 100% font-family "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif + #app + overflow-x hidden + user-select none diff --git a/src/renderer/components/SettingView/Upload.vue b/src/renderer/components/SettingView/Upload.vue index 12bb05f..8721ff4 100644 --- a/src/renderer/components/SettingView/Upload.vue +++ b/src/renderer/components/SettingView/Upload.vue @@ -1,17 +1,169 @@ \ No newline at end of file diff --git a/src/renderer/components/SettingView/Weibo.vue b/src/renderer/components/SettingView/Weibo.vue index dd9b737..bde7d8d 100644 --- a/src/renderer/components/SettingView/Weibo.vue +++ b/src/renderer/components/SettingView/Weibo.vue @@ -16,7 +16,7 @@ :rules="{ required: true, message: '用户名不能为空', trigger: 'blur' }"> - + - + + + + + 缩略图 + 中等尺寸 + 原图 + 确定 @@ -35,22 +42,25 @@