diff --git a/server/controllers/api/claim/longId/index.js b/server/controllers/api/claim/longId/index.js index c9dd78be..0eb3668c 100644 --- a/server/controllers/api/claim/longId/index.js +++ b/server/controllers/api/claim/longId/index.js @@ -1,8 +1,12 @@ -const getClaimId = require('./getClaimId.js'); +const db = require('../../../../models'); + const { handleErrorResponse } = require('../../../utils/errorHandlers.js'); +const getClaimId = require('./getClaimId.js'); + const NO_CHANNEL = 'NO_CHANNEL'; 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 channelClaimId = body.channelClaimId; const claimName = body.claimName; - const claimId = body.claimId; + let claimId = body.claimId; getClaimId(channelName, channelClaimId, claimName, claimId) .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 => { if (error === NO_CLAIM) { return res.status(404).json({ success: false, - message: 'No claim id could be found', + message: 'No matching claim id could be found for that url', }); } if (error === NO_CHANNEL) { return res.status(404).json({ 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); diff --git a/server/controllers/assets/utils/getClaimIdAndServeAsset.js b/server/controllers/assets/utils/getClaimIdAndServeAsset.js index 6bd868b2..9b48399f 100644 --- a/server/controllers/assets/utils/getClaimIdAndServeAsset.js +++ b/server/controllers/assets/utils/getClaimIdAndServeAsset.js @@ -1,13 +1,17 @@ +const logger = require('winston'); + const db = require('../../../models'); const getClaimId = require('../../api/claim/longId/getClaimId.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_CLAIM = 'NO_CLAIM'; const BLOCKED_CLAIM = 'BLOCKED_CLAIM'; +const NO_FILE = 'NO_FILE'; const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId, originalUrl, ip, res) => { getClaimId(channelName, channelClaimId, claimName, claimId) @@ -16,27 +20,37 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId return db.Blocked.isNotBlocked(fullClaimId, claimName); }) .then(() => { - serveAssetToClient(claimId, claimName, res); + return getLocalFileRecord(claimId, claimName); + }) + .then(fileRecord => { + serveFile(fileRecord, res); }) .catch(error => { if (error === NO_CLAIM) { + logger.debug('no claim found'); return res.status(404).json({ success: false, - message: 'No claim id could be found', + message: 'No matching claim id could be found for that url', }); } if (error === NO_CHANNEL) { + logger.debug('no channel found'); return res.status(404).json({ success: false, - message: 'No channel id could be found', + message: 'No matching channel id could be found for that url', }); } if (error === BLOCKED_CLAIM) { + logger.debug('claim was blocked'); 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', }); } + if (error === NO_FILE) { + logger.debug('claim was blocked'); + return res.status(307).redirect(`/api/claim/get/${name}/${claimId}`); + } handleErrorResponse(originalUrl, ip, error, res); }); }; diff --git a/server/controllers/assets/utils/serveAssetToClient.js b/server/controllers/assets/utils/serveAssetToClient.js deleted file mode 100644 index 28991d24..00000000 --- a/server/controllers/assets/utils/serveAssetToClient.js +++ /dev/null @@ -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; diff --git a/server/controllers/assets/utils/serveFile.js b/server/controllers/assets/utils/serveFile.js new file mode 100644 index 00000000..ab3c167a --- /dev/null +++ b/server/controllers/assets/utils/serveFile.js @@ -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;