]>
Commit | Line | Data |
---|---|---|
6eb72d64 JZ |
1 | (function () { |
2 | 'use strict'; | |
3 | ||
d0803a04 JZ |
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 | ||
4a27fce7 | 21 | function getProfile(accessToken, callback) { |
4a27fce7 | 22 | superagent.get('/api/profile').query({ access_token: accessToken }).end(function (error, result) { |
13df9f95 | 23 | app.ready = true; |
4a27fce7 JZ |
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 | ||
f43b3c16 JZ |
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 | }); | |
4a27fce7 JZ |
42 | }); |
43 | } | |
44 | ||
d3312ed1 JZ |
45 | function sanitize(filePath) { |
46 | filePath = '/' + filePath; | |
47 | return filePath.replace(/\/+/g, '/'); | |
48 | } | |
49 | ||
537bfb04 JZ |
50 | function encode(filePath) { |
51 | return filePath.split('/').map(encodeURIComponent).join('/'); | |
52 | } | |
53 | ||
04bc2989 JZ |
54 | function decode(filePath) { |
55 | return filePath.split('/').map(decodeURIComponent).join('/'); | |
56 | } | |
57 | ||
a26d1f9b JZ |
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 | ||
8a3d0eee JZ |
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 | ||
537bfb04 JZ |
85 | function refresh() { |
86 | loadDirectory(app.path); | |
87 | } | |
88 | ||
d3312ed1 JZ |
89 | function loadDirectory(filePath) { |
90 | app.busy = true; | |
91 | ||
92 | filePath = filePath ? sanitize(filePath) : '/'; | |
93 | ||
4a27fce7 | 94 | superagent.get('/api/files/' + encode(filePath)).query({ access_token: localStorage.accessToken }).end(function (error, result) { |
d3312ed1 JZ |
95 | app.busy = false; |
96 | ||
9209abec | 97 | if (result && result.statusCode === 401) return logout(); |
d3312ed1 | 98 | if (error) return console.error(error); |
d3312ed1 | 99 | |
04bc2989 | 100 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; }); |
a26d1f9b JZ |
101 | app.entries = result.body.entries.map(function (entry) { |
102 | entry.previewUrl = getPreviewUrl(entry, filePath); | |
8a3d0eee | 103 | entry.extension = getExtension(entry); |
a26d1f9b JZ |
104 | return entry; |
105 | }); | |
d3312ed1 | 106 | app.path = filePath; |
51723cdf J |
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 | }); | |
04bc2989 JZ |
113 | |
114 | // update in case this was triggered from code | |
115 | window.location.hash = app.path; | |
d3312ed1 JZ |
116 | }); |
117 | } | |
118 | ||
13df9f95 JZ |
119 | function open(row, event, column) { |
120 | var path = sanitize(app.path + '/' + row.filePath); | |
d3312ed1 | 121 | |
13df9f95 | 122 | if (row.isDirectory) { |
04bc2989 JZ |
123 | window.location.hash = path; |
124 | return; | |
125 | } | |
d3312ed1 | 126 | |
5f43935e | 127 | window.open(encode(path)); |
d3312ed1 JZ |
128 | } |
129 | ||
b094fada JZ |
130 | function uploadFiles(files) { |
131 | if (!files || !files.length) return; | |
132 | ||
d0803a04 JZ |
133 | app.uploadStatus.busy = true; |
134 | app.uploadStatus.count = files.length; | |
135 | app.uploadStatus.done = 0; | |
136 | app.uploadStatus.percentDone = 0; | |
b094fada | 137 | |
d0803a04 | 138 | asyncForEach(files, function (file, callback) { |
3df9f8a6 JZ |
139 | // do not handle directories (file.type is empty in such a case) |
140 | if (file.type === '') return callback(); | |
141 | ||
35355283 | 142 | var path = encode(sanitize(app.path + '/' + (file.webkitRelativePath || file.name))); |
b094fada JZ |
143 | |
144 | var formData = new FormData(); | |
145 | formData.append('file', file); | |
146 | ||
4a27fce7 | 147 | superagent.post('/api/files' + path).query({ access_token: localStorage.accessToken }).send(formData).end(function (error, result) { |
b094fada | 148 | if (result && result.statusCode === 401) return logout(); |
d0803a04 JZ |
149 | if (result && result.statusCode !== 201) return callback('Error uploading file: ', result.statusCode); |
150 | if (error) return callback(error); | |
b094fada JZ |
151 | |
152 | app.uploadStatus.done += 1; | |
153 | app.uploadStatus.percentDone = Math.round(app.uploadStatus.done / app.uploadStatus.count * 100); | |
154 | ||
d0803a04 | 155 | callback(); |
b094fada | 156 | }); |
d0803a04 JZ |
157 | }, function (error) { |
158 | if (error) console.error(error); | |
b094fada | 159 | |
d0803a04 JZ |
160 | app.uploadStatus.busy = false; |
161 | app.uploadStatus.count = 0; | |
162 | app.uploadStatus.done = 0; | |
163 | app.uploadStatus.percentDone = 100; | |
164 | ||
165 | refresh(); | |
166 | }); | |
b094fada JZ |
167 | } |
168 | ||
b094fada | 169 | function dragOver(event) { |
19efa5bc | 170 | event.stopPropagation(); |
b094fada | 171 | event.preventDefault(); |
19efa5bc | 172 | event.dataTransfer.dropEffect = 'copy'; |
b094fada JZ |
173 | } |
174 | ||
175 | function drop(event) { | |
19efa5bc | 176 | event.stopPropagation(); |
b094fada JZ |
177 | event.preventDefault(); |
178 | uploadFiles(event.dataTransfer.files || []); | |
179 | } | |
180 | ||
6eb72d64 JZ |
181 | var app = new Vue({ |
182 | el: '#app', | |
183 | data: { | |
13df9f95 | 184 | ready: false, |
25c2c5de | 185 | busy: false, |
fea6789c JZ |
186 | uploadStatus: { |
187 | busy: false, | |
188 | count: 0, | |
189 | done: 0, | |
190 | percentDone: 50 | |
191 | }, | |
d3312ed1 JZ |
192 | path: '/', |
193 | pathParts: [], | |
6eb72d64 JZ |
194 | session: { |
195 | valid: false | |
196 | }, | |
13df9f95 JZ |
197 | folderListingEnabled: false, |
198 | loginData: { | |
199 | username: '', | |
25c2c5de JZ |
200 | password: '', |
201 | busy: false | |
e628921a | 202 | }, |
d3312ed1 | 203 | entries: [] |
6eb72d64 JZ |
204 | }, |
205 | methods: { | |
13df9f95 | 206 | onLogin: function () { |
25c2c5de | 207 | var that = this; |
13df9f95 | 208 | |
25c2c5de | 209 | that.loginData.busy = true; |
13df9f95 | 210 | |
25c2c5de JZ |
211 | superagent.post('/api/login').send({ username: that.loginData.username, password: that.loginData.password }).end(function (error, result) { |
212 | that.loginData.busy = false; | |
213 | ||
214 | if (error && !result) return that.$message.error(error.message); | |
215 | if (result.statusCode === 401) return that.$message.error('Wrong username or password'); | |
13df9f95 JZ |
216 | |
217 | getProfile(result.body.accessToken, function (error) { | |
218 | if (error) return console.error(error); | |
219 | ||
220 | loadDirectory(window.location.hash.slice(1)); | |
221 | }); | |
222 | }); | |
223 | }, | |
224 | onOptionsMenu: function (command) { | |
25c2c5de JZ |
225 | var that = this; |
226 | ||
13df9f95 | 227 | if (command === 'folderListing') { |
552d44bb JZ |
228 | superagent.put('/api/settings').send({ folderListingEnabled: this.folderListingEnabled }).query({ access_token: localStorage.accessToken }).end(function (error) { |
229 | if (error) console.error(error); | |
230 | }); | |
13df9f95 JZ |
231 | } else if (command === 'about') { |
232 | this.$msgbox({ | |
233 | title: 'About Surfer', | |
234 | 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>.', | |
235 | dangerouslyUseHTMLString: true, | |
236 | confirmButtonText: 'OK', | |
237 | showCancelButton: false, | |
238 | type: 'info', | |
239 | center: true | |
240 | }).then(function () {}).catch(function () {}); | |
241 | } else if (command === 'logout') { | |
242 | superagent.post('/api/logout').query({ access_token: localStorage.accessToken }).end(function (error) { | |
243 | if (error) console.error(error); | |
244 | ||
25c2c5de | 245 | that.session.valid = false; |
13df9f95 JZ |
246 | |
247 | delete localStorage.accessToken; | |
248 | }); | |
249 | } | |
250 | }, | |
251 | onDownload: function (entry) { | |
252 | if (entry.isDirectory) return; | |
25c2c5de | 253 | window.location.href = encode('/api/files/' + sanitize(this.path + '/' + entry.filePath)) + '?access_token=' + localStorage.accessToken; |
13df9f95 JZ |
254 | }, |
255 | onUpload: function () { | |
25c2c5de JZ |
256 | var that = this; |
257 | ||
258 | $(this.$refs.upload).on('change', function () { | |
13df9f95 | 259 | // detach event handler |
25c2c5de | 260 | $(that.$refs.upload).off('change'); |
25c2c5de | 261 | uploadFiles(that.$refs.upload.files || []); |
13df9f95 JZ |
262 | }); |
263 | ||
264 | // reset the form first to make the change handler retrigger even on the same file selected | |
25c2c5de JZ |
265 | this.$refs.upload.value = ''; |
266 | this.$refs.upload.click(); | |
13df9f95 | 267 | }, |
7c36adbb JZ |
268 | onUploadFolder: function () { |
269 | var that = this; | |
270 | ||
271 | $(this.$refs.uploadFolder).on('change', function () { | |
272 | // detach event handler | |
273 | $(that.$refs.uploadFolder).off('change'); | |
274 | uploadFiles(that.$refs.uploadFolder.files || []); | |
275 | }); | |
276 | ||
277 | // reset the form first to make the change handler retrigger even on the same file selected | |
278 | this.$refs.uploadFolder.value = ''; | |
279 | this.$refs.uploadFolder.click(); | |
280 | }, | |
13df9f95 | 281 | onDelete: function (entry) { |
25c2c5de JZ |
282 | var that = this; |
283 | ||
13df9f95 JZ |
284 | var title = 'Really delete ' + (entry.isDirectory ? 'folder ' : '') + entry.filePath; |
285 | this.$confirm('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No' }).then(function () { | |
25c2c5de | 286 | var path = encode(sanitize(that.path + '/' + entry.filePath)); |
13df9f95 JZ |
287 | |
288 | superagent.del('/api/files' + path).query({ access_token: localStorage.accessToken, recursive: true }).end(function (error, result) { | |
289 | if (result && result.statusCode === 401) return logout(); | |
25c2c5de JZ |
290 | if (result && result.statusCode !== 200) return that.$message.error('Error deleting file: ' + result.statusCode); |
291 | if (error) return that.$message.error(error.message); | |
13df9f95 JZ |
292 | |
293 | refresh(); | |
294 | }); | |
09d1f4f5 | 295 | }).catch(function () {}); |
13df9f95 JZ |
296 | }, |
297 | onRename: function (entry) { | |
25c2c5de JZ |
298 | var that = this; |
299 | ||
13df9f95 JZ |
300 | var title = 'Rename ' + entry.filePath; |
301 | this.$prompt('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No', inputPlaceholder: 'new filename', inputValue: entry.filePath }).then(function (data) { | |
25c2c5de JZ |
302 | var path = encode(sanitize(that.path + '/' + entry.filePath)); |
303 | var newFilePath = sanitize(that.path + '/' + data.value); | |
13df9f95 JZ |
304 | |
305 | superagent.put('/api/files' + path).query({ access_token: localStorage.accessToken }).send({ newFilePath: newFilePath }).end(function (error, result) { | |
306 | if (result && result.statusCode === 401) return logout(); | |
25c2c5de JZ |
307 | if (result && result.statusCode !== 200) return that.$message.error('Error renaming file: ' + result.statusCode); |
308 | if (error) return that.$message.error(error.message); | |
13df9f95 JZ |
309 | |
310 | refresh(); | |
311 | }); | |
09d1f4f5 | 312 | }).catch(function () {}); |
13df9f95 JZ |
313 | }, |
314 | onNewFolder: function () { | |
25c2c5de JZ |
315 | var that = this; |
316 | ||
13df9f95 JZ |
317 | var title = 'Create New Folder'; |
318 | this.$prompt('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No', inputPlaceholder: 'new foldername' }).then(function (data) { | |
25c2c5de | 319 | var path = encode(sanitize(that.path + '/' + data.value)); |
13df9f95 JZ |
320 | |
321 | superagent.post('/api/files' + path).query({ access_token: localStorage.accessToken, directory: true }).end(function (error, result) { | |
322 | if (result && result.statusCode === 401) return logout(); | |
25c2c5de JZ |
323 | if (result && result.statusCode === 403) return that.$message.error('Folder name not allowed'); |
324 | if (result && result.statusCode === 409) return that.$message.error('Folder already exists'); | |
325 | if (result && result.statusCode !== 201) return that.$message.error('Error creating folder: ' + result.statusCode); | |
326 | if (error) return that.$message.error(error.message); | |
13df9f95 JZ |
327 | |
328 | refresh(); | |
329 | }); | |
09d1f4f5 | 330 | }).catch(function () {}); |
13df9f95 JZ |
331 | }, |
332 | prettyDate: function (row, column, cellValue, index) { | |
333 | var date = new Date(cellValue), | |
334 | diff = (((new Date()).getTime() - date.getTime()) / 1000), | |
335 | day_diff = Math.floor(diff / 86400); | |
336 | ||
337 | if (isNaN(day_diff) || day_diff < 0) | |
338 | return; | |
339 | ||
340 | return day_diff === 0 && ( | |
341 | diff < 60 && 'just now' || | |
342 | diff < 120 && '1 minute ago' || | |
343 | diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' || | |
344 | diff < 7200 && '1 hour ago' || | |
345 | diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') || | |
346 | day_diff === 1 && 'Yesterday' || | |
347 | day_diff < 7 && day_diff + ' days ago' || | |
348 | day_diff < 31 && Math.ceil( day_diff / 7 ) + ' weeks ago' || | |
349 | day_diff < 365 && Math.round( day_diff / 30 ) + ' months ago' || | |
350 | Math.round( day_diff / 365 ) + ' years ago'; | |
351 | }, | |
352 | prettyFileSize: function (row, column, cellValue, index) { | |
353 | return filesize(cellValue); | |
354 | }, | |
d3312ed1 | 355 | loadDirectory: loadDirectory, |
f8693af1 | 356 | onUp: function () { |
25c2c5de | 357 | window.location.hash = sanitize(this.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); |
f8693af1 | 358 | }, |
13df9f95 | 359 | open: open, |
b094fada JZ |
360 | drop: drop, |
361 | dragOver: dragOver | |
6eb72d64 JZ |
362 | } |
363 | }); | |
364 | ||
906f293e JZ |
365 | getProfile(localStorage.accessToken, function (error) { |
366 | if (error) return console.error(error); | |
367 | ||
368 | loadDirectory(window.location.hash.slice(1)); | |
369 | }); | |
6eb72d64 | 370 | |
04bc2989 JZ |
371 | $(window).on('hashchange', function () { |
372 | loadDirectory(window.location.hash.slice(1)); | |
373 | }); | |
374 | ||
6eb72d64 | 375 | })(); |