]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - frontend/js/app.js
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').query({ 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
));
139 window
.location
.hash
= sanitize(app
.path
.split('/').slice(0, -1).filter(function (p
) { return !!p
; }).join('/'));
142 function uploadFiles(files
) {
143 if (!files
|| !files
.length
) return;
152 function uploadFile(file
) {
153 var path
= encode(sanitize(app
.path
+ '/' + file
.name
));
155 var formData
= new FormData();
156 formData
.append('file', file
);
158 superagent
.post('/api/files' + path
).query({ access_token: localStorage
.accessToken
}).send(formData
).end(function (error
, result
) {
159 if (result
&& result
.statusCode
=== 401) return logout();
160 if (result
&& result
.statusCode
!== 201) console
.error('Error uploading file: ', result
.statusCode
);
161 if (error
) console
.error(error
);
163 app
.uploadStatus
.done
+= 1;
164 app
.uploadStatus
.percentDone
= Math
.round(app
.uploadStatus
.done
/ app
.uploadStatus
.count
* 100);
166 if (app
.uploadStatus
.done
>= app
.uploadStatus
.count
) {
179 for(var i
= 0; i
< app
.uploadStatus
.count
; ++i
) {
180 uploadFile(files
[i
]);
185 $(app
.$els
.upload
).on('change', function () {
187 // detach event handler
188 $(app
.$els
.upload
).off('change');
190 uploadFiles(app
.$els
.upload
.files
|| []);
193 // reset the form first to make the change handler retrigger even on the same file selected
194 $('#fileUploadForm')[0].reset();
196 app
.$els
.upload
.click();
199 function delAsk(entry
) {
200 $('#modalDelete').modal('show');
201 app
.deleteData
= entry
;
204 function del(entry
) {
207 var path
= encode(sanitize(app
.path
+ '/' + entry
.filePath
));
209 superagent
.del('/api/files' + path
).query({ access_token: localStorage
.accessToken
, recursive: true }).end(function (error
, result
) {
212 if (result
&& result
.statusCode
=== 401) return logout();
213 if (result
&& result
.statusCode
!== 200) return console
.error('Error deleting file: ', result
.statusCode
);
214 if (error
) return console
.error(error
);
218 $('#modalDelete').modal('hide');
222 function renameAsk(entry
) {
223 app
.renameData
.entry
= entry
;
224 app
.renameData
.error
= null;
225 app
.renameData
.newFilePath
= entry
.filePath
;
227 $('#modalRename').modal('show');
230 function rename(data
) {
233 var path
= encode(sanitize(app
.path
+ '/' + data
.entry
.filePath
));
234 var newFilePath
= sanitize(app
.path
+ '/' + data
.newFilePath
);
236 superagent
.put('/api/files' + path
).query({ access_token: localStorage
.accessToken
}).send({ newFilePath: newFilePath
}).end(function (error
, result
) {
239 if (result
&& result
.statusCode
=== 401) return logout();
240 if (result
&& result
.statusCode
!== 200) return console
.error('Error renaming file: ', result
.statusCode
);
241 if (error
) return console
.error(error
);
245 $('#modalRename').modal('hide');
249 function createDirectoryAsk() {
250 $('#modalcreateDirectory').modal('show');
251 app
.createDirectoryData
= '';
252 app
.createDirectoryError
= null;
255 function createDirectory(name
) {
257 app
.createDirectoryError
= null;
259 var path
= encode(sanitize(app
.path
+ '/' + name
));
261 superagent
.post('/api/files' + path
).query({ access_token: localStorage
.accessToken
, directory: true }).end(function (error
, result
) {
264 if (result
&& result
.statusCode
=== 401) return logout();
265 if (result
&& result
.statusCode
=== 403) {
266 app
.createDirectoryError
= 'Name not allowed';
269 if (result
&& result
.statusCode
=== 409) {
270 app
.createDirectoryError
= 'Directory already exists';
273 if (result
&& result
.statusCode
!== 201) return console
.error('Error creating directory: ', result
.statusCode
);
274 if (error
) return console
.error(error
);
276 app
.createDirectoryData
= '';
279 $('#modalcreateDirectory').modal('hide');
283 function dragOver(event
) {
284 event
.preventDefault();
287 function drop(event
) {
288 event
.preventDefault();
289 uploadFiles(event
.dataTransfer
.files
|| []);
292 Vue
.filter('prettyDate', function (value
) {
293 var d
= new Date(value
);
294 return d
.toDateString();
297 Vue
.filter('prettyFileSize', function (value
) {
298 return filesize(value
);
323 createDirectoryData: '',
324 createDirectoryError: null,
330 loadDirectory: loadDirectory
,
336 renameAsk: renameAsk
,
338 createDirectoryAsk: createDirectoryAsk
,
339 createDirectory: createDirectory
,
347 getProfile(localStorage
.accessToken
);
349 $(window
).on('hashchange', function () {
350 loadDirectory(window
.location
.hash
.slice(1));
353 // setup all the dialog focus handling
354 ['modalcreateDirectory'].forEach(function (id
) {
355 $('#' + id
).on('shown.bs.modal', function () {
356 $(this).find("[autofocus]:first").focus();