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');
|
|
|
|
const chainquery = require('chainquery');
|
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');
|
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 {
|
|
|
|
let claimData = await chainquery.claim.queries.resolveClaim(name, claimId).catch(() => {});
|
2018-11-11 01:11:12 +01:00
|
|
|
if (!claimData) {
|
2018-10-09 02:02:05 +02:00
|
|
|
claimData = await db.Claim.resolveClaim(name, claimId);
|
|
|
|
}
|
|
|
|
|
2018-11-11 01:11:12 +01:00
|
|
|
if (!claimData) {
|
2018-10-09 02:02:05 +02:00
|
|
|
throw new Error('No matching uri found in Claim table');
|
|
|
|
}
|
|
|
|
|
|
|
|
let lbrynetResult = await getClaim(`${name}#${claimId}`);
|
2018-11-11 01:11:12 +01:00
|
|
|
if (!lbrynetResult) {
|
2018-10-09 02:02:05 +02:00
|
|
|
throw new Error(`Unable to Get ${name}#${claimId}`);
|
|
|
|
}
|
|
|
|
|
2018-10-10 02:11:06 +02:00
|
|
|
let fileData = await createFileRecordDataAfterGet(await getClaimData(claimData), lbrynetResult);
|
2018-10-09 02:02:05 +02:00
|
|
|
const upsertCriteria = { name, claimId };
|
|
|
|
await db.upsert(db.File, fileData, upsertCriteria, 'File');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await waitOn({
|
|
|
|
resources: [ lbrynetResult.file_name ],
|
2018-11-11 01:11:12 +01:00
|
|
|
delay : 100,
|
|
|
|
timeout : 10000, // 10 seconds
|
2018-07-26 02:21:02 +02:00
|
|
|
});
|
2018-10-09 02:02:05 +02:00
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
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;
|