From 405548426aba2fafdd94b52bcb3a0189d3434a8b Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Thu, 20 Dec 2018 17:23:03 +0800 Subject: [PATCH] Added: clipboard image handler for picgo-core --- src/datastore/index.js | 14 ++++++++++++++ src/main/utils/uploader.js | 3 ++- static/linux.sh | 16 ++++++++++++++++ static/mac.applescript | 38 ++++++++++++++++++++++++++++++++++++++ static/windows.ps1 | 26 ++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 static/linux.sh create mode 100644 static/mac.applescript create mode 100644 static/windows.ps1 diff --git a/src/datastore/index.js b/src/datastore/index.js index 507faa2..cef73aa 100644 --- a/src/datastore/index.js +++ b/src/datastore/index.js @@ -5,6 +5,13 @@ import path from 'path' import fs from 'fs-extra' import { remote, app } from 'electron' +if (process.env.NODE_ENV !== 'development') { + global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\') +} +if (process.env.DEBUG_ENV === 'debug') { + global.__static = path.join(__dirname, '../../static').replace(/\\/g, '\\\\') +} + const APP = process.type === 'renderer' ? remote.app : app const STORE_PATH = APP.getPath('userData') @@ -35,4 +42,11 @@ if (!db.has('settings.shortKey').value()) { }).write() } +// init generate clipboard image files +if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows.ps1'))) { + fs.copySync(path.join(__static, '/linux.sh'), path.join(STORE_PATH, '/linux.sh')) + fs.copySync(path.join(__static, '/mac.applescript'), path.join(STORE_PATH, '/mac.applescript')) + fs.copySync(path.join(__static, '/windows.ps1'), path.join(STORE_PATH, '/windows.ps1')) +} + export default db diff --git a/src/main/utils/uploader.js b/src/main/utils/uploader.js index 42e99a5..5dbd3f7 100644 --- a/src/main/utils/uploader.js +++ b/src/main/utils/uploader.js @@ -84,8 +84,9 @@ class Uploader { upload () { const win = BrowserWindow.fromWebContents(this.webContents) const picgo = new PicGo(CONFIG_PATH) - console.log(picgo.pluginLoader.getList()) picgo.config.debug = true + // for picgo-core + picgo.config.PICGO_ENV = 'GUI' let input = this.img picgo.helper.beforeUploadPlugins.register('renameFn', { diff --git a/static/linux.sh b/static/linux.sh new file mode 100644 index 0000000..15b75ae --- /dev/null +++ b/static/linux.sh @@ -0,0 +1,16 @@ +# from https://github.com/favers/vscode-qiniu-upload-image/blob/master/lib/linux.sh + +#!/bin/sh + +# require xclip(see http://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script/677212#677212) +command -v xclip >/dev/null 2>&1 || { echo >&1 "no xclip"; exit 1; } + +# write image in clipboard to file (see http://unix.stackexchange.com/questions/145131/copy-image-from-clipboard-to-file) +if +xclip -selection clipboard -target image/png -o >/dev/null 2>&1 +then +xclip -selection clipboard -target image/png -o >$1 2>/dev/null +echo $1 +else +echo "no image" +fi \ No newline at end of file diff --git a/static/mac.applescript b/static/mac.applescript new file mode 100644 index 0000000..91f1b82 --- /dev/null +++ b/static/mac.applescript @@ -0,0 +1,38 @@ +-- From https://github.com/mushanshitiancai/vscode-paste-image +property fileTypes : {{«class PNGf», ".png"}} + +on run argv + if argv is {} then + return "" + end if + + set imagePath to (item 1 of argv) + set theType to getType() + + if theType is not missing value then + try + set myFile to (open for access imagePath with write permission) + set eof myFile to 0 + write (the clipboard as (first item of theType)) to myFile + close access myFile + return (POSIX path of imagePath) + on error + try + close access myFile + end try + return "" + end try + else + return "no image" + end if +end run + +on getType() + repeat with aType in fileTypes + repeat with theInfo in (clipboard info) + if (first item of theInfo) is equal to (first item of aType) then return aType + end repeat + end repeat + return missing value +end getType + diff --git a/static/windows.ps1 b/static/windows.ps1 new file mode 100644 index 0000000..f52f909 --- /dev/null +++ b/static/windows.ps1 @@ -0,0 +1,26 @@ + +param($imagePath) + +# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1 + +Add-Type -Assembly PresentationCore +$img = [Windows.Clipboard]::GetImage() + +if ($img -eq $null) { + "no image" + Exit 1 +} + +if (-not $imagePath) { + "no image" + Exit 1 +} + +$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0) +$stream = [IO.File]::Open($imagePath, "OpenOrCreate") +$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder +$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null +$encoder.Save($stream) | out-null +$stream.Dispose() | out-null + +$imagePath \ No newline at end of file