diff --git a/.vscodeignore b/.vscodeignore index c397a51..1440c57 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -2,5 +2,4 @@ typings/** test/** .gitignore -jsconfig.json -*.vsix \ No newline at end of file +*.vsix diff --git a/README.md b/README.md index 89e5ea6..64e8582 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,19 @@ # qiniu-upload-image -一个 VS Code 插件,写 Markdown 时可以快捷上传本地图片获取七牛图床外链。 +一个 VS Code 插件,编写 Markdown 时可以快捷上传本地图片获取七牛图床外链。 ## Features -![priview](https://raw.githubusercontent.com/yscoder/vscode-qiniu-upload-image/master/features/preview.gif) +![priview](https://raw.githubusercontent.com/yscoder/vscode-qiniu-upload-image/master/features/demo.gif) -> Tips: 只有在编辑 Markdown 时插件才可使用,启动快捷键 `Ctrl+Q`。 +> Tips: 只有在编辑 Markdown 时插件才可使用。 + +## Usage + +1. 粘贴图片路径上传:`SHIFT + P` +2. 直接选择图片上传:`SHIFT + O` + +> 按键需在英文编辑状态下有效,功能2 需要升级 vscode 到 v1.17+。 ## Install diff --git a/extension.js b/extension.js index c1768ba..a56d0ba 100644 --- a/extension.js +++ b/extension.js @@ -2,6 +2,28 @@ const { window, commands, workspace } = require('vscode') const path = require('path') const qnUpload = require('./lib/upload') +const upload = (config, fsPath) => { + if(!fsPath) return + + const editor = window.activeTextEditor + const mdFilePath = editor.document.fileName + const mdFileName = path.win32.basename(mdFilePath, path.extname(mdFilePath)) + + return qnUpload(config, fsPath, mdFileName).then(({ name, url }) => { + console.log('Upload success!') + + const img = `![${name}](${url})` + + editor.edit(textEditorEdit => { + textEditorEdit.insert(editor.selection.active, img) + }) + }) +} + +const error = err => { + window.showErrorMessage(err) +} + // this method is called when your extension is activated exports.activate = context => { @@ -11,36 +33,32 @@ exports.activate = context => { if (!config.enable) return - const disposable = commands.registerCommand('extension.qiniu.upload', () => { + const inputUpload = commands.registerCommand('extension.qiniu.upload', () => { - const editor = window.activeTextEditor - const mdFilePath = editor.document.fileName - const mdFileName = path.basename(mdFilePath, path.extname(mdFilePath)) - - if (!editor) { + if (!window.activeTextEditor) { window.showErrorMessage('没有打开编辑窗口') return } window.showInputBox({ - placeHolder: '输入一个本地图片地址' - }).then(path => qnUpload(config, path, mdFileName) - , err => { - window.showErrorMessage(err) + placeHolder: '输入一个图片地址' + }) + .then(fsPath => upload(config, fsPath), error) + }) + + const selectUpload = commands.registerCommand('extension.qiniu.select', () => { + window.showOpenDialog({ + filters: { 'Images': ['png', 'jpg', 'gif', 'bmp'] } + }).then(result => { + if (result) { + const { fsPath } = result[0] + return upload(config, fsPath) } - ).then(({ name, url }) => { - console.log('Upload success!') - - const img = `![${name}](${url})` - editor.edit(textEditorEdit => { - textEditorEdit.insert(editor.selection.active, img) - }) - }, err => { - window.showErrorMessage(err) - }) + }, error) }) - context.subscriptions.push(disposable) + context.subscriptions.push(inputUpload) + context.subscriptions.push(selectUpload) } // this method is called when your extension is deactivated diff --git a/features/demo.gif b/features/demo.gif new file mode 100644 index 0000000..67a682e Binary files /dev/null and b/features/demo.gif differ diff --git a/features/preview.gif b/features/preview.gif deleted file mode 100644 index 07b8de1..0000000 Binary files a/features/preview.gif and /dev/null differ diff --git a/lib/upload.js b/lib/upload.js index ee00a4a..f5d012c 100644 --- a/lib/upload.js +++ b/lib/upload.js @@ -1,6 +1,7 @@ const qiniu = require('qiniu') const path = require('path') const url = require('url') +const request = require('request') const PutPolicy = qiniu.rs.PutPolicy const PutExtra = qiniu.io.PutExtra @@ -24,7 +25,7 @@ const formatParam = (file, mdFileName) => { return { date, dateTime: `${date}${h}${mm}${s}`, - fileName: path.basename(file, ext), + fileName: path.win32.basename(file, ext), ext, mdFileName } @@ -59,24 +60,54 @@ module.exports = ({ //生成上传 Token const token = uptoken(bucket, saveFile) - return new Promise((resolve, reject) => { - const extra = new PutExtra() - - qiniu.io.putFile(token, saveFile, localFile, extra, (err, { key }) => { - - if (!err) { - // 上传成功, 处理返回值 - resolve({ - name: path.basename(key, param.ext), - url: url.resolve(domain, saveFile) - }) - } else { - // 上传失败, 处理返回代码 - reject(err) - } + if(localFile.indexOf('http')==0||localFile.indexOf('https')==0){ + return new Promise((resolve, reject) => { + const extra = new PutExtra() + request({ + headers: { + 'Referer': localFile + }, + uri: localFile, + encoding: null, + method: 'GET' + }, + function (err, res, body) { + if(!err){ + qiniu.io.put(token, saveFile, body, extra, (err, { key }) => { + if (!err) { + resolve({ + name: path.win32.basename(key, param.ext), + url: url.resolve(domain, saveFile) + }) + } else { + reject(err) + } + }) + } + } + ) }) - }) + }else{ + return new Promise((resolve, reject) => { + + const extra = new PutExtra() + + qiniu.io.putFile(token, saveFile, localFile, extra, (err, { key }) => { + + if (!err) { + // 上传成功, 处理返回值 + resolve({ + name: path.win32.basename(key, param.ext), + url: url.resolve(domain, saveFile) + }) + } else { + // 上传失败, 处理返回代码 + reject(err) + } + }) + }) + } } diff --git a/package.json b/package.json index 5b3ea26..57f6883 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,10 @@ "name": "qiniu-upload-image", "displayName": "qiniu-upload-image", "description": "Picture upload generate markdown link format.", - "version": "0.1.0", + "version": "1.1.1", "publisher": "imys", "engines": { - "vscode": "^1.0.0" + "vscode": "^1.17.0" }, "categories": [ "Other" @@ -18,8 +18,14 @@ "keybindings": [ { "command": "extension.qiniu.upload", - "key": "ctrl+q", - "mac": "ctrl+q", + "key": "shift+p", + "mac": "shift+p", + "when": "editorTextFocus && editorLangId == 'markdown'" + }, + { + "command": "extension.qiniu.select", + "key": "shift+o", + "mac": "shift+o", "when": "editorTextFocus && editorLangId == 'markdown'" } ], diff --git a/qiniu-upload-image-0.1.0.vsix b/qiniu-upload-image-0.1.0.vsix deleted file mode 100644 index 09cd71d..0000000 Binary files a/qiniu-upload-image-0.1.0.vsix and /dev/null differ diff --git a/qiniu-upload-image-1.1.0.vsix b/qiniu-upload-image-1.1.0.vsix new file mode 100644 index 0000000..ce3208b Binary files /dev/null and b/qiniu-upload-image-1.1.0.vsix differ