]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - frontend/js/app.js
0875f1459e0d820299ab9cb075e91c1ee9256dbd
4 function getProfile(accessToken
, callback
) {
5 callback
= callback
|| function (error
) { if (error
) console
.error(error
); };
7 superagent
.get('/api/profile').query({ access_token: accessToken
}).end(function (error
, result
) {
10 if (error
&& !error
.response
) return callback(error
);
11 if (result
.statusCode
!== 200) {
12 delete localStorage
.accessToken
;
13 return callback('Invalid access token');
16 localStorage
.accessToken
= accessToken
;
17 app
.session
.username
= result
.body
.username
;
18 app
.session
.valid
= true;
24 function login(username
, password
) {
25 username
= username
|| app
.loginData
.username
;
26 password
= password
|| app
.loginData
.password
;
30 superagent
.post('/api/login').send({ username: username
, password: password
}).end(function (error
, result
) {
33 if (error
) return console
.error(error
);
34 if (result
.statusCode
=== 401) return console
.error('Invalid credentials');
36 getProfile(result
.body
.accessToken
, function (error
) {
37 if (error
) return console
.error(error
);
39 loadDirectory(window
.location
.hash
.slice(1));
45 superagent
.post('/api/logout').query({ access_token: localStorage
.accessToken
}).end(function (error
) {
46 if (error
) console
.error(error
);
48 app
.session
.valid
= false;
50 delete localStorage
.accessToken
;
54 function sanitize(filePath
) {
55 filePath
= '/' + filePath
;
56 return filePath
.replace(/\/+/g, '/');
59 function encode(filePath
) {
60 return filePath
.split('/').map(encodeURIComponent
).join('/');
63 function decode(filePath
) {
64 return filePath
.split('/').map(decodeURIComponent
).join('/');
68 images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ],
69 text: [ '.txt', '.md' ],
71 html: [ '.html', '.htm', '.php' ],
72 video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv' ]
75 function getPreviewUrl(entry
, basePath
) {
76 var path
= '/_admin/img/';
78 if (entry
.isDirectory
) return path
+ 'directory.png';
79 if (mimeTypes
.images
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return sanitize(basePath
+ '/' + entry
.filePath
);
80 if (mimeTypes
.text
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+'text.png';
81 if (mimeTypes
.pdf
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+ 'pdf.png';
82 if (mimeTypes
.html
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+ 'html.png';
83 if (mimeTypes
.video
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+ 'video.png';
85 return path
+ 'unknown.png';
89 loadDirectory(app
.path
);
92 function loadDirectory(filePath
) {
95 filePath
= filePath
? sanitize(filePath
) : '/';
97 superagent
.get('/api/files/' + encode(filePath
)).query({ access_token: localStorage
.accessToken
}).end(function (error
, result
) {
100 if (result
&& result
.statusCode
=== 401) return logout();
101 if (error
) return console
.error(error
);
103 result
.body
.entries
.sort(function (a
, b
) { return a
.isDirectory
&& b
.isFile
? -1 : 1; });
104 app
.entries
= result
.body
.entries
.map(function (entry
) {
105 entry
.previewUrl
= getPreviewUrl(entry
, filePath
);
109 app
.pathParts
= decode(filePath
).split('/').filter(function (e
) { return !!e
; }).map(function (e
, i
, a
) {
112 link: '#' + sanitize('/' + a
.slice(0, i
).join('/') + '/' + e
)
116 // update in case this was triggered from code
117 window
.location
.hash
= app
.path
;
119 Vue
.nextTick(function () {
121 $('[data-toggle="tooltip"]').tooltip();
127 function open(entry
) {
128 var path
= sanitize(app
.path
+ '/' + entry
.filePath
);
130 if (entry
.isDirectory
) {
131 window
.location
.hash
= path
;
135 window
.open(encode(path
));
138 function download(entry
) {
139 if (entry
.isDirectory
) return;
141 window
.location
.href
= encode('/api/files/' + sanitize(app
.path
+ '/' + entry
.filePath
)) + '?access_token=' + localStorage
.accessToken
;
145 window
.location
.hash
= sanitize(app
.path
.split('/').slice(0, -1).filter(function (p
) { return !!p
; }).join('/'));
148 function uploadFiles(files
) {
149 if (!files
|| !files
.length
) return;
158 function uploadFile(file
) {
159 var path
= encode(sanitize(app
.path
+ '/' + file
.name
));
161 var formData
= new FormData();
162 formData
.append('file', file
);
164 superagent
.post('/api/files' + path
).query({ access_token: localStorage
.accessToken
}).send(formData
).end(function (error
, result
) {
165 if (result
&& result
.statusCode
=== 401) return logout();
166 if (result
&& result
.statusCode
!== 201) console
.error('Error uploading file: ', result
.statusCode
);
167 if (error
) console
.error(error
);
169 app
.uploadStatus
.done
+= 1;
170 app
.uploadStatus
.percentDone
= Math
.round(app
.uploadStatus
.done
/ app
.uploadStatus
.count
* 100);
172 if (app
.uploadStatus
.done
>= app
.uploadStatus
.count
) {
185 for(var i
= 0; i
< app
.uploadStatus
.count
; ++i
) {
186 uploadFile(files
[i
]);
191 $(app
.$els
.upload
).on('change', function () {
193 // detach event handler
194 $(app
.$els
.upload
).off('change');
196 uploadFiles(app
.$els
.upload
.files
|| []);
199 // reset the form first to make the change handler retrigger even on the same file selected
200 $('#fileUploadForm')[0].reset();
202 app
.$els
.upload
.click();
205 function delAsk(entry
) {
206 $('#modalDelete').modal('show');
207 app
.deleteData
= entry
;
210 function del(entry
) {
213 var path
= encode(sanitize(app
.path
+ '/' + entry
.filePath
));
215 superagent
.del('/api/files' + path
).query({ access_token: localStorage
.accessToken
, recursive: true }).end(function (error
, result
) {
218 if (result
&& result
.statusCode
=== 401) return logout();
219 if (result
&& result
.statusCode
!== 200) return console
.error('Error deleting file: ', result
.statusCode
);
220 if (error
) return console
.error(error
);
224 $('#modalDelete').modal('hide');
228 function renameAsk(entry
) {
229 app
.renameData
.entry
= entry
;
230 app
.renameData
.error
= null;
231 app
.renameData
.newFilePath
= entry
.filePath
;
233 $('#modalRename').modal('show');
236 function rename(data
) {
239 var path
= encode(sanitize(app
.path
+ '/' + data
.entry
.filePath
));
240 var newFilePath
= sanitize(app
.path
+ '/' + data
.newFilePath
);
242 superagent
.put('/api/files' + path
).query({ access_token: localStorage
.accessToken
}).send({ newFilePath: newFilePath
}).end(function (error
, result
) {
245 if (result
&& result
.statusCode
=== 401) return logout();
246 if (result
&& result
.statusCode
!== 200) return console
.error('Error renaming file: ', result
.statusCode
);
247 if (error
) return console
.error(error
);
251 $('#modalRename').modal('hide');
255 function createDirectoryAsk() {
256 $('#modalcreateDirectory').modal('show');
257 app
.createDirectoryData
= '';
258 app
.createDirectoryError
= null;
261 function createDirectory(name
) {
263 app
.createDirectoryError
= null;
265 var path
= encode(sanitize(app
.path
+ '/' + name
));
267 superagent
.post('/api/files' + path
).query({ access_token: localStorage
.accessToken
, directory: true }).end(function (error
, result
) {
270 if (result
&& result
.statusCode
=== 401) return logout();
271 if (result
&& result
.statusCode
=== 403) {
272 app
.createDirectoryError
= 'Name not allowed';
275 if (result
&& result
.statusCode
=== 409) {
276 app
.createDirectoryError
= 'Directory already exists';
279 if (result
&& result
.statusCode
!== 201) return console
.error('Error creating directory: ', result
.statusCode
);
280 if (error
) return console
.error(error
);
282 app
.createDirectoryData
= '';
285 $('#modalcreateDirectory').modal('hide');
289 function dragOver(event
) {
290 event
.preventDefault();
293 function drop(event
) {
294 event
.preventDefault();
295 uploadFiles(event
.dataTransfer
.files
|| []);
298 Vue
.filter('prettyDate', function (value
) {
299 var d
= new Date(value
);
300 return d
.toDateString();
303 Vue
.filter('prettyFileSize', function (value
) {
304 return filesize(value
);
329 createDirectoryData: '',
330 createDirectoryError: null,
336 loadDirectory: loadDirectory
,
343 renameAsk: renameAsk
,
345 createDirectoryAsk: createDirectoryAsk
,
346 createDirectory: createDirectory
,
354 getProfile(localStorage
.accessToken
, function (error
) {
355 if (error
) return console
.error(error
);
357 loadDirectory(window
.location
.hash
.slice(1));
360 $(window
).on('hashchange', function () {
361 loadDirectory(window
.location
.hash
.slice(1));
364 // setup all the dialog focus handling
365 ['modalcreateDirectory'].forEach(function (id
) {
366 $('#' + id
).on('shown.bs.modal', function () {
367 $(this).find("[autofocus]:first").focus();