]>
Commit | Line | Data |
---|---|---|
8c3ae071 JZ |
1 | 'use strict'; |
2 | ||
3 | exports.login = login; | |
15397f71 | 4 | exports.logout = logout; |
8c3ae071 JZ |
5 | exports.put = put; |
6 | exports.get = get; | |
7 | exports.del = del; | |
8 | ||
9 | var superagent = require('superagent'), | |
a90a633f JZ |
10 | config = require('./config.js'), |
11 | readlineSync = require('readline-sync'), | |
c9d33e20 | 12 | safe = require('safetydance'), |
8c3ae071 JZ |
13 | async = require('async'), |
14 | fs = require('fs'), | |
7bb99aff | 15 | request = require('request'), |
94176733 | 16 | url = require('url'), |
8c3ae071 JZ |
17 | path = require('path'); |
18 | ||
19 | require('colors'); | |
20 | ||
21 | var API = '/api/files/'; | |
22 | ||
ec4c48f2 | 23 | var gServer = ''; |
a90a633f JZ |
24 | var gQuery = {}; |
25 | ||
ec4c48f2 JZ |
26 | function checkConfig(options) { |
27 | if (!options.parent.server && !config.server()) { | |
28 | console.log('Run %s first, or provide %s', 'surfer login'.bold, '--server <url>'.bold); | |
8c3ae071 JZ |
29 | process.exit(1); |
30 | } | |
c10e69bf | 31 | |
38932f15 GR |
32 | if (options.parent.server) { |
33 | var tmp = url.parse(options.parent.server); | |
34 | if (!tmp.slashes) tmp = url.parse('https://' + options.parent.server); | |
35 | gServer = tmp.protocol + '//' + tmp.host; | |
36 | } else { | |
37 | gServer = config.server(); | |
38 | } | |
39 | ||
ec4c48f2 JZ |
40 | if (!options.parent.token && !config.accessToken()) { |
41 | console.log('Run %s first or provide %s', 'surfer login'.bold, '--token <access token>'.bold); | |
42 | process.exit(1); | |
43 | } | |
44 | ||
ec4c48f2 | 45 | gQuery = { access_token: options.parent.token || config.accessToken() }; |
a90a633f | 46 | |
ec4c48f2 | 47 | console.error('Using server %s', gServer.cyan); |
8c3ae071 JZ |
48 | } |
49 | ||
d53a1669 | 50 | function collectFiles(filesOrFolders, options) { |
8c3ae071 JZ |
51 | var tmp = []; |
52 | ||
53 | filesOrFolders.forEach(function (filePath) { | |
d53a1669 JZ |
54 | var baseName = path.basename(filePath); |
55 | if (!options.all && baseName[0] === '.' && baseName.length > 1) return; | |
56 | ||
8c3ae071 JZ |
57 | var stat = fs.statSync(filePath); |
58 | ||
59 | if (stat.isFile()) { | |
60 | tmp.push(filePath); | |
61 | } else if (stat.isDirectory()) { | |
62 | var files = fs.readdirSync(filePath).map(function (file) { return path.join(filePath, file); }); | |
d53a1669 | 63 | tmp = tmp.concat(collectFiles(files, options)); |
8c3ae071 JZ |
64 | } else { |
65 | console.log('Skipping %s', filePath.cyan); | |
66 | } | |
67 | }); | |
68 | ||
69 | return tmp; | |
70 | } | |
71 | ||
ff4aca50 | 72 | function login(uri, options) { |
94176733 | 73 | var tmp = url.parse(uri); |
7712c94e | 74 | if (!tmp.slashes) tmp = url.parse('https://' + uri); |
94176733 | 75 | |
b9ad5d91 | 76 | var server = tmp.protocol + '//' + tmp.host; |
591ad40c | 77 | |
7712c94e | 78 | console.log('Using server', server.cyan); |
a90a633f | 79 | |
ff4aca50 JZ |
80 | var username = options.username || readlineSync.question('Username: '); |
81 | var password = options.password || readlineSync.question('Password: ', { hideEchoBack: true, mask: '' }); | |
a90a633f | 82 | |
4eee848b JZ |
83 | if (!username || !password) process.exit(1); |
84 | ||
9b7a26fc | 85 | superagent.post(server + '/api/login').send({ username: username, password: password }).end(function (error, result) { |
94176733 | 86 | if (error && error.code === 'ENOTFOUND') { |
cc6510cf | 87 | console.log('Server %s not found.'.red, server.bold); |
b9ad5d91 JZ |
88 | process.exit(1); |
89 | } | |
4f897631 | 90 | if (error && error.code) { |
b9ad5d91 | 91 | console.log('Failed to connect to server %s'.red, server.bold, error.code); |
94176733 JZ |
92 | process.exit(1); |
93 | } | |
9b7a26fc JZ |
94 | if (result.status !== 201) { |
95 | console.log('Login failed.\n'.red); | |
ff4aca50 JZ |
96 | |
97 | // remove the password to avoid a login loop | |
98 | delete options.password; | |
99 | ||
100 | return login(uri, options); | |
a90a633f JZ |
101 | } |
102 | ||
9b7a26fc JZ |
103 | // TODO remove at some point, this is just to clear the previous old version values |
104 | config.set('username', ''); | |
105 | config.set('password', ''); | |
a90a633f | 106 | |
9b7a26fc JZ |
107 | config.set('server', server); |
108 | config.set('accessToken', result.body.accessToken); | |
a90a633f | 109 | |
7712c94e | 110 | console.log('Login successful'.green); |
a90a633f | 111 | }); |
8c3ae071 JZ |
112 | } |
113 | ||
15397f71 | 114 | function logout() { |
ba93623c | 115 | if (!config.accessToken()) return console.log('Done'.green); |
15397f71 | 116 | |
ec4c48f2 | 117 | superagent.post(gServer + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) { |
15397f71 JZ |
118 | if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode); |
119 | if (error) console.log(error); | |
120 | ||
121 | // TODO remove at some point, this is just to clear the previous old version values | |
122 | config.set('username', ''); | |
123 | config.set('password', ''); | |
124 | config.set('server', ''); | |
125 | config.set('accessToken', ''); | |
126 | ||
127 | console.log('Done'.green); | |
128 | }); | |
129 | } | |
130 | ||
eb83e4da | 131 | function put(filePath, otherFilePaths, options) { |
ec4c48f2 | 132 | checkConfig(options); |
8c3ae071 | 133 | |
c9d33e20 JZ |
134 | var destination = ''; |
135 | ||
136 | // take the last argument as destination | |
137 | if (otherFilePaths.length > 0) { | |
138 | destination = otherFilePaths.pop(); | |
139 | if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; | |
140 | } | |
141 | ||
d53a1669 | 142 | var files = collectFiles([ filePath ].concat(otherFilePaths), options); |
8c3ae071 JZ |
143 | |
144 | async.eachSeries(files, function (file, callback) { | |
1c20715d | 145 | var relativeFilePath; |
d904812f | 146 | |
1c20715d GR |
147 | if (path.isAbsolute(file)) { |
148 | relativeFilePath = path.basename(file); | |
d904812f | 149 | } else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir |
1c20715d GR |
150 | relativeFilePath = path.resolve(file).slice(process.cwd().length + 1); |
151 | } else { // relative but somewhere else | |
152 | relativeFilePath = path.basename(file); | |
153 | } | |
154 | ||
c9d33e20 | 155 | var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; |
0f5a7ba2 | 156 | console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); |
8c3ae071 | 157 | |
ec4c48f2 | 158 | superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { |
aa88a753 JZ |
159 | if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); |
160 | if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode)); | |
0f5a7ba2 | 161 | if (error) return callback(error); |
8c3ae071 | 162 | |
ec4c48f2 | 163 | console.log('Uploaded to ' + gServer + destinationPath); |
383e8f0c JZ |
164 | |
165 | callback(null); | |
0f5a7ba2 | 166 | }); |
8c3ae071 JZ |
167 | }, function (error) { |
168 | if (error) { | |
aa88a753 | 169 | console.log('Failed to put file.', error.message.red); |
8c3ae071 JZ |
170 | process.exit(1); |
171 | } | |
172 | ||
173 | console.log('Done'); | |
174 | }); | |
175 | } | |
176 | ||
ec4c48f2 JZ |
177 | function get(filePath, options) { |
178 | checkConfig(options); | |
8c3ae071 | 179 | |
7bb99aff JZ |
180 | // if no argument provided, fetch root |
181 | filePath = filePath || '/'; | |
182 | ||
ec4c48f2 | 183 | request.get(gServer + API + filePath, { qs: gQuery }, function (error, result, body) { |
aa88a753 JZ |
184 | if (result && result.statusCode === 401) return console.log('Login failed'); |
185 | if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); | |
7bb99aff | 186 | if (error) return console.error(error); |
8c3ae071 | 187 | |
7bb99aff JZ |
188 | // 222 indicates directory listing |
189 | if (result.statusCode === 222) { | |
c9d33e20 JZ |
190 | var files = safe.JSON.parse(body); |
191 | if (!files || files.entries.length === 0) { | |
192 | console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow); | |
193 | } else { | |
13080980 | 194 | console.log('Entries:'); |
c9d33e20 | 195 | files.entries.forEach(function (entry) { |
13080980 | 196 | console.log('\t %s', entry.isDirectory ? entry.filePath + '/' : entry.filePath); |
c9d33e20 JZ |
197 | }); |
198 | } | |
8c3ae071 | 199 | } else { |
9d2701fe | 200 | process.stdout.write(body); |
8c3ae071 JZ |
201 | } |
202 | }); | |
ec4c48f2 | 203 | // var req = superagent.get(gServer + API + filePath); |
7bb99aff JZ |
204 | // req.query(gQuery); |
205 | // req.end(function (error, result) { | |
206 | // if (error && error.status === 401) return console.log('Login failed'); | |
207 | // if (error && error.status === 404) return console.log('No such file or directory'); | |
208 | // if (error) return console.log('Failed', result ? result.body : error); | |
209 | ||
210 | // if (result.body && result.body.entries) { | |
211 | // console.log('Files:'); | |
212 | // result.body.entries.forEach(function (entry) { | |
213 | // console.log('\t %s', entry); | |
214 | // }); | |
215 | // } else { | |
216 | // req.pipe(process.stdout); | |
217 | // } | |
218 | // }); | |
8c3ae071 JZ |
219 | } |
220 | ||
898fe7c8 | 221 | function del(filePath, options) { |
ec4c48f2 | 222 | checkConfig(options); |
8c3ae071 | 223 | |
898fe7c8 JZ |
224 | var query = safe.JSON.parse(safe.JSON.stringify(gQuery)); |
225 | query.recursive = options.recursive; | |
226 | query.dryRun = options.dryRun; | |
227 | ||
8c3ae071 | 228 | var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); |
ec4c48f2 | 229 | superagent.del(gServer + API + relativeFilePath).query(query).end(function (error, result) { |
898fe7c8 | 230 | if (error && error.status === 401) return console.log('Login failed'.red); |
a90a633f | 231 | if (error && error.status === 404) return console.log('No such file or directory'); |
898fe7c8 | 232 | if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow); |
8c3ae071 | 233 | if (error) return console.log('Failed', result ? result.body : error); |
24545229 | 234 | |
898fe7c8 JZ |
235 | if (options.dryRun) { |
236 | console.log('This would remove %s files:', result.body.entries.length); | |
237 | result.body.entries.forEach(function (entry) { | |
238 | console.log('\t %s', entry); | |
239 | }); | |
240 | } else { | |
241 | console.log('Success. Removed %s files.', result.body.entries.length); | |
242 | } | |
8c3ae071 JZ |
243 | }); |
244 | } |