]>
Commit | Line | Data |
---|---|---|
1 | (function () { | |
2 | 'use strict'; | |
3 | ||
4 | function login(username, password) { | |
5 | username = username || app.loginData.username; | |
6 | password = password || app.loginData.password; | |
7 | ||
8 | app.busy = true; | |
9 | ||
10 | superagent.get('/api/files/').query({ username: username, password: password }).end(function (error, result) { | |
11 | app.busy = false; | |
12 | ||
13 | if (error) return console.error(error); | |
14 | if (result.statusCode === 401) return console.error('Invalid credentials'); | |
15 | ||
16 | app.session.valid = true; | |
17 | app.session.username = username; | |
18 | app.session.password = password; | |
19 | ||
20 | // clearly not the best option | |
21 | localStorage.username = username; | |
22 | localStorage.password = password; | |
23 | ||
24 | loadDirectory(window.location.hash.slice(1)); | |
25 | }); | |
26 | } | |
27 | ||
28 | function logout() { | |
29 | app.session.valid = false; | |
30 | app.session.username = null; | |
31 | app.session.password = null; | |
32 | ||
33 | delete localStorage.username; | |
34 | delete localStorage.password; | |
35 | } | |
36 | ||
37 | function sanitize(filePath) { | |
38 | filePath = '/' + filePath; | |
39 | return filePath.replace(/\/+/g, '/'); | |
40 | } | |
41 | ||
42 | function encode(filePath) { | |
43 | return filePath.split('/').map(encodeURIComponent).join('/'); | |
44 | } | |
45 | ||
46 | function decode(filePath) { | |
47 | return filePath.split('/').map(decodeURIComponent).join('/'); | |
48 | } | |
49 | ||
50 | var mimeTypes = { | |
51 | images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ], | |
52 | text: [ '.txt', '.md' ], | |
53 | pdf: [ '.pdf' ], | |
54 | html: [ '.html', '.htm', '.php' ], | |
55 | video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv' ] | |
56 | }; | |
57 | ||
58 | function getPreviewUrl(entry, basePath) { | |
59 | var path = '/_admin/img/'; | |
60 | ||
61 | if (entry.isDirectory) return path + 'directory.png'; | |
62 | if (mimeTypes.images.some(function (e) { return entry.filePath.endsWith(e); })) return sanitize(basePath + '/' + entry.filePath); | |
63 | if (mimeTypes.text.some(function (e) { return entry.filePath.endsWith(e); })) return path +'text.png'; | |
64 | if (mimeTypes.pdf.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'pdf.png'; | |
65 | if (mimeTypes.html.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'html.png'; | |
66 | if (mimeTypes.video.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'video.png'; | |
67 | ||
68 | return path + 'unknown.png'; | |
69 | } | |
70 | ||
71 | function refresh() { | |
72 | loadDirectory(app.path); | |
73 | } | |
74 | ||
75 | function loadDirectory(filePath) { | |
76 | app.busy = true; | |
77 | ||
78 | filePath = filePath ? sanitize(filePath) : '/'; | |
79 | ||
80 | superagent.get('/api/files/' + encode(filePath)).query({ username: app.session.username, password: app.session.password }).end(function (error, result) { | |
81 | app.busy = false; | |
82 | ||
83 | if (result && result.statusCode === 401) return logout(); | |
84 | if (error) return console.error(error); | |
85 | ||
86 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; }); | |
87 | app.entries = result.body.entries.map(function (entry) { | |
88 | entry.previewUrl = getPreviewUrl(entry, filePath); | |
89 | return entry; | |
90 | }); | |
91 | app.path = filePath; | |
92 | app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }).map(function (e, i, a) { | |
93 | return { | |
94 | name: e, | |
95 | link: '#' + sanitize('/' + a.slice(0, i).join('/') + '/' + e) | |
96 | }; | |
97 | }); | |
98 | ||
99 | // update in case this was triggered from code | |
100 | window.location.hash = app.path; | |
101 | ||
102 | Vue.nextTick(function () { | |
103 | $(function () { | |
104 | $('[data-toggle="tooltip"]').tooltip(); | |
105 | }); | |
106 | }); | |
107 | }); | |
108 | } | |
109 | ||
110 | function open(entry) { | |
111 | var path = sanitize(app.path + '/' + entry.filePath); | |
112 | ||
113 | if (entry.isDirectory) { | |
114 | window.location.hash = path; | |
115 | return; | |
116 | } | |
117 | ||
118 | window.open(encode(path)); | |
119 | } | |
120 | ||
121 | function up() { | |
122 | window.location.hash = sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); | |
123 | } | |
124 | ||
125 | function upload() { | |
126 | $(app.$els.upload).on('change', function () { | |
127 | ||
128 | // detach event handler | |
129 | $(app.$els.upload).off('change'); | |
130 | ||
131 | app.uploadStatus = { | |
132 | busy: true, | |
133 | count: app.$els.upload.files.length, | |
134 | done: 0, | |
135 | percentDone: 0 | |
136 | }; | |
137 | ||
138 | function uploadFile(file) { | |
139 | var path = encode(sanitize(app.path + '/' + file.name)); | |
140 | ||
141 | var formData = new FormData(); | |
142 | formData.append('file', file); | |
143 | ||
144 | superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) { | |
145 | if (result && result.statusCode === 401) return logout(); | |
146 | if (result && result.statusCode !== 201) console.error('Error uploading file: ', result.statusCode); | |
147 | if (error) console.error(error); | |
148 | ||
149 | app.uploadStatus.done += 1; | |
150 | app.uploadStatus.percentDone = Math.round(app.uploadStatus.done / app.uploadStatus.count * 100); | |
151 | ||
152 | console.log(Math.round(app.uploadStatus.done / app.uploadStatus.count * 100)) | |
153 | ||
154 | if (app.uploadStatus.done >= app.uploadStatus.count) { | |
155 | app.uploadStatus = { | |
156 | busy: false, | |
157 | count: 0, | |
158 | done: 0, | |
159 | percentDone: 100 | |
160 | }; | |
161 | ||
162 | refresh(); | |
163 | } | |
164 | }); | |
165 | } | |
166 | ||
167 | for(var i = 0; i < app.uploadStatus.count; ++i) { | |
168 | uploadFile(app.$els.upload.files[i]); | |
169 | } | |
170 | }); | |
171 | ||
172 | // reset the form first to make the change handler retrigger even on the same file selected | |
173 | $('#fileUploadForm')[0].reset(); | |
174 | ||
175 | app.$els.upload.click(); | |
176 | } | |
177 | ||
178 | function delAsk(entry) { | |
179 | $('#modalDelete').modal('show'); | |
180 | app.deleteData = entry; | |
181 | } | |
182 | ||
183 | function del(entry) { | |
184 | app.busy = true; | |
185 | ||
186 | var path = encode(sanitize(app.path + '/' + entry.filePath)); | |
187 | ||
188 | superagent.del('/api/files' + path).query({ username: app.session.username, password: app.session.password, recursive: true }).end(function (error, result) { | |
189 | app.busy = false; | |
190 | ||
191 | if (result && result.statusCode === 401) return logout(); | |
192 | if (result && result.statusCode !== 200) return console.error('Error deleting file: ', result.statusCode); | |
193 | if (error) return console.error(error); | |
194 | ||
195 | refresh(); | |
196 | ||
197 | $('#modalDelete').modal('hide'); | |
198 | }); | |
199 | } | |
200 | ||
201 | function createDirectoryAsk() { | |
202 | $('#modalcreateDirectory').modal('show'); | |
203 | app.createDirectoryData = ''; | |
204 | app.createDirectoryError = null; | |
205 | } | |
206 | ||
207 | function createDirectory(name) { | |
208 | app.busy = true; | |
209 | app.createDirectoryError = null; | |
210 | ||
211 | var path = encode(sanitize(app.path + '/' + name)); | |
212 | ||
213 | superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password, directory: true }).end(function (error, result) { | |
214 | app.busy = false; | |
215 | ||
216 | if (result && result.statusCode === 401) return logout(); | |
217 | if (result && result.statusCode === 403) { | |
218 | app.createDirectoryError = 'Name not allowed'; | |
219 | return; | |
220 | } | |
221 | if (result && result.statusCode === 409) { | |
222 | app.createDirectoryError = 'Directory already exists'; | |
223 | return; | |
224 | } | |
225 | if (result && result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode); | |
226 | if (error) return console.error(error); | |
227 | ||
228 | app.createDirectoryData = ''; | |
229 | refresh(); | |
230 | ||
231 | $('#modalcreateDirectory').modal('hide'); | |
232 | }); | |
233 | } | |
234 | ||
235 | Vue.filter('prettyDate', function (value) { | |
236 | var d = new Date(value); | |
237 | return d.toDateString(); | |
238 | }); | |
239 | ||
240 | Vue.filter('prettyFileSize', function (value) { | |
241 | return filesize(value); | |
242 | }); | |
243 | ||
244 | var app = new Vue({ | |
245 | el: '#app', | |
246 | data: { | |
247 | busy: true, | |
248 | uploadStatus: { | |
249 | busy: false, | |
250 | count: 0, | |
251 | done: 0, | |
252 | percentDone: 50 | |
253 | }, | |
254 | path: '/', | |
255 | pathParts: [], | |
256 | session: { | |
257 | valid: false | |
258 | }, | |
259 | loginData: {}, | |
260 | deleteData: {}, | |
261 | createDirectoryData: '', | |
262 | createDirectoryError: null, | |
263 | entries: [] | |
264 | }, | |
265 | methods: { | |
266 | login: login, | |
267 | logout: logout, | |
268 | loadDirectory: loadDirectory, | |
269 | open: open, | |
270 | up: up, | |
271 | upload: upload, | |
272 | delAsk: delAsk, | |
273 | del: del, | |
274 | createDirectoryAsk: createDirectoryAsk, | |
275 | createDirectory: createDirectory | |
276 | } | |
277 | }); | |
278 | ||
279 | window.app = app; | |
280 | ||
281 | login(localStorage.username, localStorage.password); | |
282 | ||
283 | $(window).on('hashchange', function () { | |
284 | loadDirectory(window.location.hash.slice(1)); | |
285 | }); | |
286 | ||
287 | // setup all the dialog focus handling | |
288 | ['modalcreateDirectory'].forEach(function (id) { | |
289 | $('#' + id).on('shown.bs.modal', function () { | |
290 | $(this).find("[autofocus]:first").focus(); | |
291 | }); | |
292 | }); | |
293 | ||
294 | })(); |