refactored publish controller and routes
This commit is contained in:
parent
82e7dea830
commit
1188d73f5d
8 changed files with 110 additions and 57 deletions
|
@ -1,9 +1,9 @@
|
||||||
{
|
{
|
||||||
"WalletConfig": {
|
"WalletConfig": {
|
||||||
"lbryAddress": "none"
|
"LbryAddress": "none"
|
||||||
},
|
},
|
||||||
"AnalyticsConfig":{
|
"AnalyticsConfig":{
|
||||||
"googleId": "UA-60403362-2"
|
"GoogleId": "UA-60403362-2"
|
||||||
},
|
},
|
||||||
"Database": {
|
"Database": {
|
||||||
"MySqlConnectionUri": "none",
|
"MySqlConnectionUri": "none",
|
||||||
|
|
|
@ -1,32 +1,8 @@
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
||||||
const config = require('config');
|
|
||||||
const walledAddress = config.get('WalletConfig.lbryAddress');
|
|
||||||
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
|
||||||
const db = require('../models');
|
const db = require('../models');
|
||||||
|
|
||||||
function createPublishParams (claim, filePath, license, nsfw) {
|
|
||||||
logger.debug(`Creating Publish Parameters for "${claim}"`);
|
|
||||||
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,
|
|
||||||
nsfw,
|
|
||||||
},
|
|
||||||
claim_address : walledAddress,
|
|
||||||
change_address: walledAddress,
|
|
||||||
};
|
|
||||||
logger.debug('publishParams:', publishParams);
|
|
||||||
return publishParams;
|
|
||||||
}
|
|
||||||
|
|
||||||
function deleteTemporaryFile (filePath) {
|
function deleteTemporaryFile (filePath) {
|
||||||
fs.unlink(filePath, err => {
|
fs.unlink(filePath, err => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
@ -49,51 +25,34 @@ function upsert (Model, values, condition) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
publish (name, fileName, filePath, fileType, license, nsfw, socket, visitor) {
|
publish: (publishParams, fileName, fileType) => {
|
||||||
console.log('nsfw:', nsfw);
|
|
||||||
// validate nsfw
|
|
||||||
if (typeof nsfw === 'string') {
|
|
||||||
nsfw = (nsfw.toLowerCase() === 'true');
|
|
||||||
}
|
|
||||||
// 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();
|
|
||||||
// create the publish object
|
|
||||||
const publishParams = createPublishParams(name, filePath, license, nsfw);
|
|
||||||
// 1. publish the file
|
// 1. publish the file
|
||||||
lbryApi
|
lbryApi
|
||||||
.publishClaim(publishParams, fileName, fileType)
|
.publishClaim(publishParams)
|
||||||
.then(result => {
|
.then(result => {
|
||||||
logger.info(`Successfully published ${fileName}`, result);
|
logger.info(`Successfully published ${fileName}`, result);
|
||||||
// google analytics
|
|
||||||
visitor.event('Publish Route', 'Publish Success', filePath).send();
|
|
||||||
// 2. update old record of create new one (update is in case the claim has been published before by this daemon)
|
// 2. update old record of create new one (update is in case the claim has been published before by this daemon)
|
||||||
upsert(
|
upsert(
|
||||||
db.File,
|
db.File,
|
||||||
{
|
{
|
||||||
name,
|
name : publishParams.name,
|
||||||
claimId : result.claim_id,
|
claimId : result.claim_id,
|
||||||
outpoint: `${result.txid}:${result.nout}`,
|
outpoint: `${result.txid}:${result.nout}`,
|
||||||
height : 0,
|
height : 0,
|
||||||
fileName,
|
fileName,
|
||||||
filePath,
|
filePath: publishParams.file_path,
|
||||||
fileType,
|
fileType,
|
||||||
nsfw,
|
nsfw : publishParams.metadata.nsfw,
|
||||||
},
|
},
|
||||||
{ name, claimId: result.claim_id }
|
{ name: publishParams.name, claimId: result.claim_id }
|
||||||
).catch(error => {
|
).catch(error => {
|
||||||
logger.error('Sequelize findOne error', error);
|
logger.error('Sequelize findOne error', error);
|
||||||
});
|
});
|
||||||
// update client
|
|
||||||
socket.emit('publish-complete', { name, result });
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
logger.error(`Error publishing ${fileName}`, error);
|
logger.error(`Error publishing ${fileName}`, error);
|
||||||
// google analytics
|
// delete the local file
|
||||||
visitor.event('Publish Route', 'Publish Failure', filePath).send();
|
deleteTemporaryFile(publishParams.file_path);
|
||||||
socket.emit('publish-failure', errorHandlers.handlePublishError(error));
|
|
||||||
deleteTemporaryFile(filePath);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,8 +2,8 @@ const axios = require('axios');
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
publishClaim (publishParams, fileName, fileType) {
|
publishClaim (publishParams) {
|
||||||
logger.debug(`Publishing claim for "${fileName}"`);
|
logger.debug(`Publishing claim to "${publishParams.name}"`);
|
||||||
const deferred = new Promise((resolve, reject) => {
|
const deferred = new Promise((resolve, reject) => {
|
||||||
axios
|
axios
|
||||||
.post('http://localhost:5279/lbryapi', {
|
.post('http://localhost:5279/lbryapi', {
|
||||||
|
|
27
helpers/libraries/publishHelpers.js
Normal file
27
helpers/libraries/publishHelpers.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
const logger = require('winston');
|
||||||
|
|
||||||
|
const config = require('config');
|
||||||
|
const walletAddress = config.get('WalletConfig.LbryAddress');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
createPublishParams (name, filePath, license, nsfw) {
|
||||||
|
logger.debug(`Creating Publish Parameters for "${name}"`);
|
||||||
|
const publishParams = {
|
||||||
|
name,
|
||||||
|
file_path: filePath,
|
||||||
|
bid : 0.01,
|
||||||
|
metadata : {
|
||||||
|
description: `${name} published via spee.ch`,
|
||||||
|
title : name,
|
||||||
|
author : 'spee.ch',
|
||||||
|
language : 'en',
|
||||||
|
license,
|
||||||
|
nsfw,
|
||||||
|
},
|
||||||
|
claim_address : walletAddress,
|
||||||
|
change_address: walletAddress,
|
||||||
|
};
|
||||||
|
logger.debug('publishParams:', publishParams);
|
||||||
|
return publishParams;
|
||||||
|
},
|
||||||
|
};
|
|
@ -29,6 +29,7 @@
|
||||||
"axios": "^0.16.1",
|
"axios": "^0.16.1",
|
||||||
"body-parser": "^1.17.1",
|
"body-parser": "^1.17.1",
|
||||||
"config": "^1.26.1",
|
"config": "^1.26.1",
|
||||||
|
"connect-multiparty": "^2.0.0",
|
||||||
"express": "^4.15.2",
|
"express": "^4.15.2",
|
||||||
"express-handlebars": "^3.0.0",
|
"express-handlebars": "^3.0.0",
|
||||||
"mysql2": "^1.3.5",
|
"mysql2": "^1.3.5",
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
||||||
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
const multipart = require('connect-multiparty');
|
||||||
|
const multipartMiddleware = multipart();
|
||||||
|
const publishController = require('../helpers/libraries/publishController.js');
|
||||||
|
const publishHelpers = require('../helpers/libraries/publishHelpers.js');
|
||||||
|
|
||||||
module.exports = app => {
|
module.exports = app => {
|
||||||
// route to run a claim_list request on the daemon
|
// route to run a claim_list request on the daemon
|
||||||
|
@ -27,4 +31,36 @@ module.exports = app => {
|
||||||
errorHandlers.handleRequestError(error, res);
|
errorHandlers.handleRequestError(error, res);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
// route to run a publish request on the daemon
|
||||||
|
app.post('/api/publish', multipartMiddleware, ({ originalUrl, body, files }, res) => {
|
||||||
|
logger.debug(`POST request on ${originalUrl}`);
|
||||||
|
console.log('>> req.files:', files);
|
||||||
|
console.log(' >> req.body:', body);
|
||||||
|
const name = body.claimName;
|
||||||
|
const license = body.license;
|
||||||
|
const nsfw = body.nsfw;
|
||||||
|
const fileName = files.file1.name;
|
||||||
|
const filePath = files.file1.path;
|
||||||
|
const fileType = files.file1.type;
|
||||||
|
/*
|
||||||
|
make sure it's not a harmful file type
|
||||||
|
*/
|
||||||
|
// prepare the publish parameters
|
||||||
|
const publishParams = publishHelpers.createPublishParams(name, filePath, license, nsfw);
|
||||||
|
// publish the file
|
||||||
|
publishController
|
||||||
|
.publish(publishParams, fileName, fileType)
|
||||||
|
.then(result => {
|
||||||
|
res.status(200).json(result);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
errorHandlers.handleRequestError(error, res);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (files) {
|
||||||
|
res.status(200).json({'status': 'file(s) received'});
|
||||||
|
} else {
|
||||||
|
res.status(400).josn({'status': 'no files(s) received'});
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
const publishController = require('../controllers/publishController.js');
|
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
|
const publishController = require('../controllers/publishController.js');
|
||||||
|
const publishHelpers = require('../controllers/publishHelpers.js');
|
||||||
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
||||||
|
|
||||||
module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
|
module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
|
||||||
const http = require('http').Server(app);
|
const http = require('http').Server(app);
|
||||||
|
@ -7,8 +9,9 @@ module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
|
||||||
|
|
||||||
io.on('connection', socket => {
|
io.on('connection', socket => {
|
||||||
logger.silly('a user connected via sockets');
|
logger.silly('a user connected via sockets');
|
||||||
// create visitor record
|
// google analytics
|
||||||
const visitor = ua(googleAnalyticsId, { https: true });
|
const visitor = ua(googleAnalyticsId, { https: true });
|
||||||
|
visitor.event('Publish Route', 'Publish Request').send();
|
||||||
// attach upload listeners
|
// attach upload listeners
|
||||||
const uploader = new siofu();
|
const uploader = new siofu();
|
||||||
uploader.dir = hostedContentPath;
|
uploader.dir = hostedContentPath;
|
||||||
|
@ -27,8 +30,18 @@ module.exports = (app, siofu, hostedContentPath, ua, googleAnalyticsId) => {
|
||||||
uploader.on('saved', ({ file }) => {
|
uploader.on('saved', ({ file }) => {
|
||||||
if (file.success) {
|
if (file.success) {
|
||||||
logger.debug(`Client successfully uploaded ${file.name}`);
|
logger.debug(`Client successfully uploaded ${file.name}`);
|
||||||
socket.emit('publish-status', 'file upload successfully completed');
|
socket.emit('publish-status', 'file upload successfully completed. Your image is being published (this might take a second)...');
|
||||||
publishController.publish(file.meta.name, file.name, file.pathName, file.meta.type, file.meta.license, file.meta.nsfw, socket, visitor);
|
// prepare the publish parameters
|
||||||
|
const publishParams = publishHelpers.createPublishParams(file.meta.name, file.pathName, file.meta.license, file.meta.nsfw);
|
||||||
|
// publish the file
|
||||||
|
publishController
|
||||||
|
.publish(publishParams, file.name, file.meta.type)
|
||||||
|
.then(result => {
|
||||||
|
socket.emit('publish-complete', { name: publishParams.name, result });
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
socket.emit('publish-failure', errorHandlers.handlePublishError(error));
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
logger.error(`An error occurred in uploading the client's file`);
|
logger.error(`An error occurred in uploading the client's file`);
|
||||||
socket.emit('publish-failure', 'file uploaded, but with errors');
|
socket.emit('publish-failure', 'file uploaded, but with errors');
|
||||||
|
|
17
uploadTest.html
Normal file
17
uploadTest.html
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<title>upload</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="http://localhost:3000/api/publish" method="post" enctype="multipart/form-data">
|
||||||
|
<p><input type="text" name="text1" value="text default">
|
||||||
|
<p><input type="text" name="text2" value="aωb">
|
||||||
|
<p><input type="file" name="file1">
|
||||||
|
<p><input type="file" name="file2">
|
||||||
|
<p><input type="file" name="file3">
|
||||||
|
<p><button type="submit">Submit</button>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in a new issue