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');
|
2017-08-03 02:13:02 +02:00
|
|
|
const lbryApi = require('../helpers/lbryApi.js');
|
|
|
|
const { createPublishParams, validateFile } = require('../helpers/publishHelpers.js');
|
|
|
|
const errorHandlers = require('../helpers/errorHandlers.js');
|
2017-06-30 07:26:29 +02:00
|
|
|
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
2017-05-24 20:07:43 +02:00
|
|
|
|
2017-07-18 18:52:18 +02:00
|
|
|
module.exports = (app, hostedContentPath) => {
|
2017-06-17 22:51:30 +02:00
|
|
|
// route to run a claim_list request on the daemon
|
2017-07-06 03:26:33 +02:00
|
|
|
app.get('/api/claim_list/:name', ({ headers, ip, originalUrl, params }, res) => {
|
2017-06-30 02:10:14 +02:00
|
|
|
// google analytics
|
2017-08-04 06:59:22 +02:00
|
|
|
sendGoogleAnalytics('SERVE', headers, ip, originalUrl);
|
2017-06-30 02:10:14 +02:00
|
|
|
// serve the content
|
2017-06-17 22:51:30 +02:00
|
|
|
lbryApi
|
2017-08-15 22:48:42 +02:00
|
|
|
.getClaimList(params.name)
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(claimsList => {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('serve', originalUrl, ip, null, null, 'success');
|
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-28 07:41:48 +02:00
|
|
|
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-03 23:48:35 +02:00
|
|
|
// route to check whether spee.ch has published to a claim
|
|
|
|
app.get('/api/isClaimAvailable/:name', ({ ip, originalUrl, params }, res) => {
|
|
|
|
// send response
|
|
|
|
publishController
|
|
|
|
.checkNameAvailability(params.name)
|
|
|
|
.then(result => {
|
2017-07-04 03:27:35 +02:00
|
|
|
if (result === true) {
|
2017-07-03 23:48:35 +02:00
|
|
|
res.status(200).json(true);
|
2017-07-04 03:27:35 +02:00
|
|
|
} else {
|
|
|
|
logger.debug(`Rejecting publish request because ${params.name} has already been published via spee.ch`);
|
|
|
|
res.status(200).json(false);
|
2017-07-03 23:48:35 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
res.status(500).json(error);
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
// route to run a resolve request on the daemon
|
2017-07-06 03:26:33 +02:00
|
|
|
app.get('/api/resolve/:uri', ({ headers, ip, originalUrl, params }, res) => {
|
2017-06-30 02:10:14 +02:00
|
|
|
// google analytics
|
2017-08-04 06:59:22 +02:00
|
|
|
sendGoogleAnalytics('SERVE', headers, ip, originalUrl);
|
2017-06-30 02:10:14 +02:00
|
|
|
// serve content
|
2017-06-17 22:51:30 +02:00
|
|
|
lbryApi
|
|
|
|
.resolveUri(params.uri)
|
|
|
|
.then(resolvedUri => {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('serve', originalUrl, ip, null, null, 'success');
|
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-28 07:41:48 +02:00
|
|
|
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
});
|
2017-07-08 01:08:35 +02:00
|
|
|
|
2017-06-26 19:02:14 +02:00
|
|
|
// route to run a publish request on the daemon
|
2017-07-06 03:26:33 +02:00
|
|
|
app.post('/api/publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl }, res) => {
|
2017-06-30 02:10:14 +02:00
|
|
|
// google analytics
|
2017-08-04 06:59:22 +02:00
|
|
|
sendGoogleAnalytics('PUBLISH', headers, ip, originalUrl);
|
2017-06-27 04:39:12 +02:00
|
|
|
// validate that a file was provided
|
2017-06-27 04:26:37 +02:00
|
|
|
const file = files.speech || files.null;
|
|
|
|
const name = body.name || file.name.substring(0, file.name.indexOf('.'));
|
2017-06-26 19:43:35 +02:00
|
|
|
const license = body.license || 'No License Provided';
|
|
|
|
const nsfw = body.nsfw || true;
|
2017-07-08 01:08:35 +02:00
|
|
|
try {
|
|
|
|
validateFile(file, name, license, nsfw);
|
|
|
|
} catch (error) {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('publish', originalUrl, ip, null, null, error.message);
|
2017-07-08 01:08:35 +02:00
|
|
|
logger.debug('rejected >>', error.message);
|
|
|
|
res.status(400).send(error.message);
|
|
|
|
return;
|
2017-06-27 04:26:37 +02:00
|
|
|
}
|
2017-07-08 01:08:35 +02:00
|
|
|
// prepare the publish parameters
|
2017-06-26 19:43:35 +02:00
|
|
|
const fileName = file.name;
|
|
|
|
const filePath = file.path;
|
|
|
|
const fileType = file.type;
|
2017-07-08 01:08:35 +02:00
|
|
|
const publishParams = createPublishParams(name, filePath, license, nsfw);
|
2017-06-26 19:02:14 +02:00
|
|
|
// publish the file
|
|
|
|
publishController
|
|
|
|
.publish(publishParams, fileName, fileType)
|
|
|
|
.then(result => {
|
2017-07-13 00:30:31 +02:00
|
|
|
postToStats('publish', originalUrl, ip, null, null, 'success');
|
2017-06-26 19:02:14 +02:00
|
|
|
res.status(200).json(result);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-28 07:41:48 +02:00
|
|
|
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
|
2017-06-26 19:02:14 +02:00
|
|
|
});
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|