Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
complete basic upload local image
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean10 committed Oct 15, 2018
0 parents commit 6560f01
Show file tree
Hide file tree
Showing 16 changed files with 3,381 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"env": {
"browser": false,
"commonjs": true,
"es6": true,
"node": true
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"rules": {
"no-const-assign": "warn",
"no-this-before-super": "warn",
"no-undef": "warn",
"no-unreachable": "warn",
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn"
}
}
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Set default behavior to automatically normalize line endings.
* text=auto

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.vscode-test/
*.vsix
.DS_Store
7 changes: 7 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"dbaeumer.vscode-eslint"
]
}
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// A launch configuration that launches the extension inside a new window
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
]
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/test"
]
}
]
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place your settings in this file to overwrite default and user settings.
{
}
7 changes: 7 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.vscode/**
.vscode-test/**
test/**
.gitignore
jsconfig.json
vsc-extension-quickstart.md
.eslintrc.json
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log
All notable changes to the "vscode-upload-tencentcos" extension will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [Unreleased]
- Initial release
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# vscode-upload-tencentcos

vscode extension to upload image into tencent COS for markdown


# Reference
1. [vscode-qiniu-upload-image](https://github.com/yscoder/vscode-qiniu-upload-image.git)
76 changes: 76 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const path = require('path')
const COSUpload = require('./lib/upload')

const upload = (config, fsPath) => {
if (!fsPath) return
console.log(fsPath)

const editor = vscode.window.activeTextEditor
const mdFilePath = editor.document.fileName
const mdFileName = path.win32.basename(mdFilePath, path.extname(mdFilePath))

return COSUpload(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 => vscode.window.showErrorMessage(err)

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {

// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('vscode-upload-tencentcos is now active!');

const config = vscode.workspace.getConfiguration('tencentCOS')
console.log(config)
// if (!config.enable) return

// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let inputUpload = vscode.commands.registerCommand('extension.tencentCOS.upload', () => {

if (!vscode.window.activeTextEditor) {
vscode.window.showErrorMessage("没有打开编辑窗口")
return
}

vscode.window.showInputBox({
placeHolder: '请输入一个图片地址'
})
.then(fsPath => upload(config, fsPath), error)
})

let selectUpload = vscode.commands.registerCommand('extension.tencentCOS.select', () => {
vscode.window.showOpenDialog({
filters: {'Images': ['png', 'jpg', 'gif', 'bmp']}
}).then(result => {
console.log(result)
if (result) {
const {fsPath} = result[0]
return upload(config, fsPath)
}
}, error)
})

context.subscriptions.push(inputUpload);
context.subscriptions.push(selectUpload);
}
exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;
13 changes: 13 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"checkJs": true, /* Typecheck .js files. */
"lib": [
"es6"
]
},
"exclude": [
"node_modules"
]
}
53 changes: 53 additions & 0 deletions lib/upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const COS = require('cos-nodejs-sdk-v5')
const path = require('path')
const url = require('url')
const request = require('request')
const fs = require('fs')

const formatParam = file => {
return {
dirname: path.dirname(file),
filename: path.basename(file),
ext: path.extname(file)
}
}

module.exports = ({
secret_id,
secret_key,
bucket,
region
}, file, mdfile) => {
var cos = new COS({
SecretId: secret_id,
SecretKey: secret_key
});
let domain = url.format({
protocol: "https:",
host: bucket+".cos."+region+".myqcloud.com"
})

const param = formatParam(file)
console.log(domain)
console.log(cos)
// let filename = "avatar_001.jpg"
// console.log(filename)
cos.putObject({
Bucket: bucket,
Region: region,
Key: file,
Body: fs.readFileSync(path.resolve(__dirname, file))

}, function (err, data) {
console.log(err || data);

})

// console.log("succeed")
return new Promise((resolve, reject) => {
resolve({
name: param.filename,
url: url.resolve(domain, param.filename)
})
})
}
Loading

0 comments on commit 6560f01

Please sign in to comment.