spee.ch/controllers/publishController.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-06-03 07:19:47 +02:00
var fs = require('fs');
var lbryApi = require('../helpers/libraries/lbryApi.js');
2017-06-12 23:56:40 +02:00
var config = require('config');
var errorHandlers = require("../helpers/libraries/errorHandlers.js");
2017-06-13 00:57:14 +02:00
2017-06-12 23:56:40 +02:00
var walledAddress = config.get('WalletConfig.lbryAddress');
2017-06-03 05:56:33 +02:00
function createPublishParams(name, filePath, license, nsfw) {
2017-06-03 05:56:33 +02:00
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
function deleteTemporaryFile(filePath) {
fs.unlink(filePath, function(err) {
2017-06-03 05:56:33 +02:00
if (err) throw err;
console.log('successfully deleted ' + filePath);
2017-06-03 05:56:33 +02:00
});
}
module.exports = {
publish: function(name, filePath, fileType, license, nsfw, socket, visitor) {
2017-06-03 05:56:33 +02:00
// update the client
socket.emit("publish-status", "Your image is being published (this might take a second)...");
// send to analytics
visitor.event("Publish Route", "Publish Request", filePath).send();
2017-06-03 05:56:33 +02:00
// create the publish object
var publishParams = createPublishParams(name, filePath, license, nsfw);
2017-06-03 05:56:33 +02:00
// get a promise to publish
2017-06-16 19:07:31 +02:00
lbryApi.publishClaim(publishParams, fileType)
.then(function(result){
visitor.event("Publish Route", "Publish Success", filePath).send();
2017-06-16 19:07:31 +02:00
console.log("publish promise success. Tx info:", result)
socket.emit("publish-complete", {name: name, result: result});
2017-06-03 05:56:33 +02:00
})
.catch(function(error){
visitor.event("Publish Route", "Publish Failure", filePath).send();
2017-06-03 05:56:33 +02:00
console.log("error:", error);
socket.emit("publish-failure", errorHandlers.handlePublishError(error));
deleteTemporaryFile(filePath);
2017-06-03 05:56:33 +02:00
});
}
}