]>
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 | ||
ff4aca50 | 58 | function login(uri, options) { |
94176733 | 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 | |
ff4aca50 JZ |
66 | var username = options.username || readlineSync.question('Username: '); |
67 | var password = options.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); | |
ff4aca50 JZ |
80 | |
81 | // remove the password to avoid a login loop | |
82 | delete options.password; | |
83 | ||
84 | return login(uri, options); | |
a90a633f JZ |
85 | } |
86 | ||
9b7a26fc JZ |
87 | // TODO remove at some point, this is just to clear the previous old version values |
88 | config.set('username', ''); | |
89 | config.set('password', ''); | |
a90a633f | 90 | |
9b7a26fc JZ |
91 | config.set('server', server); |
92 | config.set('accessToken', result.body.accessToken); | |
a90a633f | 93 | |
7712c94e | 94 | console.log('Login successful'.green); |
a90a633f | 95 | }); |
8c3ae071 JZ |
96 | } |
97 | ||
15397f71 | 98 | function logout() { |
ba93623c | 99 | if (!config.accessToken()) return console.log('Done'.green); |
15397f71 | 100 | |
ba93623c | 101 | superagent.post(config.server() + '/api/logout').query({ access_token: config.accessToken() }).end(function (error, result) { |
15397f71 JZ |
102 | if (result && result.statusCode !== 200) console.log('Failed to logout: ' + result.statusCode); |
103 | if (error) console.log(error); | |
104 | ||
105 | // TODO remove at some point, this is just to clear the previous old version values | |
106 | config.set('username', ''); | |
107 | config.set('password', ''); | |
108 | config.set('server', ''); | |
109 | config.set('accessToken', ''); | |
110 | ||
111 | console.log('Done'.green); | |
112 | }); | |
113 | } | |
114 | ||
eb83e4da | 115 | function put(filePath, otherFilePaths, options) { |
8c3ae071 JZ |
116 | checkConfig(); |
117 | ||
c9d33e20 JZ |
118 | var destination = ''; |
119 | ||
120 | // take the last argument as destination | |
121 | if (otherFilePaths.length > 0) { | |
122 | destination = otherFilePaths.pop(); | |
123 | if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; | |
124 | } | |
125 | ||
d53a1669 | 126 | var files = collectFiles([ filePath ].concat(otherFilePaths), options); |
8c3ae071 JZ |
127 | |
128 | async.eachSeries(files, function (file, callback) { | |
1c20715d | 129 | var relativeFilePath; |
d904812f | 130 | |
1c20715d GR |
131 | if (path.isAbsolute(file)) { |
132 | relativeFilePath = path.basename(file); | |
d904812f | 133 | } else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir |
1c20715d GR |
134 | relativeFilePath = path.resolve(file).slice(process.cwd().length + 1); |
135 | } else { // relative but somewhere else | |
136 | relativeFilePath = path.basename(file); | |
137 | } | |
138 | ||
c9d33e20 | 139 | var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath; |
0f5a7ba2 | 140 | console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan); |
8c3ae071 | 141 | |
e628921a | 142 | superagent.post(config.server() + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { |
aa88a753 JZ |
143 | if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); |
144 | if (result && result.statusCode !== 201) return callback(new Error('Error uploading file: ' + result.statusCode)); | |
0f5a7ba2 | 145 | if (error) return callback(error); |
8c3ae071 | 146 | |
0f5a7ba2 | 147 | console.log('Uploaded to ' + config.server() + destinationPath); |
383e8f0c JZ |
148 | |
149 | callback(null); | |
0f5a7ba2 | 150 | }); |
8c3ae071 JZ |
151 | }, function (error) { |
152 | if (error) { | |
aa88a753 | 153 | console.log('Failed to put file.', error.message.red); |
8c3ae071 JZ |
154 | process.exit(1); |
155 | } | |
156 | ||
157 | console.log('Done'); | |
158 | }); | |
159 | } | |
160 | ||
161 | function get(filePath) { | |
162 | checkConfig(); | |
163 | ||
7bb99aff JZ |
164 | // if no argument provided, fetch root |
165 | filePath = filePath || '/'; | |
166 | ||
167 | request.get(config.server() + API + filePath, { qs: gQuery }, function (error, result, body) { | |
aa88a753 JZ |
168 | if (result && result.statusCode === 401) return console.log('Login failed'); |
169 | if (result && result.statusCode === 404) return console.log('No such file or directory %s', filePath.yellow); | |
7bb99aff | 170 | if (error) return console.error(error); |
8c3ae071 | 171 | |
7bb99aff JZ |
172 | // 222 indicates directory listing |
173 | if (result.statusCode === 222) { | |
c9d33e20 JZ |
174 | var files = safe.JSON.parse(body); |
175 | if (!files || files.entries.length === 0) { | |
176 | console.log('No files on the server. Use %s to upload some.', 'surfer put <file>'.yellow); | |
177 | } else { | |
13080980 | 178 | console.log('Entries:'); |
c9d33e20 | 179 | files.entries.forEach(function (entry) { |
13080980 | 180 | console.log('\t %s', entry.isDirectory ? entry.filePath + '/' : entry.filePath); |
c9d33e20 JZ |
181 | }); |
182 | } | |
8c3ae071 | 183 | } else { |
9d2701fe | 184 | process.stdout.write(body); |
8c3ae071 JZ |
185 | } |
186 | }); | |
7bb99aff JZ |
187 | // var req = superagent.get(config.server() + API + filePath); |
188 | // req.query(gQuery); | |
189 | // req.end(function (error, result) { | |
190 | // if (error && error.status === 401) return console.log('Login failed'); | |
191 | // if (error && error.status === 404) return console.log('No such file or directory'); | |
192 | // if (error) return console.log('Failed', result ? result.body : error); | |
193 | ||
194 | // if (result.body && result.body.entries) { | |
195 | // console.log('Files:'); | |
196 | // result.body.entries.forEach(function (entry) { | |
197 | // console.log('\t %s', entry); | |
198 | // }); | |
199 | // } else { | |
200 | // req.pipe(process.stdout); | |
201 | // } | |
202 | // }); | |
8c3ae071 JZ |
203 | } |
204 | ||
898fe7c8 | 205 | function del(filePath, options) { |
8c3ae071 JZ |
206 | checkConfig(); |
207 | ||
898fe7c8 JZ |
208 | var query = safe.JSON.parse(safe.JSON.stringify(gQuery)); |
209 | query.recursive = options.recursive; | |
210 | query.dryRun = options.dryRun; | |
211 | ||
8c3ae071 | 212 | var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); |
898fe7c8 JZ |
213 | superagent.del(config.server() + API + relativeFilePath).query(query).end(function (error, result) { |
214 | if (error && error.status === 401) return console.log('Login failed'.red); | |
a90a633f | 215 | if (error && error.status === 404) return console.log('No such file or directory'); |
898fe7c8 | 216 | if (error && error.status === 403) return console.log('Failed. Target is a directory. Use %s to delete directories.', '--recursive'.yellow); |
8c3ae071 | 217 | if (error) return console.log('Failed', result ? result.body : error); |
24545229 | 218 | |
898fe7c8 JZ |
219 | if (options.dryRun) { |
220 | console.log('This would remove %s files:', result.body.entries.length); | |
221 | result.body.entries.forEach(function (entry) { | |
222 | console.log('\t %s', entry); | |
223 | }); | |
224 | } else { | |
225 | console.log('Success. Removed %s files.', result.body.entries.length); | |
226 | } | |
8c3ae071 JZ |
227 | }); |
228 | } |