2017-06-19 18:37:35 +02:00
|
|
|
const axios = require('axios');
|
|
|
|
const db = require('../../models');
|
2017-06-20 00:25:14 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
|
|
|
|
function createFilesRecord (name, claimId, outpoint, fileName, filePath, fileType, nsfw) {
|
|
|
|
db.File
|
|
|
|
.create({ name, claimId, outpoint, fileName, filePath, fileType, nsfw })
|
|
|
|
.catch(error => {
|
|
|
|
logger.error(`Sequelize File.create error`, error);
|
|
|
|
});
|
|
|
|
}
|
2017-05-24 20:07:43 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2017-06-17 22:51:30 +02:00
|
|
|
publishClaim (publishParams, fileName, fileType) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`Publishing claim for "${fileName}"`);
|
2017-06-17 22:51:30 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post('http://localhost:5279/lbryapi', {
|
|
|
|
method: 'publish',
|
|
|
|
params: publishParams,
|
|
|
|
})
|
|
|
|
.then(response => {
|
2017-06-19 18:37:35 +02:00
|
|
|
const result = response.data.result;
|
2017-06-20 00:25:14 +02:00
|
|
|
createFilesRecord(
|
|
|
|
publishParams.name, result.claim_id, `${result.txid}:${result.nout}`, fileName, publishParams.file_path, fileType, publishParams.metadata.nsfw);
|
2017-06-19 18:37:35 +02:00
|
|
|
resolve(result);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
|
|
|
getClaim (uri) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`Getting Claim for "${uri}"`);
|
2017-06-17 22:51:30 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post('http://localhost:5279/lbryapi', {
|
|
|
|
method: 'get',
|
2017-06-20 00:25:14 +02:00
|
|
|
params: { uri, timeout: 30 },
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
// check to make sure the daemon didn't just time out
|
2017-06-20 00:25:14 +02:00
|
|
|
if (!data.result) {
|
|
|
|
reject(JSON.stringify(data));
|
|
|
|
}
|
2017-06-17 22:51:30 +02:00
|
|
|
if (data.result.error) {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(data.result.error);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
note: put in a check to make sure we do not resolve until the download is actually complete (response.data.completed === true)
|
|
|
|
*/
|
|
|
|
// save a record of the file to the Files table
|
2017-06-19 18:37:35 +02:00
|
|
|
const result = data.result;
|
2017-06-20 00:25:14 +02:00
|
|
|
createFilesRecord(
|
|
|
|
result.name, result.claim_id, result.outpoint, result.file_name, result.download_path, result.mime_type, result.metadata.stream.metadata.nsfw);
|
2017-06-19 18:37:35 +02:00
|
|
|
resolve(result);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
|
|
|
getClaimsList (claimName) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`Getting Claim List for "${claimName}"`);
|
2017-06-17 22:51:30 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post('http://localhost:5279/lbryapi', {
|
|
|
|
method: 'claim_list',
|
|
|
|
params: { name: claimName },
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
2017-06-19 18:37:35 +02:00
|
|
|
resolve(data.result);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
|
|
|
resolveUri (uri) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`Resolving URI for "${uri}"`);
|
2017-06-17 22:51:30 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
axios
|
|
|
|
.post('http://localhost:5279/lbryapi', {
|
|
|
|
method: 'resolve',
|
|
|
|
params: { uri },
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
2017-06-19 18:37:35 +02:00
|
|
|
resolve(data.result);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|