spee.ch/server/controllers/assets/utils/getClaimIdAndServeAsset.js

133 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

2018-04-29 22:00:41 +02:00
const logger = require('winston');
const db = require('../../../models');
2018-12-14 18:42:37 +01:00
const chainquery = require('chainquery').default;
const isApprovedChannel = require('../../../../utils/isApprovedChannel');
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-27 19:24:40 +02:00
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
const BLOCKED_CLAIM = 'BLOCKED_CLAIM';
2018-04-29 22:00:41 +02:00
const NO_FILE = 'NO_FILE';
const CONTENT_UNAVAILABLE = 'CONTENT_UNAVAILABLE';
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 => {
claimId = fullClaimId;
return chainquery.claim.queries.resolveClaim(claimName, fullClaimId).catch(() => {});
})
.then(claim => {
if (!claim) {
logger.debug('Full claim id:', claimId);
return db.Claim.findOne({
where: {
2018-11-11 01:11:12 +01:00
name: claimName,
claimId,
},
});
}
return claim;
})
.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
)
) {
throw new Error(CONTENT_UNAVAILABLE);
}
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) {
db.Views.create({
2019-02-20 07:30:46 +01:00
time: Date.now(),
isChannel: false,
claimId: claimDataValues.claim_id || claimDataValues.claimId,
publisherId: claimDataValues.publisher_id || claimDataValues.certificateId,
ip,
});
}
2018-10-24 10:43:30 +02:00
});
})
.then(() => {
return db.File.findOne({
where: {
claimId,
name: claimName,
},
});
2018-04-29 22:00:41 +02:00
})
.then(fileRecord => {
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 => {
if (error === NO_CLAIM) {
2018-04-29 22:00:41 +02:00
logger.debug('no claim found');
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',
});
}
if (error === NO_CHANNEL) {
2018-04-29 22:00:41 +02:00
logger.debug('no channel found');
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',
});
}
if (error === CONTENT_UNAVAILABLE) {
logger.debug('unapproved channel');
return res.status(400).json({
success: false,
message: 'This content is unavailable',
});
}
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({
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 22:00:41 +02:00
if (error === NO_FILE) {
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;