spee.ch/routes/sockets-routes.js

80 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-05-25 07:50:02 +02:00
module.exports = function(app) {
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var path = require('path');
var lbryApi = require('../helpers/lbryApi.js');
2017-06-01 04:48:09 +02:00
var queueApi = require('../helpers/queueApi.js');
var siofu = require("socketio-file-upload");
2017-06-01 01:44:45 +02:00
// functions to create a publishing object
2017-06-01 04:48:09 +02:00
function createPublishParams(name, filepath, license, nsfw){
var publishParams = {
"name": name,
2017-06-01 05:11:00 +02:00
"file_path": filepath,
2017-06-01 04:48:09 +02:00
"bid": 0.1,
"metadata": {
2017-06-01 05:29:16 +02:00
"description": name + " published via spee.ch",
2017-06-01 04:48:09 +02:00
"title": name,
"author": "spee.ch",
"language": "en",
"license": license,
"nsfw": (nsfw.toLowerCase() === "true")
2017-06-01 01:44:45 +02:00
}
};
2017-06-01 04:48:09 +02:00
return publishParams;
}
2017-06-01 01:44:45 +02:00
// publish an image to lbry
2017-06-01 04:48:09 +02:00
function publish(name, filepath, license, nsfw, socket){
// update the client
2017-06-01 05:11:00 +02:00
socket.emit("publish-status", "Your image is being published (this might take a second)...");
2017-06-01 04:48:09 +02:00
// create the publish object
var publishParams = createPublishParams(name, filepath, license, nsfw);
// get a promise to publish
var promise = lbryApi.publishClaim(publishParams);
// handle promise
promise.then(function(data){
console.log("publish promise success. Tx info:", data)
socket.emit("publish-complete", data);
/*
note: remember to delete the local file
*/
})
.catch(function(error){
console.log("error:", error);
socket.emit("publish-status", "publish failed");
/*
note: remember to delete the local file
*/
});
2017-06-01 01:44:45 +02:00
};
2017-05-25 07:50:02 +02:00
io.on('connection', function(socket){
console.log('a user connected');
2017-06-01 04:48:09 +02:00
// listener for uploader
var uploader = new siofu();
2017-06-01 05:29:16 +02:00
uploader.dir = path.join(__dirname, '../../Uploads');
2017-06-01 04:48:09 +02:00
uploader.listen(socket);
// attach upload listeners
uploader.on("error", function(event){
console.log("an error occured while uploading", event.error);
socket.emit("publish-status", event.error)
2017-05-25 07:50:02 +02:00
})
2017-06-01 04:48:09 +02:00
uploader.on("saved", function(event){
console.log("saved " + event.file.name);
if (event.file.success){
socket.emit("publish-status", "file upload successfully completed");
publish(event.file.meta.name, event.file.pathName, event.file.meta.license,event.file.meta.nsfw, socket)
2017-06-01 01:44:45 +02:00
} else {
2017-06-01 04:48:09 +02:00
socket.emit("publish-status", "file saved, but with errors")
};
2017-06-01 01:44:45 +02:00
});
2017-05-25 07:50:02 +02:00
// handle disconnect
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
return http;
}