2017-07-17 13:16:11 -07:00
const logger = require ( 'winston' ) ;
2017-08-02 17:13:02 -07:00
const db = require ( '../models' ) ;
2017-07-17 13:16:11 -07:00
2017-08-23 12:21:15 -07:00
function createOpenGraphInfo ( { fileType , claimId , name , fileName , fileExt } ) {
return {
embedUrl : ` https://spee.ch/embed/ ${ claimId } / ${ name } ` ,
showUrl : ` https://spee.ch/ ${ claimId } / ${ name } ` ,
source : ` https://spee.ch/ ${ claimId } / ${ name } ${ fileExt } ` ,
directFileUrl : ` https://spee.ch/media/ ${ fileName } ` ,
} ;
}
2017-08-22 15:50:20 -07:00
2017-08-22 16:32:27 -07:00
function getLongChannelIdFromShortChannelId ( channelName , channelId ) {
2017-08-22 15:50:20 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-22 16:32:27 -07:00
logger . debug ( ` finding long channel id for ${ channelName } : ${ channelId } ` ) ;
// get the long channel Id
db . sequelize . query ( ` SELECT claimId, height FROM Certificate WHERE name = ' ${ channelName } ' AND claimId LIKE ' ${ channelId } %' ORDER BY height ASC LIMIT 1; ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-22 15:50:20 -07:00
. then ( result => {
2017-08-22 16:32:27 -07:00
logger . debug ( 'result >>' , result ) ;
2017-08-22 15:50:20 -07:00
switch ( result . length ) {
case 0 :
2017-08-22 16:32:27 -07:00
throw new Error ( 'That is an invalid Short Channel Id' ) ;
default : // note results must be sorted
2017-08-22 15:50:20 -07:00
return resolve ( result [ 0 ] . claimId ) ;
}
} )
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
}
2017-08-22 16:32:27 -07:00
function getLongChannelIdFromChannelName ( channelName ) {
// select the top top channel id
2017-08-22 15:50:20 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-22 16:32:27 -07:00
logger . debug ( ` finding long channel id for ${ channelName } ` ) ;
2017-08-22 15:50:20 -07:00
// get the long channel Id
2017-08-22 16:32:27 -07:00
db . sequelize . query ( ` SELECT claimId, amount, height FROM Certificate WHERE name = ' ${ channelName } ' ORDER BY amount DESC, height ASC LIMIT 1; ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-22 15:50:20 -07:00
. then ( result => {
logger . debug ( 'result >>' , result ) ;
switch ( result . length ) {
case 0 :
2017-08-22 16:32:27 -07:00
throw new Error ( 'That is an invalid Channel Name' ) ;
default :
return resolve ( result [ 0 ] . claimId ) ;
2017-08-22 15:50:20 -07:00
}
} )
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
}
2017-08-23 16:01:28 -07:00
function sortResult ( result , longId ) {
let claimIndex ;
let shortId = longId . substring ( 0 , 1 ) ; // default sort id is the first letter
let shortIdLength = 0 ;
// find the index of this certificate
claimIndex = result . findIndex ( element => {
return element . claimId === longId ;
2017-07-19 07:58:44 -07:00
} ) ;
2017-08-23 16:01:28 -07:00
if ( claimIndex < 0 ) { throw new Error ( 'claimid not found in possible sorted list' ) }
// get an array of all certificates with lower height
let possibleMatches = result . slice ( 0 , claimIndex ) ;
// remove certificates with the same prefixes until none are left.
while ( possibleMatches . length > 0 ) {
shortIdLength += 1 ;
shortId = longId . substring ( 0 , shortIdLength ) ;
possibleMatches = possibleMatches . filter ( element => {
return ( element . claimId . substring ( 0 , shortIdLength ) === shortId ) ;
2017-08-10 13:04:31 -07:00
} ) ;
2017-07-19 07:58:44 -07:00
}
2017-08-23 16:01:28 -07:00
// return the short Id
logger . debug ( 'short channel id ===' , shortId ) ;
return shortId ;
2017-08-04 17:41:06 -07:00
}
2017-07-17 13:16:11 -07:00
module . exports = {
serveFile ( { fileName , fileType , filePath } , res ) {
logger . info ( ` serving file ${ fileName } ` ) ;
// set default options
let options = {
headers : {
'X-Content-Type-Options' : 'nosniff' ,
'Content-Type' : fileType ,
} ,
} ;
// adjust default options as needed
switch ( fileType ) {
case 'image/jpeg' :
case 'image/gif' :
case 'image/png' :
case 'video/mp4' :
break ;
default :
logger . warn ( 'sending file with unknown type as .jpeg' ) ;
options [ 'headers' ] [ 'Content-Type' ] = 'image/jpeg' ;
break ;
}
// send the file
res . status ( 200 ) . sendFile ( filePath , options ) ;
} ,
2017-08-01 18:58:13 -07:00
showFile ( fileInfo , res ) {
2017-08-04 17:41:06 -07:00
const openGraphInfo = createOpenGraphInfo ( fileInfo ) ;
res . status ( 200 ) . render ( 'show' , { layout : 'show' , fileInfo , openGraphInfo } ) ;
2017-07-31 17:02:39 -07:00
} ,
2017-08-01 18:58:13 -07:00
showFileLite ( fileInfo , res ) {
2017-08-04 17:41:06 -07:00
const openGraphInfo = createOpenGraphInfo ( fileInfo ) ;
res . status ( 200 ) . render ( 'showLite' , { layout : 'show' , fileInfo , openGraphInfo } ) ;
2017-07-31 17:02:39 -07:00
} ,
2017-08-10 10:49:19 -07:00
getFullClaimIdFromShortId ( shortId , name ) {
2017-08-02 13:16:39 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-04 11:32:21 -07:00
logger . debug ( 'getting claim_id from short url' ) ;
2017-08-02 13:16:39 -07:00
// use the daemon to check for claims list
2017-08-16 00:03:26 -07:00
db . sequelize . query ( ` SELECT claimId FROM Claim WHERE name = ' ${ name } ' AND claimId LIKE ' ${ shortId } %' ORDER BY height ASC LIMIT 1; ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-15 15:35:03 -07:00
. then ( result => {
switch ( result . length ) {
case 0 :
2017-08-22 15:50:20 -07:00
return reject ( new Error ( 'That is an invalid Short Claim Id' ) ) ;
2017-08-15 15:35:03 -07:00
default : // note results must be sorted
2017-08-16 00:03:26 -07:00
return resolve ( result [ 0 ] . claimId ) ;
2017-07-20 10:15:20 -07:00
}
} )
2017-08-15 15:35:03 -07:00
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
} ,
2017-08-23 16:01:28 -07:00
getShortClaimIdFromLongClaimId ( claimId , claimName ) {
2017-08-15 15:35:03 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-23 16:01:28 -07:00
logger . debug ( 'finding short channel id' ) ;
db . sequelize . query ( ` SELECT claimId, height FROM Claim WHERE name = ' ${ claimName } ' ORDER BY height; ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-15 15:35:03 -07:00
. then ( result => {
switch ( result . length ) {
2017-07-19 09:11:08 -07:00
case 0 :
2017-08-22 15:50:20 -07:00
return reject ( new Error ( 'That is an invalid claim name' ) ) ;
2017-08-23 16:01:28 -07:00
default :
return resolve ( sortResult ( result , claimId ) ) ;
2017-07-19 09:11:08 -07:00
}
} )
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
} ,
2017-08-16 12:23:02 -07:00
getAllFreeClaims ( name ) {
2017-08-02 13:16:39 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-16 12:23:02 -07:00
db . sequelize . query ( ` SELECT * FROM Claim WHERE name = ' ${ name } ' ORDER BY amount DESC, height ASC ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-15 15:35:03 -07:00
. then ( result => {
2017-08-16 00:03:26 -07:00
switch ( result . length ) {
case 0 :
return resolve ( null ) ;
default :
return resolve ( result ) ;
2017-08-15 15:35:03 -07:00
}
} )
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
} ,
2017-08-16 12:23:02 -07:00
getTopFreeClaim ( name ) {
2017-08-15 15:35:03 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-16 12:23:02 -07:00
db . sequelize . query ( ` SELECT * FROM Claim WHERE name = ' ${ name } ' ORDER BY amount DESC, height ASC LIMIT 1 ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-15 15:35:03 -07:00
. then ( result => {
2017-08-16 00:03:26 -07:00
switch ( result . length ) {
case 0 :
return resolve ( null ) ;
default :
2017-08-16 00:40:49 -07:00
return resolve ( result [ 0 ] ) ;
2017-08-15 15:35:03 -07:00
}
2017-07-19 09:11:08 -07:00
} )
. catch ( error => {
reject ( error ) ;
} ) ;
2017-07-17 13:16:11 -07:00
} ) ;
2017-07-19 14:11:47 -07:00
} ,
2017-08-16 12:23:02 -07:00
resolveAgainstClaimTable ( name , claimId ) {
return new Promise ( ( resolve , reject ) => {
db . sequelize . query ( ` SELECT * FROM Claim WHERE name = ' ${ name } ' AND claimId = ' ${ claimId } ' ` , { type : db . sequelize . QueryTypes . SELECT } )
. then ( result => {
switch ( result . length ) {
case 0 :
return resolve ( null ) ;
case 1 :
return resolve ( result [ 0 ] ) ;
default :
return new Error ( 'more than one entry matches that name and claimID' ) ;
}
} )
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
} ,
2017-08-23 16:01:28 -07:00
getClaimIdByLongChannelId ( channelId , claimName ) {
2017-08-22 15:50:20 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-23 16:01:28 -07:00
logger . debug ( ` finding claim id for claim " ${ claimName } " from channel " ${ channelId } " ` ) ;
db . sequelize . query ( ` SELECT claimId FROM Claim WHERE name = ' ${ claimName } ' AND certificateId = ' ${ channelId } ' LIMIT 1; ` , { type : db . sequelize . QueryTypes . SELECT } )
. then ( result => {
switch ( result . length ) {
case 0 :
return reject ( new Error ( 'There is no such claim for that channel' ) ) ;
default :
return resolve ( result [ 0 ] . claimId ) ;
}
2017-08-22 15:50:20 -07:00
} )
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
} ,
2017-08-23 16:01:28 -07:00
getAllChannelClaims ( channelId ) {
2017-08-23 12:21:15 -07:00
return new Promise ( ( resolve , reject ) => {
2017-08-23 16:01:28 -07:00
logger . debug ( ` finding all claims in channel " ${ channelId } " ` ) ;
db . sequelize . query ( ` SELECT * FROM Claim WHERE certificateId = ' ${ channelId } ' ORDeR BY height DESC; ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-23 12:21:15 -07:00
. then ( result => {
2017-08-23 16:01:28 -07:00
switch ( result . length ) {
case 0 :
return resolve ( null ) ;
default :
return resolve ( result ) ;
}
2017-08-23 12:21:15 -07:00
} )
2017-08-23 16:01:28 -07:00
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
} ,
getLongChannelId ( channelName , channelId ) {
if ( channelId && ( channelId . length === 40 ) ) { // full channel id
return new Promise ( ( resolve , reject ) => resolve ( channelId ) ) ;
} else if ( channelId && channelId . length < 40 ) { // short channel id
return getLongChannelIdFromShortChannelId ( channelName , channelId ) ;
} else {
return getLongChannelIdFromChannelName ( channelName ) ;
}
} ,
getShortChannelIdFromLongChannelId ( channelName , longChannelId ) {
return new Promise ( ( resolve , reject ) => {
logger . debug ( 'finding short channel id' ) ;
db . sequelize . query ( ` SELECT claimId, height FROM Certificate WHERE name = ' ${ channelName } ' ORDER BY height; ` , { type : db . sequelize . QueryTypes . SELECT } )
2017-08-23 12:21:15 -07:00
. then ( result => {
2017-08-23 16:01:28 -07:00
switch ( result . length ) {
case 0 :
return reject ( new Error ( 'That is an invalid channel name' ) ) ;
default :
return resolve ( sortResult ( result , longChannelId ) ) ;
2017-08-23 14:40:35 -07:00
}
2017-08-23 12:21:15 -07:00
} )
. catch ( error => {
reject ( error ) ;
} ) ;
} ) ;
} ,
2017-07-17 13:16:11 -07:00
} ;