2017-05-24 20:07:43 +02:00
|
|
|
var axios = require('axios');
|
2017-06-17 02:48:51 +02:00
|
|
|
var db = require("../../models");
|
2017-05-24 20:07:43 +02:00
|
|
|
|
|
|
|
module.exports = {
|
2017-06-17 04:31:31 +02:00
|
|
|
publishClaim: function(publishParams, fileName, fileType){
|
2017-06-01 04:48:09 +02:00
|
|
|
var deferred = new Promise(function(resolve, reject){
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> lbryApi >> publishClaim:", publishParams);
|
2017-06-01 04:48:09 +02:00
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
|
|
|
"method": "publish",
|
|
|
|
"params": publishParams
|
2017-06-14 02:13:38 +02:00
|
|
|
}).then(function (response) {
|
2017-06-17 04:31:31 +02:00
|
|
|
console.log(">> 'publish' success", response);
|
2017-06-17 02:48:51 +02:00
|
|
|
var result = response.data.result;
|
2017-06-16 19:07:31 +02:00
|
|
|
db.File.create({
|
|
|
|
name: publishParams.name,
|
2017-06-17 02:48:51 +02:00
|
|
|
claim_id: result.claim_id,
|
|
|
|
outpoint: result.txid + ":" + result.nout,
|
2017-06-17 04:31:31 +02:00
|
|
|
file_name: fileName,
|
2017-06-17 02:48:51 +02:00
|
|
|
file_path: publishParams.file_path,
|
2017-06-16 19:07:31 +02:00
|
|
|
file_type: fileType,
|
2017-06-17 02:48:51 +02:00
|
|
|
nsfw: publishParams.metadata.nsfw,
|
2017-06-16 19:07:31 +02:00
|
|
|
}).catch(function(error){
|
|
|
|
console.log('An error occurred when writing to the MySQL database:', error);
|
|
|
|
});
|
2017-06-17 02:48:51 +02:00
|
|
|
resolve(result);
|
2017-06-01 04:48:09 +02:00
|
|
|
}).catch(function(error){
|
|
|
|
console.log(">> 'publish' error");
|
2017-06-03 05:56:33 +02:00
|
|
|
reject(error);
|
2017-06-14 02:13:38 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
},
|
2017-06-14 02:52:29 +02:00
|
|
|
getClaim: function(uri){
|
2017-06-14 02:13:38 +02:00
|
|
|
var deferred = new Promise(function(resolve, reject){
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> lbryApi >> getClaim:", uri);
|
2017-06-14 02:13:38 +02:00
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
|
|
|
"method": "get",
|
2017-06-14 21:53:55 +02:00
|
|
|
"params": { "uri": uri, "timeout": 20}
|
2017-06-16 19:07:31 +02:00
|
|
|
}).then(function (response) {
|
2017-06-17 02:48:51 +02:00
|
|
|
console.log(">> 'get' success");
|
2017-06-16 00:56:40 +02:00
|
|
|
//check to make sure the daemon didn't just time out
|
2017-06-16 19:07:31 +02:00
|
|
|
if (response.data.result.error){
|
|
|
|
reject(response.data.result.error);
|
2017-06-14 02:52:29 +02:00
|
|
|
}
|
2017-06-14 02:13:38 +02:00
|
|
|
/*
|
2017-06-14 02:52:29 +02:00
|
|
|
note: put in a check to make sure we do not resolve until the download is actually complete (response.data.completed === true)
|
2017-06-14 02:13:38 +02:00
|
|
|
*/
|
2017-06-16 00:03:40 +02:00
|
|
|
// save a record of the file to the Files table
|
2017-06-17 02:48:51 +02:00
|
|
|
var result = response.data.result
|
2017-06-16 00:03:40 +02:00
|
|
|
db.File.create({
|
2017-06-17 02:48:51 +02:00
|
|
|
name: result.name,
|
|
|
|
claim_id: result.claim_id,
|
|
|
|
outpoint: result.outpoint,
|
|
|
|
file_name: result.file_name,
|
|
|
|
file_path: result.download_path,
|
|
|
|
file_type: result.mime_type,
|
|
|
|
nsfw: result.metadata.stream.metadata.nsfw,
|
2017-06-16 00:03:40 +02:00
|
|
|
}).catch(function(error){
|
2017-06-16 19:07:31 +02:00
|
|
|
console.log('An error occurred when writing to the MySQL database:', error);
|
2017-06-16 00:03:40 +02:00
|
|
|
});
|
2017-06-17 02:48:51 +02:00
|
|
|
resolve(result);
|
2017-06-16 19:07:31 +02:00
|
|
|
}).catch(function(error){
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> 'get' error");
|
2017-06-16 19:07:31 +02:00
|
|
|
reject(error);
|
2017-06-14 02:13:38 +02:00
|
|
|
});
|
|
|
|
});
|
2017-06-01 04:48:09 +02:00
|
|
|
return deferred;
|
2017-05-24 20:07:43 +02:00
|
|
|
},
|
2017-06-14 00:39:38 +02:00
|
|
|
getClaimsList: function(claimName){
|
|
|
|
var deferred = new Promise(function(resolve, reject){
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> lbryApi >> getClaimList:", claimName);
|
2017-06-01 05:54:56 +02:00
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
2017-06-14 00:39:38 +02:00
|
|
|
method: "claim_list",
|
|
|
|
params: { name: claimName }
|
|
|
|
}).then(function (response) {
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> 'claim_list' success");
|
2017-06-17 02:48:51 +02:00
|
|
|
resolve(response.data.result);
|
2017-06-14 00:39:38 +02:00
|
|
|
}).catch(function(error){
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> 'claim_list' error");
|
2017-06-03 05:56:33 +02:00
|
|
|
reject(error);
|
2017-05-25 09:59:22 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-05-24 20:07:43 +02:00
|
|
|
},
|
2017-06-14 00:39:38 +02:00
|
|
|
resolveUri: function(uri){
|
2017-06-01 01:44:45 +02:00
|
|
|
var deferred = new Promise(function(resolve, reject){
|
2017-06-14 22:07:30 +02:00
|
|
|
console.log(">> lbryApi >> resolveUri:", uri);
|
2017-06-01 01:44:45 +02:00
|
|
|
axios.post('http://localhost:5279/lbryapi', {
|
2017-06-14 00:39:38 +02:00
|
|
|
"method": "resolve",
|
|
|
|
"params": { "uri": uri}
|
|
|
|
}).then(function(response){
|
2017-06-14 21:53:55 +02:00
|
|
|
console.log(">> 'resolve' success");
|
2017-06-16 21:29:02 +02:00
|
|
|
resolve(response.data.result);
|
2017-06-01 01:44:45 +02:00
|
|
|
}).catch(function(error){
|
2017-06-14 00:39:38 +02:00
|
|
|
console.log(">> 'resolve' error");
|
2017-06-03 05:56:33 +02:00
|
|
|
reject(error);
|
2017-06-14 00:39:38 +02:00
|
|
|
});
|
2017-06-01 01:44:45 +02:00
|
|
|
});
|
2017-06-01 02:00:13 +02:00
|
|
|
return deferred;
|
2017-05-24 20:07:43 +02:00
|
|
|
}
|
|
|
|
}
|