2017-11-02 15:12:26 -07:00
var stagedFiles = null ;
2017-11-01 13:07:13 -07:00
2017-11-13 15:21:26 -05:00
const publishFileFunctions = {
2017-11-02 15:12:26 -07:00
cancelPublish : function ( ) {
window . location . href = '/' ;
} ,
2018-01-02 17:12:57 -08:00
2017-11-02 15:12:26 -07:00
hidePrimaryDropzone : function ( ) {
const primaryDropzone = document . getElementById ( 'primary-dropzone' ) ;
const publishForm = document . getElementById ( 'publish-form' ) ;
primaryDropzone . setAttribute ( 'class' , 'hidden' ) ;
publishForm . setAttribute ( 'class' , 'row' )
} ,
2017-11-01 17:14:38 -07:00
returnNullOrChannel : function ( ) {
2017-11-03 13:37:23 -07:00
const channelRadio = document . getElementById ( 'channel-radio' ) ;
if ( channelRadio . checked ) {
const channelInput = document . getElementById ( 'channel-name-select' ) ;
return channelInput . value . trim ( ) ;
2017-11-01 13:07:13 -07:00
}
2017-11-03 13:37:23 -07:00
return null ;
2017-11-01 17:14:38 -07:00
} ,
createMetadata : function ( ) {
const nameInput = document . getElementById ( 'claim-name-input' ) ;
const titleInput = document . getElementById ( 'publish-title' ) ;
const descriptionInput = document . getElementById ( 'publish-description' ) ;
const licenseInput = document . getElementById ( 'publish-license' ) ;
const nsfwInput = document . getElementById ( 'publish-nsfw' ) ;
const thumbnailInput = document . getElementById ( 'claim-thumbnail-input' ) ;
2017-12-15 11:02:04 -08:00
const channelName = this . returnNullOrChannel ( ) ;
let metadata = {
2017-11-01 17:14:38 -07:00
name : nameInput . value . trim ( ) ,
title : titleInput . value . trim ( ) ,
description : descriptionInput . value . trim ( ) ,
license : licenseInput . value . trim ( ) ,
nsfw : nsfwInput . checked ,
type : stagedFiles [ 0 ] . type ,
2017-11-03 13:37:23 -07:00
thumbnail : thumbnailInput . value . trim ( ) ,
2017-12-15 11:02:04 -08:00
} ;
if ( channelName ) {
metadata [ 'channelName' ] = channelName ;
2017-11-01 17:14:38 -07:00
}
2017-12-15 11:02:04 -08:00
return metadata ;
2017-11-01 17:14:38 -07:00
} ,
appendDataToFormData : function ( file , metadata ) {
var fd = new FormData ( ) ;
fd . append ( 'file' , file )
for ( var key in metadata ) {
if ( metadata . hasOwnProperty ( key ) ) {
console . log ( key , metadata [ key ] ) ;
fd . append ( key , metadata [ key ] ) ;
}
}
return fd ;
} ,
publishFile : function ( file , metadata ) {
2017-12-05 10:18:49 -08:00
var uri = "/api/claim-publish" ;
2017-11-01 17:14:38 -07:00
var xhr = new XMLHttpRequest ( ) ;
var fd = this . appendDataToFormData ( file , metadata ) ;
2017-11-02 15:12:26 -07:00
var that = this ;
2017-11-01 17:14:38 -07:00
xhr . upload . addEventListener ( "loadstart" , function ( e ) {
2017-11-02 15:12:26 -07:00
that . showUploadStartedMessage ( ) ;
2017-11-01 17:14:38 -07:00
} )
xhr . upload . addEventListener ( "progress" , function ( e ) {
if ( e . lengthComputable ) {
var percentage = Math . round ( ( e . loaded * 100 ) / e . total ) ;
console . log ( 'progress:' , percentage ) ;
2017-11-02 15:12:26 -07:00
that . showUploadProgressMessage ( percentage ) ;
2017-11-01 17:14:38 -07:00
}
} , false ) ;
xhr . upload . addEventListener ( "load" , function ( e ) {
console . log ( 'loaded 100%' ) ;
2017-11-02 15:12:26 -07:00
that . showFilePublishUpdate ( "Your file has been loaded, and is now being published to the blockchain. Sit tight..." )
2017-11-01 17:14:38 -07:00
} , false ) ;
xhr . open ( "POST" , uri , true ) ;
xhr . onreadystatechange = function ( ) {
if ( xhr . readyState == 4 ) {
2017-12-12 21:55:07 -08:00
console . log ( 'publish response:' , xhr . response )
2017-11-01 17:14:38 -07:00
if ( xhr . status == 200 ) {
2017-12-12 21:55:07 -08:00
console . log ( 'publish complete!' ) ;
that . showFilePublishComplete ( JSON . parse ( xhr . response ) . message ) ;
} else if ( xhr . status == 502 ) {
that . showFilePublishFailure ( 'Spee.ch was not able to get a response from the LBRY network.' ) ;
2017-11-01 17:14:38 -07:00
} else {
2017-12-12 21:55:07 -08:00
that . showFilePublishFailure ( JSON . parse ( xhr . response ) . message ) ;
2017-11-01 17:14:38 -07:00
}
}
} ;
// Initiate a multipart/form-data upload
xhr . send ( fd ) ;
} ,
// Validate the publish submission and then trigger upload
publishStagedFile : function ( event ) {
event . preventDefault ( ) ; // prevent default so this script can handle submission
const metadata = this . createMetadata ( ) ;
2017-12-15 11:02:04 -08:00
const that = this ;
2017-11-01 17:14:38 -07:00
const fileSelectionInputError = document . getElementById ( 'input-error-file-selection' ) ;
const claimNameError = document . getElementById ( 'input-error-claim-name' ) ;
const channelSelectError = document . getElementById ( 'input-error-channel-select' ) ;
const publishSubmitError = document . getElementById ( 'input-error-publish-submit' ) ;
// validate, submit, and handle response
2017-11-02 15:47:55 -07:00
validationFunctions . validateFilePublishSubmission ( stagedFiles , metadata )
2017-11-01 17:14:38 -07:00
. then ( function ( ) {
that . publishFile ( stagedFiles [ 0 ] , metadata ) ;
} )
. catch ( error => {
if ( error . name === 'FileError' ) {
2017-11-02 15:47:55 -07:00
validationFunctions . showError ( fileSelectionInputError , error . message ) ;
2017-11-01 17:14:38 -07:00
} else if ( error . name === 'NameError' ) {
2017-11-02 15:47:55 -07:00
validationFunctions . showError ( claimNameError , error . message ) ;
2017-11-01 17:14:38 -07:00
} else if ( error . name === 'ChannelNameError' ) {
console . log ( error ) ;
2017-11-02 15:47:55 -07:00
validationFunctions . showError ( channelSelectError , error . message ) ;
2017-11-01 17:14:38 -07:00
} else {
2017-11-02 15:47:55 -07:00
validationFunctions . showError ( publishSubmitError , error . message ) ;
2017-11-01 17:14:38 -07:00
}
return ;
} )
} ,
2017-11-02 15:12:26 -07:00
showUploadStartedMessage : function ( ) {
console . log ( 'starting upload' ) ;
// hide the publish tool
this . hidePublishTools ( ) ;
// show the progress status and animation
this . showPublishStatus ( ) ;
this . showPublishProgressBar ( ) ;
} ,
showUploadProgressMessage : function ( percentage ) {
this . updatePublishStatus ( '<p>File is loading to server</p>' ) ;
2017-11-06 15:18:45 -08:00
this . updateUploadPercent ( '<p class="blue">' + percentage + '% </p>' ) ;
2017-11-02 15:12:26 -07:00
} ,
showFilePublishUpdate : function ( msg ) {
this . updatePublishStatus ( '<p>' + msg + '</p>' ) ;
this . updateUploadPercent ( '<p>Curious what magic is happening here? <a class="link--primary" target="blank" href="https://lbry.io/faq/what-is-lbry">Learn more.</a></p>' ) ;
} ,
showFilePublishFailure : function ( msg ) {
2017-11-13 15:12:16 -05:00
this . updatePublishStatus ( '<p>Something went wrong...</p><p><strong>' + msg + '</strong></p><p>For help, post the above error text in the #speech channel on the <a class="link--primary" href="https://discord.gg/YjYbwhS" target="_blank">lbry discord</a>' ) ;
2017-11-02 15:12:26 -07:00
this . hidePublishProgressBar ( ) ;
this . hideUploadPercent ( ) ;
} ,
showFilePublishComplete : function ( msg ) {
console . log ( 'Publish complete!' ) ;
const showUrl = msg . lbryTx . claim _id + "/" + msg . name ;
// update status
this . updatePublishStatus ( '<p>Your publish is complete! You are being redirected to it now.</p>' ) ;
this . updateUploadPercent ( '<p><a class="link--primary" target="_blank" href="' + showUrl + '">If you do not get redirected, click here.</a></p>' )
// redirect the user
window . location . href = showUrl ;
} ,
hidePublishTools : function ( ) {
const publishFormWrapper = document . getElementById ( 'publish-form' ) ;
publishFormWrapper . setAttribute ( 'class' , 'hidden' ) ;
} ,
// publish status functions
showPublishStatus : function ( ) {
const publishStatus = document . getElementById ( 'publish-status' ) ;
publishStatus . setAttribute ( 'class' , 'row row--tall flex-container--column flex-container--center-center' ) ;
} ,
updatePublishStatus : function ( msg ) {
const publishUpdate = document . getElementById ( 'publish-update' ) ;
publishUpdate . innerHTML = msg ;
} ,
// progress bar functions
showPublishProgressBar : function ( ) {
const publishProgressBar = document . getElementById ( 'publish-progress-bar' ) ;
createProgressBar ( publishProgressBar , 12 ) ;
} ,
hidePublishProgressBar : function ( ) {
const publishProgressBar = document . getElementById ( 'publish-progress-bar' ) ;
publishProgressBar . hidden = true ;
} ,
// upload percent functions
updateUploadPercent : function ( msg ) {
const uploadPercent = document . getElementById ( 'upload-percent' ) ;
uploadPercent . innerHTML = msg ;
} ,
hideUploadPercent : function ( ) {
const uploadPercent = document . getElementById ( 'upload-percent' ) ;
uploadPercent . hidden = true ;
} ,
2017-11-01 17:14:38 -07:00
}
2017-11-01 13:07:13 -07:00