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

61 lines
2.1 KiB
JavaScript
Raw Normal View History

const { getClaim } = require('server/lbrynet');
const { createFileRecordDataAfterGet } = require('server/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;
const db = require('server/models');
2019-01-07 23:47:56 +01:00
const logger = require('winston');
2019-02-26 07:56:44 +01:00
const awaitFileSize = require('server/utils/awaitFileSize');
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');
}
2019-03-01 06:29:45 +01:00
const fileReady = await awaitFileSize(lbrynetResult.outpoint, 10000000, 250, 10000);
2019-02-26 07:56:44 +01:00
if (fileReady !== 'ready') {
throw new Error('claim/get: failed to get file after 10 seconds');
}
2019-01-07 23:47:56 +01:00
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;