From 4a0d398b4faa8b19c56118e19d8475954926670d Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Mon, 17 Sep 2018 15:50:24 +0800 Subject: [PATCH] Added: picgo-core support for uploading --- package.json | 2 +- src/main/index.js | 3 +- src/main/utils/uploader.js | 127 +++++++++++++++++++++++++++---------- yarn.lock | 15 +++-- 4 files changed, 107 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 1b3f0600..fad6d07b 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,7 @@ "lowdb": "^1.0.0", "md5": "^2.2.1", "melody.css": "^1.0.2", - "picgo": "^1.0.4", + "picgo": "^1.1.0", "qiniu": "^7.1.1", "request": "^2.83.0", "request-promise": "^4.2.2", diff --git a/src/main/index.js b/src/main/index.js index 720dea9d..44a313e8 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -428,7 +428,8 @@ ipcMain.on('uploadClipboardFilesFromUploadPage', () => { }) ipcMain.on('uploadChoosedFiles', async (evt, files) => { - const imgs = await uploader(files, 'imgFromUploader', evt.sender) + const input = files.map(item => item.path) + const imgs = await uploader(input, 'imgFromUploader', evt.sender) if (imgs !== false) { const pasteStyle = db.read().get('picBed.pasteStyle').value() || 'markdown' let pasteText = '' diff --git a/src/main/utils/uploader.js b/src/main/utils/uploader.js index 72e6d3eb..611d7349 100644 --- a/src/main/utils/uploader.js +++ b/src/main/utils/uploader.js @@ -1,36 +1,75 @@ -// import db from '../../datastore/index' -import { app, Notification } from 'electron' -// import picBeds from '../../datastore/pic-bed-handler' +import { + app, + Notification, + BrowserWindow, + ipcMain +} from 'electron' import PicGo from 'picgo' import path from 'path' import fecha from 'fecha' +const renameURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080/#rename-page` : `file://${__dirname}/index.html#rename-page` + +const createRenameWindow = () => { + let options = { + height: 175, + width: 300, + show: true, + fullscreenable: false, + resizable: false, + vibrancy: 'ultra-dark', + webPreferences: { + backgroundThrottling: false + } + } + + if (process.platform !== 'darwin') { + options.show = true + options.backgroundColor = '#3f3c37' + options.autoHideMenuBar = true + options.transparent = false + } + + const window = new BrowserWindow(options) + window.loadURL(renameURL) + return window +} + const STORE_PATH = app.getPath('userData') const CONFIG_PATH = path.join(STORE_PATH, '/data.json') -// const checkUploader = (type) => { -// const currentUploader = db.read().get(`picBed.${type}`).value() -// if (currentUploader) { -// return true -// } else { -// return false -// } -// } +const waitForShow = (webcontent) => { + return new Promise((resolve, reject) => { + webcontent.on('dom-ready', () => { + resolve() + }) + }) +} + +const waitForRename = (window, id) => { + return new Promise((resolve, reject) => { + ipcMain.once(`rename${id}`, (evt, newName) => { + resolve(newName) + window.hide() + }) + window.on('close', () => { + resolve(null) + ipcMain.removeAllListeners(`rename${id}`) + }) + }) +} const uploader = (img, type, webContents) => { const picgo = new PicGo(CONFIG_PATH) let input = [] switch (type) { - case 'imgFromPath': - input = img - break case 'imgFromClipboard': if (img !== null) { const today = fecha.format(new Date(), 'YYYYMMDDHHmmss') + '.png' input = [ { base64Image: img.imgUrl.replace(/^data\S+,/, ''), - fileName: name || today, + fileName: today, width: img.width, height: img.height, extname: '.png' @@ -39,28 +78,45 @@ const uploader = (img, type, webContents) => { } break default: - input = img.map(item => item.path) + input = img break } + + picgo.helper.beforeUploadPlugins.register('renameFn', { + handle: async ctx => { + const rename = picgo.getConfig('picBed.rename') + const autoRename = picgo.getConfig('picBed.autoRename') + await Promise.all(ctx.output.map(async item => { + let name + let fileName + if (autoRename) { + fileName = fecha.format(new Date(), 'YYYYMMDDHHmmss') + item.extname + } else { + fileName = item.fileName + } + if (rename) { + const window = createRenameWindow() + await waitForShow(window.webContents) + window.webContents.send('rename', fileName, window.webContents.id) + name = await waitForRename(window, window.webContents.id) + } + item.fileName = name || fileName + console.log(item) + })) + } + }) + + picgo.on('beforeTransform', ctx => { + if (ctx.getConfig('picBed.uploadNotification')) { + const notification = new Notification({ + title: '上传进度', + body: '正在上传' + }) + notification.show() + } + }) + picgo.upload(input) - // if (db.read().get('picBed.uploadNotification').value()) { - // const notification = new Notification({ - // title: '上传进度', - // body: '正在上传' - // }) - // notification.show() - // } - // const uploadType = db.read().get('picBed.current').value() - // // if (checkUploader(uploadType)) { - // try { - // return picBeds[uploadType](img, type, webContents) - // } catch (e) { - // console.log(e) - // return false - // } - // } else { - // return false - // } picgo.on('notification', message => { const notification = new Notification(message) @@ -79,6 +135,9 @@ const uploader = (img, type, webContents) => { resolve(false) } }) + picgo.on('failed', ctx => { + resolve(false) + }) }) } diff --git a/yarn.lock b/yarn.lock index e83d78d1..a573e502 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6137,8 +6137,8 @@ path-key@^2.0.0, path-key@^2.0.1: resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40" path-parse@^1.0.5: - version "1.0.5" - resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1" + version "1.0.6" + resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c" path-to-regexp@0.1.7: version "0.1.7" @@ -6191,8 +6191,8 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" picgo@^1.0.4: - version "1.0.4" - resolved "https://registry.yarnpkg.com/picgo/-/picgo-1.0.4.tgz#4bf2009a917bfe4db13d262bc10625f4a6a5c90a" + version "1.1.0" + resolved "https://registry.yarnpkg.com/picgo/-/picgo-1.1.0.tgz#b9033815f40a77b7f471e478c33382622ccc54de" dependencies: chalk "^2.4.1" commander "^2.17.0" @@ -6207,6 +6207,7 @@ picgo@^1.0.4: qiniu "^7.2.1" request "^2.87.0" request-promise-native "^1.0.5" + resolve "^1.8.1" pify@^2.0.0: version "2.3.0" @@ -7242,6 +7243,12 @@ resolve@^1.1.6, resolve@^1.1.7, resolve@^1.2.0, resolve@^1.3.3, resolve@^1.4.0: dependencies: path-parse "^1.0.5" +resolve@^1.8.1: + version "1.8.1" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.8.1.tgz#82f1ec19a423ac1fbd080b0bab06ba36e84a7a26" + dependencies: + path-parse "^1.0.5" + restore-cursor@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-1.0.1.tgz#34661f46886327fed2991479152252df92daa541"