2017-07-28 23:39:39 +02:00
// validation function which checks the proposed file's type, size, and name
function validateFile ( file ) {
if ( ! file ) {
2017-10-03 19:05:09 +02:00
console . log ( 'no file found' ) ;
2017-07-28 23:39:39 +02:00
throw new Error ( 'no file provided' ) ;
}
2017-07-28 23:54:02 +02:00
if ( /'/ . test ( file . name ) ) {
2017-10-03 19:05:09 +02:00
console . log ( 'file name had apostrophe in it' ) ;
2017-07-28 23:54:02 +02:00
throw new Error ( 'apostrophes are not allowed in the file name' ) ;
}
2017-07-28 23:39:39 +02:00
// validate size and type
switch ( file . type ) {
case 'image/jpeg' :
case 'image/jpg' :
case 'image/png' :
2017-10-03 19:05:09 +02:00
if ( file . size > 10000000 ) {
console . log ( 'file was too big' ) ;
throw new Error ( 'Sorry, images are limited to 10 megabytes.' ) ;
}
break ;
2017-07-28 23:39:39 +02:00
case 'image/gif' :
2017-07-31 19:24:17 +02:00
if ( file . size > 50000000 ) {
2017-10-03 19:05:09 +02:00
console . log ( 'file was too big' ) ;
throw new Error ( 'Sorry, .gifs are limited to 50 megabytes.' ) ;
2017-07-28 23:39:39 +02:00
}
break ;
case 'video/mp4' :
if ( file . size > 50000000 ) {
2017-10-03 19:05:09 +02:00
console . log ( 'file was too big' ) ;
2017-07-28 23:39:39 +02:00
throw new Error ( 'Sorry, videos are limited to 50 megabytes.' ) ;
}
break ;
default :
2017-10-03 19:05:09 +02:00
console . log ( 'file type is not supported' ) ;
2017-07-28 23:39:39 +02:00
throw new Error ( file . type + ' is not a supported file type. Only, .jpeg, .png, .gif, and .mp4 files are currently supported.' )
}
}
2017-09-19 21:54:23 +02:00
// validation function that checks to make sure the claim name is valid
function validateClaimName ( name ) {
// ensure a name was entered
if ( name . length < 1 ) {
2017-09-20 20:36:20 +02:00
throw new NameError ( "You must enter a name for your url" ) ;
2017-09-19 21:54:23 +02:00
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-]/g . exec ( name ) ;
if ( invalidCharacters ) {
2017-09-20 20:36:20 +02:00
throw new NameError ( '"' + invalidCharacters + '" characters are not allowed in the url.' ) ;
2017-09-19 21:54:23 +02:00
}
2017-09-19 17:47:24 +02:00
}
2017-09-20 03:50:25 +02:00
function validateChannelName ( name ) {
name = name . substring ( name . indexOf ( '@' ) + 1 ) ;
// ensure a name was entered
if ( name . length < 1 ) {
2017-09-20 18:49:05 +02:00
throw new ChannelNameError ( "You must enter a name for your channel" ) ;
2017-09-20 03:50:25 +02:00
}
// validate the characters in the 'name' field
const invalidCharacters = /[^A-Za-z0-9,-,@]/g . exec ( name ) ;
if ( invalidCharacters ) {
2017-09-20 18:49:05 +02:00
throw new ChannelNameError ( '"' + invalidCharacters + '" characters are not allowed in the channel name.' ) ;
2017-09-20 03:50:25 +02:00
}
}
function validatePassword ( password ) {
if ( password . length < 1 ) {
2017-09-20 19:14:00 +02:00
throw new ChannelPasswordError ( "You must enter a password for you channel" ) ;
2017-09-20 03:50:25 +02:00
}
}
2017-09-19 21:54:23 +02:00
function cleanseClaimName ( name ) {
name = name . replace ( /\s+/g , '-' ) ; // replace spaces with dashes
name = name . replace ( /[^A-Za-z0-9-]/g , '' ) ; // remove all characters that are not A-Z, a-z, 0-9, or '-'
return name ;
}
// validation functions to check claim & channel name eligibility as the inputs change
function isNameAvailable ( name , apiUrl ) {
2017-09-29 00:47:55 +02:00
const url = apiUrl + name ;
return getRequest ( url )
2017-07-28 23:39:39 +02:00
}
2017-09-19 17:47:24 +02:00
2017-09-19 21:54:23 +02:00
function showError ( errorDisplay , errorMsg ) {
errorDisplay . hidden = false ;
errorDisplay . innerText = errorMsg ;
2017-07-28 23:39:39 +02:00
}
2017-09-19 21:54:23 +02:00
function hideError ( errorDisplay ) {
errorDisplay . hidden = true ;
errorDisplay . innerText = '' ;
2017-07-28 23:39:39 +02:00
}
2017-09-19 21:54:23 +02:00
function showSuccess ( successElement ) {
successElement . hidden = false ;
successElement . innerHTML = "✔" ;
2017-07-28 23:39:39 +02:00
}
2017-09-19 17:47:24 +02:00
2017-09-19 21:54:23 +02:00
function hideSuccess ( successElement ) {
successElement . hidden = true ;
successElement . innerHTML = "" ;
}
2017-09-19 17:47:24 +02:00
2017-09-29 00:47:55 +02:00
function checkAvailability ( name , successDisplayElement , errorDisplayElement , validateName , isNameAvailable , errorMessage , apiUrl ) {
2017-09-19 17:47:24 +02:00
try {
2017-09-19 21:54:23 +02:00
// check to make sure the characters are valid
2017-09-20 03:50:25 +02:00
validateName ( name ) ;
2017-09-19 17:47:24 +02:00
// check to make sure it is available
2017-09-19 21:54:23 +02:00
isNameAvailable ( name , apiUrl )
2017-09-29 00:47:55 +02:00
. then ( result => {
console . log ( 'result:' , result )
if ( result === true ) {
hideError ( errorDisplayElement ) ;
showSuccess ( successDisplayElement )
} else {
hideSuccess ( successDisplayElement ) ;
showError ( errorDisplayElement , errorMessage ) ;
}
2017-09-19 17:47:24 +02:00
} )
2017-09-29 00:47:55 +02:00
. catch ( error => {
2017-09-19 21:54:23 +02:00
hideSuccess ( successDisplayElement ) ;
showError ( errorDisplayElement , error . message ) ;
2017-09-19 17:47:24 +02:00
} ) ;
} catch ( error ) {
2017-09-19 21:54:23 +02:00
hideSuccess ( successDisplayElement ) ;
showError ( errorDisplayElement , error . message ) ;
2017-09-19 17:47:24 +02:00
}
}
2017-09-19 21:54:23 +02:00
function checkClaimName ( name ) {
2017-09-20 00:39:54 +02:00
const successDisplayElement = document . getElementById ( 'input-success-claim-name' ) ;
const errorDisplayElement = document . getElementById ( 'input-error-claim-name' ) ;
2017-09-29 00:47:55 +02:00
checkAvailability ( name , successDisplayElement , errorDisplayElement , validateClaimName , isNameAvailable , 'Sorry, that url ending has been taken by another user' , '/api/isClaimAvailable/' ) ;
2017-09-19 21:54:23 +02:00
}
function checkChannelName ( name ) {
2017-09-20 00:39:54 +02:00
const successDisplayElement = document . getElementById ( 'input-success-channel-name' ) ;
const errorDisplayElement = document . getElementById ( 'input-error-channel-name' ) ;
2017-09-20 03:50:25 +02:00
name = ` @ ${ name } ` ;
2017-10-03 20:23:10 +02:00
checkAvailability ( name , successDisplayElement , errorDisplayElement , validateChannelName , isNameAvailable , 'Sorry, that Channel name has been taken by another user' , '/api/isChannelAvailable/' ) ;
2017-09-19 21:54:23 +02:00
}
2017-07-28 23:39:39 +02:00
// validation function which checks all aspects of the publish submission
2017-09-20 18:49:05 +02:00
function validateFilePublishSubmission ( stagedFiles , claimName , channelName ) {
2017-10-03 19:28:16 +02:00
console . log ( ` validating publish submission > name: ${ claimName } channel: ${ channelName } file: ` , stagedFiles ) ;
2017-08-03 19:44:19 +02:00
return new Promise ( function ( resolve , reject ) {
2017-10-03 19:05:09 +02:00
// 1. make sure 1 file was staged
2017-07-28 23:39:39 +02:00
if ( ! stagedFiles ) {
2017-10-03 19:28:16 +02:00
reject ( new FileError ( "Please select a file" ) ) ;
return ;
2017-07-28 23:39:39 +02:00
} else if ( stagedFiles . length > 1 ) {
2017-10-03 19:28:16 +02:00
reject ( new FileError ( "Only one file is allowed at a time" ) ) ;
return ;
}
2017-09-20 00:39:54 +02:00
// 2. validate the file's name, type, and size
2017-07-28 23:39:39 +02:00
try {
validateFile ( stagedFiles [ 0 ] ) ;
} catch ( error ) {
2017-10-03 19:28:16 +02:00
reject ( error ) ;
return ;
}
2017-09-20 00:39:54 +02:00
// 3. validate that a channel was chosen
2017-09-21 18:18:19 +02:00
if ( channelName === 'new' || channelName === 'login' ) {
2017-10-03 19:28:16 +02:00
reject ( new ChannelNameError ( "Please select a valid channel" ) ) ;
return ;
2017-09-20 00:39:54 +02:00
} ;
// 4. validate the claim name
2017-07-28 23:39:39 +02:00
try {
2017-09-20 18:49:05 +02:00
validateClaimName ( claimName ) ;
2017-07-28 23:39:39 +02:00
} catch ( error ) {
2017-10-03 19:28:16 +02:00
reject ( error ) ;
return ;
2017-07-28 23:39:39 +02:00
}
2017-09-20 18:49:05 +02:00
// if all validation passes, check availability of the name
2017-10-03 19:28:16 +02:00
return isNameAvailable ( claimName , '/api/isClaimAvailable/' )
. then ( result => {
if ( result ) {
resolve ( ) ;
} else {
reject ( new NameError ( 'that url ending is already taken' ) ) ;
}
2017-07-28 23:39:39 +02:00
} )
2017-09-20 19:14:00 +02:00
. catch ( error => {
2017-07-28 23:39:39 +02:00
reject ( error ) ;
} ) ;
} ) ;
2017-09-20 18:49:05 +02:00
}
2017-10-03 19:28:16 +02:00
// validation function which checks all aspects of a new channel submission
2017-09-20 18:49:05 +02:00
function validateNewChannelSubmission ( channelName , password ) {
return new Promise ( function ( resolve , reject ) {
// 1. validate name
try {
validateChannelName ( channelName ) ;
} catch ( error ) {
return reject ( error ) ;
}
// 2. validate password
try {
validatePassword ( password ) ;
} catch ( error ) {
return reject ( error ) ;
}
// 3. if all validation passes, check availability of the name
isNameAvailable ( channelName , '/api/isChannelAvailable/' ) // validate the availability
2017-09-20 19:14:00 +02:00
. then ( ( ) => {
2017-10-03 20:23:10 +02:00
console . log ( 'channel is available' ) ;
2017-09-20 18:49:05 +02:00
resolve ( ) ;
} )
2017-09-20 19:14:00 +02:00
. catch ( error => {
console . log ( 'error: channel is not avaliable' ) ;
2017-09-20 18:49:05 +02:00
reject ( error ) ;
} ) ;
} ) ;
2017-09-20 03:50:25 +02:00
}