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');
|
|
|
|
const db = require('../../../../models');
|
2018-03-29 02:35:41 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
route to get a claim
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-03-29 20:24:52 +02:00
|
|
|
const claimGet = ({ ip, originalUrl, params }, res) => {
|
|
|
|
const name = params.name;
|
|
|
|
const claimId = params.claimId;
|
2018-07-26 02:21:02 +02:00
|
|
|
let resolveResult;
|
|
|
|
let getResult;
|
2018-03-29 20:24:52 +02:00
|
|
|
// resolve the claim
|
|
|
|
db.Claim.resolveClaim(name, claimId)
|
2018-07-26 02:21:02 +02:00
|
|
|
.then(result => {
|
|
|
|
if (!result) {
|
2018-03-29 20:24:52 +02:00
|
|
|
throw new Error('No matching uri found in Claim table');
|
|
|
|
}
|
2018-07-26 02:21:02 +02:00
|
|
|
resolveResult = result;
|
|
|
|
return getClaim(`${name}#${claimId}`);
|
2018-03-29 20:24:52 +02:00
|
|
|
})
|
2018-07-26 02:21:02 +02:00
|
|
|
.then(result => {
|
|
|
|
getResult = result;
|
2018-07-26 02:29:59 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
2018-07-26 22:48:53 +02:00
|
|
|
const fileData = createFileRecordDataAfterGet(resolveResult, getResult);
|
2018-08-01 01:43:08 +02:00
|
|
|
const upsertCriteria = { name, claimId };
|
|
|
|
return db.upsert(db.File, fileData, upsertCriteria, 'File');
|
2018-03-29 20:24:52 +02:00
|
|
|
})
|
2018-07-26 02:21:02 +02:00
|
|
|
.then(() => {
|
2018-07-26 02:29:59 +02:00
|
|
|
const { message, completed } = getResult;
|
2018-07-26 02:21:02 +02:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
message,
|
|
|
|
completed,
|
|
|
|
});
|
2018-03-29 20:24:52 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
handleErrorResponse(originalUrl, ip, error, res);
|
|
|
|
});
|
2018-03-29 02:35:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = claimGet;
|