]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - app/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 console
.log(filePath
);
82 superagent
.get('/api/files/' + filePath
).query({ username: app
.session
.username
, password: app
.session
.password
}).end(function (error
, result
) {
85 if (error
) return console
.error(error
);
86 if (result
.statusCode
=== 401) return logout();
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
);
94 app
.pathParts
= decode(filePath
).split('/').filter(function (e
) { return !!e
; });
96 // update in case this was triggered from code
97 window
.location
.hash
= app
.path
;
99 Vue
.nextTick(function () {
101 $('[data-toggle="tooltip"]').tooltip();
107 function open(entry
) {
108 var path
= encode(sanitize(app
.path
+ '/' + entry
.filePath
));
110 if (entry
.isDirectory
) {
111 window
.location
.hash
= path
;
119 window
.location
.hash
= encode(sanitize(app
.path
.split('/').slice(0, -1).filter(function (p
) { return !!p
; }).join('/')));
123 $(app
.$els
.upload
).on('change', function () {
126 // detach event handler
127 $(app
.$els
.upload
).off('change');
129 var file
= app
.$els
.upload
.files
[0];
130 var path
= encode(sanitize(app
.path
+ '/' + file
.name
));
132 var formData
= new FormData();
133 formData
.append('file', file
);
135 superagent
.put('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
}).send(formData
).end(function (error
, result
) {
138 if (error
) return console
.error(error
);
139 if (result
.statusCode
!== 201) return console
.error('Error uploading file: ', result
.statusCode
);
145 app
.$els
.upload
.click();
148 function delAsk(entry
) {
149 $('#modalDelete').modal('show');
150 app
.deleteData
= entry
;
153 function del(entry
) {
156 var path
= encode(sanitize(app
.path
+ '/' + entry
.filePath
));
158 superagent
.del('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
, recursive: true }).end(function (error
, result
) {
161 if (error
) return console
.error(error
);
162 if (result
.statusCode
!== 200) return console
.error('Error deleting file: ', result
.statusCode
);
166 $('#modalDelete').modal('hide');
170 function createDirectoryAsk() {
171 $('#modalcreateDirectory').modal('show');
172 app
.createDirectoryData
= '';
175 function createDirectory(name
) {
178 var path
= encode(sanitize(app
.path
+ '/' + name
));
180 superagent
.put('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
, directory: true }).end(function (error
, result
) {
183 if (error
) return console
.error(error
);
184 if (result
.statusCode
!== 201) return console
.error('Error creating directory: ', result
.statusCode
);
186 app
.createDirectoryData
= '';
189 $('#modalcreateDirectory').modal('hide');
193 Vue
.filter('prettyDate', function (value
) {
194 var d
= new Date(value
);
195 return d
.toDateString();
198 Vue
.filter('prettyFileSize', function (value
) {
199 return filesize(value
);
213 createDirectoryData: '',
219 loadDirectory: loadDirectory
,
225 createDirectoryAsk: createDirectoryAsk
,
226 createDirectory: createDirectory
230 login(localStorage
.username
, localStorage
.password
);
232 $(window
).on('hashchange', function () {
233 loadDirectory(window
.location
.hash
.slice(1));