2017-06-19 18:37:35 +02:00
|
|
|
const fs = require('fs');
|
2017-06-20 01:15:28 +02:00
|
|
|
const logger = require('winston');
|
2017-06-19 18:37:35 +02:00
|
|
|
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
|
|
|
const config = require('config');
|
|
|
|
const walledAddress = config.get('WalletConfig.lbryAddress');
|
2017-06-20 01:15:28 +02:00
|
|
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
2017-06-22 01:36:08 +02:00
|
|
|
const db = require('../models');
|
2017-06-03 05:56:33 +02:00
|
|
|
|
2017-06-17 22:51:30 +02:00
|
|
|
function createPublishParams (claim, filePath, license, nsfw) {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`Creating Publish Parameters for "${claim}"`);
|
2017-06-17 22:51:30 +02:00
|
|
|
const publishParams = {
|
|
|
|
name : claim,
|
|
|
|
file_path: filePath,
|
|
|
|
bid : 0.01,
|
|
|
|
metadata : {
|
|
|
|
description: `${claim} published via spee.ch`,
|
|
|
|
title : claim,
|
|
|
|
author : 'spee.ch',
|
|
|
|
language : 'en',
|
|
|
|
license,
|
2017-06-21 01:36:19 +02:00
|
|
|
nsfw,
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
|
|
|
claim_address : walledAddress,
|
|
|
|
change_address: walledAddress,
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|
2017-06-21 01:36:19 +02:00
|
|
|
logger.debug('publishParams:', publishParams);
|
2017-06-19 18:37:35 +02:00
|
|
|
return publishParams;
|
2017-06-03 05:56:33 +02:00
|
|
|
}
|
2017-06-03 09:41:02 +02:00
|
|
|
|
2017-06-17 22:51:30 +02:00
|
|
|
function deleteTemporaryFile (filePath) {
|
|
|
|
fs.unlink(filePath, err => {
|
2017-06-19 18:37:35 +02:00
|
|
|
if (err) throw err;
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`successfully deleted ${filePath}`);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-03 05:56:33 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 01:36:08 +02:00
|
|
|
function upsert (Model, values, condition) {
|
|
|
|
return Model
|
|
|
|
.findOne({ where: condition })
|
|
|
|
.then(function (obj) {
|
|
|
|
if (obj) { // update
|
|
|
|
return obj.update(values);
|
|
|
|
} else { // insert
|
|
|
|
return Model.create(values);
|
|
|
|
}
|
|
|
|
}).catch(function (error) {
|
|
|
|
logger.error('Sequelize findOne error', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-03 05:56:33 +02:00
|
|
|
module.exports = {
|
2017-06-22 01:36:08 +02:00
|
|
|
publish (name, fileName, filePath, fileType, license, nsfw, socket, visitor) {
|
2017-06-22 19:08:41 +02:00
|
|
|
console.log('nsfw:', nsfw);
|
2017-06-22 01:36:08 +02:00
|
|
|
// validate nsfw
|
|
|
|
if (typeof nsfw === 'string') {
|
2017-06-22 19:08:41 +02:00
|
|
|
nsfw = (nsfw.toLowerCase() === 'true');
|
2017-06-22 01:36:08 +02:00
|
|
|
}
|
2017-06-17 22:51:30 +02:00
|
|
|
// update the client
|
2017-06-19 18:37:35 +02:00
|
|
|
socket.emit('publish-status', 'Your image is being published (this might take a second)...');
|
2017-06-17 22:51:30 +02:00
|
|
|
// send to analytics
|
2017-06-19 18:37:35 +02:00
|
|
|
visitor.event('Publish Route', 'Publish Request', filePath).send();
|
2017-06-17 22:51:30 +02:00
|
|
|
// create the publish object
|
2017-06-22 01:36:08 +02:00
|
|
|
const publishParams = createPublishParams(name, filePath, license, nsfw);
|
2017-06-22 19:08:41 +02:00
|
|
|
// 1. publish the file
|
2017-06-17 22:51:30 +02:00
|
|
|
lbryApi
|
|
|
|
.publishClaim(publishParams, fileName, fileType)
|
|
|
|
.then(result => {
|
2017-06-22 01:36:08 +02:00
|
|
|
logger.info(`Successfully published ${fileName}`, result);
|
|
|
|
// google analytics
|
2017-06-19 18:37:35 +02:00
|
|
|
visitor.event('Publish Route', 'Publish Success', filePath).send();
|
2017-06-22 19:08:41 +02:00
|
|
|
// 2. update old record of create new one (update is in case the claim has been published before by this daemon)
|
2017-06-22 01:36:08 +02:00
|
|
|
upsert(
|
|
|
|
db.File,
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
claimId : result.claim_id,
|
|
|
|
outpoint: `${result.txid}:${result.nout}`,
|
2017-06-22 08:33:03 +02:00
|
|
|
height : 0,
|
2017-06-22 01:36:08 +02:00
|
|
|
fileName,
|
|
|
|
filePath,
|
|
|
|
fileType,
|
|
|
|
nsfw,
|
|
|
|
},
|
|
|
|
{ name, claimId: result.claim_id }
|
|
|
|
).catch(error => {
|
|
|
|
logger.error('Sequelize findOne error', error);
|
|
|
|
});
|
|
|
|
// update client
|
|
|
|
socket.emit('publish-complete', { name, result });
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-21 01:36:19 +02:00
|
|
|
logger.error(`Error publishing ${fileName}`, error);
|
2017-06-22 01:36:08 +02:00
|
|
|
// google analytics
|
2017-06-19 18:37:35 +02:00
|
|
|
visitor.event('Publish Route', 'Publish Failure', filePath).send();
|
|
|
|
socket.emit('publish-failure', errorHandlers.handlePublishError(error));
|
|
|
|
deleteTemporaryFile(filePath);
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|