forked from iann0036/cloud9-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileManager.js
330 lines (330 loc) · 15.5 KB
/
fileManager.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var vscode = require("vscode");
var Utils = require("./utils");
var request = require("request");
var path = require("path");
var fs = require("fs");
var SYNC_TIME_VARIANCE = 20000;
var FileManager = /** @class */ (function () {
function FileManager(eventEmitter) {
this.eventEmitter = eventEmitter;
this.awsregion = Utils.GetRegion();
}
FileManager.prototype.getFileWorkspacePath = function () {
if (vscode.workspace.workspaceFolders) {
var folders = vscode.workspace.workspaceFolders;
var workspaceFolder = void 0;
while (workspaceFolder = folders.pop()) {
if (workspaceFolder.uri.scheme == "file") {
return workspaceFolder.uri.fsPath;
}
}
;
}
return null;
};
FileManager.prototype.recursiveDownload = function (startPath) {
var _this = this;
console.log("Performing lookup from " + startPath);
return new Promise(function (resolve, reject) {
request.get({
url: 'https://vfs.cloud9.' + _this.awsregion + '.amazonaws.com/vfs/' + _this.environmentId + '/environment' + startPath + "/",
jar: _this.cookieJar,
headers: {
'Content-Type': 'text/plain',
'Origin': 'https://' + _this.awsregion + '.console.aws.amazon.com',
'Referer': 'https://' + _this.awsregion + '.console.aws.amazon.com/cloud9/ide/' + _this.environmentId,
'x-authorization': _this.xauth
},
proxy: Utils.GetProxy()
}, function (err, httpResponse, body) {
Utils.ReducePromises(JSON.parse(body), function (inode) {
if (startPath + "/" + inode['name'] == "/.c9" || startPath + "/" + inode['name'] == "/.vscode") {
return Promise.resolve();
}
var workspacePath = _this.getFileWorkspacePath();
if (workspacePath == null) {
vscode.window.showWarningMessage("Could not find local directory to sync to");
return Promise.reject();
}
var inodePath = path.join(workspacePath, startPath + "/" + inode['name']);
if (inode['mime'] == "inode/directory") {
if (!fs.existsSync(inodePath)) {
console.log("Creating the directory " + inodePath);
fs.mkdirSync(inodePath);
}
return _this.recursiveDownload(startPath + "/" + inode['name']);
}
else {
if (fs.existsSync(inodePath)) {
var ftime = inode['mtime'];
var fstat = fs.statSync(inodePath);
var lftime = new Date(fstat['mtime']).getTime();
console.log(lftime);
console.log(ftime);
if (lftime + SYNC_TIME_VARIANCE > ftime) {
console.log(inodePath + " is up to date, skipping...");
return Promise.resolve();
}
}
return _this.downloadFile(startPath + "/" + inode['name'], inodePath, inode);
}
})
.then(resolve, reject)
.catch(function (err) {
console.error(err);
});
});
});
};
FileManager.prototype.stat = function (filename) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.eventEmitter.emit('send_ch4_message', ["stat", "/" + filename, {}, { $: 91 }]);
_this.eventEmitter.on('ch4_data', function (data, environmentId) {
if (Array.isArray(data)) {
if (data.length > 2) {
if (data[0] == 91) {
var contents = data[2];
resolve(contents);
}
}
}
});
});
};
FileManager.prototype.listdir = function (filename) {
var _this = this;
return new Promise(function (resolve, reject) {
if (!filename.endsWith("/")) {
filename += "/";
}
request.get({
url: 'https://vfs.cloud9.' + _this.awsregion + '.amazonaws.com/vfs/' + _this.environmentId + '/environment/' + filename,
jar: _this.cookieJar,
headers: {
'Content-Type': 'application/json',
'Origin': 'https://' + _this.awsregion + '.console.aws.amazon.com',
'Referer': 'https://' + _this.awsregion + '.console.aws.amazon.com/cloud9/ide/' + _this.environmentId,
'x-authorization': _this.xauth
},
proxy: Utils.GetProxy()
}, function (err, httpResponse, body) {
console.warn("LISTDIR RESPONSE");
console.log(httpResponse);
console.log(body);
try {
var parsed_body = JSON.parse(body);
resolve(parsed_body);
}
catch (err) {
reject(body);
}
});
});
};
FileManager.prototype.downloadFile = function (filename, inodePath, inode) {
var _this = this;
return new Promise(function (resolve, reject) {
request.get({
url: 'https://vfs.cloud9.' + _this.awsregion + '.amazonaws.com/vfs/' + _this.environmentId + '/environment/' + filename,
jar: _this.cookieJar,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Origin': 'https://' + _this.awsregion + '.console.aws.amazon.com',
'Referer': 'https://' + _this.awsregion + '.console.aws.amazon.com/cloud9/ide/' + _this.environmentId,
'x-authorization': _this.xauth
},
proxy: Utils.GetProxy()
}, function (err, httpResponse, body) {
if (inodePath != null) {
console.log("Downloading the file " + inodePath);
fs.writeFileSync(inodePath, body);
//fs.utimes(inodePath, parseInt(inode.mtime/1000), parseInt(inode.mtime/1000), resolve); TODO: Fix
}
resolve(body); // REMOVE ME
});
});
};
FileManager.prototype.uploadExistingFile = function (filename, content) {
var _this = this;
return new Promise(function (resolve, reject) {
request.post({
url: 'https://vfs.cloud9.' + _this.awsregion + '.amazonaws.com/vfs/' + _this.environmentId + '/environment/' + filename,
jar: _this.cookieJar,
headers: {
'Content-Type': 'text/plain',
'Origin': 'https://' + _this.awsregion + '.console.aws.amazon.com',
'Referer': 'https://' + _this.awsregion + '.console.aws.amazon.com/cloud9/ide/' + _this.environmentId,
'x-authorization': _this.xauth
},
body: content,
proxy: Utils.GetProxy()
}, function (err, httpResponse, body) {
console.log(httpResponse);
console.log(body);
resolve();
});
});
};
FileManager.prototype.uploadRemoteFile = function (filename, content) {
var _this = this;
return new Promise(function (resolve, reject) {
request.put({
url: 'https://vfs.cloud9.' + _this.awsregion + '.amazonaws.com/vfs/' + _this.environmentId + '/environment/' + filename,
jar: _this.cookieJar,
headers: {
'Content-Type': 'text/plain',
'Origin': 'https://' + _this.awsregion + '.console.aws.amazon.com',
'Referer': 'https://' + _this.awsregion + '.console.aws.amazon.com/cloud9/ide/' + _this.environmentId,
'x-authorization': _this.xauth
},
body: content,
proxy: Utils.GetProxy()
}, function (err, httpResponse, body) {
var _this = this;
console.log(httpResponse);
console.log(body);
if (httpResponse.statusCode == 429) { // retry with backoff
console.warn("retrying with backoff");
var response = JSON.parse(httpResponse);
setTimeout(function () {
_this.uploadRemoteFile(filename, content).then(function () {
resolve();
}).catch(function (err) {
reject(err);
});
}, response.error.retryIn);
}
else {
resolve();
}
});
});
};
FileManager.prototype.deleteRemoteFile = function (filename) {
var _this = this;
return new Promise(function (resolve, reject) {
request.delete({
url: 'https://vfs.cloud9.' + _this.awsregion + '.amazonaws.com/vfs/' + _this.environmentId + '/environment/' + filename,
jar: _this.cookieJar,
headers: {
'Content-Type': 'text/plain',
'Origin': 'https://' + _this.awsregion + '.console.aws.amazon.com',
'Referer': 'https://' + _this.awsregion + '.console.aws.amazon.com/cloud9/ide/' + _this.environmentId,
'x-authorization': _this.xauth
},
proxy: Utils.GetProxy()
}, function (err, httpResponse, body) {
console.log(err);
console.log(httpResponse);
console.log(body);
resolve();
});
});
};
FileManager.prototype.recursiveUpload = function (startPath) {
var _this = this;
console.log("Performing lookup from " + startPath);
return new Promise(function (resolve, reject) {
var workspacePath = _this.getFileWorkspacePath();
if (workspacePath == null) {
vscode.window.showWarningMessage("Could not find local directory to sync to");
reject("no local directory");
return;
}
var inodePath = path.join(workspacePath, startPath + "/");
request.get({
url: 'https://vfs.cloud9.' + _this.awsregion + '.amazonaws.com/vfs/' + _this.environmentId + '/environment' + Utils.EnsureLeadingSlash(startPath) + "/",
jar: _this.cookieJar,
headers: {
'Content-Type': 'text/plain',
'Origin': 'https://' + _this.awsregion + '.console.aws.amazon.com',
'Referer': 'https://' + _this.awsregion + '.console.aws.amazon.com/cloud9/ide/' + _this.environmentId,
'x-authorization': _this.xauth
},
proxy: Utils.GetProxy()
}, function (err, httpResponse, body) {
console.log("REMOTE STAT FOR UPLOAD");
console.log(err);
console.log(body);
var remoteStats = [];
try {
remoteStats = JSON.parse(body);
if ('error' in remoteStats) {
reject();
}
}
catch (err) {
console.log("Could not get remote stats");
console.log(body);
resolve();
}
console.log("About to do readdir on" + inodePath);
fs.readdir(inodePath, function (err, files) {
if (err)
reject(err);
console.log("Reading - " + inodePath);
console.log(files);
if (!files) {
console.log("No files, skipping...");
resolve();
}
Utils.ReducePromises(files, function (file) {
var relativePath = path.join(startPath + "/", file);
console.log("Processing file: " + relativePath);
if (relativePath == "/.c9" || relativePath == "/.vscode" || relativePath == "/.git") {
return Promise.resolve();
}
return new Promise(function (resolve, reject) {
var filepath = path.join(inodePath, file);
console.log("Beginning upload processing of " + filepath + " (" + relativePath + ")");
fs.stat(filepath, function (err, fstat) {
if (fstat.isDirectory()) {
_this.eventEmitter.emit('send_ch4_message', ["mkdir", relativePath, {}, { "$": 33 }]);
_this.recursiveUpload(relativePath).then(function () {
resolve();
}).catch(function (err) {
console.error(err);
});
}
else if (fstat.isFile()) {
var ftime_1 = null;
var lftime = new Date(fstat['mtime']).getTime();
console.log("Checking remote stats now");
remoteStats.forEach(function (remoteStat) {
if (remoteStat.name == relativePath) {
ftime_1 = remoteStat.mtime;
}
});
console.log("ftime: " + ftime_1);
if (ftime_1 == null) {
console.log("Uploading new file");
_this.uploadRemoteFile(relativePath, fs.readFileSync(filepath));
}
else if (lftime > ftime_1 + SYNC_TIME_VARIANCE) {
console.log("Updating existing file");
_this.uploadExistingFile(relativePath, fs.readFileSync(filepath));
}
resolve();
}
else {
console.log("Unknown file type");
resolve();
}
});
});
})
.then(resolve, reject)
.catch(function (err) {
console.error(err);
});
});
});
});
};
return FileManager;
}());
exports.FileManager = FileManager;