]>
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 | console.log(filePath); | |
81 | ||
82 | superagent.get('/api/files/' + filePath).query({ username: app.session.username, password: app.session.password }).end(function (error, result) { | |
83 | app.busy = false; | |
84 | ||
85 | if (result && result.statusCode === 401) return logout(); | |
86 | if (error) return console.error(error); | |
87 | ||
88 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1; }); | |
89 | app.entries = result.body.entries.map(function (entry) { | |
90 | entry.previewUrl = getPreviewUrl(entry, filePath); | |
91 | return entry; | |
92 | }); | |
93 | app.path = filePath; | |
94 | app.pathParts = decode(filePath).split('/').filter(function (e) { return !!e; }); | |
95 | ||
96 | // update in case this was triggered from code | |
97 | window.location.hash = app.path; | |
98 | ||
99 | Vue.nextTick(function () { | |
100 | $(function () { | |
101 | $('[data-toggle="tooltip"]').tooltip(); | |
102 | }); | |
103 | }); | |
104 | }); | |
105 | } | |
106 | ||
107 | function open(entry) { | |
108 | var path = encode(sanitize(app.path + '/' + entry.filePath)); | |
109 | ||
110 | if (entry.isDirectory) { | |
111 | window.location.hash = path; | |
112 | return; | |
113 | } | |
114 | ||
115 | window.open(path); | |
116 | } | |
117 | ||
118 | function up() { | |
119 | window.location.hash = encode(sanitize(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/'))); | |
120 | } | |
121 | ||
122 | function upload() { | |
123 | $(app.$els.upload).on('change', function () { | |
124 | app.busy = true; | |
125 | ||
126 | // detach event handler | |
127 | $(app.$els.upload).off('change'); | |
128 | ||
129 | var file = app.$els.upload.files[0]; | |
130 | var path = encode(sanitize(app.path + '/' + file.name)); | |
131 | ||
132 | var formData = new FormData(); | |
133 | formData.append('file', file); | |
134 | ||
135 | superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) { | |
136 | app.busy = false; | |
137 | ||
138 | if (result && result.statusCode === 401) return logout(); | |
139 | if (result && result.statusCode !== 201) return console.error('Error uploading file: ', result.statusCode); | |
140 | if (error) return console.error(error); | |
141 | ||
142 | refresh(); | |
143 | }); | |
144 | }); | |
145 | ||
146 | app.$els.upload.click(); | |
147 | } | |
148 | ||
149 | function delAsk(entry) { | |
150 | $('#modalDelete').modal('show'); | |
151 | app.deleteData = entry; | |
152 | } | |
153 | ||
154 | function del(entry) { | |
155 | app.busy = true; | |
156 | ||
157 | var path = encode(sanitize(app.path + '/' + entry.filePath)); | |
158 | ||
159 | superagent.del('/api/files' + path).query({ username: app.session.username, password: app.session.password, recursive: true }).end(function (error, result) { | |
160 | app.busy = false; | |
161 | ||
162 | if (result && result.statusCode === 401) return logout(); | |
163 | if (result && result.statusCode !== 200) return console.error('Error deleting file: ', result.statusCode); | |
164 | if (error) return console.error(error); | |
165 | ||
166 | refresh(); | |
167 | ||
168 | $('#modalDelete').modal('hide'); | |
169 | }); | |
170 | } | |
171 | ||
172 | function createDirectoryAsk() { | |
173 | $('#modalcreateDirectory').modal('show'); | |
174 | app.createDirectoryData = ''; | |
175 | app.createDirectoryError = null; | |
176 | } | |
177 | ||
178 | function createDirectory(name) { | |
179 | app.busy = true; | |
180 | app.createDirectoryError = null; | |
181 | ||
182 | var path = encode(sanitize(app.path + '/' + name)); | |
183 | ||
184 | superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password, directory: true }).end(function (error, result) { | |
185 | app.busy = false; | |
186 | ||
187 | if (result && result.statusCode === 401) return logout(); | |
188 | if (result && result.statusCode === 403) { | |
189 | app.createDirectoryError = 'Name not allowed'; | |
190 | return; | |
191 | } | |
192 | if (result && result.statusCode === 409) { | |
193 | app.createDirectoryError = 'Directory already exists'; | |
194 | return; | |
195 | } | |
196 | if (result && result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode); | |
197 | if (error) return console.error(error); | |
198 | ||
199 | app.createDirectoryData = ''; | |
200 | refresh(); | |
201 | ||
202 | $('#modalcreateDirectory').modal('hide'); | |
203 | }); | |
204 | } | |
205 | ||
206 | Vue.filter('prettyDate', function (value) { | |
207 | var d = new Date(value); | |
208 | return d.toDateString(); | |
209 | }); | |
210 | ||
211 | Vue.filter('prettyFileSize', function (value) { | |
212 | return filesize(value); | |
213 | }); | |
214 | ||
215 | var app = new Vue({ | |
216 | el: '#app', | |
217 | data: { | |
218 | busy: true, | |
219 | path: '/', | |
220 | pathParts: [], | |
221 | session: { | |
222 | valid: false | |
223 | }, | |
224 | loginData: {}, | |
225 | deleteData: {}, | |
226 | createDirectoryData: '', | |
227 | createDirectoryError: null, | |
228 | entries: [] | |
229 | }, | |
230 | methods: { | |
231 | login: login, | |
232 | logout: logout, | |
233 | loadDirectory: loadDirectory, | |
234 | open: open, | |
235 | up: up, | |
236 | upload: upload, | |
237 | delAsk: delAsk, | |
238 | del: del, | |
239 | createDirectoryAsk: createDirectoryAsk, | |
240 | createDirectory: createDirectory | |
241 | } | |
242 | }); | |
243 | ||
244 | window.app = app; | |
245 | ||
246 | login(localStorage.username, localStorage.password); | |
247 | ||
248 | $(window).on('hashchange', function () { | |
249 | loadDirectory(window.location.hash.slice(1)); | |
250 | }); | |
251 | ||
252 | // setup all the dialog focus handling | |
253 | ['modalcreateDirectory'].forEach(function (id) { | |
254 | $('#' + id).on('shown.bs.modal', function () { | |
255 | $(this).find("[autofocus]:first").focus(); | |
256 | }); | |
257 | }); | |
258 | ||
259 | })(); |