]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - frontend/js/app.js
c1a63a9df6398760e3b1799569bcc033d54cbceb
5 function asyncForEach ( items
, handler
, callback
) {
8 if ( items
. length
=== 0 ) return callback ();
10 ( function iterator () {
11 handler ( items
[ cur
], function ( error
) {
12 if ( error
) return callback ( error
);
13 if ( cur
>= items
. length
- 1 ) return callback ();
21 function getProfile ( accessToken
, callback
) {
22 superagent
. get ( '/api/profile' ). query ({ access_token : accessToken
}). end ( function ( error
, result
) {
25 if ( error
&& ! error
. response
) return callback ( error
);
26 if ( result
. statusCode
!== 200 ) {
27 delete localStorage
. accessToken
;
28 return callback ( 'Invalid access token' );
31 localStorage
. accessToken
= accessToken
;
32 app
. session
. username
= result
. body
. username
;
33 app
. session
. valid
= true ;
35 superagent
. get ( '/api/settings' ). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
, result
) {
36 if ( error
) console
. error ( error
);
38 app
. folderListingEnabled
= !! result
. body
. folderListingEnabled
;
45 function sanitize ( filePath
) {
46 filePath
= '/' + filePath
;
47 return filePath
. replace ( /\/+/g , '/' );
50 function encode ( filePath
) {
51 return filePath
. split ( '/' ). map ( encodeURIComponent
). join ( '/' );
54 function decode ( filePath
) {
55 return filePath
. split ( '/' ). map ( decodeURIComponent
). join ( '/' );
59 images : [ '.png' , '.jpg' , '.jpeg' , '.tiff' , '.gif' ],
60 text : [ '.txt' , '.md' ],
62 html : [ '.html' , '.htm' , '.php' ],
63 video : [ '.mp4' , '.mpg' , '.mpeg' , '.ogg' , '.mkv' ]
66 function getPreviewUrl ( entry
, basePath
) {
67 var path
= '/_admin/img/' ;
69 if ( entry
. isDirectory
) return path
+ 'directory.png' ;
70 if ( mimeTypes
. images
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return sanitize ( basePath
+ '/' + entry
. filePath
);
71 if ( mimeTypes
. text
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'text.png' ;
72 if ( mimeTypes
. pdf
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'pdf.png' ;
73 if ( mimeTypes
. html
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'html.png' ;
74 if ( mimeTypes
. video
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'video.png' ;
76 return path
+ 'unknown.png' ;
79 // simple extension detection, does not work with double extension like .tar.gz
80 function getExtension ( entry
) {
81 if ( entry
. isFile
) return entry
. filePath
. slice ( entry
. filePath
. lastIndexOf ( '.' ) + 1 );
86 loadDirectory ( app
. path
);
89 function loadDirectory ( filePath
) {
92 filePath
= filePath
? sanitize ( filePath
) : '/' ;
94 superagent
. get ( '/api/files/' + encode ( filePath
)). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
, result
) {
97 if ( result
&& result
. statusCode
=== 401 ) return logout ();
98 if ( error
) return console
. error ( error
);
100 result
. body
. entries
. sort ( function ( a
, b
) { return a
. isDirectory
&& b
. isFile
? - 1 : 1 ; });
101 app
. entries
= result
. body
. entries
. map ( function ( entry
) {
102 entry
. previewUrl
= getPreviewUrl ( entry
, filePath
);
103 entry
. extension
= getExtension ( entry
);
107 app
. pathParts
= decode ( filePath
). split ( '/' ). filter ( function ( e
) { return !! e
; }). map ( function ( e
, i
, a
) {
110 link : '#' + sanitize ( '/' + a
. slice ( 0 , i
). join ( '/' ) + '/' + e
)
114 // update in case this was triggered from code
115 window
. location
. hash
= app
. path
;
119 function open ( row
, event
, column
) {
120 var path
= sanitize ( app
. path
+ '/' + row
. filePath
);
122 if ( row
. isDirectory
) {
123 window
. location
. hash
= path
;
127 window
. open ( encode ( path
));
130 function uploadFiles ( files
) {
131 if (! files
|| ! files
. length
) return ;
133 app
. uploadStatus
. busy
= true ;
134 app
. uploadStatus
. count
= files
. length
;
135 app
. uploadStatus
. size
= 0 ;
136 app
. uploadStatus
. done
= 0 ;
137 app
. uploadStatus
. percentDone
= 0 ;
139 for ( var i
= 0 ; i
< files
. length
; ++ i
) {
140 app
. uploadStatus
. size
+= files
[ i
]. size
;
143 asyncForEach ( files
, function ( file
, callback
) {
144 var path
= encode ( sanitize ( app
. path
+ '/' + ( file
. webkitRelativePath
|| file
. name
)));
146 var formData
= new FormData ();
147 formData
. append ( 'file' , file
);
149 var finishedUploadSize
= app
. uploadStatus
. done
;
151 superagent
. post ( '/api/files' + path
)
152 . query ({ access_token : localStorage
. accessToken
})
154 . on ( 'progress' , function ( event
) {
155 // only handle upload events
156 if (!( event
. target
instanceof XMLHttpRequestUpload
)) return ;
158 app
. uploadStatus
. done
= finishedUploadSize
+ event
. loaded
;
159 app
. uploadStatus
. percentDone
= Math
. round ( app
. uploadStatus
. done
/ app
. uploadStatus
. size
* 100 );
160 }). end ( function ( error
, result
) {
161 if ( result
&& result
. statusCode
=== 401 ) return logout ();
162 if ( result
&& result
. statusCode
!== 201 ) return callback ( 'Error uploading file: ' , result
. statusCode
);
163 if ( error
) return callback ( error
);
167 }, function ( error
) {
168 if ( error
) console
. error ( error
);
170 app
. uploadStatus
. busy
= false ;
171 app
. uploadStatus
. count
= 0 ;
172 app
. uploadStatus
. size
= 0 ;
173 app
. uploadStatus
. done
= 0 ;
174 app
. uploadStatus
. percentDone
= 100 ;
180 function dragOver ( event
) {
181 event
. stopPropagation ();
182 event
. preventDefault ();
183 event
. dataTransfer
. dropEffect
= 'copy' ;
186 function drop ( event
) {
187 event
. stopPropagation ();
188 event
. preventDefault ();
190 if (! event
. dataTransfer
. items
[ 0 ]) return ;
192 // figure if a folder was dropped on a modern browser, in this case the first would have to be a directory
195 folderItem
= event
. dataTransfer
. items
[ 0 ]. webkitGetAsEntry ();
196 if ( folderItem
. isFile
) return uploadFiles ( event
. dataTransfer
. files
);
198 return uploadFiles ( event
. dataTransfer
. files
);
201 // if we got here we have a folder drop and a modern browser
202 // now traverse the folder tree and create a file list
203 app
. uploadStatus
. busy
= true ;
204 app
. uploadStatus
. uploadListCount
= 0 ;
207 function traverseFileTree ( item
, path
, callback
) {
210 item
. file ( function ( file
) {
212 ++ app
. uploadStatus
. uploadListCount
;
215 } else if ( item
. isDirectory
) {
216 // Get folder contents
217 var dirReader
= item
. createReader ();
218 dirReader
. readEntries ( function ( entries
) {
219 asyncForEach ( entries
, function ( entry
, callback
) {
220 traverseFileTree ( entry
, path
+ item
. name
+ '/' , callback
);
226 traverseFileTree ( folderItem
, '' , function ( error
) {
227 app
. uploadStatus
. busy
= false ;
228 app
. uploadStatus
. uploadListCount
= 0 ;
230 if ( error
) return console
. error ( error
);
232 uploadFiles ( fileList
);
253 folderListingEnabled : false ,
262 onLogin : function () {
265 that
. loginData
. busy
= true ;
267 superagent
. post ( '/api/login' ). send ({ username : that
. loginData
. username
, password : that
. loginData
. password
}). end ( function ( error
, result
) {
268 that
. loginData
. busy
= false ;
270 if ( error
&& ! result
) return that
.$ message
. error ( error
. message
);
271 if ( result
. statusCode
=== 401 ) return that
.$ message
. error ( 'Wrong username or password' );
273 getProfile ( result
. body
. accessToken
, function ( error
) {
274 if ( error
) return console
. error ( error
);
276 loadDirectory ( window
. location
. hash
. slice ( 1 ));
280 onOptionsMenu : function ( command
) {
283 if ( command
=== 'folderListing' ) {
284 superagent
. put ( '/api/settings' ). send ({ folderListingEnabled : this . folderListingEnabled
}). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
) {
285 if ( error
) console
. error ( error
);
287 } else if ( command
=== 'about' ) {
289 title : 'About Surfer' ,
290 message : 'Surfer is a static file server written by <a href="https://cloudron.io" target="_blank">Cloudron</a>.<br/><br/>The source code is licensed under MIT and available <a href="https://git.cloudron.io/cloudron/surfer" target="_blank">here</a>.' ,
291 dangerouslyUseHTMLString : true ,
292 confirmButtonText : 'OK' ,
293 showCancelButton : false ,
296 }). then ( function () {}). catch ( function () {});
297 } else if ( command
=== 'logout' ) {
298 superagent
. post ( '/api/logout' ). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
) {
299 if ( error
) console
. error ( error
);
301 that
. session
. valid
= false ;
303 delete localStorage
. accessToken
;
307 onDownload : function ( entry
) {
308 if ( entry
. isDirectory
) return ;
309 window
. location
. href
= encode ( '/api/files/' + sanitize ( this . path
+ '/' + entry
. filePath
)) + '?access_token=' + localStorage
. accessToken
;
311 onUpload : function () {
314 $( this .$ refs
. upload
). on ( 'change' , function () {
315 // detach event handler
316 $( that
.$ refs
. upload
). off ( 'change' );
317 uploadFiles ( that
.$ refs
. upload
. files
|| []);
320 // reset the form first to make the change handler retrigger even on the same file selected
321 this .$ refs
. upload
. value
= '' ;
322 this .$ refs
. upload
. click ();
324 onUploadFolder : function () {
327 $( this .$ refs
. uploadFolder
). on ( 'change' , function () {
328 // detach event handler
329 $( that
.$ refs
. uploadFolder
). off ( 'change' );
330 uploadFiles ( that
.$ refs
. uploadFolder
. files
|| []);
333 // reset the form first to make the change handler retrigger even on the same file selected
334 this .$ refs
. uploadFolder
. value
= '' ;
335 this .$ refs
. uploadFolder
. click ();
337 onDelete : function ( entry
) {
340 var title
= 'Really delete ' + ( entry
. isDirectory
? 'folder ' : '' ) + entry
. filePath
;
341 this .$ confirm ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' }). then ( function () {
342 var path
= encode ( sanitize ( that
. path
+ '/' + entry
. filePath
));
344 superagent
. del ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
, recursive : true }). end ( function ( error
, result
) {
345 if ( result
&& result
. statusCode
=== 401 ) return logout ();
346 if ( result
&& result
. statusCode
!== 200 ) return that
.$ message
. error ( 'Error deleting file: ' + result
. statusCode
);
347 if ( error
) return that
.$ message
. error ( error
. message
);
351 }). catch ( function () {});
353 onRename : function ( entry
) {
356 var title
= 'Rename ' + entry
. filePath
;
357 this .$ prompt ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' , inputPlaceholder : 'new filename' , inputValue : entry
. filePath
}). then ( function ( data
) {
358 var path
= encode ( sanitize ( that
. path
+ '/' + entry
. filePath
));
359 var newFilePath
= sanitize ( that
. path
+ '/' + data
. value
);
361 superagent
. put ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
}). send ({ newFilePath : newFilePath
}). end ( function ( error
, result
) {
362 if ( result
&& result
. statusCode
=== 401 ) return logout ();
363 if ( result
&& result
. statusCode
!== 200 ) return that
.$ message
. error ( 'Error renaming file: ' + result
. statusCode
);
364 if ( error
) return that
.$ message
. error ( error
. message
);
368 }). catch ( function () {});
370 onNewFolder : function () {
373 var title
= 'Create New Folder' ;
374 this .$ prompt ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' , inputPlaceholder : 'new foldername' }). then ( function ( data
) {
375 var path
= encode ( sanitize ( that
. path
+ '/' + data
. value
));
377 superagent
. post ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
, directory : true }). end ( function ( error
, result
) {
378 if ( result
&& result
. statusCode
=== 401 ) return logout ();
379 if ( result
&& result
. statusCode
=== 403 ) return that
.$ message
. error ( 'Folder name not allowed' );
380 if ( result
&& result
. statusCode
=== 409 ) return that
.$ message
. error ( 'Folder already exists' );
381 if ( result
&& result
. statusCode
!== 201 ) return that
.$ message
. error ( 'Error creating folder: ' + result
. statusCode
);
382 if ( error
) return that
.$ message
. error ( error
. message
);
386 }). catch ( function () {});
388 prettyDate : function ( row
, column
, cellValue
, index
) {
389 var date
= new Date ( cellValue
),
390 diff
= ((( new Date ()). getTime () - date
. getTime ()) / 1000 ),
391 day_diff
= Math
. floor ( diff
/ 86400 );
393 if ( isNaN ( day_diff
) || day_diff
< 0 )
396 return day_diff
=== 0 && (
397 diff
< 60 && 'just now' ||
398 diff
< 120 && '1 minute ago' ||
399 diff
< 3600 && Math
. floor ( diff
/ 60 ) + ' minutes ago' ||
400 diff
< 7200 && '1 hour ago' ||
401 diff
< 86400 && Math
. floor ( diff
/ 3600 ) + ' hours ago' ) ||
402 day_diff
=== 1 && 'Yesterday' ||
403 day_diff
< 7 && day_diff
+ ' days ago' ||
404 day_diff
< 31 && Math
. ceil ( day_diff
/ 7 ) + ' weeks ago' ||
405 day_diff
< 365 && Math
. round ( day_diff
/ 30 ) + ' months ago' ||
406 Math
. round ( day_diff
/ 365 ) + ' years ago' ;
408 prettyFileSize : function ( row
, column
, cellValue
, index
) {
409 return filesize ( cellValue
);
411 loadDirectory : loadDirectory
,
413 window
. location
. hash
= sanitize ( this . path
. split ( '/' ). slice ( 0 , - 1 ). filter ( function ( p
) { return !! p
; }). join ( '/' ));
421 getProfile ( localStorage
. accessToken
, function ( error
) {
422 if ( error
) return console
. error ( error
);
424 loadDirectory ( window
. location
. hash
. slice ( 1 ));
427 $( window
). on ( 'hashchange' , function () {
428 loadDirectory ( window
. location
. hash
. slice ( 1 ));