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-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-04-27 19:24:40 +02:00
const getClaimIdAndServeAsset = ( channelName , channelClaimId , claimName , claimId , originalUrl , ip , res ) => {
getClaimId ( channelName , channelClaimId , claimName , claimId )
. then ( fullClaimId => {
2018-04-29 21:17:23 +02:00
claimId = fullClaimId ;
2018-06-30 05:34:48 +02:00
logger . debug ( 'Full claim id:' , fullClaimId ) ;
return db . Claim . getOutpoint ( claimName , fullClaimId ) ;
} )
. then ( outpoint => {
logger . debug ( 'Outpoint:' , outpoint ) ;
return db . Blocked . isNotBlocked ( outpoint ) ;
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 ;
}
serveFile ( fileRecord . dataValues , res ) ;
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
} ) ;
}
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 ,
message : '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.io/faq/dmca' ,
} ) ;
}
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-04-29 22:00:41 +02:00
return res . status ( 307 ) . redirect ( ` /api/claim/get/ ${ name } / ${ claimId } ` ) ;
}
2018-04-27 19:24:40 +02:00
handleErrorResponse ( originalUrl , ip , error , res ) ;
} ) ;
} ;
module . exports = getClaimIdAndServeAsset ;