2017-06-03 07:19:47 +02:00
|
|
|
var fs = require('fs');
|
2017-06-17 02:48:51 +02:00
|
|
|
var lbryApi = require('../helpers/libraries/lbryApi.js');
|
2017-06-12 23:56:40 +02:00
|
|
|
var config = require('config');
|
2017-06-17 02:48:51 +02:00
|
|
|
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
|
|
|
|
2017-06-15 20:15:13 +02:00
|
|
|
function createPublishParams(name, filePath, license, nsfw) {
|
2017-06-03 05:56:33 +02:00
|
|
|
var publishParams = {
|
|
|
|
"name": name,
|
2017-06-15 20:15:13 +02:00
|
|
|
"file_path": filePath,
|
2017-06-12 21:07:01 +02:00
|
|
|
"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,
|
2017-06-17 02:48:51 +02:00
|
|
|
"nsfw": (nsfw.toLowerCase() === "true")
|
2017-06-12 21:07:01 +02:00
|
|
|
},
|
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-15 20:15:13 +02:00
|
|
|
function deleteTemporaryFile(filePath) {
|
|
|
|
fs.unlink(filePath, function(err) {
|
2017-06-03 05:56:33 +02:00
|
|
|
if (err) throw err;
|
2017-06-15 20:15:13 +02:00
|
|
|
console.log('successfully deleted ' + filePath);
|
2017-06-03 05:56:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2017-06-15 20:15:13 +02:00
|
|
|
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)...");
|
2017-06-15 20:15:13 +02:00
|
|
|
// send to analytics
|
|
|
|
visitor.event("Publish Route", "Publish Request", filePath).send();
|
2017-06-03 05:56:33 +02:00
|
|
|
// create the publish object
|
2017-06-15 20:15:13 +02:00
|
|
|
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){
|
2017-06-15 20:15:13 +02:00
|
|
|
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){
|
2017-06-15 20:15:13 +02:00
|
|
|
visitor.event("Publish Route", "Publish Failure", filePath).send();
|
2017-06-03 05:56:33 +02:00
|
|
|
console.log("error:", error);
|
2017-06-17 02:48:51 +02:00
|
|
|
socket.emit("publish-failure", errorHandlers.handlePublishError(error));
|
2017-06-15 20:15:13 +02:00
|
|
|
deleteTemporaryFile(filePath);
|
2017-06-03 05:56:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|