]>
git.immae.eu Git - perso/Immae/Projets/Nodejs/Surfer.git/blob - frontend/js/app.js
4 /* global superagent */
10 function asyncForEach ( items
, handler
, callback
) {
13 if ( items
. length
=== 0 ) return callback ();
15 ( function iterator () {
16 handler ( items
[ cur
], function ( error
) {
17 if ( error
) return callback ( error
);
18 if ( cur
>= items
. length
- 1 ) return callback ();
26 function getProfile ( accessToken
, callback
) {
27 superagent
. get ( '/api/profile' ). query ({ access_token : accessToken
}). end ( function ( error
, result
) {
30 if ( error
&& ! error
. response
) return callback ( error
);
31 if ( result
. statusCode
!== 200 ) {
32 delete localStorage
. accessToken
;
33 return callback ( 'Invalid access token' );
36 localStorage
. accessToken
= accessToken
;
37 app
. session
. username
= result
. body
. username
;
38 app
. session
. valid
= true ;
40 superagent
. get ( '/api/settings' ). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
, result
) {
41 if ( error
) console
. error ( error
);
43 app
. folderListingEnabled
= !! result
. body
. folderListingEnabled
;
50 function sanitize ( filePath
) {
51 filePath
= '/' + filePath
;
52 return filePath
. replace ( /\/+/g , '/' );
55 function encode ( filePath
) {
56 return filePath
. split ( '/' ). map ( encodeURIComponent
). join ( '/' );
59 function decode ( filePath
) {
60 return filePath
. split ( '/' ). map ( decodeURIComponent
). join ( '/' );
64 images : [ '.png' , '.jpg' , '.jpeg' , '.tiff' , '.gif' ],
65 text : [ '.txt' , '.md' ],
67 html : [ '.html' , '.htm' , '.php' ],
68 video : [ '.mp4' , '.mpg' , '.mpeg' , '.ogg' , '.mkv' , '.avi' , '.mov' ]
71 function getPreviewUrl ( entry
, basePath
) {
72 var path
= '/_admin/img/' ;
74 if ( entry
. isDirectory
) return path
+ 'directory.png' ;
75 if ( mimeTypes
. images
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return sanitize ( basePath
+ '/' + entry
. filePath
);
76 if ( mimeTypes
. text
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'text.png' ;
77 if ( mimeTypes
. pdf
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'pdf.png' ;
78 if ( mimeTypes
. html
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'html.png' ;
79 if ( mimeTypes
. video
. some ( function ( e
) { return entry
. filePath
. endsWith ( e
); })) return path
+ 'video.png' ;
81 return path
+ 'unknown.png' ;
84 // simple extension detection, does not work with double extension like .tar.gz
85 function getExtension ( entry
) {
86 if ( entry
. isFile
) return entry
. filePath
. slice ( entry
. filePath
. lastIndexOf ( '.' ) + 1 );
91 loadDirectory ( app
. path
);
95 superagent
. post ( '/api/logout' ). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
) {
96 if ( error
) console
. error ( error
);
98 app
. session
. valid
= false ;
100 delete localStorage
. accessToken
;
104 function loadDirectory ( filePath
) {
107 filePath
= filePath
? sanitize ( filePath
) : '/' ;
109 superagent
. get ( '/api/files/' + encode ( filePath
)). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
, result
) {
112 if ( result
&& result
. statusCode
=== 401 ) return logout ();
113 if ( error
) return console
. error ( error
);
115 result
. body
. entries
. sort ( function ( a
, b
) { return a
. isDirectory
&& b
. isFile
? - 1 : 1 ; });
116 app
. entries
= result
. body
. entries
. map ( function ( entry
) {
117 entry
. previewUrl
= getPreviewUrl ( entry
, filePath
);
118 entry
. extension
= getExtension ( entry
);
122 app
. pathParts
= decode ( filePath
). split ( '/' ). filter ( function ( e
) { return !! e
; }). map ( function ( e
, i
, a
) {
125 link : '#' + sanitize ( '/' + a
. slice ( 0 , i
). join ( '/' ) + '/' + e
)
129 // update in case this was triggered from code
130 window
. location
. hash
= app
. path
;
134 function open ( row
, event
, column
) {
135 var path
= sanitize ( app
. path
+ '/' + row
. filePath
);
137 if ( row
. isDirectory
) {
138 window
. location
. hash
= path
;
142 window
. open ( encode ( path
));
145 function uploadFiles ( files
) {
146 if (! files
|| ! files
. length
) return ;
148 app
. uploadStatus
. busy
= true ;
149 app
. uploadStatus
. count
= files
. length
;
150 app
. uploadStatus
. size
= 0 ;
151 app
. uploadStatus
. done
= 0 ;
152 app
. uploadStatus
. percentDone
= 0 ;
154 for ( var i
= 0 ; i
< files
. length
; ++ i
) {
155 app
. uploadStatus
. size
+= files
[ i
]. size
;
158 asyncForEach ( files
, function ( file
, callback
) {
159 var path
= encode ( sanitize ( app
. path
+ '/' + ( file
. webkitRelativePath
|| file
. name
)));
161 var formData
= new FormData ();
162 formData
. append ( 'file' , file
);
164 var finishedUploadSize
= app
. uploadStatus
. done
;
166 superagent
. post ( '/api/files' + path
)
167 . query ({ access_token : localStorage
. accessToken
})
169 . on ( 'progress' , function ( event
) {
170 // only handle upload events
171 if (!( event
. target
instanceof XMLHttpRequestUpload
)) return ;
173 app
. uploadStatus
. done
= finishedUploadSize
+ event
. loaded
;
174 var tmp
= Math
. round ( app
. uploadStatus
. done
/ app
. uploadStatus
. size
* 100 );
175 app
. uploadStatus
. percentDone
= tmp
> 100 ? 100 : tmp
;
176 }). end ( function ( error
, result
) {
177 if ( result
&& result
. statusCode
=== 401 ) return logout ();
178 if ( result
&& result
. statusCode
!== 201 ) return callback ( 'Error uploading file: ' , result
. statusCode
);
179 if ( error
) return callback ( error
);
183 }, function ( error
) {
184 if ( error
) console
. error ( error
);
186 app
. uploadStatus
. busy
= false ;
187 app
. uploadStatus
. count
= 0 ;
188 app
. uploadStatus
. size
= 0 ;
189 app
. uploadStatus
. done
= 0 ;
190 app
. uploadStatus
. percentDone
= 100 ;
196 function dragOver ( event
) {
197 event
. stopPropagation ();
198 event
. preventDefault ();
199 event
. dataTransfer
. dropEffect
= 'copy' ;
202 function drop ( event
) {
203 event
. stopPropagation ();
204 event
. preventDefault ();
206 if (! event
. dataTransfer
. items
[ 0 ]) return ;
208 // figure if a folder was dropped on a modern browser, in this case the first would have to be a directory
211 folderItem
= event
. dataTransfer
. items
[ 0 ]. webkitGetAsEntry ();
212 if ( folderItem
. isFile
) return uploadFiles ( event
. dataTransfer
. files
);
214 return uploadFiles ( event
. dataTransfer
. files
);
217 // if we got here we have a folder drop and a modern browser
218 // now traverse the folder tree and create a file list
219 app
. uploadStatus
. busy
= true ;
220 app
. uploadStatus
. uploadListCount
= 0 ;
223 function traverseFileTree ( item
, path
, callback
) {
226 item
. file ( function ( file
) {
228 ++ app
. uploadStatus
. uploadListCount
;
231 } else if ( item
. isDirectory
) {
232 // Get folder contents
233 var dirReader
= item
. createReader ();
234 dirReader
. readEntries ( function ( entries
) {
235 asyncForEach ( entries
, function ( entry
, callback
) {
236 traverseFileTree ( entry
, path
+ item
. name
+ '/' , callback
);
242 traverseFileTree ( folderItem
, '' , function ( error
) {
243 app
. uploadStatus
. busy
= false ;
244 app
. uploadStatus
. uploadListCount
= 0 ;
246 if ( error
) return console
. error ( error
);
248 uploadFiles ( fileList
);
269 folderListingEnabled : false ,
278 onLogin : function () {
281 that
. loginData
. busy
= true ;
283 superagent
. post ( '/api/login' ). send ({ username : that
. loginData
. username
, password : that
. loginData
. password
}). end ( function ( error
, result
) {
284 that
. loginData
. busy
= false ;
286 if ( error
&& ! result
) return that
.$ message
. error ( error
. message
);
287 if ( result
. statusCode
=== 401 ) return that
.$ message
. error ( 'Wrong username or password' );
289 getProfile ( result
. body
. accessToken
, function ( error
) {
290 if ( error
) return console
. error ( error
);
292 loadDirectory ( decode ( window
. location
. hash
. slice ( 1 )));
296 onOptionsMenu : function ( command
) {
297 if ( command
=== 'folderListing' ) {
298 superagent
. put ( '/api/settings' ). send ({ folderListingEnabled : this . folderListingEnabled
}). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
) {
299 if ( error
) console
. error ( error
);
301 } else if ( command
=== 'about' ) {
303 title : 'About Surfer' ,
304 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>.' ,
305 dangerouslyUseHTMLString : true ,
306 confirmButtonText : 'OK' ,
307 showCancelButton : false ,
310 }). then ( function () {}). catch ( function () {});
311 } else if ( command
=== 'logout' ) {
315 onDownload : function ( entry
) {
316 if ( entry
. isDirectory
) return ;
317 window
. location
. href
= encode ( '/api/files/' + sanitize ( this . path
+ '/' + entry
. filePath
)) + '?access_token=' + localStorage
. accessToken
;
319 onUpload : function () {
322 $( this .$ refs
. upload
). on ( 'change' , function () {
323 // detach event handler
324 $( that
.$ refs
. upload
). off ( 'change' );
325 uploadFiles ( that
.$ refs
. upload
. files
|| []);
328 // reset the form first to make the change handler retrigger even on the same file selected
329 this .$ refs
. upload
. value
= '' ;
330 this .$ refs
. upload
. click ();
332 onUploadFolder : function () {
335 $( this .$ refs
. uploadFolder
). on ( 'change' , function () {
336 // detach event handler
337 $( that
.$ refs
. uploadFolder
). off ( 'change' );
338 uploadFiles ( that
.$ refs
. uploadFolder
. files
|| []);
341 // reset the form first to make the change handler retrigger even on the same file selected
342 this .$ refs
. uploadFolder
. value
= '' ;
343 this .$ refs
. uploadFolder
. click ();
345 onDelete : function ( entry
) {
348 var title
= 'Really delete ' + ( entry
. isDirectory
? 'folder ' : '' ) + entry
. filePath
;
349 this .$ confirm ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' }). then ( function () {
350 var path
= encode ( sanitize ( that
. path
+ '/' + entry
. filePath
));
352 superagent
. del ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
, recursive : true }). end ( function ( error
, result
) {
353 if ( result
&& result
. statusCode
=== 401 ) return logout ();
354 if ( result
&& result
. statusCode
!== 200 ) return that
.$ message
. error ( 'Error deleting file: ' + result
. statusCode
);
355 if ( error
) return that
.$ message
. error ( error
. message
);
359 }). catch ( function () {});
361 onRename : function ( entry
) {
364 var title
= 'Rename ' + entry
. filePath
;
365 this .$ prompt ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' , inputPlaceholder : 'new filename' , inputValue : entry
. filePath
}). then ( function ( data
) {
366 var path
= encode ( sanitize ( that
. path
+ '/' + entry
. filePath
));
367 var newFilePath
= sanitize ( that
. path
+ '/' + data
. value
);
369 superagent
. put ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
}). send ({ newFilePath : newFilePath
}). end ( function ( error
, result
) {
370 if ( result
&& result
. statusCode
=== 401 ) return logout ();
371 if ( result
&& result
. statusCode
!== 200 ) return that
.$ message
. error ( 'Error renaming file: ' + result
. statusCode
);
372 if ( error
) return that
.$ message
. error ( error
. message
);
376 }). catch ( function () {});
378 onNewFolder : function () {
381 var title
= 'Create New Folder' ;
382 this .$ prompt ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' , inputPlaceholder : 'new foldername' }). then ( function ( data
) {
383 var path
= encode ( sanitize ( that
. path
+ '/' + data
. value
));
385 superagent
. post ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
, directory : true }). end ( function ( error
, result
) {
386 if ( result
&& result
. statusCode
=== 401 ) return logout ();
387 if ( result
&& result
. statusCode
=== 403 ) return that
.$ message
. error ( 'Folder name not allowed' );
388 if ( result
&& result
. statusCode
=== 409 ) return that
.$ message
. error ( 'Folder already exists' );
389 if ( result
&& result
. statusCode
!== 201 ) return that
.$ message
. error ( 'Error creating folder: ' + result
. statusCode
);
390 if ( error
) return that
.$ message
. error ( error
. message
);
394 }). catch ( function () {});
396 prettyDate : function ( row
, column
, cellValue
, index
) {
397 var date
= new Date ( cellValue
),
398 diff
= ((( new Date ()). getTime () - date
. getTime ()) / 1000 ),
399 day_diff
= Math
. floor ( diff
/ 86400 );
401 if ( isNaN ( day_diff
) || day_diff
< 0 )
404 return day_diff
=== 0 && (
405 diff
< 60 && 'just now' ||
406 diff
< 120 && '1 minute ago' ||
407 diff
< 3600 && Math
. floor ( diff
/ 60 ) + ' minutes ago' ||
408 diff
< 7200 && '1 hour ago' ||
409 diff
< 86400 && Math
. floor ( diff
/ 3600 ) + ' hours ago' ) ||
410 day_diff
=== 1 && 'Yesterday' ||
411 day_diff
< 7 && day_diff
+ ' days ago' ||
412 day_diff
< 31 && Math
. ceil ( day_diff
/ 7 ) + ' weeks ago' ||
413 day_diff
< 365 && Math
. round ( day_diff
/ 30 ) + ' months ago' ||
414 Math
. round ( day_diff
/ 365 ) + ' years ago' ;
416 prettyFileSize : function ( row
, column
, cellValue
, index
) {
417 return filesize ( cellValue
);
419 loadDirectory : loadDirectory
,
421 window
. location
. hash
= sanitize ( this . path
. split ( '/' ). slice ( 0 , - 1 ). filter ( function ( p
) { return !! p
; }). join ( '/' ));
429 getProfile ( localStorage
. accessToken
, function ( error
) {
430 if ( error
) return console
. error ( error
);
432 loadDirectory ( decode ( window
. location
. hash
. slice ( 1 )));
435 $( window
). on ( 'hashchange' , function () {
436 loadDirectory ( decode ( window
. location
. hash
. slice ( 1 )));