2017-06-22 04:45:56 +02:00
// define variables
var socket = io ( ) ;
var uploader = new SocketIOFileUpload ( socket ) ;
var stagedFiles = null ;
/* configure the submit button */
2017-07-08 01:08:35 +02:00
function publishSelectedImage ( event ) {
2017-06-22 04:45:56 +02:00
event . preventDefault ( ) ;
2017-07-08 01:08:35 +02:00
// validate inputs
2017-06-23 19:06:43 +02:00
var name = document . getElementById ( 'publish-name' ) . value ;
2017-07-08 01:08:35 +02:00
try {
validateSubmission ( stagedFiles , name ) ;
} catch ( error ) {
2017-07-14 17:57:12 +02:00
if ( error . name === 'FileError' ) {
showError ( 'input-error-file-selection' , error . message ) ;
} else if ( error . name === 'NameError' ) {
showError ( 'input-error-claim-name' , error . message ) ;
} else {
showError ( 'input-error-publish-submit' , error . message ) ;
}
2017-06-23 19:06:43 +02:00
return ;
2017-06-22 04:45:56 +02:00
}
2017-07-08 05:26:46 +02:00
// make sure the name is available then start the upload
validateClaimName ( name )
2017-07-08 06:20:41 +02:00
. then ( function ( ) {
2017-07-08 05:26:46 +02:00
uploader . submitFiles ( stagedFiles ) ; //note: must pass the file as part of an array.
} )
. catch ( function ( error ) {
2017-07-14 17:57:12 +02:00
showError ( 'input-error-claim-name' , error ) ;
2017-07-08 05:26:46 +02:00
} )
2017-07-08 01:08:35 +02:00
} ;
2017-06-22 04:45:56 +02:00
/* socketio-file-upload listeners */
uploader . addEventListener ( 'start' , function ( event ) {
var name = document . getElementById ( 'publish-name' ) . value ;
var license = document . getElementById ( 'publish-license' ) . value ;
2017-06-22 19:08:41 +02:00
var nsfw = document . getElementById ( 'publish-nsfw' ) . checked ;
2017-06-22 04:45:56 +02:00
event . file . meta . name = name ;
event . file . meta . license = license ;
event . file . meta . nsfw = nsfw ;
event . file . meta . type = stagedFiles [ 0 ] . type ;
// re-set the html in the publish area
document . getElementById ( 'publish-active-area' ) . innerHTML = '<div id="publish-status"></div><div id="progress-bar"></div>' ;
// start a progress animation
createProgressBar ( document . getElementById ( 'progress-bar' ) , 12 ) ;
2017-06-30 07:26:29 +02:00
// google analytics
ga ( 'send' , {
hitType : 'event' ,
eventCategory : 'publish' ,
eventAction : name
} ) ;
2017-06-22 04:45:56 +02:00
} ) ;
uploader . addEventListener ( 'progress' , function ( event ) {
var percent = event . bytesLoaded / event . file . size * 100 ;
updatePublishStatus ( 'File is ' + percent . toFixed ( 2 ) + '% loaded to the server' ) ;
} ) ;
/* socket.io message listeners */
socket . on ( 'publish-status' , function ( msg ) {
updatePublishStatus ( msg ) ;
} ) ;
socket . on ( 'publish-failure' , function ( msg ) {
2017-07-07 02:38:57 +02:00
document . getElementById ( 'publish-active-area' ) . innerHTML = '<p> --(✖╭╮✖)→ </p><p>' + JSON . stringify ( msg ) + '</p><strong>For help, post the above error text in the #speech channel on the <a href="https://lbry.slack.com/" target="_blank">lbry slack</a></strong>' ;
2017-06-22 04:45:56 +02:00
} ) ;
socket . on ( 'publish-complete' , function ( msg ) {
var publishResults ;
2017-07-07 03:03:29 +02:00
var showUrl = '/show/' + msg . name + '/' + msg . result . claim _id ;
2017-06-22 04:45:56 +02:00
// build new publish area
2017-07-07 02:38:57 +02:00
publishResults = '<p>Your publish is complete! You are being redirected to it now.</p>' ;
publishResults += '<p><a target="_blank" href="' + showUrl + '">If you do not get redirected, click here.</a></p>' ;
2017-06-22 04:45:56 +02:00
// update publish area
document . getElementById ( 'publish-active-area' ) . innerHTML = publishResults ;
2017-07-07 02:38:57 +02:00
window . location . href = showUrl ;
2017-06-22 04:45:56 +02:00
} ) ;