]>
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 app
. uploadStatus
. percentDone
= Math
. round ( app
. uploadStatus
. done
/ app
. uploadStatus
. size
* 100 );
175 }). end ( function ( error
, result
) {
176 if ( result
&& result
. statusCode
=== 401 ) return logout ();
177 if ( result
&& result
. statusCode
!== 201 ) return callback ( 'Error uploading file: ' , result
. statusCode
);
178 if ( error
) return callback ( error
);
182 }, function ( error
) {
183 if ( error
) console
. error ( error
);
185 app
. uploadStatus
. busy
= false ;
186 app
. uploadStatus
. count
= 0 ;
187 app
. uploadStatus
. size
= 0 ;
188 app
. uploadStatus
. done
= 0 ;
189 app
. uploadStatus
. percentDone
= 100 ;
195 function dragOver ( event
) {
196 event
. stopPropagation ();
197 event
. preventDefault ();
198 event
. dataTransfer
. dropEffect
= 'copy' ;
201 function drop ( event
) {
202 event
. stopPropagation ();
203 event
. preventDefault ();
205 if (! event
. dataTransfer
. items
[ 0 ]) return ;
207 // figure if a folder was dropped on a modern browser, in this case the first would have to be a directory
210 folderItem
= event
. dataTransfer
. items
[ 0 ]. webkitGetAsEntry ();
211 if ( folderItem
. isFile
) return uploadFiles ( event
. dataTransfer
. files
);
213 return uploadFiles ( event
. dataTransfer
. files
);
216 // if we got here we have a folder drop and a modern browser
217 // now traverse the folder tree and create a file list
218 app
. uploadStatus
. busy
= true ;
219 app
. uploadStatus
. uploadListCount
= 0 ;
222 function traverseFileTree ( item
, path
, callback
) {
225 item
. file ( function ( file
) {
227 ++ app
. uploadStatus
. uploadListCount
;
230 } else if ( item
. isDirectory
) {
231 // Get folder contents
232 var dirReader
= item
. createReader ();
233 dirReader
. readEntries ( function ( entries
) {
234 asyncForEach ( entries
, function ( entry
, callback
) {
235 traverseFileTree ( entry
, path
+ item
. name
+ '/' , callback
);
241 traverseFileTree ( folderItem
, '' , function ( error
) {
242 app
. uploadStatus
. busy
= false ;
243 app
. uploadStatus
. uploadListCount
= 0 ;
245 if ( error
) return console
. error ( error
);
247 uploadFiles ( fileList
);
268 folderListingEnabled : false ,
277 onLogin : function () {
280 that
. loginData
. busy
= true ;
282 superagent
. post ( '/api/login' ). send ({ username : that
. loginData
. username
, password : that
. loginData
. password
}). end ( function ( error
, result
) {
283 that
. loginData
. busy
= false ;
285 if ( error
&& ! result
) return that
.$ message
. error ( error
. message
);
286 if ( result
. statusCode
=== 401 ) return that
.$ message
. error ( 'Wrong username or password' );
288 getProfile ( result
. body
. accessToken
, function ( error
) {
289 if ( error
) return console
. error ( error
);
291 loadDirectory ( window
. location
. hash
. slice ( 1 ));
295 onOptionsMenu : function ( command
) {
296 if ( command
=== 'folderListing' ) {
297 superagent
. put ( '/api/settings' ). send ({ folderListingEnabled : this . folderListingEnabled
}). query ({ access_token : localStorage
. accessToken
}). end ( function ( error
) {
298 if ( error
) console
. error ( error
);
300 } else if ( command
=== 'about' ) {
302 title : 'About Surfer' ,
303 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>.' ,
304 dangerouslyUseHTMLString : true ,
305 confirmButtonText : 'OK' ,
306 showCancelButton : false ,
309 }). then ( function () {}). catch ( function () {});
310 } else if ( command
=== 'logout' ) {
314 onDownload : function ( entry
) {
315 if ( entry
. isDirectory
) return ;
316 window
. location
. href
= encode ( '/api/files/' + sanitize ( this . path
+ '/' + entry
. filePath
)) + '?access_token=' + localStorage
. accessToken
;
318 onUpload : function () {
321 $( this .$ refs
. upload
). on ( 'change' , function () {
322 // detach event handler
323 $( that
.$ refs
. upload
). off ( 'change' );
324 uploadFiles ( that
.$ refs
. upload
. files
|| []);
327 // reset the form first to make the change handler retrigger even on the same file selected
328 this .$ refs
. upload
. value
= '' ;
329 this .$ refs
. upload
. click ();
331 onUploadFolder : function () {
334 $( this .$ refs
. uploadFolder
). on ( 'change' , function () {
335 // detach event handler
336 $( that
.$ refs
. uploadFolder
). off ( 'change' );
337 uploadFiles ( that
.$ refs
. uploadFolder
. files
|| []);
340 // reset the form first to make the change handler retrigger even on the same file selected
341 this .$ refs
. uploadFolder
. value
= '' ;
342 this .$ refs
. uploadFolder
. click ();
344 onDelete : function ( entry
) {
347 var title
= 'Really delete ' + ( entry
. isDirectory
? 'folder ' : '' ) + entry
. filePath
;
348 this .$ confirm ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' }). then ( function () {
349 var path
= encode ( sanitize ( that
. path
+ '/' + entry
. filePath
));
351 superagent
. del ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
, recursive : true }). end ( function ( error
, result
) {
352 if ( result
&& result
. statusCode
=== 401 ) return logout ();
353 if ( result
&& result
. statusCode
!== 200 ) return that
.$ message
. error ( 'Error deleting file: ' + result
. statusCode
);
354 if ( error
) return that
.$ message
. error ( error
. message
);
358 }). catch ( function () {});
360 onRename : function ( entry
) {
363 var title
= 'Rename ' + entry
. filePath
;
364 this .$ prompt ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' , inputPlaceholder : 'new filename' , inputValue : entry
. filePath
}). then ( function ( data
) {
365 var path
= encode ( sanitize ( that
. path
+ '/' + entry
. filePath
));
366 var newFilePath
= sanitize ( that
. path
+ '/' + data
. value
);
368 superagent
. put ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
}). send ({ newFilePath : newFilePath
}). end ( function ( error
, result
) {
369 if ( result
&& result
. statusCode
=== 401 ) return logout ();
370 if ( result
&& result
. statusCode
!== 200 ) return that
.$ message
. error ( 'Error renaming file: ' + result
. statusCode
);
371 if ( error
) return that
.$ message
. error ( error
. message
);
375 }). catch ( function () {});
377 onNewFolder : function () {
380 var title
= 'Create New Folder' ;
381 this .$ prompt ( '' , title
, { confirmButtonText : 'Yes' , cancelButtonText : 'No' , inputPlaceholder : 'new foldername' }). then ( function ( data
) {
382 var path
= encode ( sanitize ( that
. path
+ '/' + data
. value
));
384 superagent
. post ( '/api/files' + path
). query ({ access_token : localStorage
. accessToken
, directory : true }). end ( function ( error
, result
) {
385 if ( result
&& result
. statusCode
=== 401 ) return logout ();
386 if ( result
&& result
. statusCode
=== 403 ) return that
.$ message
. error ( 'Folder name not allowed' );
387 if ( result
&& result
. statusCode
=== 409 ) return that
.$ message
. error ( 'Folder already exists' );
388 if ( result
&& result
. statusCode
!== 201 ) return that
.$ message
. error ( 'Error creating folder: ' + result
. statusCode
);
389 if ( error
) return that
.$ message
. error ( error
. message
);
393 }). catch ( function () {});
395 prettyDate : function ( row
, column
, cellValue
, index
) {
396 var date
= new Date ( cellValue
),
397 diff
= ((( new Date ()). getTime () - date
. getTime ()) / 1000 ),
398 day_diff
= Math
. floor ( diff
/ 86400 );
400 if ( isNaN ( day_diff
) || day_diff
< 0 )
403 return day_diff
=== 0 && (
404 diff
< 60 && 'just now' ||
405 diff
< 120 && '1 minute ago' ||
406 diff
< 3600 && Math
. floor ( diff
/ 60 ) + ' minutes ago' ||
407 diff
< 7200 && '1 hour ago' ||
408 diff
< 86400 && Math
. floor ( diff
/ 3600 ) + ' hours ago' ) ||
409 day_diff
=== 1 && 'Yesterday' ||
410 day_diff
< 7 && day_diff
+ ' days ago' ||
411 day_diff
< 31 && Math
. ceil ( day_diff
/ 7 ) + ' weeks ago' ||
412 day_diff
< 365 && Math
. round ( day_diff
/ 30 ) + ' months ago' ||
413 Math
. round ( day_diff
/ 365 ) + ' years ago' ;
415 prettyFileSize : function ( row
, column
, cellValue
, index
) {
416 return filesize ( cellValue
);
418 loadDirectory : loadDirectory
,
420 window
. location
. hash
= sanitize ( this . path
. split ( '/' ). slice ( 0 , - 1 ). filter ( function ( p
) { return !! p
; }). join ( '/' ));
428 getProfile ( localStorage
. accessToken
, function ( error
) {
429 if ( error
) return console
. error ( error
);
431 loadDirectory ( window
. location
. hash
. slice ( 1 ));
434 $( window
). on ( 'hashchange' , function () {
435 loadDirectory ( window
. location
. hash
. slice ( 1 ));