picosh 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +42 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,17 +3,43 @@
|
|
|
3
3
|
const path = require('path');
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const os = require('os');
|
|
6
|
+
const https = require('https');
|
|
7
|
+
const http = require('http');
|
|
6
8
|
|
|
7
9
|
const SAVE_DIR = path.join(os.tmpdir(), 'picosh');
|
|
8
10
|
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
function hasImage(clipboard) {
|
|
12
|
+
if (!clipboard.readImage().isEmpty()) return {type: 'bitmap'};
|
|
13
|
+
const html = clipboard.readHTML();
|
|
14
|
+
if (html) {
|
|
15
|
+
const match = html.match(/<img[^>]+src=["']([^"']+)["']/i);
|
|
16
|
+
if (match) return {type: 'url', src: match[1]};
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
12
20
|
|
|
21
|
+
function saveBitmap(clipboard) {
|
|
13
22
|
fs.mkdirSync(SAVE_DIR, {recursive: true});
|
|
14
23
|
const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
|
|
15
|
-
fs.writeFileSync(filepath,
|
|
16
|
-
return filepath;
|
|
24
|
+
fs.writeFileSync(filepath, clipboard.readImage().toPNG());
|
|
25
|
+
return Promise.resolve(filepath);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function downloadImage(url) {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
fs.mkdirSync(SAVE_DIR, {recursive: true});
|
|
31
|
+
const filepath = path.join(SAVE_DIR, `clip_${Date.now()}.png`);
|
|
32
|
+
const file = fs.createWriteStream(filepath);
|
|
33
|
+
const client = url.startsWith('https') ? https : http;
|
|
34
|
+
|
|
35
|
+
client.get(url, (res) => {
|
|
36
|
+
res.pipe(file);
|
|
37
|
+
file.on('finish', () => file.close(() => resolve(filepath)));
|
|
38
|
+
}).on('error', () => {
|
|
39
|
+
fs.unlink(filepath, () => {});
|
|
40
|
+
resolve(null);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
17
43
|
}
|
|
18
44
|
|
|
19
45
|
exports.decorateTerm = (Term, {React}) => {
|
|
@@ -24,15 +50,21 @@ exports.decorateTerm = (Term, {React}) => {
|
|
|
24
50
|
|
|
25
51
|
try {
|
|
26
52
|
const {clipboard} = require('electron');
|
|
27
|
-
const
|
|
28
|
-
if (!
|
|
53
|
+
const found = hasImage(clipboard);
|
|
54
|
+
if (!found) return;
|
|
29
55
|
|
|
30
56
|
e.preventDefault();
|
|
31
57
|
e.stopPropagation();
|
|
32
58
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
59
|
+
const save = found.type === 'bitmap'
|
|
60
|
+
? saveBitmap(clipboard)
|
|
61
|
+
: downloadImage(found.src);
|
|
62
|
+
|
|
63
|
+
save.then((filepath) => {
|
|
64
|
+
if (filepath && this.props.onData) {
|
|
65
|
+
this.props.onData(filepath);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
36
68
|
} catch (_) {}
|
|
37
69
|
};
|
|
38
70
|
|