2018-04-29 22:00:41 +02:00
const logger = require ( 'winston' ) ;
2018-04-29 21:17:23 +02:00
const db = require ( '../../../models' ) ;
2018-12-14 18:42:37 +01:00
const chainquery = require ( 'chainquery' ) . default ;
2018-09-21 22:16:01 +02:00
const isApprovedChannel = require ( '../../../../utils/isApprovedChannel' ) ;
2018-04-29 21:17:23 +02:00
2018-07-30 22:48:47 +02:00
const getClaimId = require ( '../../utils/getClaimId.js' ) ;
2018-04-27 19:24:40 +02:00
const { handleErrorResponse } = require ( '../../utils/errorHandlers.js' ) ;
2018-04-29 22:00:41 +02:00
const serveFile = require ( './serveFile.js' ) ;
2018-04-29 21:17:23 +02:00
2018-04-27 19:24:40 +02:00
const NO _CHANNEL = 'NO_CHANNEL' ;
const NO _CLAIM = 'NO_CLAIM' ;
2018-04-29 21:17:23 +02:00
const BLOCKED _CLAIM = 'BLOCKED_CLAIM' ;
2018-04-29 22:00:41 +02:00
const NO _FILE = 'NO_FILE' ;
2018-09-20 20:09:01 +02:00
const CONTENT _UNAVAILABLE = 'CONTENT_UNAVAILABLE' ;
2018-09-17 15:01:46 +02:00
2019-02-20 07:30:46 +01:00
const {
publishing : { serveOnlyApproved , approvedChannels } ,
} = require ( '@config/siteConfig' ) ;
2018-04-27 19:24:40 +02:00
2019-02-20 07:30:46 +01:00
const getClaimIdAndServeAsset = (
channelName ,
channelClaimId ,
claimName ,
claimId ,
originalUrl ,
ip ,
res ,
headers
) => {
2018-04-27 19:24:40 +02:00
getClaimId ( channelName , channelClaimId , claimName , claimId )
. then ( fullClaimId => {
2018-04-29 21:17:23 +02:00
claimId = fullClaimId ;
2018-10-09 02:09:04 +02:00
return chainquery . claim . queries . resolveClaim ( claimName , fullClaimId ) . catch ( ( ) => { } ) ;
2018-10-09 02:02:05 +02:00
} )
2018-10-09 02:09:04 +02:00
. then ( claim => {
if ( ! claim ) {
2018-10-10 19:19:51 +02:00
logger . debug ( 'Full claim id:' , claimId ) ;
2018-10-09 02:09:04 +02:00
return db . Claim . findOne ( {
where : {
2018-11-11 01:11:12 +01:00
name : claimName ,
2018-10-10 19:19:51 +02:00
claimId ,
2018-10-09 02:09:04 +02:00
} ,
} ) ;
2018-10-09 02:02:05 +02:00
}
2018-10-09 02:09:04 +02:00
return claim ;
2018-06-30 05:34:48 +02:00
} )
2018-09-17 15:01:46 +02:00
. then ( claim => {
2018-10-24 10:43:30 +02:00
let claimDataValues = claim . dataValues ;
2019-02-20 07:30:46 +01:00
if (
serveOnlyApproved &&
! isApprovedChannel (
{ longId : claimDataValues . publisher _id || claimDataValues . certificateId } ,
approvedChannels
)
) {
2018-09-20 20:09:01 +02:00
throw new Error ( CONTENT _UNAVAILABLE ) ;
2018-09-17 15:01:46 +02:00
}
2018-10-24 10:43:30 +02:00
2019-02-20 07:30:46 +01:00
let outpoint =
claimDataValues . outpoint ||
` ${ claimDataValues . transaction _hash _id } : ${ claimDataValues . vout } ` ;
2018-10-24 10:43:30 +02:00
logger . debug ( 'Outpoint:' , outpoint ) ;
return db . Blocked . isNotBlocked ( outpoint ) . then ( ( ) => {
// If content was found, is approved, and not blocked - log a view.
2018-11-11 01:11:12 +01:00
if ( headers && headers [ 'user-agent' ] && /LBRY/ . test ( headers [ 'user-agent' ] ) === false ) {
2018-10-25 00:11:52 +02:00
db . Views . create ( {
2019-02-20 07:30:46 +01:00
time : Date . now ( ) ,
isChannel : false ,
claimId : claimDataValues . claim _id || claimDataValues . claimId ,
2018-10-25 00:11:52 +02:00
publisherId : claimDataValues . publisher _id || claimDataValues . certificateId ,
ip ,
} ) ;
}
2018-10-24 10:43:30 +02:00
} ) ;
2018-04-29 21:17:23 +02:00
} )
. then ( ( ) => {
2018-07-30 22:48:47 +02:00
return db . File . findOne ( {
where : {
claimId ,
name : claimName ,
} ,
} ) ;
2018-04-29 22:00:41 +02:00
} )
. then ( fileRecord => {
2018-07-30 22:48:47 +02:00
if ( ! fileRecord ) {
throw NO _FILE ;
}
2019-02-20 07:30:46 +01:00
serveFile ( fileRecord . dataValues , res , originalUrl ) ;
2018-04-27 19:24:40 +02:00
} )
. catch ( error => {
2018-04-29 21:17:23 +02:00
if ( error === NO _CLAIM ) {
2018-04-29 22:00:41 +02:00
logger . debug ( 'no claim found' ) ;
2018-04-29 21:17:23 +02:00
return res . status ( 404 ) . json ( {
success : false ,
2018-04-29 22:00:41 +02:00
message : 'No matching claim id could be found for that url' ,
2018-04-29 21:17:23 +02:00
} ) ;
}
if ( error === NO _CHANNEL ) {
2018-04-29 22:00:41 +02:00
logger . debug ( 'no channel found' ) ;
2018-04-29 21:17:23 +02:00
return res . status ( 404 ) . json ( {
success : false ,
2018-04-29 22:00:41 +02:00
message : 'No matching channel id could be found for that url' ,
2018-04-29 21:17:23 +02:00
} ) ;
}
2018-09-20 20:09:01 +02:00
if ( error === CONTENT _UNAVAILABLE ) {
2018-09-17 15:01:46 +02:00
logger . debug ( 'unapproved channel' ) ;
return res . status ( 400 ) . json ( {
success : false ,
2018-09-20 20:09:01 +02:00
message : 'This content is unavailable' ,
2018-09-17 15:01:46 +02:00
} ) ;
}
2018-04-29 21:17:23 +02:00
if ( error === BLOCKED _CLAIM ) {
2018-04-29 22:00:41 +02:00
logger . debug ( 'claim was blocked' ) ;
2018-05-01 00:36:03 +02:00
return res . status ( 451 ) . json ( {
2018-04-29 21:17:23 +02:00
success : false ,
2019-02-20 07:30:46 +01:00
message :
2019-03-18 04:13:00 +01:00
'In response to a complaint we received under the US Digital Millennium Copyright Act, we have blocked access to this content from our applications. For more details, see https://lbry.com/faq/dmca' ,
2018-04-29 21:17:23 +02:00
} ) ;
}
2018-04-29 22:00:41 +02:00
if ( error === NO _FILE ) {
2018-07-30 22:48:47 +02:00
logger . debug ( 'no file available' ) ;
2018-08-07 21:54:13 +02:00
return res . status ( 307 ) . redirect ( ` /api/claim/get/ ${ claimName } / ${ claimId } ` ) ;
2018-04-29 22:00:41 +02:00
}
2018-04-27 19:24:40 +02:00
handleErrorResponse ( originalUrl , ip , error , res ) ;
} ) ;
} ;
module . exports = getClaimIdAndServeAsset ;