spee.ch/helpers/socketHelpers.js

68 lines
2.1 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');
2017-06-12 23:56:40 +02:00
var config = require('config');
2017-06-13 04:09:39 +02:00
var ua = require('universal-analytics');
2017-06-13 00:57:14 +02:00
2017-06-12 23:56:40 +02:00
var walledAddress = config.get('WalletConfig.lbryAddress');
2017-06-13 04:09:39 +02:00
var googleAnalyticsId = config.get('AnalyticsConfig.googleId');
var visitor = ua(googleAnalyticsId, {https: true});
2017-06-03 05:56:33 +02: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 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.01,
2017-06-03 05:56:33 +02:00
"metadata": {
"description": name + " published via spee.ch",
"title": name,
"author": "spee.ch",
"language": "en",
"license": license,
"nsfw": (nsfw.toLowerCase() === "true")
},
2017-06-12 23:56:40 +02:00
"claim_address": walledAddress,
"change_address": walledAddress //requires daemon 0.12.2rc1 or above
2017-06-03 05:56:33 +02:00
};
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)...");
2017-06-13 04:09:39 +02:00
visitor.event("Publish", "Publish Request", name, {filepath, license, nsfw}).send();
2017-06-03 05:56:33 +02: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 04:09:39 +02:00
visitor.event("Publish", "Publish Success", name, {filepath, license, nsfw}).send();
2017-06-03 05:56:33 +02:00
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){
2017-06-13 04:09:39 +02:00
visitor.event("Publish", "Publish Failure", name, {filepath, license, nsfw}).send();
2017-06-03 05:56:33 +02:00
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
});
}
}