2019-02-15 21:52:28 -05:00
$ ( function ( ) {
// Add a flash message
var flash = function ( category , message ) {
$ ( '#flashes' ) . append ( $ ( '<li>' ) . addClass ( category ) . text ( message ) ) ;
} ;
2020-05-28 01:06:06 -04:00
var scriptEls = document . getElementsByTagName ( 'script' ) ;
var thisScriptEl = scriptEls [ scriptEls . length - 1 ] ;
var scriptPath = thisScriptEl . src ;
var scriptFolder = scriptPath . substr ( 0 , scriptPath . lastIndexOf ( '/' ) + 1 ) ;
2019-02-17 12:51:19 -05:00
// Intercept submitting the form
$ ( '#send' ) . submit ( function ( event ) {
event . preventDefault ( ) ;
// Create form data, and list of filenames
var files = $ ( '#file-select' ) . get ( 0 ) . files ;
var filenames = [ ] ;
var formData = new FormData ( ) ;
for ( var i = 0 ; i < files . length ; i ++ ) {
var file = files [ i ] ;
filenames . push ( file . name ) ;
formData . append ( 'file[]' , file , file . name ) ;
}
// Reset the upload form
$ ( '#send' ) . get ( 0 ) . reset ( ) ;
// Don't use jQuery for ajax request, because the upload progress event doesn't
// have access to the the XMLHttpRequest object
var ajax = new XMLHttpRequest ( ) ;
ajax . upload . addEventListener ( 'progress' , function ( event ) {
// Update progress bar for this specific upload
if ( event . lengthComputable ) {
$ ( 'progress' , ajax . $upload _div ) . attr ( {
value : event . loaded ,
max : event . total ,
} ) ;
}
// If it's finished sending all data to the first Tor node, remove cancel button
// and update the status
if ( event . loaded == event . total ) {
$ ( '.cancel' , ajax . $upload _div ) . remove ( ) ;
2020-05-28 01:06:06 -04:00
$ ( '.upload-status' , ajax . $upload _div ) . html ( '<img src="' + scriptFolder + '../img/ajax.gif" alt="" /> Waiting for data to finish traversing Tor network ...' ) ;
2019-02-17 12:51:19 -05:00
}
} , false ) ;
ajax . addEventListener ( 'load' , function ( event ) {
// Remove the upload div
ajax . $upload _div . remove ( ) ;
// Parse response
try {
var response = JSON . parse ( ajax . response ) ;
// The 'new_body' response replaces the whole HTML document and ends
if ( 'new_body' in response ) {
$ ( 'body' ) . html ( response [ 'new_body' ] ) ;
return ;
}
// Show error flashes
if ( 'error_flashes' in response ) {
for ( var i = 0 ; i < response [ 'error_flashes' ] . length ; i ++ ) {
flash ( 'error' , response [ 'error_flashes' ] [ i ] ) ;
}
}
// Show info flashes
if ( 'info_flashes' in response ) {
for ( var i = 0 ; i < response [ 'info_flashes' ] . length ; i ++ ) {
flash ( 'info' , response [ 'info_flashes' ] [ i ] ) ;
}
}
} catch ( e ) {
flash ( 'error' , 'Invalid response from server: ' + data ) ;
}
} , false ) ;
ajax . addEventListener ( 'error' , function ( event ) {
2019-02-17 12:58:57 -05:00
flash ( 'error' , 'Error uploading: ' + filenames . join ( ', ' ) ) ;
// Remove the upload div
ajax . $upload _div . remove ( )
2019-02-17 12:51:19 -05:00
} , false ) ;
2019-02-15 21:52:28 -05:00
2019-02-17 12:51:19 -05:00
ajax . addEventListener ( 'abort' , function ( event ) {
flash ( 'error' , 'Upload aborted: ' + filenames . join ( ', ' ) ) ;
} , false ) ;
// Make the upload div
/ * T h e D O M f o r a n u p l o a d l o o k s s o m e t h i n g l i k e t h i s :
2019-02-15 21:52:28 -05:00
< div class = "upload" >
< div class = "upload-meta" >
< input class = "cancel" type = "button" value = "Cancel" / >
< div class = "upload-filename" > educational - video . mp4 , secret - plans . pdf < / d i v >
< div class = "upload-status" > Sending to first Tor node ... < / d i v >
< / d i v >
< progress value = "25" max = "100" > < / p r o g r e s s >
2019-02-17 12:51:19 -05:00
< /div> */
2019-02-15 21:52:28 -05:00
var $progress = $ ( '<progress>' ) . attr ( { value : '0' , max : 100 } ) ;
var $cancel _button = $ ( '<input>' ) . addClass ( 'cancel' ) . attr ( { type : 'button' , value : 'Cancel' } ) ;
var $upload _filename = $ ( '<div>' ) . addClass ( 'upload-filename' ) . text ( filenames . join ( ', ' ) ) ;
var $upload _status = $ ( '<div>' ) . addClass ( 'upload-status' ) . text ( 'Sending data to initial Tor node ...' ) ;
2019-02-17 12:51:19 -05:00
var $upload _div = $ ( '<div>' )
. addClass ( 'upload' )
2019-02-15 21:52:28 -05:00
. append (
$ ( '<div>' ) . addClass ( 'upload-meta' )
. append ( $cancel _button )
. append ( $upload _filename )
. append ( $upload _status )
)
. append ( $progress ) ;
$cancel _button . click ( function ( ) {
// Abort the upload, and remove the upload div
2019-02-17 12:51:19 -05:00
ajax . abort ( ) ;
2019-02-15 21:52:28 -05:00
$upload _div . remove ( )
} ) ;
2019-02-17 12:51:19 -05:00
ajax . $upload _div = $upload _div ;
$ ( '#uploads' ) . append ( $upload _div ) ;
2019-02-15 17:52:00 -05:00
2019-02-17 12:51:19 -05:00
// Send the request
2019-05-21 13:18:40 -04:00
ajax . open ( 'POST' , '/upload-ajax' , true ) ;
2019-02-17 12:51:19 -05:00
ajax . send ( formData ) ;
2019-02-15 21:52:28 -05:00
} ) ;
} ) ;