spee.ch/server/controllers/api/claim/get/index.js

58 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-04-27 18:54:36 +02:00
const { getClaim } = require('../../../../lbrynet');
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');
const waitOn = require('wait-on');
2018-03-29 02:35:41 +02:00
/*
route to get a claim
*/
const claimGet = async ({ ip, originalUrl, params }, res) => {
2018-03-29 20:24:52 +02:00
const name = params.name;
const claimId = params.claimId;
try {
let claimData = await chainquery.claim.queries.resolveClaim(name, claimId).catch(() => {});
2018-11-11 01:11:12 +01:00
if (!claimData) {
claimData = await db.Claim.resolveClaim(name, claimId);
}
2018-11-11 01:11:12 +01:00
if (!claimData) {
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) {
throw new Error(`Unable to Get ${name}#${claimId}`);
}
let fileData = await createFileRecordDataAfterGet(await getClaimData(claimData), lbrynetResult);
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
});
} 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) {
handleErrorResponse(originalUrl, ip, error, res);
}
2018-03-29 02:35:41 +02:00
};
module.exports = claimGet;