]>
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(app
.path
);
29 app
.session
.valid
= false;
30 app
.session
.username
= username
;
31 app
.session
.password
= password
;
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('/');
47 images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ],
48 text: [ '.txt', '.md' ],
50 html: [ '.html', '.htm', '.php' ],
51 video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv' ]
54 function getPreviewUrl(entry
, basePath
) {
55 var path
= '/_admin/img/';
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';
64 return path
+ 'unknown.png';
68 loadDirectory(app
.path
);
71 function loadDirectory(filePath
) {
74 filePath
= filePath
? sanitize(filePath
) : '/';
76 console
.log(filePath
);
78 superagent
.get('/api/files/' + filePath
).query({ username: app
.session
.username
, password: app
.session
.password
}).end(function (error
, result
) {
81 if (error
) return console
.error(error
);
82 if (result
.statusCode
=== 401) return logout();
84 result
.body
.entries
.sort(function (a
, b
) { return a
.isDirectory
&& b
.isFile
? -1 : 1 });
85 app
.entries
= result
.body
.entries
.map(function (entry
) {
86 entry
.previewUrl
= getPreviewUrl(entry
, filePath
);
90 app
.pathParts
= filePath
.split('/').filter(function (e
) { return !!e
; });
92 Vue
.nextTick(function () {
94 $('[data-toggle="tooltip"]').tooltip();
100 function open(entry
) {
101 var path
= sanitize(app
.path
+ '/' + entry
.filePath
);
103 if (entry
.isDirectory
) return loadDirectory(path
);
109 loadDirectory(app
.path
.split('/').slice(0, -1).filter(function (p
) { return !!p
; }).join('/'));
113 $(app
.$els
.upload
).on('change', function () {
116 // detach event handler
117 $(app
.$els
.upload
).off('change');
119 var file
= app
.$els
.upload
.files
[0];
120 var path
= encode(sanitize(app
.path
+ '/' + file
.name
));
122 var formData
= new FormData();
123 formData
.append('file', file
);
125 superagent
.put('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
}).send(formData
).end(function (error
, result
) {
128 if (error
) return console
.error(error
);
129 if (result
.statusCode
!== 201) return console
.error('Error uploading file: ', result
.statusCode
);
135 app
.$els
.upload
.click();
138 function delAsk(entry
) {
139 $('#modalDelete').modal('show');
140 app
.deleteData
= entry
;
143 function del(entry
) {
146 var path
= encode(sanitize(app
.path
+ '/' + entry
.filePath
));
148 superagent
.del('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
, recursive: true }).end(function (error
, result
) {
151 if (error
) return console
.error(error
);
152 if (result
.statusCode
!== 200) return console
.error('Error deleting file: ', result
.statusCode
);
156 $('#modalDelete').modal('hide');
160 function createDirectoryAsk() {
161 $('#modalcreateDirectory').modal('show');
162 app
.createDirectoryData
= '';
165 function createDirectory(name
) {
168 var path
= encode(sanitize(app
.path
+ '/' + name
));
170 superagent
.put('/api/files' + path
).query({ username: app
.session
.username
, password: app
.session
.password
, directory: true }).end(function (error
, result
) {
173 if (error
) return console
.error(error
);
174 if (result
.statusCode
!== 201) return console
.error('Error creating directory: ', result
.statusCode
);
176 app
.createDirectoryData
= '';
179 $('#modalcreateDirectory').modal('hide');
183 Vue
.filter('prettyDate', function (value
) {
184 var d
= new Date(value
);
185 return d
.toDateString();
188 Vue
.filter('prettyFileSize', function (value
) {
189 return filesize(value
);
203 createDirectoryData: '',
209 loadDirectory: loadDirectory
,
215 createDirectoryAsk: createDirectoryAsk
,
216 createDirectory: createDirectory
220 login(localStorage
.username
, localStorage
.password
);