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