]>
Commit | Line | Data |
---|---|---|
6eb72d64 JZ |
1 | (function () { |
2 | 'use strict'; | |
3 | ||
996e13b3 JZ |
4 | /* global superagent */ |
5 | /* global Vue */ | |
6 | /* global $ */ | |
7 | /* global filesize */ | |
8 | ||
d0803a04 JZ |
9 | // poor man's async |
10 | function asyncForEach(items, handler, callback) { | |
11 | var cur = 0; | |
12 | ||
13 | if (items.length === 0) return callback(); | |
14 | ||
15 | (function iterator() { | |
16 | handler(items[cur], function (error) { | |
17 | if (error) return callback(error); | |
18 | if (cur >= items.length-1) return callback(); | |
19 | ++cur; | |
20 | ||
21 | iterator(); | |
22 | }); | |
23 | })(); | |
24 | } | |
25 | ||
4a27fce7 | 26 | function getProfile(accessToken, callback) { |
4a27fce7 | 27 | superagent.get('/api/profile').query({ access_token: accessToken }).end(function (error, result) { |
13df9f95 | 28 | app.ready = true; |
4a27fce7 JZ |
29 | |
30 | if (error && !error.response) return callback(error); | |
31 | if (result.statusCode !== 200) { | |
32 | delete localStorage.accessToken; | |
33 | return callback('Invalid access token'); | |
34 | } | |
35 | ||
36 | localStorage.accessToken = accessToken; | |
37 | app.session.username = result.body.username; | |
38 | app.session.valid = true; | |
39 | ||
f43b3c16 JZ |
40 | superagent.get('/api/settings').query({ access_token: localStorage.accessToken }).end(function (error, result) { |
41 | if (error) console.error(error); | |
42 | ||
43 | app.folderListingEnabled = !!result.body.folderListingEnabled; | |
44 | ||
45 | callback(); | |
46 | }); | |
4a27fce7 JZ |
47 | }); |
48 | } | |
49 | ||
d3312ed1 JZ |
50 | function sanitize(filePath) { |
51 | filePath = '/' + filePath; | |
52 | return filePath.replace(/\/+/g, '/'); | |
53 | } | |
54 | ||
537bfb04 JZ |
55 | function encode(filePath) { |
56 | return filePath.split('/').map(encodeURIComponent).join('/'); | |
57 | } | |
58 | ||
04bc2989 JZ |
59 | function decode(filePath) { |
60 | return filePath.split('/').map(decodeURIComponent).join('/'); | |
61 | } | |
62 | ||
a26d1f9b JZ |
63 | var mimeTypes = { |
64 | images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ], | |
65 | text: [ '.txt', '.md' ], | |
66 | pdf: [ '.pdf' ], | |
67 | html: [ '.html', '.htm', '.php' ], | |
c66d7093 | 68 | video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv', '.avi', '.mov' ] |
a26d1f9b JZ |
69 | }; |
70 | ||
71 | function getPreviewUrl(entry, basePath) { | |
72 | var path = '/_admin/img/'; | |
73 | ||
74 | if (entry.isDirectory) return path + 'directory.png'; | |
75 | if (mimeTypes.images.some(function (e) { return entry.filePath.endsWith(e); })) return sanitize(basePath + '/' + entry.filePath); | |
76 | if (mimeTypes.text.some(function (e) { return entry.filePath.endsWith(e); })) return path +'text.png'; | |
77 | if (mimeTypes.pdf.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'pdf.png'; | |
78 | if (mimeTypes.html.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'html.png'; | |
79 | if (mimeTypes.video.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'video.png'; | |
80 | ||
81 | return path + 'unknown.png'; | |
82 | } | |
83 | ||
8a3d0eee JZ |
84 | // simple extension detection, does not work with double extension like .tar.gz |
85 | function getExtension(entry) { | |
86 | if (entry.isFile) return entry.filePath.slice(entry.filePath.lastIndexOf('.') + 1); | |
87 | return ''; | |
88 | } | |
89 | ||
537bfb04 JZ |
90 | function refresh() { |
91 | loadDirectory(app.path); | |
92 | } | |
93 | ||
996e13b3 JZ |
94 | function logout() { |
95 | superagent.post('/api/logout').query({ access_token: localStorage.accessToken }).end(function (error) { | |
96 | if (error) console.error(error); | |
97 | ||
98 | app.session.valid = false; | |
99 | ||
100 | delete localStorage.accessToken; | |
101 | }); | |
102 | } | |
103 | ||
d3312ed1 JZ |
104 | function loadDirectory(filePath) { |
105 | app.busy = true; | |
106 | ||
107 | filePath = filePath ? sanitize(filePath) : '/'; | |
108 | ||
4a27fce7 | 109 | superagent.get('/api/files/' + encode(filePath)).query({ access_token: localStorage.accessToken }).end(function (error, result) { |
d3312ed1 JZ |
110 | app.busy = false; |
111 | ||
9209abec | 112 | if (result && result.statusCode === 401) return logout(); |
d3312ed1 | 113 | if (error) return console.error(error); |
d3312ed1 | 114 | |
04bc2989 | 115 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; }); |
a26d1f9b JZ |
116 | app.entries = result.body.entries.map(function (entry) { |
117 | entry.previewUrl = getPreviewUrl(entry, filePath); | |
8a3d0eee | 118 | entry.extension = getExtension(entry); |
a26d1f9b JZ |
119 | return entry; |
120 | }); | |
d3312ed1 | 121 | app.path = filePath; |
51723cdf J |
122 | app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }).map(function (e, i, a) { |
123 | return { | |
124 | name: e, | |
125 | link: '#' + sanitize('/' + a.slice(0, i).join('/') + '/' + e) | |
126 | }; | |
127 | }); | |
04bc2989 JZ |
128 | |
129 | // update in case this was triggered from code | |
130 | window.location.hash = app.path; | |
d3312ed1 JZ |
131 | }); |
132 | } | |
133 | ||
13df9f95 JZ |
134 | function open(row, event, column) { |
135 | var path = sanitize(app.path + '/' + row.filePath); | |
d3312ed1 | 136 | |
13df9f95 | 137 | if (row.isDirectory) { |
04bc2989 JZ |
138 | window.location.hash = path; |
139 | return; | |
140 | } | |
d3312ed1 | 141 | |
5f43935e | 142 | window.open(encode(path)); |
d3312ed1 JZ |
143 | } |
144 | ||
b094fada JZ |
145 | function uploadFiles(files) { |
146 | if (!files || !files.length) return; | |
147 | ||
d0803a04 JZ |
148 | app.uploadStatus.busy = true; |
149 | app.uploadStatus.count = files.length; | |
5c17272a | 150 | app.uploadStatus.size = 0; |
d0803a04 JZ |
151 | app.uploadStatus.done = 0; |
152 | app.uploadStatus.percentDone = 0; | |
b094fada | 153 | |
5c17272a JZ |
154 | for (var i = 0; i < files.length; ++i) { |
155 | app.uploadStatus.size += files[i].size; | |
156 | } | |
157 | ||
d0803a04 | 158 | asyncForEach(files, function (file, callback) { |
35355283 | 159 | var path = encode(sanitize(app.path + '/' + (file.webkitRelativePath || file.name))); |
b094fada JZ |
160 | |
161 | var formData = new FormData(); | |
162 | formData.append('file', file); | |
163 | ||
3760489f JZ |
164 | var finishedUploadSize = app.uploadStatus.done; |
165 | ||
5c17272a JZ |
166 | superagent.post('/api/files' + path) |
167 | .query({ access_token: localStorage.accessToken }) | |
168 | .send(formData) | |
169 | .on('progress', function (event) { | |
3760489f JZ |
170 | // only handle upload events |
171 | if (!(event.target instanceof XMLHttpRequestUpload)) return; | |
172 | ||
173 | app.uploadStatus.done = finishedUploadSize + event.loaded; | |
701c2be6 JZ |
174 | var tmp = Math.round(app.uploadStatus.done / app.uploadStatus.size * 100); |
175 | app.uploadStatus.percentDone = tmp > 100 ? 100 : tmp; | |
5c17272a | 176 | }).end(function (error, result) { |
b094fada | 177 | if (result && result.statusCode === 401) return logout(); |
d0803a04 JZ |
178 | if (result && result.statusCode !== 201) return callback('Error uploading file: ', result.statusCode); |
179 | if (error) return callback(error); | |
b094fada | 180 | |
d0803a04 | 181 | callback(); |
b094fada | 182 | }); |
d0803a04 JZ |
183 | }, function (error) { |
184 | if (error) console.error(error); | |
b094fada | 185 | |
d0803a04 JZ |
186 | app.uploadStatus.busy = false; |
187 | app.uploadStatus.count = 0; | |
5c17272a | 188 | app.uploadStatus.size = 0; |
d0803a04 JZ |
189 | app.uploadStatus.done = 0; |
190 | app.uploadStatus.percentDone = 100; | |
191 | ||
192 | refresh(); | |
193 | }); | |
b094fada JZ |
194 | } |
195 | ||
b094fada | 196 | function dragOver(event) { |
19efa5bc | 197 | event.stopPropagation(); |
b094fada | 198 | event.preventDefault(); |
19efa5bc | 199 | event.dataTransfer.dropEffect = 'copy'; |
b094fada JZ |
200 | } |
201 | ||
202 | function drop(event) { | |
19efa5bc | 203 | event.stopPropagation(); |
b094fada | 204 | event.preventDefault(); |
8370d9f0 JZ |
205 | |
206 | if (!event.dataTransfer.items[0]) return; | |
207 | ||
208 | // figure if a folder was dropped on a modern browser, in this case the first would have to be a directory | |
209 | var folderItem; | |
210 | try { | |
211 | folderItem = event.dataTransfer.items[0].webkitGetAsEntry(); | |
212 | if (folderItem.isFile) return uploadFiles(event.dataTransfer.files); | |
213 | } catch (e) { | |
214 | return uploadFiles(event.dataTransfer.files); | |
215 | } | |
216 | ||
217 | // if we got here we have a folder drop and a modern browser | |
218 | // now traverse the folder tree and create a file list | |
219 | app.uploadStatus.busy = true; | |
220 | app.uploadStatus.uploadListCount = 0; | |
221 | ||
222 | var fileList = []; | |
223 | function traverseFileTree(item, path, callback) { | |
224 | if (item.isFile) { | |
225 | // Get file | |
226 | item.file(function (file) { | |
227 | fileList.push(file); | |
228 | ++app.uploadStatus.uploadListCount; | |
229 | callback(); | |
230 | }); | |
231 | } else if (item.isDirectory) { | |
232 | // Get folder contents | |
233 | var dirReader = item.createReader(); | |
234 | dirReader.readEntries(function (entries) { | |
235 | asyncForEach(entries, function (entry, callback) { | |
236 | traverseFileTree(entry, path + item.name + '/', callback); | |
237 | }, callback); | |
238 | }); | |
239 | } | |
240 | } | |
241 | ||
242 | traverseFileTree(folderItem, '', function (error) { | |
243 | app.uploadStatus.busy = false; | |
244 | app.uploadStatus.uploadListCount = 0; | |
245 | ||
246 | if (error) return console.error(error); | |
247 | ||
248 | uploadFiles(fileList); | |
249 | }); | |
b094fada JZ |
250 | } |
251 | ||
6eb72d64 JZ |
252 | var app = new Vue({ |
253 | el: '#app', | |
254 | data: { | |
13df9f95 | 255 | ready: false, |
25c2c5de | 256 | busy: false, |
fea6789c JZ |
257 | uploadStatus: { |
258 | busy: false, | |
259 | count: 0, | |
260 | done: 0, | |
8370d9f0 JZ |
261 | percentDone: 50, |
262 | uploadListCount: 0 | |
fea6789c | 263 | }, |
d3312ed1 JZ |
264 | path: '/', |
265 | pathParts: [], | |
6eb72d64 JZ |
266 | session: { |
267 | valid: false | |
268 | }, | |
13df9f95 JZ |
269 | folderListingEnabled: false, |
270 | loginData: { | |
271 | username: '', | |
25c2c5de JZ |
272 | password: '', |
273 | busy: false | |
e628921a | 274 | }, |
d3312ed1 | 275 | entries: [] |
6eb72d64 JZ |
276 | }, |
277 | methods: { | |
13df9f95 | 278 | onLogin: function () { |
25c2c5de | 279 | var that = this; |
13df9f95 | 280 | |
25c2c5de | 281 | that.loginData.busy = true; |
13df9f95 | 282 | |
25c2c5de JZ |
283 | superagent.post('/api/login').send({ username: that.loginData.username, password: that.loginData.password }).end(function (error, result) { |
284 | that.loginData.busy = false; | |
285 | ||
286 | if (error && !result) return that.$message.error(error.message); | |
287 | if (result.statusCode === 401) return that.$message.error('Wrong username or password'); | |
13df9f95 JZ |
288 | |
289 | getProfile(result.body.accessToken, function (error) { | |
290 | if (error) return console.error(error); | |
291 | ||
4dce7a3d | 292 | loadDirectory(decode(window.location.hash.slice(1))); |
13df9f95 JZ |
293 | }); |
294 | }); | |
295 | }, | |
296 | onOptionsMenu: function (command) { | |
297 | if (command === 'folderListing') { | |
552d44bb JZ |
298 | superagent.put('/api/settings').send({ folderListingEnabled: this.folderListingEnabled }).query({ access_token: localStorage.accessToken }).end(function (error) { |
299 | if (error) console.error(error); | |
300 | }); | |
13df9f95 JZ |
301 | } else if (command === 'about') { |
302 | this.$msgbox({ | |
303 | title: 'About Surfer', | |
304 | message: 'Surfer is a static file server written by <a href="https://cloudron.io" target="_blank">Cloudron</a>.<br/><br/>The source code is licensed under MIT and available <a href="https://git.cloudron.io/cloudron/surfer" target="_blank">here</a>.', | |
305 | dangerouslyUseHTMLString: true, | |
306 | confirmButtonText: 'OK', | |
307 | showCancelButton: false, | |
308 | type: 'info', | |
309 | center: true | |
310 | }).then(function () {}).catch(function () {}); | |
311 | } else if (command === 'logout') { | |
996e13b3 | 312 | logout(); |
13df9f95 JZ |
313 | } |
314 | }, | |
315 | onDownload: function (entry) { | |
316 | if (entry.isDirectory) return; | |
25c2c5de | 317 | window.location.href = encode('/api/files/' + sanitize(this.path + '/' + entry.filePath)) + '?access_token=' + localStorage.accessToken; |
13df9f95 JZ |
318 | }, |
319 | onUpload: function () { | |
25c2c5de JZ |
320 | var that = this; |
321 | ||
322 | $(this.$refs.upload).on('change', function () { | |
13df9f95 | 323 | // detach event handler |
25c2c5de | 324 | $(that.$refs.upload).off('change'); |
25c2c5de | 325 | uploadFiles(that.$refs.upload.files || []); |
13df9f95 JZ |
326 | }); |
327 | ||
328 | // reset the form first to make the change handler retrigger even on the same file selected | |
25c2c5de JZ |
329 | this.$refs.upload.value = ''; |
330 | this.$refs.upload.click(); | |
13df9f95 | 331 | }, |
7c36adbb JZ |
332 | onUploadFolder: function () { |
333 | var that = this; | |
334 | ||
335 | $(this.$refs.uploadFolder).on('change', function () { | |
336 | // detach event handler | |
337 | $(that.$refs.uploadFolder).off('change'); | |
338 | uploadFiles(that.$refs.uploadFolder.files || []); | |
339 | }); | |
340 | ||
341 | // reset the form first to make the change handler retrigger even on the same file selected | |
342 | this.$refs.uploadFolder.value = ''; | |
343 | this.$refs.uploadFolder.click(); | |
344 | }, | |
13df9f95 | 345 | onDelete: function (entry) { |
25c2c5de JZ |
346 | var that = this; |
347 | ||
13df9f95 JZ |
348 | var title = 'Really delete ' + (entry.isDirectory ? 'folder ' : '') + entry.filePath; |
349 | this.$confirm('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No' }).then(function () { | |
25c2c5de | 350 | var path = encode(sanitize(that.path + '/' + entry.filePath)); |
13df9f95 JZ |
351 | |
352 | superagent.del('/api/files' + path).query({ access_token: localStorage.accessToken, recursive: true }).end(function (error, result) { | |
353 | if (result && result.statusCode === 401) return logout(); | |
25c2c5de JZ |
354 | if (result && result.statusCode !== 200) return that.$message.error('Error deleting file: ' + result.statusCode); |
355 | if (error) return that.$message.error(error.message); | |
13df9f95 JZ |
356 | |
357 | refresh(); | |
358 | }); | |
09d1f4f5 | 359 | }).catch(function () {}); |
13df9f95 JZ |
360 | }, |
361 | onRename: function (entry) { | |
25c2c5de JZ |
362 | var that = this; |
363 | ||
13df9f95 JZ |
364 | var title = 'Rename ' + entry.filePath; |
365 | this.$prompt('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No', inputPlaceholder: 'new filename', inputValue: entry.filePath }).then(function (data) { | |
25c2c5de JZ |
366 | var path = encode(sanitize(that.path + '/' + entry.filePath)); |
367 | var newFilePath = sanitize(that.path + '/' + data.value); | |
13df9f95 JZ |
368 | |
369 | superagent.put('/api/files' + path).query({ access_token: localStorage.accessToken }).send({ newFilePath: newFilePath }).end(function (error, result) { | |
370 | if (result && result.statusCode === 401) return logout(); | |
25c2c5de JZ |
371 | if (result && result.statusCode !== 200) return that.$message.error('Error renaming file: ' + result.statusCode); |
372 | if (error) return that.$message.error(error.message); | |
13df9f95 JZ |
373 | |
374 | refresh(); | |
375 | }); | |
09d1f4f5 | 376 | }).catch(function () {}); |
13df9f95 JZ |
377 | }, |
378 | onNewFolder: function () { | |
25c2c5de JZ |
379 | var that = this; |
380 | ||
13df9f95 JZ |
381 | var title = 'Create New Folder'; |
382 | this.$prompt('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No', inputPlaceholder: 'new foldername' }).then(function (data) { | |
25c2c5de | 383 | var path = encode(sanitize(that.path + '/' + data.value)); |
13df9f95 JZ |
384 | |
385 | superagent.post('/api/files' + path).query({ access_token: localStorage.accessToken, directory: true }).end(function (error, result) { | |
386 | if (result && result.statusCode === 401) return logout(); | |
25c2c5de JZ |
387 | if (result && result.statusCode === 403) return that.$message.error('Folder name not allowed'); |
388 | if (result && result.statusCode === 409) return that.$message.error('Folder already exists'); | |
389 | if (result && result.statusCode !== 201) return that.$message.error('Error creating folder: ' + result.statusCode); | |
390 | if (error) return that.$message.error(error.message); | |
13df9f95 JZ |
391 | |
392 | refresh(); | |
393 | }); | |
09d1f4f5 | 394 | }).catch(function () {}); |
13df9f95 JZ |
395 | }, |
396 | prettyDate: function (row, column, cellValue, index) { | |
397 | var date = new Date(cellValue), | |
398 | diff = (((new Date()).getTime() - date.getTime()) / 1000), | |
399 | day_diff = Math.floor(diff / 86400); | |
400 | ||
401 | if (isNaN(day_diff) || day_diff < 0) | |
402 | return; | |
403 | ||
404 | return day_diff === 0 && ( | |
405 | diff < 60 && 'just now' || | |
406 | diff < 120 && '1 minute ago' || | |
407 | diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' || | |
408 | diff < 7200 && '1 hour ago' || | |
409 | diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') || | |
410 | day_diff === 1 && 'Yesterday' || | |
411 | day_diff < 7 && day_diff + ' days ago' || | |
412 | day_diff < 31 && Math.ceil( day_diff / 7 ) + ' weeks ago' || | |
413 | day_diff < 365 && Math.round( day_diff / 30 ) + ' months ago' || | |
414 | Math.round( day_diff / 365 ) + ' years ago'; | |
415 | }, | |
416 | prettyFileSize: function (row, column, cellValue, index) { | |
417 | return filesize(cellValue); | |
418 | }, | |
d3312ed1 | 419 | loadDirectory: loadDirectory, |
f8693af1 | 420 | onUp: function () { |
25c2c5de | 421 | window.location.hash = sanitize(this.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); |
f8693af1 | 422 | }, |
13df9f95 | 423 | open: open, |
b094fada JZ |
424 | drop: drop, |
425 | dragOver: dragOver | |
6eb72d64 JZ |
426 | } |
427 | }); | |
428 | ||
906f293e JZ |
429 | getProfile(localStorage.accessToken, function (error) { |
430 | if (error) return console.error(error); | |
431 | ||
4dce7a3d | 432 | loadDirectory(decode(window.location.hash.slice(1))); |
906f293e | 433 | }); |
6eb72d64 | 434 | |
04bc2989 | 435 | $(window).on('hashchange', function () { |
4dce7a3d | 436 | loadDirectory(decode(window.location.hash.slice(1))); |
04bc2989 JZ |
437 | }); |
438 | ||
6eb72d64 | 439 | })(); |