forked from huichi2028/vscode-qiniu-upload-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
179 lines (157 loc) · 5.88 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const vscode = require('vscode');
const path = require('path');
const moment = require('moment');
const fs = require('fs');
const { spawn } = require('child_process');
const qnUpload = require('./lib/upload');
exports.activate = (context) => {
const disposable = vscode.commands.registerCommand('extension.pasteImageToQiniu', () => {
start();
});
context.subscriptions.push(disposable);
}
// this method is called when your extension is deactivated
exports.deactivate = () => { }
function start() {
// 获取当前编辑文件
let editor = vscode.window.activeTextEditor;
if (!editor) return;
let fileUri = editor.document.uri;
if (!fileUri) return;
if (fileUri.scheme === 'untitled') {
vscode.window.showInformationMessage('Before paste image, you need to save current edit file first.');
return;
}
let selection = editor.selection;
let selectText = editor.document.getText(selection);
selectText = selectText.replace(new RegExp(' ', 'g'), '-');
if (selectText && !/^[\w\-_.]+$/.test(selectText)) {
vscode.window.showInformationMessage('Your selection is not a valid file name!');
return;
}
let config = vscode.workspace.getConfiguration('pasteImageToQiniu');
let localPath = config['localPath'];
if (localPath && (localPath.length !== localPath.trim().length)) {
vscode.window.showErrorMessage('The specified path is invalid. "' + localPath + '"');
return;
}
let filePath = fileUri.fsPath;
let imagelocalPath = getImagePath(filePath, selectText, localPath);
let mdFilePath = editor.document.fileName;
if (vscode.workspace.workspaceFolders.length > 0) {
mdFilePath = mdFilePath.slice(vscode.workspace.workspaceFolders[0].uri.path.length + 1, mdFilePath.length - path.extname(mdFilePath).length);
mdFilePath = mdFilePath.replace(new RegExp('\\\\', 'g'), '/');
}
createImageDirWithImagePath(imagelocalPath).then(imagePath => {
saveClipboardImageToFileAndGetPath(imagePath, (imagePath) => {
if (!imagePath) return;
if (imagePath === 'no image') {
vscode.window.showInformationMessage('There is not a image in clipboard.');
return;
}
qnUpload(config, imagePath, mdFilePath).then(({ name, url }) => {
vscode.window.showInformationMessage('上传成功');
const img = `![${name}](${url})`;
editor.edit(textEditorEdit => {
textEditorEdit.delete(editor.selection);
textEditorEdit.insert(editor.selection.active, img)
});
// 是否删除本地文件
if (config.delLocalPath) {
fs.unlink(imagePath, function (err) {
if (err) {
vscode.window.showErrorMessage('Delete temp file "' + imagePath + '" error: ' + err.toString());
}
})
}
}).catch((err) => {
vscode.window.showErrorMessage('Upload error.' + err.message);
});
});
}).catch(err => {
vscode.window.showErrorMessage('Failed make folder.');
return;
});
}
function getImagePath(filePath, selectText, localPath) {
// 图片名称
let imageFileName = '';
if (!selectText) {
imageFileName = moment().format("YMMDDHHmmss") + '.png';
} else {
imageFileName = selectText + '.png';
}
// 图片本地保存路径
let folderPath = path.dirname(filePath);
let imagePath = '';
if (path.isAbsolute(localPath)) {
imagePath = path.join(localPath, imageFileName);
} else {
imagePath = path.join(folderPath, localPath, imageFileName);
}
return imagePath;
}
function createImageDirWithImagePath(imagePath) {
return new Promise((resolve, reject) => {
let imageDir = path.dirname(imagePath);
fs.exists(imageDir, (exists) => {
if (exists) {
resolve(imagePath);
return;
}
fs.mkdir(imageDir, (err) => {
if (err) {
reject(err);
return;
}
resolve(imagePath);
});
});
});
}
function saveClipboardImageToFileAndGetPath(imagePath, cb) {
if (!imagePath) return;
let platform = process.platform;
if (platform === 'win32') {
// Windows
const scriptPath = path.join(__dirname, './lib/pc.ps1');
const powershell = spawn('powershell', [
'-noprofile',
'-noninteractive',
'-nologo',
'-sta',
'-executionpolicy', 'unrestricted',
'-windowstyle', 'hidden',
'-file', scriptPath,
imagePath
]);
powershell.on('exit', function (code, signal) {
});
powershell.stdout.on('data', function (data) {
cb(data.toString().trim());
});
} else if (platform === 'darwin') {
// Mac
let scriptPath = path.join(__dirname, './lib/mac.applescript');
let ascript = spawn('osascript', [scriptPath, imagePath]);
ascript.on('exit', function (code, signal) {
});
ascript.stdout.on('data', function (data) {
cb(data.toString().trim());
});
} else {
// Linux
let scriptPath = path.join(__dirname, './lib/linux.sh');
let ascript = spawn('sh', [scriptPath, imagePath]);
ascript.on('exit', function (code, signal) {
});
ascript.stdout.on('data', function (data) {
let result = data.toString().trim();
if (result == "no xclip") {
vscode.window.showInformationMessage('You need to install xclip command first.');
return;
}
cb(result);
});
}
}