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

64 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-04-29 22:00:41 +02:00
const logger = require('winston');
const db = require('../../../models');
const getClaimId = require('../../api/claim/longId/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 getLocalFileRecord = require('./getLocalFileRecord.js');
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';
2018-04-27 19:24:40 +02:00
const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId, originalUrl, ip, res) => {
getClaimId(channelName, channelClaimId, claimName, claimId)
.then(fullClaimId => {
claimId = fullClaimId;
logger.debug('Full claim id:', fullClaimId);
return db.Claim.getOutpoint(claimName, fullClaimId);
})
.then(outpoint => {
logger.debug('Outpoint:', outpoint);
return db.Blocked.isNotBlocked(outpoint);
})
.then(() => {
2018-04-29 22:00:41 +02:00
return getLocalFileRecord(claimId, claimName);
})
.then(fileRecord => {
serveFile(fileRecord, res);
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 === 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,
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) {
logger.debug('claim was blocked');
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;