]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - frontend/js/public.js
4 /* global superagent */
9 function sanitize(filePath
) {
10 filePath
= '/' + filePath
;
11 return filePath
.replace(/\/+/g, '/');
14 function encode(filePath
) {
15 return filePath
.split('/').map(encodeURIComponent
).join('/');
18 function decode(filePath
) {
19 return filePath
.split('/').map(decodeURIComponent
).join('/');
23 images: [ '.png', '.jpg', '.jpeg', '.tiff', '.gif' ],
24 text: [ '.txt', '.md' ],
26 html: [ '.html', '.htm', '.php' ],
27 video: [ '.mp4', '.mpg', '.mpeg', '.ogg', '.mkv', '.avi', '.mov' ]
30 function getPreviewUrl(entry
, basePath
) {
31 var path
= '/_admin/img/';
33 if (entry
.isDirectory
) return path
+ 'directory.png';
34 if (mimeTypes
.images
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return sanitize(basePath
+ '/' + entry
.filePath
);
35 if (mimeTypes
.text
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+'text.png';
36 if (mimeTypes
.pdf
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+ 'pdf.png';
37 if (mimeTypes
.html
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+ 'html.png';
38 if (mimeTypes
.video
.some(function (e
) { return entry
.filePath
.endsWith(e
); })) return path
+ 'video.png';
40 return path
+ 'unknown.png';
43 // simple extension detection, does not work with double extension like .tar.gz
44 function getExtension(entry
) {
45 if (entry
.isFile
) return entry
.filePath
.slice(entry
.filePath
.lastIndexOf('.') + 1);
49 function loadDirectory() {
52 var filePath
= sanitize(window
.location
.pathname
);
56 superagent
.get('/api/files/' + encode(filePath
)).query({ access_token: localStorage
.accessToken
}).end(function (error
, result
) {
59 if (result
&& result
.statusCode
=== 401) return logout();
60 if (error
) return console
.error(error
);
62 result
.body
.entries
.sort(function (a
, b
) { return a
.isDirectory
&& b
.isFile
? -1 : 1; });
63 app
.entries
= result
.body
.entries
.map(function (entry
) {
64 entry
.previewUrl
= getPreviewUrl(entry
, filePath
);
65 entry
.extension
= getExtension(entry
);
67 entry
.filePathNew
= entry
.filePath
;
71 app
.pathParts
= decode(filePath
).split('/').filter(function (e
) { return !!e
; }).map(function (e
, i
, a
) {
74 link: '#' + sanitize('/' + a
.slice(0, i
).join('/') + '/' + e
)
80 function open(row
, column
, event
) {
81 var fullPath
= encode(sanitize(app
.path
+ '/' + row
.filePath
));
83 if (row
.isDirectory
) return window
.location
.href
= fullPath
;
85 app
.activeEntry
= row
;
86 app
.activeEntry
.fullPath
= fullPath
;
87 app
.previewDrawerVisible
= true
89 // need to wait for DOM element to exist
90 setTimeout(function () {
91 $('iframe').on('load', function (e
) {
92 if (!e
.target
.contentWindow
.document
.body
) return;
94 e
.target
.contentWindow
.document
.body
.style
.display
= 'flex'
95 e
.target
.contentWindow
.document
.body
.style
.justifyContent
= 'center'
106 previewDrawerVisible: false,
111 onDownload: function (entry
) {
112 if (entry
.isDirectory
) return;
113 window
.location
.href
= encode('/api/files/' + sanitize(this.path
+ '/' + entry
.filePath
)) + '?access_token=' + localStorage
.accessToken
;
115 prettyDate: function (row
, column
, cellValue
, index
) {
116 var date
= new Date(cellValue
),
117 diff
= (((new Date()).getTime() - date
.getTime()) / 1000),
118 day_diff
= Math
.floor(diff
/ 86400);
120 if (isNaN(day_diff
) || day_diff
< 0)
123 return day_diff
=== 0 && (
124 diff
< 60 && 'just now' ||
125 diff
< 120 && '1 minute ago' ||
126 diff
< 3600 && Math
.floor( diff
/ 60 ) + ' minutes ago' ||
127 diff
< 7200 && '1 hour ago' ||
128 diff
< 86400 && Math
.floor( diff
/ 3600 ) + ' hours ago') ||
129 day_diff
=== 1 && 'Yesterday' ||
130 day_diff
< 7 && day_diff
+ ' days ago' ||
131 day_diff
< 31 && Math
.ceil( day_diff
/ 7 ) + ' weeks ago' ||
132 day_diff
< 365 && Math
.round( day_diff
/ 30 ) + ' months ago' ||
133 Math
.round( day_diff
/ 365 ) + ' years ago';
135 prettyFileSize: function (row
, column
, cellValue
, index
) {
136 return filesize(cellValue
);
138 loadDirectory: loadDirectory
,