2017-06-19 22:10:06 +02:00
|
|
|
const logger = require('winston');
|
2017-06-26 19:02:14 +02:00
|
|
|
const multipart = require('connect-multiparty');
|
|
|
|
const multipartMiddleware = multipart();
|
2017-06-26 19:43:35 +02:00
|
|
|
const publishController = require('../controllers/publishController.js');
|
|
|
|
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
2017-06-26 19:02:14 +02:00
|
|
|
const publishHelpers = require('../helpers/libraries/publishHelpers.js');
|
2017-06-26 19:43:35 +02:00
|
|
|
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
2017-05-24 20:07:43 +02:00
|
|
|
|
2017-06-17 22:51:30 +02:00
|
|
|
module.exports = app => {
|
|
|
|
// route to run a claim_list request on the daemon
|
2017-06-19 22:10:06 +02:00
|
|
|
app.get('/api/claim_list/:claim', ({ originalUrl, params }, res) => {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`GET request on ${originalUrl}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
lbryApi
|
|
|
|
.getClaimsList(params.claim)
|
|
|
|
.then(claimsList => {
|
2017-06-19 18:37:35 +02:00
|
|
|
res.status(200).json(claimsList);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
errorHandlers.handleRequestError(error, res);
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
// route to run a resolve request on the daemon
|
2017-06-19 22:10:06 +02:00
|
|
|
app.get('/api/resolve/:uri', ({ originalUrl, params }, res) => {
|
2017-06-20 04:34:34 +02:00
|
|
|
logger.debug(`GET request on ${originalUrl}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
lbryApi
|
|
|
|
.resolveUri(params.uri)
|
|
|
|
.then(resolvedUri => {
|
2017-06-19 18:37:35 +02:00
|
|
|
res.status(200).json(resolvedUri);
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
errorHandlers.handleRequestError(error, res);
|
|
|
|
});
|
|
|
|
});
|
2017-06-26 19:02:14 +02:00
|
|
|
// 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);
|
2017-06-26 19:43:35 +02:00
|
|
|
console.log('>> req.body:', body);
|
|
|
|
const file = files.file1;
|
|
|
|
if (!file) {
|
|
|
|
res.status(500).json({ 'error': 'No file was submitted. Form submission must have key "speechFile"' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const name = body.claim || file.name.substring(0, file.name.indexOf('.'));
|
|
|
|
const license = body.license || 'No License Provided';
|
|
|
|
const nsfw = body.nsfw || true;
|
|
|
|
const fileName = file.name;
|
|
|
|
const filePath = file.path;
|
|
|
|
const fileType = file.type;
|
2017-06-26 19:02:14 +02:00
|
|
|
/*
|
|
|
|
make sure it's not a harmful file type
|
|
|
|
*/
|
|
|
|
// prepare the publish parameters
|
|
|
|
const publishParams = publishHelpers.createPublishParams(name, filePath, license, nsfw);
|
2017-06-26 19:43:35 +02:00
|
|
|
console.log('publish params', publishParams);
|
2017-06-26 19:02:14 +02:00
|
|
|
// publish the file
|
|
|
|
publishController
|
|
|
|
.publish(publishParams, fileName, fileType)
|
|
|
|
.then(result => {
|
|
|
|
res.status(200).json(result);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
errorHandlers.handleRequestError(error, res);
|
|
|
|
});
|
|
|
|
|
2017-06-26 19:43:35 +02:00
|
|
|
// if (files) {
|
|
|
|
// res.status(200).json({'status': 'file(s) received'});
|
|
|
|
// } else {
|
|
|
|
// res.status(400).josn({'status': 'no files(s) received'});
|
|
|
|
// }
|
2017-06-26 19:02:14 +02:00
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|