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-09-28 20:42:29 +02:00
|
|
|
const db = require('../models');
|
2017-08-16 20:00:17 +02:00
|
|
|
const { publish } = require('../controllers/publishController.js');
|
|
|
|
const { getClaimList, resolveUri } = require('../helpers/lbryApi.js');
|
2017-09-19 21:54:23 +02:00
|
|
|
const { createPublishParams, validateFile, checkClaimNameAvailability, checkChannelAvailability } = require('../helpers/publishHelpers.js');
|
2017-08-03 02:13:02 +02:00
|
|
|
const errorHandlers = require('../helpers/errorHandlers.js');
|
2017-06-30 07:26:29 +02:00
|
|
|
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
2017-09-28 19:51:02 +02:00
|
|
|
const { authenticateApiPublish } = require('../auth/authentication.js');
|
2017-05-24 20:07:43 +02:00
|
|
|
|
2017-09-20 23:39:20 +02:00
|
|
|
module.exports = (app) => {
|
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-08-16 20:00:17 +02:00
|
|
|
getClaimList(params.name)
|
|
|
|
.then(claimsList => {
|
|
|
|
postToStats('serve', originalUrl, ip, null, null, 'success');
|
|
|
|
res.status(200).json(claimsList);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
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
|
2017-09-19 21:54:23 +02:00
|
|
|
checkClaimNameAvailability(params.name)
|
2017-08-16 20:00:17 +02:00
|
|
|
.then(result => {
|
|
|
|
if (result === true) {
|
|
|
|
res.status(200).json(true);
|
|
|
|
} else {
|
2017-09-21 02:14:33 +02:00
|
|
|
logger.debug(`Rejecting '${params.name}' because that name has already been claimed on spee.ch`);
|
2017-08-16 20:00:17 +02:00
|
|
|
res.status(200).json(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
res.status(500).json(error);
|
|
|
|
});
|
2017-07-03 23:48:35 +02:00
|
|
|
});
|
2017-09-19 17:47:24 +02:00
|
|
|
// route to check whether spee.ch has published to a channel
|
2017-09-19 21:54:23 +02:00
|
|
|
app.get('/api/isChannelAvailable/:name', ({ params }, res) => {
|
2017-09-19 17:47:24 +02:00
|
|
|
checkChannelAvailability(params.name)
|
|
|
|
.then(result => {
|
|
|
|
if (result === true) {
|
|
|
|
res.status(200).json(true);
|
|
|
|
} else {
|
2017-09-21 02:14:33 +02:00
|
|
|
logger.debug(`Rejecting '${params.name}' because that channel has already been claimed on spee.ch`);
|
2017-09-19 17:47:24 +02:00
|
|
|
res.status(200).json(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-09-19 21:54:23 +02:00
|
|
|
logger.debug('api/isChannelAvailable/ error', error);
|
2017-09-19 17:47:24 +02:00
|
|
|
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-08-16 20:00:17 +02:00
|
|
|
resolveUri(params.uri)
|
|
|
|
.then(resolvedUri => {
|
|
|
|
postToStats('serve', originalUrl, ip, null, null, 'success');
|
|
|
|
res.status(200).json(resolvedUri);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
errorHandlers.handleRequestError('publish', originalUrl, ip, error, res);
|
|
|
|
});
|
2017-06-19 18:37: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-09-28 19:51:02 +02:00
|
|
|
const title = body.title || null;
|
|
|
|
const description = body.description || null;
|
2017-06-26 19:43:35 +02:00
|
|
|
const license = body.license || 'No License Provided';
|
2017-09-28 19:51:02 +02:00
|
|
|
const nsfw = body.nsfw || null;
|
|
|
|
const channelName = body.channelName || 'none';
|
|
|
|
const channelPassword = body.channelPassword || null;
|
|
|
|
logger.debug(`name: ${name}, license: ${license}, nsfw: ${nsfw}`);
|
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-06-26 19:43:35 +02:00
|
|
|
const fileName = file.name;
|
|
|
|
const filePath = file.path;
|
|
|
|
const fileType = file.type;
|
2017-09-28 19:51:02 +02:00
|
|
|
// channel authorization
|
|
|
|
authenticateApiPublish(channelName, channelPassword)
|
|
|
|
.then(result => {
|
|
|
|
if (!result) {
|
|
|
|
res.status(401).send('Authentication failed, you do not have access to that channel');
|
|
|
|
throw new Error('authentication failed');
|
|
|
|
}
|
|
|
|
return createPublishParams(name, filePath, title, description, license, nsfw, channelName);
|
|
|
|
})
|
2017-09-28 20:42:29 +02:00
|
|
|
// create publish parameters object
|
2017-09-28 19:51:02 +02:00
|
|
|
.then(publishParams => {
|
|
|
|
return publish(publishParams, fileName, fileType);
|
|
|
|
})
|
2017-09-28 20:42:29 +02:00
|
|
|
// publish the asset
|
2017-08-16 20:00:17 +02:00
|
|
|
.then(result => {
|
|
|
|
postToStats('publish', originalUrl, ip, null, null, 'success');
|
|
|
|
res.status(200).json(result);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-09-28 19:51:02 +02:00
|
|
|
logger.error('publish api error', error);
|
2017-08-16 20:00:17 +02:00
|
|
|
});
|
2017-06-26 19:02:14 +02:00
|
|
|
});
|
2017-09-28 20:42:29 +02:00
|
|
|
// route to get a short claim id from long claim Id
|
|
|
|
app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => {
|
|
|
|
// serve content
|
|
|
|
db.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
|
|
|
.then(shortId => {
|
|
|
|
res.status(200).json(shortId);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('api error getting short channel id', error);
|
|
|
|
res.status(400).json(error.message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
// route to get a short channel id from long channel Id
|
|
|
|
app.get('/api/shortChannelId/:longId/:name', ({ params }, res) => {
|
|
|
|
// serve content
|
|
|
|
db.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
|
|
|
.then(shortId => {
|
2017-09-29 00:47:55 +02:00
|
|
|
console.log('sending back short channel id', shortId);
|
2017-09-28 20:42:29 +02:00
|
|
|
res.status(200).json(shortId);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('api error getting short channel id', error);
|
|
|
|
res.status(400).json(error.message);
|
|
|
|
});
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|