forked from bansicloud/store
-
Notifications
You must be signed in to change notification settings - Fork 2
/
upload.js
52 lines (40 loc) · 1.58 KB
/
upload.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
const { exec } = require('child_process');
const path = require('path');
const { sendMessage } = require('./telegram');
module.exports = (username, blockName, filePath) => new Promise((resolve, reject) => {
let isResolved = false
// NOW WE CAN LAUNCH BASH SCRIPT TO UPLOAD SINGLE FILE
const script = exec(`bash add_file.sh ${username} ${blockName} "${filePath}"`);
const filename = path.parse(filePath).base;
script.stdout.on('data', (data) => {
console.log('sh:', data)
// resolve with a github link
if (data.includes('raw.githubusercontent.com')) {
data = data.replace(/\n/g, '');
// Check if need to remove garbage before link
link = data.substr(data.indexOf('http'));
resolve(link);
sendMessage(`🖼 Uploaded <a href="${link}">${filename}</a> to ${blockName}`, true);
isResolved = true;
};
if (data.includes('ERROR: Upload failed.')) {
// check error type here
// reject({ error: 'unknown error' })
// for now, let's always think the problem is free space
// BUT
// TODO: check if that's a connection error (e.g. when no internet access)
reject({ error: 'no free space' })
sendMessage(`⚠️ Rejected uploading ${filename} to ${blockName}`);
isResolved = true;
}
});
script.stderr.on('data', (data) => {
console.log('sh err:', data)
})
script.on('exit', (code, signal) => {
if (!isResolved) {
reject({ code, signal, error: 'unknown error' })
sendMessage(`🛑 Rejected uploading ${filename} to ${blockName} with unknown error`);
}
})
});