updated api/longId route

This commit is contained in:
bill bittner 2018-04-29 13:00:41 -07:00
parent d57a6b3340
commit bbd691018b
4 changed files with 51 additions and 37 deletions

View file

@ -1,8 +1,12 @@
const getClaimId = require('./getClaimId.js'); const db = require('../../../../models');
const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const getClaimId = require('./getClaimId.js');
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM'; const NO_CLAIM = 'NO_CLAIM';
const BLOCKED_CLAIM = 'BLOCKED_CLAIM';
/* /*
@ -14,22 +18,32 @@ const claimLongId = ({ ip, originalUrl, body, params }, res) => {
const channelName = body.channelName; const channelName = body.channelName;
const channelClaimId = body.channelClaimId; const channelClaimId = body.channelClaimId;
const claimName = body.claimName; const claimName = body.claimName;
const claimId = body.claimId; let claimId = body.claimId;
getClaimId(channelName, channelClaimId, claimName, claimId) getClaimId(channelName, channelClaimId, claimName, claimId)
.then(fullClaimId => { .then(fullClaimId => {
res.status(200).json({success: true, data: fullClaimId}); claimId = fullClaimId;
return db.Blocked.isNotBlocked(fullClaimId, claimName);
})
.then(() => {
res.status(200).json({success: true, data: claimId});
}) })
.catch(error => { .catch(error => {
if (error === NO_CLAIM) { if (error === NO_CLAIM) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'No claim id could be found', message: 'No matching claim id could be found for that url',
}); });
} }
if (error === NO_CHANNEL) { if (error === NO_CHANNEL) {
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'No channel id could be found', message: 'No matching channel id could be found for that url',
});
}
if (error === BLOCKED_CLAIM) {
return res.status(410).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',
}); });
} }
handleErrorResponse(originalUrl, ip, error, res); handleErrorResponse(originalUrl, ip, error, res);

View file

@ -1,13 +1,17 @@
const logger = require('winston');
const db = require('../../../models'); const db = require('../../../models');
const getClaimId = require('../../api/claim/longId/getClaimId.js'); const getClaimId = require('../../api/claim/longId/getClaimId.js');
const { handleErrorResponse } = require('../../utils/errorHandlers.js'); const { handleErrorResponse } = require('../../utils/errorHandlers.js');
const serveAssetToClient = require('./serveAssetToClient.js'); const getLocalFileRecord = require('./getLocalFileRecord.js');
const serveFile = require('./serveFile.js');
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM'; const NO_CLAIM = 'NO_CLAIM';
const BLOCKED_CLAIM = 'BLOCKED_CLAIM'; const BLOCKED_CLAIM = 'BLOCKED_CLAIM';
const NO_FILE = 'NO_FILE';
const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId, originalUrl, ip, res) => { const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId, originalUrl, ip, res) => {
getClaimId(channelName, channelClaimId, claimName, claimId) getClaimId(channelName, channelClaimId, claimName, claimId)
@ -16,27 +20,37 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId
return db.Blocked.isNotBlocked(fullClaimId, claimName); return db.Blocked.isNotBlocked(fullClaimId, claimName);
}) })
.then(() => { .then(() => {
serveAssetToClient(claimId, claimName, res); return getLocalFileRecord(claimId, claimName);
})
.then(fileRecord => {
serveFile(fileRecord, res);
}) })
.catch(error => { .catch(error => {
if (error === NO_CLAIM) { if (error === NO_CLAIM) {
logger.debug('no claim found');
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'No claim id could be found', message: 'No matching claim id could be found for that url',
}); });
} }
if (error === NO_CHANNEL) { if (error === NO_CHANNEL) {
logger.debug('no channel found');
return res.status(404).json({ return res.status(404).json({
success: false, success: false,
message: 'No channel id could be found', message: 'No matching channel id could be found for that url',
}); });
} }
if (error === BLOCKED_CLAIM) { if (error === BLOCKED_CLAIM) {
logger.debug('claim was blocked');
return res.status(410).json({ return res.status(410).json({
success: false, 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', 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',
}); });
} }
if (error === NO_FILE) {
logger.debug('claim was blocked');
return res.status(307).redirect(`/api/claim/get/${name}/${claimId}`);
}
handleErrorResponse(originalUrl, ip, error, res); handleErrorResponse(originalUrl, ip, error, res);
}); });
}; };

View file

@ -1,28 +0,0 @@
const logger = require('winston');
const getLocalFileRecord = require('./getLocalFileRecord.js');
const NO_FILE = 'NO_FILE';
const serveAssetToClient = (claimId, name, res) => {
return getLocalFileRecord(claimId, name)
.then(fileRecord => {
// check that a local record was found
if (fileRecord === NO_FILE) {
return res.status(307).redirect(`/api/claim/get/${name}/${claimId}`);
}
// serve the file
const {filePath, fileType} = fileRecord;
logger.verbose(`serving file: ${filePath}`);
const sendFileOptions = {
headers: {
'X-Content-Type-Options': 'nosniff',
'Content-Type' : fileType || 'image/jpeg',
},
};
res.status(200).sendFile(filePath, sendFileOptions);
})
.catch(error => {
throw error;
});
};
module.exports = serveAssetToClient;

View file

@ -0,0 +1,14 @@
const logger = require('winston');
const serveFile = ({ filePath, fileType }, res) => {
logger.verbose(`serving file: ${filePath}`);
const sendFileOptions = {
headers: {
'X-Content-Type-Options': 'nosniff',
'Content-Type' : fileType || 'image/jpeg',
},
};
res.status(200).sendFile(filePath, sendFileOptions);
};
module.exports = serveFile;