spee.ch/server/controllers/api/file/availability/index.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
2018-04-27 18:54:36 +02:00
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const { getFileListFileByOutpoint } = require('server/lbrynet');
const chainquery = require('chainquery').default;
2018-03-29 02:35:41 +02:00
/*
route to see if asset is available locally
*/
2018-03-29 20:24:52 +02:00
const fileAvailability = ({ ip, originalUrl, params }, res) => {
const name = params.name;
const claimId = params.claimId;
logger.debug(`fileAvailability params: name:${name} claimId:${claimId}`);
// TODO: we probably eventually want to check the publishCache for the claimId too,
// and shop the outpoint to file_list.
return chainquery.claim.queries
.resolveClaim(name, claimId)
.then(result => {
return `${result.dataValues.transaction_hash_id}:${result.dataValues.vout}`;
})
.then(outpoint => {
logger.debug(`fileAvailability: outpoint: ${outpoint}`);
return getFileListFileByOutpoint(outpoint);
2018-03-29 20:24:52 +02:00
})
.then(result => {
if (result && result[0]) {
return res.status(200).json({ success: true, data: true });
} else {
res.status(200).json({ success: true, data: false });
2018-03-29 20:24:52 +02:00
}
})
.catch(error => {
handleErrorResponse(originalUrl, ip, error, res);
});
2018-03-29 02:35:41 +02:00
};
module.exports = fileAvailability;