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