forked from TarsCloud/TarsWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
execUpload.js
119 lines (84 loc) · 3.68 KB
/
execUpload.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
const PatchService = require('./app/service/patch/PatchService');
const logger = require("./logger");
const _ = require('lodash');
const util = require('./tools/util');
const WebConf = require('./config/webConf');
const fs = require('fs-extra');
const path = require('path');
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
let ExecUpload = async (request) => {
console.log('ExecUpload');
let connection = null;
try {
let uploadFile = -1;
connection = request.accept('upload-protocol', request.origin);
logger.info('Connection accepted');
connection.on('close', function (reasonCode, description) {
logger.info(`peer ${connection.remoteAddress} disconnected, ${reasonCode}, ${description}`);
});
let params = request.resourceURL.query;
let baseUploadPath = WebConf.pkgUploadPath.path;
let filename = util.getUUID().toString();
logger.info('upload file: ', baseUploadPath);
fs.mkdirSync(baseUploadPath, {
recursive: true
});
let updateTgzFile = `${baseUploadPath}/${filename}`;
let total = 0;
connection.on('message', async function (message) {
// logger.info("web->nodejs:", message.type);
if (message.type == "utf8") {
logger.info("start web->nodejs type:", message.type, message.utf8Data);
if (message.utf8Data == "start") {
console.log("start openSync:", updateTgzFile);
uploadFile = fs.openSync(updateTgzFile, 'w');
} else if (message.utf8Data == "end") {
logger.info(`end web->nodejs, type: ${message.type}, total: ${total}`);
if (uploadFile >= 0) {
fs.closeSync(uploadFile);
uploadFile = -1;
}
logger.info(`upload file size:`, fs.statSync(updateTgzFile).size);
if (fs.statSync(updateTgzFile).size != total) {
throw Error('upload file size not correct.');
}
let task_no = util.getUUID().toString();
try {
let rst = await PatchService.uploadAndPatch(params.app, params.server, '', task_no, "install from cloud", filename, 'cloud', params.uid || '', (total, length) => {
// console.log(total, length);
let data = {
code: 201,
data: {
total: total,
length: length
}
}
connection.send(JSON.stringify(data));
});
connection.send(JSON.stringify(rst));
} catch (e) {
logger.error('call PatchService.uploadAndPatch error:', e.message);
connection.send(JSON.stringify({
code: 500,
msg: e.message
}));
}
}
} else {
fs.writeSync(uploadFile, message.binaryData, 0, message.binaryData.length, total);
total += message.binaryData.length;
}
});
} catch (e) {
logger.error(e);
if (connection) {
connection.close();
connection = null;
}
}
}
module.exports = ExecUpload;