2018-04-27 18:54:36 +02:00
|
|
|
const { getClaim } = require('../../../../lbrynet');
|
2018-07-26 22:48:53 +02:00
|
|
|
const { createFileRecordDataAfterGet } = require('../../../../models/utils/createFileRecordData.js');
|
2018-04-27 18:54:36 +02:00
|
|
|
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
|
2018-09-26 03:20:59 +02:00
|
|
|
const getClaimData = require('server/utils/getClaimData');
|
2018-12-14 18:42:37 +01:00
|
|
|
const chainquery = require('chainquery').default;
|
2018-04-27 18:54:36 +02:00
|
|
|
const db = require('../../../../models');
|
2018-10-09 02:02:05 +02:00
|
|
|
const waitOn = require('wait-on');
|
2019-01-07 23:47:56 +01:00
|
|
|
const logger = require('winston');
|
2018-03-29 02:35:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
route to get a claim
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-10-09 02:02:05 +02:00
|
|
|
const claimGet = async ({ ip, originalUrl, params }, res) => {
|
2018-03-29 20:24:52 +02:00
|
|
|
const name = params.name;
|
|
|
|
const claimId = params.claimId;
|
2018-08-09 20:06:32 +02:00
|
|
|
|
2018-10-09 02:02:05 +02:00
|
|
|
try {
|
2019-01-07 23:47:56 +01:00
|
|
|
let claimInfo = await chainquery.claim.queries.resolveClaim(name, claimId).catch(() => {});
|
|
|
|
if (claimInfo) {
|
|
|
|
logger.info('claim/get: claim resolved in chainquery');
|
2018-10-09 02:02:05 +02:00
|
|
|
}
|
2019-01-07 23:47:56 +01:00
|
|
|
if (!claimInfo) {
|
|
|
|
claimInfo = await db.Claim.resolveClaim(name, claimId);
|
|
|
|
}
|
|
|
|
if (!claimInfo) {
|
|
|
|
throw new Error('claim/get: resolveClaim: No matching uri found in Claim table');
|
2018-10-09 02:02:05 +02:00
|
|
|
}
|
|
|
|
let lbrynetResult = await getClaim(`${name}#${claimId}`);
|
2018-11-11 01:11:12 +01:00
|
|
|
if (!lbrynetResult) {
|
2019-01-07 23:47:56 +01:00
|
|
|
throw new Error(`claim/get: getClaim Unable to Get ${name}#${claimId}`);
|
|
|
|
}
|
|
|
|
const claimData = await getClaimData(claimInfo);
|
|
|
|
if (!claimData) {
|
|
|
|
throw new Error('claim/get: getClaimData failed to get file blobs');
|
|
|
|
}
|
|
|
|
await waitOn({
|
|
|
|
resources: [ lbrynetResult.download_path ],
|
|
|
|
timeout : 10000, // 10 seconds
|
|
|
|
window : 500,
|
|
|
|
});
|
|
|
|
const fileData = await createFileRecordDataAfterGet(claimData, lbrynetResult);
|
|
|
|
if (!fileData) {
|
|
|
|
throw new Error('claim/get: createFileRecordDataAfterGet failed to create file in time');
|
2018-10-09 02:02:05 +02:00
|
|
|
}
|
|
|
|
const upsertCriteria = { name, claimId };
|
|
|
|
await db.upsert(db.File, fileData, upsertCriteria, 'File');
|
|
|
|
const { message, completed } = lbrynetResult;
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
message,
|
|
|
|
completed,
|
2018-03-29 20:24:52 +02:00
|
|
|
});
|
2018-11-11 01:11:12 +01:00
|
|
|
} catch (error) {
|
2018-10-09 02:02:05 +02:00
|
|
|
handleErrorResponse(originalUrl, ip, error, res);
|
|
|
|
}
|
2018-03-29 02:35:41 +02:00
|
|
|
};
|
|
|
|
module.exports = claimGet;
|