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