]>
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 JZ |
21 | function getProfile(accessToken, callback) { |
22 | callback = callback || function (error) { if (error) console.error(error); }; | |
23 | ||
24 | superagent.get('/api/profile').query({ access_token: accessToken }).end(function (error, result) { | |
25 | app.busy = false; | |
13df9f95 | 26 | app.ready = true; |
4a27fce7 JZ |
27 | |
28 | if (error && !error.response) return callback(error); | |
29 | if (result.statusCode !== 200) { | |
30 | delete localStorage.accessToken; | |
31 | return callback('Invalid access token'); | |
32 | } | |
33 | ||
34 | localStorage.accessToken = accessToken; | |
35 | app.session.username = result.body.username; | |
36 | app.session.valid = true; | |
37 | ||
38 | callback(); | |
39 | }); | |
40 | } | |
41 | ||
d3312ed1 JZ |
42 | function sanitize(filePath) { |
43 | filePath = '/' + filePath; | |
44 | return filePath.replace(/\/+/g, '/'); | |
45 | } | |
46 | ||
537bfb04 JZ |
47 | function encode(filePath) { |
48 | return filePath.split('/').map(encodeURIComponent).join('/'); | |
49 | } | |
50 | ||
04bc2989 JZ |
51 | function decode(filePath) { |
52 | return filePath.split('/').map(decodeURIComponent).join('/'); | |
53 | } | |
54 | ||
a26d1f9b JZ |
55 | var mimeTypes = { |
56 | images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ], | |
57 | text: [ '.txt', '.md' ], | |
58 | pdf: [ '.pdf' ], | |
59 | html: [ '.html', '.htm', '.php' ], | |
60 | video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv' ] | |
61 | }; | |
62 | ||
63 | function getPreviewUrl(entry, basePath) { | |
64 | var path = '/_admin/img/'; | |
65 | ||
66 | if (entry.isDirectory) return path + 'directory.png'; | |
67 | if (mimeTypes.images.some(function (e) { return entry.filePath.endsWith(e); })) return sanitize(basePath + '/' + entry.filePath); | |
68 | if (mimeTypes.text.some(function (e) { return entry.filePath.endsWith(e); })) return path +'text.png'; | |
69 | if (mimeTypes.pdf.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'pdf.png'; | |
70 | if (mimeTypes.html.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'html.png'; | |
71 | if (mimeTypes.video.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'video.png'; | |
72 | ||
73 | return path + 'unknown.png'; | |
74 | } | |
75 | ||
8a3d0eee JZ |
76 | // simple extension detection, does not work with double extension like .tar.gz |
77 | function getExtension(entry) { | |
78 | if (entry.isFile) return entry.filePath.slice(entry.filePath.lastIndexOf('.') + 1); | |
79 | return ''; | |
80 | } | |
81 | ||
537bfb04 JZ |
82 | function refresh() { |
83 | loadDirectory(app.path); | |
84 | } | |
85 | ||
d3312ed1 JZ |
86 | function loadDirectory(filePath) { |
87 | app.busy = true; | |
88 | ||
89 | filePath = filePath ? sanitize(filePath) : '/'; | |
90 | ||
4a27fce7 | 91 | superagent.get('/api/files/' + encode(filePath)).query({ access_token: localStorage.accessToken }).end(function (error, result) { |
d3312ed1 JZ |
92 | app.busy = false; |
93 | ||
9209abec | 94 | if (result && result.statusCode === 401) return logout(); |
d3312ed1 | 95 | if (error) return console.error(error); |
d3312ed1 | 96 | |
04bc2989 | 97 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; }); |
a26d1f9b JZ |
98 | app.entries = result.body.entries.map(function (entry) { |
99 | entry.previewUrl = getPreviewUrl(entry, filePath); | |
8a3d0eee | 100 | entry.extension = getExtension(entry); |
a26d1f9b JZ |
101 | return entry; |
102 | }); | |
d3312ed1 | 103 | app.path = filePath; |
51723cdf J |
104 | app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }).map(function (e, i, a) { |
105 | return { | |
106 | name: e, | |
107 | link: '#' + sanitize('/' + a.slice(0, i).join('/') + '/' + e) | |
108 | }; | |
109 | }); | |
04bc2989 JZ |
110 | |
111 | // update in case this was triggered from code | |
112 | window.location.hash = app.path; | |
d3312ed1 JZ |
113 | }); |
114 | } | |
115 | ||
13df9f95 JZ |
116 | function open(row, event, column) { |
117 | var path = sanitize(app.path + '/' + row.filePath); | |
d3312ed1 | 118 | |
13df9f95 | 119 | if (row.isDirectory) { |
04bc2989 JZ |
120 | window.location.hash = path; |
121 | return; | |
122 | } | |
d3312ed1 | 123 | |
5f43935e | 124 | window.open(encode(path)); |
d3312ed1 JZ |
125 | } |
126 | ||
b094fada JZ |
127 | function uploadFiles(files) { |
128 | if (!files || !files.length) return; | |
129 | ||
d0803a04 JZ |
130 | app.uploadStatus.busy = true; |
131 | app.uploadStatus.count = files.length; | |
132 | app.uploadStatus.done = 0; | |
133 | app.uploadStatus.percentDone = 0; | |
b094fada | 134 | |
d0803a04 | 135 | asyncForEach(files, function (file, callback) { |
b094fada JZ |
136 | var path = encode(sanitize(app.path + '/' + file.name)); |
137 | ||
138 | var formData = new FormData(); | |
139 | formData.append('file', file); | |
140 | ||
4a27fce7 | 141 | superagent.post('/api/files' + path).query({ access_token: localStorage.accessToken }).send(formData).end(function (error, result) { |
b094fada | 142 | if (result && result.statusCode === 401) return logout(); |
d0803a04 JZ |
143 | if (result && result.statusCode !== 201) return callback('Error uploading file: ', result.statusCode); |
144 | if (error) return callback(error); | |
b094fada JZ |
145 | |
146 | app.uploadStatus.done += 1; | |
147 | app.uploadStatus.percentDone = Math.round(app.uploadStatus.done / app.uploadStatus.count * 100); | |
148 | ||
d0803a04 | 149 | callback(); |
b094fada | 150 | }); |
d0803a04 JZ |
151 | }, function (error) { |
152 | if (error) console.error(error); | |
b094fada | 153 | |
d0803a04 JZ |
154 | app.uploadStatus.busy = false; |
155 | app.uploadStatus.count = 0; | |
156 | app.uploadStatus.done = 0; | |
157 | app.uploadStatus.percentDone = 100; | |
158 | ||
159 | refresh(); | |
160 | }); | |
b094fada JZ |
161 | } |
162 | ||
b094fada JZ |
163 | function dragOver(event) { |
164 | event.preventDefault(); | |
165 | } | |
166 | ||
167 | function drop(event) { | |
168 | event.preventDefault(); | |
169 | uploadFiles(event.dataTransfer.files || []); | |
170 | } | |
171 | ||
6eb72d64 JZ |
172 | var app = new Vue({ |
173 | el: '#app', | |
174 | data: { | |
13df9f95 | 175 | ready: false, |
6eb72d64 | 176 | busy: true, |
fea6789c JZ |
177 | uploadStatus: { |
178 | busy: false, | |
179 | count: 0, | |
180 | done: 0, | |
181 | percentDone: 50 | |
182 | }, | |
d3312ed1 JZ |
183 | path: '/', |
184 | pathParts: [], | |
6eb72d64 JZ |
185 | session: { |
186 | valid: false | |
187 | }, | |
13df9f95 JZ |
188 | folderListingEnabled: false, |
189 | loginData: { | |
190 | username: '', | |
191 | password: '' | |
e628921a | 192 | }, |
d3312ed1 | 193 | entries: [] |
6eb72d64 JZ |
194 | }, |
195 | methods: { | |
13df9f95 JZ |
196 | onLogin: function () { |
197 | app.busy = true; | |
198 | ||
199 | superagent.post('/api/login').send({ username: app.loginData.username, password: app.loginData.password }).end(function (error, result) { | |
200 | app.busy = false; | |
201 | ||
202 | if (error) return console.error(error); | |
203 | if (result.statusCode === 401) return console.error('Invalid credentials'); | |
204 | ||
205 | getProfile(result.body.accessToken, function (error) { | |
206 | if (error) return console.error(error); | |
207 | ||
208 | loadDirectory(window.location.hash.slice(1)); | |
209 | }); | |
210 | }); | |
211 | }, | |
212 | onOptionsMenu: function (command) { | |
213 | if (command === 'folderListing') { | |
214 | console.log('Not implemented'); | |
215 | } else if (command === 'about') { | |
216 | this.$msgbox({ | |
217 | title: 'About Surfer', | |
218 | 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>.', | |
219 | dangerouslyUseHTMLString: true, | |
220 | confirmButtonText: 'OK', | |
221 | showCancelButton: false, | |
222 | type: 'info', | |
223 | center: true | |
224 | }).then(function () {}).catch(function () {}); | |
225 | } else if (command === 'logout') { | |
226 | superagent.post('/api/logout').query({ access_token: localStorage.accessToken }).end(function (error) { | |
227 | if (error) console.error(error); | |
228 | ||
229 | app.session.valid = false; | |
230 | ||
231 | delete localStorage.accessToken; | |
232 | }); | |
233 | } | |
234 | }, | |
235 | onDownload: function (entry) { | |
236 | if (entry.isDirectory) return; | |
237 | window.location.href = encode('/api/files/' + sanitize(app.path + '/' + entry.filePath)) + '?access_token=' + localStorage.accessToken; | |
238 | }, | |
239 | onUpload: function () { | |
240 | $(app.$refs.upload).on('change', function () { | |
241 | ||
242 | // detach event handler | |
243 | $(app.$refs.upload).off('change'); | |
244 | ||
245 | uploadFiles(app.$refs.upload.files || []); | |
246 | }); | |
247 | ||
248 | // reset the form first to make the change handler retrigger even on the same file selected | |
249 | app.$refs.upload.value = ''; | |
250 | app.$refs.upload.click(); | |
251 | }, | |
252 | onDelete: function (entry) { | |
253 | var title = 'Really delete ' + (entry.isDirectory ? 'folder ' : '') + entry.filePath; | |
254 | this.$confirm('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No' }).then(function () { | |
255 | var path = encode(sanitize(app.path + '/' + entry.filePath)); | |
256 | ||
257 | superagent.del('/api/files' + path).query({ access_token: localStorage.accessToken, recursive: true }).end(function (error, result) { | |
258 | if (result && result.statusCode === 401) return logout(); | |
259 | if (result && result.statusCode !== 200) return console.error('Error deleting file: ', result.statusCode); | |
260 | if (error) return console.error(error); | |
261 | ||
262 | refresh(); | |
263 | }); | |
264 | }).catch(function () { | |
265 | console.log('delete error:', arguments); | |
266 | }); | |
267 | }, | |
268 | onRename: function (entry) { | |
269 | var title = 'Rename ' + entry.filePath; | |
270 | this.$prompt('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No', inputPlaceholder: 'new filename', inputValue: entry.filePath }).then(function (data) { | |
271 | var path = encode(sanitize(app.path + '/' + entry.filePath)); | |
272 | var newFilePath = sanitize(app.path + '/' + data.value); | |
273 | ||
274 | superagent.put('/api/files' + path).query({ access_token: localStorage.accessToken }).send({ newFilePath: newFilePath }).end(function (error, result) { | |
275 | if (result && result.statusCode === 401) return logout(); | |
276 | if (result && result.statusCode !== 200) return console.error('Error renaming file: ', result.statusCode); | |
277 | if (error) return console.error(error); | |
278 | ||
279 | refresh(); | |
280 | }); | |
281 | }).catch(function () { | |
282 | console.log('rename error:', arguments); | |
283 | }); | |
284 | }, | |
285 | onNewFolder: function () { | |
286 | var title = 'Create New Folder'; | |
287 | this.$prompt('', title, { confirmButtonText: 'Yes', cancelButtonText: 'No', inputPlaceholder: 'new foldername' }).then(function (data) { | |
288 | var path = encode(sanitize(app.path + '/' + data.value)); | |
289 | ||
290 | superagent.post('/api/files' + path).query({ access_token: localStorage.accessToken, directory: true }).end(function (error, result) { | |
291 | if (result && result.statusCode === 401) return logout(); | |
292 | if (result && result.statusCode === 403) return console.error('Name not allowed'); | |
293 | if (result && result.statusCode === 409) return console.error('Directory already exists'); | |
294 | if (result && result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode); | |
295 | if (error) return console.error(error); | |
296 | ||
297 | refresh(); | |
298 | }); | |
299 | }).catch(function () { | |
300 | console.log('create folder error:', arguments); | |
301 | }); | |
302 | }, | |
303 | prettyDate: function (row, column, cellValue, index) { | |
304 | var date = new Date(cellValue), | |
305 | diff = (((new Date()).getTime() - date.getTime()) / 1000), | |
306 | day_diff = Math.floor(diff / 86400); | |
307 | ||
308 | if (isNaN(day_diff) || day_diff < 0) | |
309 | return; | |
310 | ||
311 | return day_diff === 0 && ( | |
312 | diff < 60 && 'just now' || | |
313 | diff < 120 && '1 minute ago' || | |
314 | diff < 3600 && Math.floor( diff / 60 ) + ' minutes ago' || | |
315 | diff < 7200 && '1 hour ago' || | |
316 | diff < 86400 && Math.floor( diff / 3600 ) + ' hours ago') || | |
317 | day_diff === 1 && 'Yesterday' || | |
318 | day_diff < 7 && day_diff + ' days ago' || | |
319 | day_diff < 31 && Math.ceil( day_diff / 7 ) + ' weeks ago' || | |
320 | day_diff < 365 && Math.round( day_diff / 30 ) + ' months ago' || | |
321 | Math.round( day_diff / 365 ) + ' years ago'; | |
322 | }, | |
323 | prettyFileSize: function (row, column, cellValue, index) { | |
324 | return filesize(cellValue); | |
325 | }, | |
d3312ed1 | 326 | loadDirectory: loadDirectory, |
f8693af1 JZ |
327 | onUp: function () { |
328 | window.location.hash = sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); | |
329 | }, | |
13df9f95 | 330 | open: open, |
b094fada JZ |
331 | drop: drop, |
332 | dragOver: dragOver | |
6eb72d64 JZ |
333 | } |
334 | }); | |
335 | ||
906f293e JZ |
336 | getProfile(localStorage.accessToken, function (error) { |
337 | if (error) return console.error(error); | |
338 | ||
339 | loadDirectory(window.location.hash.slice(1)); | |
340 | }); | |
6eb72d64 | 341 | |
04bc2989 JZ |
342 | $(window).on('hashchange', function () { |
343 | loadDirectory(window.location.hash.slice(1)); | |
344 | }); | |
345 | ||
6eb72d64 | 346 | })(); |