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
|
'use strict';
exports.login = login;
exports.logout = logout;
exports.put = put;
exports.get = get;
exports.del = del;
var superagent = require('superagent'),
config = require('./config.js'),
readlineSync = require('readline-sync'),
safe = require('safetydance'),
async = require('async'),
fs = require('fs'),
request = require('request'),
url = require('url'),
path = require('path');
require('colors');
var API = '/api/files/';
var gServer = '';
var gQuery = {};
function checkConfig(options) {
if (!options.parent.server && !config.server()) {
console.log('Run %s first, or provide %s', 'surfer login'.bold, '--server <url>'.bold);
process.exit(1);
}
if (options.parent.server) {
var tmp = url.parse(options.parent.server);
if (!tmp.slashes) tmp = url.parse('https://' + options.parent.server);
gServer = tmp.protocol + '//' + tmp.host;
} else {
gServer = config.server();
}
if (!options.parent.token && !config.accessToken()) {
console.log('Run %s first or provide %s', 'surfer login'.bold, '--token <access token>'.bold);
process.exit(1);
}
gQuery = { access_token: options.parent.token || config.accessToken() };
console.error('Using server %s', gServer.cyan);
}
function collectFiles(filesOrFolders, options) {
var tmp = [];
filesOrFolders.forEach(function (filePath) {
var baseName = path.basename(filePath);
if (!options.all && baseName[0] === '.' && baseName.length > 1) return;
var stat = fs.statSync(filePath);
if (stat.isFile()) {
tmp.push(filePath);
} else if (stat.isDirectory()) {
var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); });
tmp = tmp.concat(collectFiles(files, options));
} else {
console.log('Skipping %s', filePath.cyan);
}
});
return tmp;
}
function login(uri, options) {
var tmp = url.parse(uri);
if (!tmp.slashes) tmp = url.parse('https://' + uri);
var server = tmp.protocol + '//' + tmp.host;
console.log('Using server', server.cyan);
var username = options.username || readlineSync.question('Username: ');
var password = options.password || readlineSync.question('Password: ', { hideEchoBack: true, mask: '' });
if (!username || !password) process.exit(1);
superagent.post(server + '/api/login').send({ username: username, password: password }).end(function (error, result) {
if (error && error.code === 'ENOTFOUND') {
console.log('Server %s not found.'.red, server.bold);
process.exit(1);
}
if (error && error.code) {
console.log('Failed to connect to server %s'.red, server.bold, error.code);
process.exit(1);
}
if (result.status !== 201) {
console.log('Login failed.\n'.red);
// remove the password to avoid a login loop
delete options.password;
return login(uri, options);
}
// TODO remove at some point, this is just to clear the previous old version values
config.set('username', '');
config.set('password', '');
config.set('server', server);
config.set('accessToken', result.body.accessToken);
console.log('Login successful'.green);
});
}
function logout() {
if (!config.accessToken()) return console.log('Done'.green);
superagent.post(gServer + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) {
if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode);
if (error) console.log(error);
// TODO remove at some point, this is just to clear the previous old version values
config.set('username', '');
config.set('password', '');
config.set('server', '');
config.set('accessToken', '');
console.log('Done'.green);
});
}
function putOne(filePath, destination, options, callback) {
const absoluteFilePath = path.resolve(filePath);
const stat = safe.fs.statSync(absoluteFilePath);
if (!stat) return callback(`Could not stat ${filePath}: ${safe.error.message}`);
let files, base;
if (stat.isFile()) {
base = destination + path.basename(filePath);
files = [ absoluteFilePath ];
} else if (stat.isDirectory()) {
base = destination + (filePath.endsWith('.') ? '' : path.basename(filePath) + '/');
files = collectFiles([ absoluteFilePath ], options);
} else {
return callback(); // ignore
}
async.eachSeries(files, function (file, callback) {
let relativeFilePath = file.slice(absoluteFilePath.length + 1); // will be '' when filePath is a file
let destinationPath = base + relativeFilePath;
console.log('Uploading file %s -> %s', file.cyan, destinationPath.cyan);
superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) {
if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed'));
if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode));
if (error) return callback(error);
console.log('Uploaded to ' + gServer + destinationPath);
callback(null);
});
}, callback);
}
function put(filePaths, options) {
checkConfig(options);
if (filePaths.length < 2) {
console.log('target directory is required.'.red);
process.exit(1);
}
let destination = filePaths.pop();
if (!path.isAbsolute(destination)) {
console.log('target directory must be absolute'.red);
process.exit(1);
}
if (!destination.endsWith('/')) destination += '/';
async.eachSeries(filePaths, (filePath, iteratorDone) => putOne(filePath, destination, options, iteratorDone), function (error) {
if (error) {
console.log('Failed to put file.', error.message.red);
process.exit(1);
}
console.log('Done');
});
}
function get(filePath, options) {
checkConfig(options);
// if no argument provided, fetch root
filePath = filePath || '/';
request.get(gServer + API + filePath, { qs: gQuery }, function (error, result, body) {
if (result && result.statusCode === 401) return console.log('Login failed');
if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow);
if (error) return console.error(error);
// 222 indicates directory listing
if (result.statusCode === 222) {
var files = safe.JSON.parse(body);
if (!files || files.entries.length === 0) {
console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow);
} else {
console.log('Entries:');
files.entries.forEach(function (entry) {
console.log('\t %s', entry.isDirectory ? entry.filePath + '/' : entry.filePath);
});
}
} else {
process.stdout.write(body);
}
});
// var req = superagent.get(gServer + API + filePath);
// req.query(gQuery);
// req.end(function (error, result) {
// if (error && error.status === 401) return console.log('Login failed');
// if (error && error.status === 404) return console.log('No such file or directory');
// if (error) return console.log('Failed', result ? result.body : error);
// if (result.body && result.body.entries) {
// console.log('Files:');
// result.body.entries.forEach(function (entry) {
// console.log('\t %s', entry);
// });
// } else {
// req.pipe(process.stdout);
// }
// });
}
function del(filePath, options) {
checkConfig(options);
var query = safe.JSON.parse(safe.JSON.stringify(gQuery));
query.recursive = options.recursive;
query.dryRun = options.dryRun;
var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
superagent.del(gServer + API + relativeFilePath).query(query).end(function (error, result) {
if (error && error.status === 401) return console.log('Login failed'.red);
if (error && error.status === 404) return console.log('No such file or directory');
if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow);
if (error) return console.log('Failed', result ? result.body : error);
if (options.dryRun) {
console.log('This would remove %s files:', result.body.entries.length);
result.body.entries.forEach(function (entry) {
console.log('\t %s', entry);
});
} else {
console.log('Success. Removed %s files.', result.body.entries.length);
}
});
}
|