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