-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathftpExplorer.ts
More file actions
181 lines (145 loc) · 5.09 KB
/
ftpExplorer.ts
File metadata and controls
181 lines (145 loc) · 5.09 KB
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
import * as vscode from 'vscode';
import * as Client from 'ftp';
import { basename, dirname } from 'path';
export interface FtpNode {
resource: vscode.Uri;
isDirectory: boolean;
}
export class FtpModel {
constructor(readonly host: string, private user: string, private password: string) {
}
public connect(): Thenable<Client> {
return new Promise((c, e) => {
const client = new Client();
client.on('ready', () => {
c(client);
});
client.on('error', error => {
e('Error while connecting: ' + error.message);
});
client.connect({
host: this.host,
user: this.user,
password: this.password
});
});
}
public get roots(): Thenable<FtpNode[]> {
return this.connect().then(client => {
return new Promise((c, e) => {
client.list((err, list) => {
if (err) {
return e(err);
}
client.end();
return c(this.sort(list.map(entry => ({ resource: vscode.Uri.parse(`ftp://${this.host}//${entry.name}`), isDirectory: entry.type === 'd' }))));
});
});
});
}
public getChildren(node: FtpNode): Thenable<FtpNode[]> {
return this.connect().then(client => {
return new Promise((c, e) => {
client.list(node.resource.fsPath, (err, list) => {
if (err) {
return e(err);
}
client.end();
return c(this.sort(list.map(entry => ({ resource: vscode.Uri.parse(`${node.resource.fsPath}/${entry.name}`), isDirectory: entry.type === 'd' }))));
});
});
});
}
private sort(nodes: FtpNode[]): FtpNode[] {
return nodes.sort((n1, n2) => {
if (n1.isDirectory && !n2.isDirectory) {
return -1;
}
if (!n1.isDirectory && n2.isDirectory) {
return 1;
}
return basename(n1.resource.fsPath).localeCompare(basename(n2.resource.fsPath));
});
}
public getContent(resource: vscode.Uri): Thenable<string> {
return this.connect().then(client => {
return new Promise((c, e) => {
client.get(resource.path.substr(1), (err, stream) => {
if (err) {
return e(err);
}
let string = '';
stream.on('data', function(buffer) {
if (buffer) {
const part = buffer.toString();
string += part;
}
});
stream.on('end', function() {
client.end();
c(string);
});
});
});
});
}
}
export class FtpTreeDataProvider implements vscode.TreeDataProvider<FtpNode>, vscode.TextDocumentContentProvider {
private _onDidChangeTreeData: vscode.EventEmitter<any> = new vscode.EventEmitter<any>();
readonly onDidChangeTreeData: vscode.Event<any> = this._onDidChangeTreeData.event;
constructor(private readonly model: FtpModel) { }
public refresh() {
this._onDidChangeTreeData.fire(undefined);
}
public getTreeItem(element: FtpNode): vscode.TreeItem {
return {
resourceUri: element.resource,
collapsibleState: element.isDirectory ? vscode.TreeItemCollapsibleState.Collapsed : void 0,
command: element.isDirectory ? void 0 : {
command: 'ftpExplorer.openFtpResource',
arguments: [element.resource],
title: 'Open FTP Resource'
}
};
}
public getChildren(element?: FtpNode): FtpNode[] | Thenable<FtpNode[]> {
return element ? this.model.getChildren(element) : this.model.roots;
}
public getParent(element: FtpNode): FtpNode | undefined {
const parent = element.resource.with({ path: dirname(element.resource.path) });
return parent.path !== '//' ? { resource: parent, isDirectory: true } : undefined;
}
public provideTextDocumentContent(uri: vscode.Uri, _token: vscode.CancellationToken): vscode.ProviderResult<string> {
return this.model.getContent(uri).then(content => content);
}
}
export class FtpExplorer {
private ftpViewer: vscode.TreeView<FtpNode>;
constructor(context: vscode.ExtensionContext) {
/* Please note that login information is hardcoded only for this example purpose and recommended not to do it in general. */
const ftpModel = new FtpModel('mirror.switch.ch', 'anonymous', 'anonymous@anonymous.de');
const treeDataProvider = new FtpTreeDataProvider(ftpModel);
context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider('ftp', treeDataProvider));
this.ftpViewer = vscode.window.createTreeView('ftpExplorer', { treeDataProvider });
vscode.commands.registerCommand('ftpExplorer.refresh', () => treeDataProvider.refresh());
vscode.commands.registerCommand('ftpExplorer.openFtpResource', resource => this.openResource(resource));
vscode.commands.registerCommand('ftpExplorer.revealResource', () => this.reveal());
}
private openResource(resource: vscode.Uri): void {
vscode.window.showTextDocument(resource);
}
private async reveal(): Promise<void> {
const node = this.getNode();
if (node) {
return this.ftpViewer.reveal(node);
}
}
private getNode(): FtpNode | undefined {
if (vscode.window.activeTextEditor) {
if (vscode.window.activeTextEditor.document.uri.scheme === 'ftp') {
return { resource: vscode.window.activeTextEditor.document.uri, isDirectory: false };
}
}
return undefined;
}
}