2018-06-28 03:10:47 +02:00
const logger = require ( 'winston' ) ;
2018-06-23 22:19:58 +02:00
const { details : { host } , publishing : { disabled , disabledMessage } } = require ( '@config/siteConfig' ) ;
2018-04-27 23:29:00 +02:00
2018-04-27 18:54:36 +02:00
const { sendGATimingEvent } = require ( '../../../../utils/googleAnalytics.js' ) ;
2018-04-27 23:29:00 +02:00
2018-04-27 18:54:36 +02:00
const { handleErrorResponse } = require ( '../../../utils/errorHandlers.js' ) ;
2018-04-27 23:29:00 +02:00
2018-04-29 21:17:23 +02:00
const checkClaimAvailability = require ( '../availability/checkClaimAvailability.js' ) ;
2018-04-27 23:29:00 +02:00
2018-04-27 18:54:36 +02:00
const publish = require ( './publish.js' ) ;
const createBasicPublishParams = require ( './createBasicPublishParams.js' ) ;
const createThumbnailPublishParams = require ( './createThumbnailPublishParams.js' ) ;
const parsePublishApiRequestBody = require ( './parsePublishApiRequestBody.js' ) ;
const parsePublishApiRequestFiles = require ( './parsePublishApiRequestFiles.js' ) ;
2018-04-27 23:29:00 +02:00
const authenticateUser = require ( './authentication.js' ) ;
2018-03-29 02:35:41 +02:00
/ *
route to publish a claim through the daemon
* /
2018-06-28 03:10:47 +02:00
const claimPublish = ( { body , files , headers , ip , originalUrl , user , tor } , res ) => {
2018-06-23 22:19:58 +02:00
// check for disabled publishing
if ( disabled ) {
2018-06-23 22:53:35 +02:00
return res . status ( 503 ) . json ( {
2018-06-23 22:19:58 +02:00
success : false ,
message : disabledMessage
} ) ;
}
2018-06-28 03:10:47 +02:00
// check for tor
logger . debug ( 'tor:' , tor ) ;
if ( tor ) {
const failureResponse = {
success : 'false' ,
message : 'Unfortunately this api route is not currently available for tor users. We are working on a solution that will allow tor users to use this endpoint in the future.' ,
} ;
return res . status ( 400 ) . json ( failureResponse ) ;
}
2018-03-29 20:24:52 +02:00
// define variables
let channelName , channelId , channelPassword , description , fileName , filePath , fileType , gaStartTime , license , name , nsfw , thumbnail , thumbnailFileName , thumbnailFilePath , thumbnailFileType , title ;
// record the start time of the request
gaStartTime = Date . now ( ) ;
// validate the body and files of the request
try {
// validateApiPublishRequest(body, files);
( { name , nsfw , license , title , description , thumbnail } = parsePublishApiRequestBody ( body ) ) ;
( { fileName , filePath , fileType , thumbnailFileName , thumbnailFilePath , thumbnailFileType } = parsePublishApiRequestFiles ( files ) ) ;
( { channelName , channelId , channelPassword } = body ) ;
} catch ( error ) {
return res . status ( 400 ) . json ( { success : false , message : error . message } ) ;
}
// check channel authorization
Promise
. all ( [
authenticateUser ( channelName , channelId , channelPassword , user ) ,
2018-04-27 08:42:22 +02:00
checkClaimAvailability ( name ) ,
2018-03-29 20:24:52 +02:00
createBasicPublishParams ( filePath , name , title , description , license , nsfw , thumbnail ) ,
createThumbnailPublishParams ( thumbnailFilePath , name , license , nsfw ) ,
] )
. then ( ( [ { channelName , channelClaimId } , validatedClaimName , publishParams , thumbnailPublishParams ] ) => {
// add channel details to the publish params
if ( channelName && channelClaimId ) {
publishParams [ 'channel_name' ] = channelName ;
publishParams [ 'channel_id' ] = channelClaimId ;
}
2018-04-27 08:42:22 +02:00
// publish the thumbnail, if one exists
2018-03-29 20:24:52 +02:00
if ( thumbnailPublishParams ) {
publish ( thumbnailPublishParams , thumbnailFileName , thumbnailFileType ) ;
}
// publish the asset
return publish ( publishParams , fileName , fileType ) ;
} )
. then ( result => {
res . status ( 200 ) . json ( {
success : true ,
message : 'publish completed successfully' ,
data : {
name ,
claimId : result . claim _id ,
url : ` ${ host } / ${ result . claim _id } / ${ name } ` ,
lbryTx : result ,
} ,
2018-03-29 02:35:41 +02:00
} ) ;
2018-03-29 20:24:52 +02:00
// record the publish end time and send to google analytics
sendGATimingEvent ( 'end-to-end' , 'publish' , fileType , gaStartTime , Date . now ( ) ) ;
} )
. catch ( error => {
handleErrorResponse ( originalUrl , ip , error , res ) ;
} ) ;
2018-03-29 02:35:41 +02:00
} ;
module . exports = claimPublish ;