]>
Commit | Line | Data |
---|---|---|
6eb72d64 JZ |
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; | |
d3312ed1 JZ |
23 | |
24 | loadDirectory(app.path); | |
6eb72d64 JZ |
25 | }); |
26 | } | |
27 | ||
28 | function logout() { | |
29 | app.session.valid = false; | |
30 | app.session.username = username; | |
31 | app.session.password = password; | |
32 | ||
33 | delete localStorage.username; | |
34 | delete localStorage.password; | |
35 | } | |
36 | ||
d3312ed1 JZ |
37 | function sanitize(filePath) { |
38 | filePath = '/' + filePath; | |
39 | return filePath.replace(/\/+/g, '/'); | |
40 | } | |
41 | ||
537bfb04 JZ |
42 | function encode(filePath) { |
43 | return filePath.split('/').map(encodeURIComponent).join('/'); | |
44 | } | |
45 | ||
a26d1f9b JZ |
46 | var mimeTypes = { |
47 | images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ], | |
48 | text: [ '.txt', '.md' ], | |
49 | pdf: [ '.pdf' ], | |
50 | html: [ '.html', '.htm', '.php' ], | |
51 | video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv' ] | |
52 | }; | |
53 | ||
54 | function getPreviewUrl(entry, basePath) { | |
55 | var path = '/_admin/img/'; | |
56 | ||
57 | if (entry.isDirectory) return path + 'directory.png'; | |
58 | if (mimeTypes.images.some(function (e) { return entry.filePath.endsWith(e); })) return sanitize(basePath + '/' + entry.filePath); | |
59 | if (mimeTypes.text.some(function (e) { return entry.filePath.endsWith(e); })) return path +'text.png'; | |
60 | if (mimeTypes.pdf.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'pdf.png'; | |
61 | if (mimeTypes.html.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'html.png'; | |
62 | if (mimeTypes.video.some(function (e) { return entry.filePath.endsWith(e); })) return path + 'video.png'; | |
63 | ||
64 | return path + 'unknown.png'; | |
65 | } | |
66 | ||
537bfb04 JZ |
67 | function refresh() { |
68 | loadDirectory(app.path); | |
69 | } | |
70 | ||
d3312ed1 JZ |
71 | function loadDirectory(filePath) { |
72 | app.busy = true; | |
73 | ||
74 | filePath = filePath ? sanitize(filePath) : '/'; | |
75 | ||
76 | console.log(filePath); | |
77 | ||
78 | superagent.get('/api/files/' + filePath).query({ username: app.session.username, password: app.session.password }).end(function (error, result) { | |
79 | app.busy = false; | |
80 | ||
81 | if (error) return console.error(error); | |
82 | if (result.statusCode === 401) return logout(); | |
83 | ||
572e87c9 | 84 | result.body.entries.sort(function (a, b) { return a.isDirectory && b.isFile ? -1 : 1 }); |
a26d1f9b JZ |
85 | app.entries = result.body.entries.map(function (entry) { |
86 | entry.previewUrl = getPreviewUrl(entry, filePath); | |
87 | return entry; | |
88 | }); | |
d3312ed1 JZ |
89 | app.path = filePath; |
90 | app.pathParts = filePath.split('/').filter(function (e) { return !!e; }); | |
3e98fb0c JZ |
91 | |
92 | Vue.nextTick(function () { | |
93 | $(function () { | |
94 | $('[data-toggle="tooltip"]').tooltip(); | |
95 | }); | |
96 | }); | |
d3312ed1 JZ |
97 | }); |
98 | } | |
99 | ||
100 | function open(entry) { | |
101 | var path = sanitize(app.path + '/' + entry.filePath); | |
102 | ||
103 | if (entry.isDirectory) return loadDirectory(path); | |
104 | ||
ee9d1ada | 105 | window.open(path); |
d3312ed1 JZ |
106 | } |
107 | ||
108 | function up() { | |
109 | loadDirectory(app.path.split('/').slice(0, -1).filter(function (p) { return !!p; }).join('/')); | |
110 | } | |
111 | ||
537bfb04 | 112 | function upload() { |
403359cf | 113 | $(app.$els.upload).on('change', function () { |
537bfb04 JZ |
114 | app.busy = true; |
115 | ||
403359cf JZ |
116 | // detach event handler |
117 | $(app.$els.upload).off('change'); | |
118 | ||
537bfb04 JZ |
119 | var file = app.$els.upload.files[0]; |
120 | var path = encode(sanitize(app.path + '/' + file.name)); | |
121 | ||
122 | var formData = new FormData(); | |
123 | formData.append('file', file); | |
124 | ||
125 | superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password }).send(formData).end(function (error, result) { | |
126 | app.busy = false; | |
127 | ||
128 | if (error) return console.error(error); | |
129 | if (result.statusCode !== 201) return console.error('Error uploading file: ', result.statusCode); | |
130 | ||
131 | refresh(); | |
132 | }); | |
133 | }); | |
134 | ||
135 | app.$els.upload.click(); | |
136 | } | |
137 | ||
9138d7d4 JZ |
138 | function delAsk(entry) { |
139 | $('#modalDelete').modal('show'); | |
140 | app.deleteData = entry; | |
141 | } | |
142 | ||
143 | function del(entry) { | |
144 | app.busy = true; | |
145 | ||
146 | var path = encode(sanitize(app.path + '/' + entry.filePath)); | |
147 | ||
148 | superagent.del('/api/files' + path).query({ username: app.session.username, password: app.session.password, recursive: true }).end(function (error, result) { | |
149 | app.busy = false; | |
150 | ||
151 | if (error) return console.error(error); | |
152 | if (result.statusCode !== 200) return console.error('Error deleting file: ', result.statusCode); | |
153 | ||
154 | refresh(); | |
155 | ||
156 | $('#modalDelete').modal('hide'); | |
157 | }); | |
158 | } | |
159 | ||
403359cf JZ |
160 | function createDirectoryAsk() { |
161 | $('#modalcreateDirectory').modal('show'); | |
162 | app.createDirectoryData = ''; | |
163 | } | |
164 | ||
165 | function createDirectory(name) { | |
166 | app.busy = true; | |
167 | ||
168 | var path = encode(sanitize(app.path + '/' + name)); | |
169 | ||
170 | superagent.put('/api/files' + path).query({ username: app.session.username, password: app.session.password, directory: true }).end(function (error, result) { | |
171 | app.busy = false; | |
172 | ||
173 | if (error) return console.error(error); | |
174 | if (result.statusCode !== 201) return console.error('Error creating directory: ', result.statusCode); | |
175 | ||
176 | app.createDirectoryData = ''; | |
177 | refresh(); | |
178 | ||
179 | $('#modalcreateDirectory').modal('hide'); | |
180 | }); | |
181 | } | |
182 | ||
235212c4 JZ |
183 | Vue.filter('prettyDate', function (value) { |
184 | var d = new Date(value); | |
185 | return d.toDateString(); | |
186 | }); | |
187 | ||
6eb72d64 JZ |
188 | var app = new Vue({ |
189 | el: '#app', | |
190 | data: { | |
191 | busy: true, | |
d3312ed1 JZ |
192 | path: '/', |
193 | pathParts: [], | |
6eb72d64 JZ |
194 | session: { |
195 | valid: false | |
196 | }, | |
d3312ed1 | 197 | loginData: {}, |
9138d7d4 | 198 | deleteData: {}, |
403359cf | 199 | createDirectoryData: '', |
d3312ed1 | 200 | entries: [] |
6eb72d64 JZ |
201 | }, |
202 | methods: { | |
203 | login: login, | |
d3312ed1 JZ |
204 | logout: logout, |
205 | loadDirectory: loadDirectory, | |
206 | open: open, | |
537bfb04 | 207 | up: up, |
9138d7d4 JZ |
208 | upload: upload, |
209 | delAsk: delAsk, | |
403359cf JZ |
210 | del: del, |
211 | createDirectoryAsk: createDirectoryAsk, | |
212 | createDirectory: createDirectory | |
6eb72d64 JZ |
213 | } |
214 | }); | |
215 | ||
6eb72d64 JZ |
216 | login(localStorage.username, localStorage.password); |
217 | ||
218 | })(); |