]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - frontend/js/app.js
4 function login(username
, password
) {
5 username
= username
|| app
.loginData
.username
;
6 password
= password
|| app
.loginData
.password
;
10 superagent
.get('/api/files/').query({ username: username
, password: password
}).end(function (error
, result
) {
13 if (error
) return console
.error(error
);
14 if (result
.statusCode
=== 401) return console
.error('Invalid credentials');
16 app
.session
.valid
= true;
17 app
.session
.username
= username
;
18 app
.session
.password
= password
;
20 // clearly not the best option
21 localStorage
.username
= username
;
22 localStorage
.password
= password
;
24 loadDirectory(window
.location
.hash
.slice(1));
29 app
.session
.valid
= false;
30 app
.session
.username
= null;
31 app
.session
.password
= null;
33 delete localStorage
.username
;
34 delete localStorage
.password
;
37 function sanitize(filePath
) {
38 filePath
= '/' + filePath
;
39 return filePath
.replace(/\/+/g, '/');
42 function encode(filePath
) {
43 return filePath
.split('/').map(encodeURIComponent
).join('/');
46 function decode(filePath
) {
47 return filePath
.split('/').map(decodeURIComponent
).join('/');
51 images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ],
52 text: [ '.txt', '.md' ],
54 html: [ '.html', '.htm', '.php' ],
55 video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv' ]
58 function getPreviewUrl(entry
, basePath
) {
59 var path
= '/_admin/img/';
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';
68 return path
+ 'unknown.png';
72 loadDirectory(app
.path
);
75 function loadDirectory(filePath
) {
78 filePath
= filePath
? sanitize(filePath
) : '/';
80 superagent
.get('/api/files/' + encode(filePath
)).query({ username: app
.session
.username
, password: app
.session
.password
}).end(function (error
, result
) {
83 if (result
&& result
.statusCode
=== 401) return logout();
84 if (error
) return console
.error(error
);
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
);
92 app
.pathParts
= decode(filePath
).split('/').filter(function (e
) { return !!e
; }).map(function (e
, i
, a
) {
95 link: '#' + sanitize('/' + a
.slice(0, i
).join('/') + '/' + e
)
99 // update in case this was triggered from code
100 window
.location
.hash
= app
.path
;
102 Vue
.nextTick(function () {
104 $('[data-toggle="tooltip"]').tooltip();
110 function open(entry
) {
111 var path
= sanitize(app
.path
+ '/' + entry
.filePath
);
113 if (entry
.isDirectory
) {
114 window
.location
.hash
= path
;
118 window
.open(encode(path
));
122 window
.location
.hash
= sanitize(app
.path
.split('/').slice(0, -1).filter(function (p
) { return !!p
; }).join('/'));
125 function uploadFiles(files
) {
126 if (!files
|| !files
.length
) return;
135 function uploadFile(file
) {
136 var path
= encode(sanitize(app
.path
+ '/' + file
.name
));
138 var formData
= new FormData();
139 formData
.append('file', file
);
141 superagent
.post('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
}).send(formData
).end(function (error
, result
) {
142 if (result
&& result
.statusCode
=== 401) return logout();
143 if (result
&& result
.statusCode
!== 201) console
.error('Error uploading file: ', result
.statusCode
);
144 if (error
) console
.error(error
);
146 app
.uploadStatus
.done
+= 1;
147 app
.uploadStatus
.percentDone
= Math
.round(app
.uploadStatus
.done
/ app
.uploadStatus
.count
* 100);
149 if (app
.uploadStatus
.done
>= app
.uploadStatus
.count
) {
162 for(var i
= 0; i
< app
.uploadStatus
.count
; ++i
) {
163 uploadFile(files
[i
]);
168 $(app
.$els
.upload
).on('change', function () {
170 // detach event handler
171 $(app
.$els
.upload
).off('change');
173 uploadFiles(app
.$els
.upload
.files
|| []);
176 // reset the form first to make the change handler retrigger even on the same file selected
177 $('#fileUploadForm')[0].reset();
179 app
.$els
.upload
.click();
182 function delAsk(entry
) {
183 $('#modalDelete').modal('show');
184 app
.deleteData
= entry
;
187 function del(entry
) {
190 var path
= encode(sanitize(app
.path
+ '/' + entry
.filePath
));
192 superagent
.del('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
, recursive: true }).end(function (error
, result
) {
195 if (result
&& result
.statusCode
=== 401) return logout();
196 if (result
&& result
.statusCode
!== 200) return console
.error('Error deleting file: ', result
.statusCode
);
197 if (error
) return console
.error(error
);
201 $('#modalDelete').modal('hide');
205 function renameAsk(entry
) {
206 app
.renameData
.entry
= entry
;
207 app
.renameData
.error
= null;
208 app
.renameData
.newFilePath
= entry
.filePath
;
210 $('#modalRename').modal('show');
213 function rename(data
) {
216 var path
= encode(sanitize(app
.path
+ '/' + data
.entry
.filePath
));
217 var newFilePath
= sanitize(app
.path
+ '/' + data
.newFilePath
);
219 superagent
.put('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
}).send({ newFilePath: newFilePath
}).end(function (error
, result
) {
222 if (result
&& result
.statusCode
=== 401) return logout();
223 if (result
&& result
.statusCode
!== 200) return console
.error('Error renaming file: ', result
.statusCode
);
224 if (error
) return console
.error(error
);
228 $('#modalRename').modal('hide');
232 function createDirectoryAsk() {
233 $('#modalcreateDirectory').modal('show');
234 app
.createDirectoryData
= '';
235 app
.createDirectoryError
= null;
238 function createDirectory(name
) {
240 app
.createDirectoryError
= null;
242 var path
= encode(sanitize(app
.path
+ '/' + name
));
244 superagent
.post('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
, directory: true }).end(function (error
, result
) {
247 if (result
&& result
.statusCode
=== 401) return logout();
248 if (result
&& result
.statusCode
=== 403) {
249 app
.createDirectoryError
= 'Name not allowed';
252 if (result
&& result
.statusCode
=== 409) {
253 app
.createDirectoryError
= 'Directory already exists';
256 if (result
&& result
.statusCode
!== 201) return console
.error('Error creating directory: ', result
.statusCode
);
257 if (error
) return console
.error(error
);
259 app
.createDirectoryData
= '';
262 $('#modalcreateDirectory').modal('hide');
266 function dragOver(event
) {
267 event
.preventDefault();
270 function drop(event
) {
271 event
.preventDefault();
272 uploadFiles(event
.dataTransfer
.files
|| []);
275 Vue
.filter('prettyDate', function (value
) {
276 var d
= new Date(value
);
277 return d
.toDateString();
280 Vue
.filter('prettyFileSize', function (value
) {
281 return filesize(value
);
306 createDirectoryData: '',
307 createDirectoryError: null,
313 loadDirectory: loadDirectory
,
319 renameAsk: renameAsk
,
321 createDirectoryAsk: createDirectoryAsk
,
322 createDirectory: createDirectory
,
330 login(localStorage
.username
, localStorage
.password
);
332 $(window
).on('hashchange', function () {
333 loadDirectory(window
.location
.hash
.slice(1));
336 // setup all the dialog focus handling
337 ['modalcreateDirectory'].forEach(function (id
) {
338 $('#' + id
).on('shown.bs.modal', function () {
339 $(this).find("[autofocus]:first").focus();