spee.ch/helpers/socketHelpers.js

65 lines
2 KiB
JavaScript
Raw Normal View History

2017-06-02 22:19:47 -07:00
var fs = require('fs');
2017-06-02 20:56:33 -07:00
var lbryApi = require('../helpers/lbryApi.js');
2017-06-12 14:56:40 -07:00
var config = require('config');
2017-06-12 15:57:14 -07:00
2017-06-12 14:56:40 -07:00
var walledAddress = config.get('WalletConfig.lbryAddress');
2017-06-02 20:56:33 -07:00
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 00:41:02 -07:00
2017-06-02 20:56:33 -07:00
function createPublishParams(name, filepath, license, nsfw) {
var publishParams = {
"name": name,
"file_path": filepath,
"bid": 0.01,
2017-06-02 20:56:33 -07:00
"metadata": {
"description": name + " published via spee.ch",
"title": name,
"author": "spee.ch",
"language": "en",
"license": license,
"nsfw": (nsfw.toLowerCase() === "true")
},
2017-06-12 14:56:40 -07:00
"claim_address": walledAddress,
"change_address": walledAddress //requires daemon 0.12.2rc1 or above
2017-06-02 20:56:33 -07:00
};
return publishParams;
}
2017-06-03 00:41:02 -07:00
2017-06-02 22:19:47 -07:00
function deleteTemporaryFile(filepath) {
fs.unlink(filepath, function(err) {
2017-06-02 20:56:33 -07:00
if (err) throw err;
2017-06-02 22:19:47 -07:00
console.log('successfully deleted ' + filepath);
2017-06-02 20:56:33 -07:00
});
}
module.exports = {
2017-06-13 12:12:41 -07:00
publish: function(name, filepath, license, nsfw, socket, visitor) {
2017-06-02 20:56:33 -07:00
// update the client
socket.emit("publish-status", "Your image is being published (this might take a second)...");
2017-06-13 12:12:41 -07:00
visitor.event("Publish Route", "Publish Request", filepath).send();
2017-06-02 20:56:33 -07:00
// create the publish object
var publishParams = createPublishParams(name, filepath, license, nsfw);
// get a promise to publish
lbryApi.publishClaim(publishParams)
.then(function(data){
2017-06-13 12:12:41 -07:00
visitor.event("Publish Route", "Publish Success", filepath).send();
2017-06-02 20:56:33 -07:00
console.log("publish promise success. Tx info:", data)
2017-06-03 00:41:02 -07:00
socket.emit("publish-complete", {name: name, result: data.result});
2017-06-02 22:19:47 -07:00
deleteTemporaryFile(filepath);
2017-06-02 20:56:33 -07:00
})
.catch(function(error){
2017-06-13 12:12:41 -07:00
visitor.event("Publish Route", "Publish Failure", filepath).send();
2017-06-02 20:56:33 -07:00
console.log("error:", error);
2017-06-03 00:41:02 -07:00
socket.emit("publish-failure", handlePublishError(error));
2017-06-02 22:19:47 -07:00
deleteTemporaryFile(filepath);
2017-06-02 20:56:33 -07:00
});
}
}