spee.ch/helpers/socketHelpers.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-06-03 07:19:47 +02:00
var fs = require('fs');
2017-06-03 05:56:33 +02:00
var lbryApi = require('../helpers/lbryApi.js');
function handlePublishError(error) {
if (error.code === "ECONNREFUSED"){
return "Connection refused. The daemon may not be running.";
} else if (error.response.data.error) {
return error.response.data.error;
} else {
return error;
};
}
2017-06-03 09:41:02 +02:00
2017-06-03 05:56:33 +02:00
function createPublishParams(name, filepath, license, nsfw) {
var publishParams = {
"name": name,
"file_path": filepath,
"bid": 0.1,
"metadata": {
"description": name + " published via spee.ch",
"title": name,
"author": "spee.ch",
"language": "en",
"license": license,
"nsfw": (nsfw.toLowerCase() === "true")
}
};
return publishParams;
}
2017-06-03 09:41:02 +02:00
2017-06-03 07:19:47 +02:00
function deleteTemporaryFile(filepath) {
fs.unlink(filepath, function(err) {
2017-06-03 05:56:33 +02:00
if (err) throw err;
2017-06-03 07:19:47 +02:00
console.log('successfully deleted ' + filepath);
2017-06-03 05:56:33 +02:00
});
}
module.exports = {
publish: function(name, filepath, license, nsfw, socket) {
// update the client
socket.emit("publish-status", "Your image is being published (this might take a second)...");
// create the publish object
var publishParams = createPublishParams(name, filepath, license, nsfw);
// get a promise to publish
lbryApi.publishClaim(publishParams)
.then(function(data){
console.log("publish promise success. Tx info:", data)
2017-06-03 09:41:02 +02:00
socket.emit("publish-complete", {name: name, result: data.result});
2017-06-03 07:19:47 +02:00
deleteTemporaryFile(filepath);
2017-06-03 05:56:33 +02:00
})
.catch(function(error){
console.log("error:", error);
2017-06-03 09:41:02 +02:00
socket.emit("publish-failure", handlePublishError(error));
2017-06-03 07:19:47 +02:00
deleteTemporaryFile(filepath);
2017-06-03 05:56:33 +02:00
});
}
}