-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfileSystemProvider.ts
242 lines (203 loc) · 8.14 KB
/
fileSystemProvider.ts
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
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import { workspace } from 'vscode';
import * as FileManager from './fileManager';
import { resolve } from 'dns';
import { TextEncoder } from 'text-encoding';
export class File implements vscode.FileStat {
type: vscode.FileType;
ctime: number;
mtime: number;
size: number;
name: string;
data: Uint8Array;
constructor(name: string) {
this.type = vscode.FileType.File;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
}
}
export class Directory implements vscode.FileStat {
type: vscode.FileType;
ctime: number;
mtime: number;
size: number;
name: string;
entries: Map<string, File | Directory>;
constructor(name: string) {
this.type = vscode.FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
this.entries = new Map();
}
}
export type Entry = File | Directory;
export class Cloud9FileSystemProvider implements vscode.FileSystemProvider {
root = new Directory('');
environmentConnections = {}
constructor(
private fileManager: FileManager.FileManager,
private eventEmitter,
private websocketProvider
) {
//this.createDirectory(vscode.Uri.parse(`cloud9:/123/`));
}
// --- manage file metadata
private _getEnvConnection(id): Thenable<string> {
return new Promise((resolve, reject) => {
if (id in this.environmentConnections) {
if (this.environmentConnections[id].status == "connected") {
resolve(id);
return;
}
} else {
this.environmentConnections[id] = {
'status': 'connecting'
};
this.eventEmitter.emit("request_connect", {
id: id
});
}
this.eventEmitter.once('websocket_init_complete', () => {
this.environmentConnections[id] = {
'status': 'connected'
};
resolve(id);
});
});
}
private _c9stattovsstat(stat): Entry {
let entry: Entry;
if (stat['mime'] == "inode/directory") {
entry = new Directory(stat.name);
} else {
entry = new File(stat.name);
}
entry.size = stat.size;
entry.ctime = stat.ctime;
entry.mtime = stat.mtime;
return entry;
}
stat(uri: vscode.Uri): Thenable<vscode.FileStat> {
return new Promise((resolve, reject) => {
let splituri = uri.path.split("/");
let environmentId = splituri[1];
this._getEnvConnection(environmentId).then(() => {
this.fileManager.stat(splituri.slice(2).join('/')).then(stats => {
resolve(this._c9stattovsstat(stats));
}).catch(err => {
reject(err);
});
});
});
//reject(vscode.FileSystemError.FileNotFound());
}
readDirectory(uri: vscode.Uri): Thenable<[string, vscode.FileType][]> {
return new Promise((resolve, reject) => {
let splituri = uri.path.split("/");
let environmentId = splituri[1];
this._getEnvConnection(environmentId).then(() => {
this.fileManager.listdir(splituri.slice(2).join('/')).then(stats => {
let converted_stats = [];
stats.forEach(stat => {
let converted_stat = [stat['name'], vscode.FileType.File];
if (stat['mime'] == "inode/directory") {
converted_stat[1] = vscode.FileType.Directory;
}
converted_stats.push(converted_stat);
});
resolve(converted_stats);
});
});
});
}
// --- manage file contents
readFile(uri: vscode.Uri): Thenable<Uint8Array> {
return new Promise((resolve, reject) => {
let splituri = uri.path.split("/");
let environmentId = splituri[1];
this._getEnvConnection(environmentId).then(() => {
this.fileManager.downloadFile("/" + splituri.slice(2).join('/'), null, null).then(body => {
let uint8 = new TextEncoder().encode(body);
resolve(uint8);
});
});
});
}
writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean, overwrite: boolean }): Thenable<void> {
return new Promise((resolve, reject) => {
let splituri = uri.path.split("/");
let environmentId = splituri[1];
this._getEnvConnection(environmentId).then(() => {
if (options.create) {
this.fileManager.uploadRemoteFile("/" + splituri.slice(2).join('/'), content.toString()).then(() => {
resolve();
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
return;
});
} else if (options.overwrite) {
this.fileManager.uploadExistingFile("/" + splituri.slice(2).join('/'), content.toString()).then(() => {
resolve();
this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
return;
});
}
});
});
}
// --- manage files/folders
rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean }): void { // TODO: Check return
let oldsplituri = oldUri.path.split("/");
let newsplituri = newUri.path.split("/");
this.eventEmitter.emit("send_ch4_message", ["rename","/" + newsplituri.slice(2).join('/'),{"from":"/" + oldsplituri.slice(2).join('/')},{"$": this.websocketProvider.next_event_id()}]);
this._fireSoon(
{ type: vscode.FileChangeType.Deleted, uri: oldUri },
{ type: vscode.FileChangeType.Created, uri: newUri }
);
}
delete(uri: vscode.Uri): Thenable<void> {
return new Promise((resolve, reject) => {
let splituri = uri.path.split("/");
let environmentId = splituri[1];
this._getEnvConnection(environmentId).then(() => {
this.fileManager.deleteRemoteFile("/" + splituri.slice(2).join('/')).then(() => {
resolve();
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: uri }, { uri, type: vscode.FileChangeType.Deleted });
return;
});
});
});
}
createDirectory(uri: vscode.Uri): Thenable<void> {
return new Promise((resolve, reject) => {
let splituri = uri.path.split("/");
this.eventEmitter.emit("send_ch4_message", ["mkdir","/" + splituri.slice(2).join('/'),{},{"$": this.websocketProvider.next_event_id()}]);
setTimeout(() => { // TODO: Fix dodgy timeout
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: uri }, { type: vscode.FileChangeType.Created, uri });
resolve();
}, 200);
});
}
// --- manage file events
private _emitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
private _bufferedEvents: vscode.FileChangeEvent[] = [];
private _fireSoonHandle: NodeJS.Timer;
readonly onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> = this._emitter.event;
watch(resource: vscode.Uri, opts): vscode.Disposable {
// ignore, fires for all changes...
return new vscode.Disposable(() => { });
}
private _fireSoon(...events: vscode.FileChangeEvent[]): void {
this._bufferedEvents.push(...events);
clearTimeout(this._fireSoonHandle);
this._fireSoonHandle = setTimeout(() => {
this._emitter.fire(this._bufferedEvents);
this._bufferedEvents.length = 0;
}, 5);
}
}