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

61 lines
2 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');
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');
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
*/
const claimGet = async ({ ip, originalUrl, params }, res) => {
2018-03-29 20:24:52 +02:00
const name = params.name;
const claimId = params.claimId;
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');
}
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');
}
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');
}
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) {
handleErrorResponse(originalUrl, ip, error, res);
}
2018-03-29 02:35:41 +02:00
};
module.exports = claimGet;