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');
|
2017-12-14 21:32:20 +01:00
|
|
|
const { files, site } = require('../config/speechConfig.js');
|
|
|
|
const multipartMiddleware = multipart({uploadDir: files.uploadDirectory});
|
2017-09-28 20:42:29 +02:00
|
|
|
const db = require('../models');
|
2017-12-09 02:50:47 +01:00
|
|
|
const { checkClaimNameAvailability, checkChannelAvailability, publish } = require('../controllers/publishController.js');
|
2017-11-21 00:51:05 +01:00
|
|
|
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
|
2018-01-23 21:08:53 +01:00
|
|
|
const { createPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, parsePublishApiChannel, addGetResultsToFileData, createFileData, returnPublishTimingActionType } = require('../helpers/publishHelpers.js');
|
2017-08-03 02:13:02 +02:00
|
|
|
const errorHandlers = require('../helpers/errorHandlers.js');
|
2018-01-23 01:14:05 +01:00
|
|
|
const { sendGoogleAnalyticsTiming } = require('../helpers/statsHelpers.js');
|
2017-12-15 19:26:51 +01:00
|
|
|
const { authenticateIfNoUserToken } = require('../auth/authentication.js');
|
2018-02-01 01:00:11 +01:00
|
|
|
const { getChannelData, getChannelClaims, getClaimId } = require('../controllers/serveController.js');
|
2018-01-30 20:46:22 +01:00
|
|
|
|
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2018-01-31 02:15:23 +01:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
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-11-29 21:41:53 +01:00
|
|
|
app.get('/api/claim-list/:name', ({ ip, originalUrl, params }, res) => {
|
2017-08-16 20:00:17 +02:00
|
|
|
getClaimList(params.name)
|
|
|
|
.then(claimsList => {
|
|
|
|
res.status(200).json(claimsList);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-12-07 03:49:05 +01:00
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
2017-08-16 20:00:17 +02:00
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-11-22 00:44:27 +01:00
|
|
|
// route to see if asset is available locally
|
2017-12-05 19:18:49 +01:00
|
|
|
app.get('/api/file-is-available/:name/:claimId', ({ ip, originalUrl, params }, res) => {
|
2017-11-22 00:44:27 +01:00
|
|
|
const name = params.name;
|
|
|
|
const claimId = params.claimId;
|
|
|
|
let isLocalFileAvailable = false;
|
|
|
|
db.File.findOne({where: {name, claimId}})
|
|
|
|
.then(result => {
|
|
|
|
if (result) {
|
|
|
|
isLocalFileAvailable = true;
|
|
|
|
}
|
2017-11-28 01:53:32 +01:00
|
|
|
res.status(200).json({status: 'success', message: isLocalFileAvailable});
|
2017-11-22 00:44:27 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-12-07 03:49:05 +01:00
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
2017-11-22 00:44:27 +01:00
|
|
|
});
|
|
|
|
});
|
2017-11-21 00:51:05 +01:00
|
|
|
// route to get an asset
|
2017-12-05 19:18:49 +01:00
|
|
|
app.get('/api/claim-get/:name/:claimId', ({ ip, originalUrl, params }, res) => {
|
2017-11-30 00:36:23 +01:00
|
|
|
const name = params.name;
|
|
|
|
const claimId = params.claimId;
|
2017-11-28 21:17:22 +01:00
|
|
|
// resolve the claim
|
2017-11-30 00:36:23 +01:00
|
|
|
db.Claim.resolveClaim(name, claimId)
|
2017-11-21 21:53:43 +01:00
|
|
|
.then(resolveResult => {
|
2017-11-28 21:17:22 +01:00
|
|
|
// make sure a claim actually exists at that uri
|
2017-11-21 21:53:43 +01:00
|
|
|
if (!resolveResult) {
|
|
|
|
throw new Error('No matching uri found in Claim table');
|
|
|
|
}
|
2017-11-28 21:17:22 +01:00
|
|
|
let fileData = createFileData(resolveResult);
|
2017-11-22 00:44:27 +01:00
|
|
|
// get the claim
|
2017-11-30 00:36:23 +01:00
|
|
|
return Promise.all([fileData, getClaim(`${name}#${claimId}`)]);
|
2017-11-21 21:53:43 +01:00
|
|
|
})
|
2017-11-28 21:17:22 +01:00
|
|
|
.then(([ fileData, getResult ]) => {
|
|
|
|
fileData = addGetResultsToFileData(fileData, getResult);
|
2017-11-30 00:36:23 +01:00
|
|
|
return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]);
|
2017-11-21 21:53:43 +01:00
|
|
|
})
|
2017-11-28 21:17:22 +01:00
|
|
|
.then(([ fileRecord, {message, completed} ]) => {
|
|
|
|
res.status(200).json({ status: 'success', message, completed });
|
2017-11-21 00:51:05 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-12-07 03:49:05 +01:00
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
2017-11-21 00:51:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-14 21:32:20 +01:00
|
|
|
// route to check whether this site published to a claim
|
2017-12-05 19:18:49 +01:00
|
|
|
app.get('/api/claim-is-available/:name', ({ params }, res) => {
|
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 {
|
|
|
|
res.status(200).json(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
res.status(500).json(error);
|
|
|
|
});
|
2017-07-03 23:48:35 +02:00
|
|
|
});
|
2017-12-14 21:32:20 +01:00
|
|
|
// route to check whether site has published to a channel
|
2017-12-05 19:18:49 +01:00
|
|
|
app.get('/api/channel-is-available/: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 {
|
|
|
|
res.status(200).json(false);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
res.status(500).json(error);
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
// route to run a resolve request on the daemon
|
2018-01-31 00:32:42 +01:00
|
|
|
app.get('/api/claim-resolve/:name/:claimId', ({ headers, ip, originalUrl, params }, res) => {
|
|
|
|
resolveUri(`${params.name}#${params.claimId}`)
|
2017-08-16 20:00:17 +02:00
|
|
|
.then(resolvedUri => {
|
|
|
|
res.status(200).json(resolvedUri);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-12-07 03:49:05 +01:00
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
2017-08-16 20:00:17 +02:00
|
|
|
});
|
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
|
2018-01-23 01:14:05 +01:00
|
|
|
app.post('/api/claim-publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl, user }, res) => {
|
2017-12-15 20:02:04 +01:00
|
|
|
logger.debug('api/claim-publish body:', body);
|
2017-12-15 23:10:34 +01:00
|
|
|
logger.debug('api/claim-publish files:', files);
|
2018-01-23 21:08:53 +01:00
|
|
|
// record the start time of the request and create variable for storing the action type
|
2018-01-23 05:09:28 +01:00
|
|
|
const publishStartTime = Date.now();
|
|
|
|
logger.debug('publish request started @', publishStartTime);
|
2018-01-23 21:08:53 +01:00
|
|
|
let timingActionType;
|
|
|
|
// define variables
|
2017-12-15 19:26:51 +01:00
|
|
|
let name, fileName, filePath, fileType, nsfw, license, title, description, thumbnail, channelName, channelPassword;
|
2017-12-09 02:50:47 +01:00
|
|
|
// validate the body and files of the request
|
2017-07-08 01:08:35 +02:00
|
|
|
try {
|
2017-12-09 02:50:47 +01:00
|
|
|
// validateApiPublishRequest(body, files);
|
|
|
|
({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body));
|
|
|
|
({fileName, filePath, fileType} = parsePublishApiRequestFiles(files));
|
2017-12-15 19:26:51 +01:00
|
|
|
({channelName, channelPassword} = parsePublishApiChannel(body, user));
|
2017-07-08 01:08:35 +02:00
|
|
|
} catch (error) {
|
2017-12-15 23:10:34 +01:00
|
|
|
logger.debug('publish request rejected, insufficient request parameters', error);
|
2017-12-09 02:50:47 +01:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2017-06-27 04:26:37 +02:00
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
// check channel authorization
|
2017-12-15 19:26:51 +01:00
|
|
|
authenticateIfNoUserToken(channelName, channelPassword, user)
|
2017-11-13 19:21:39 +01:00
|
|
|
.then(authenticated => {
|
|
|
|
if (!authenticated) {
|
2017-10-05 23:48:08 +02:00
|
|
|
throw new Error('Authentication failed, you do not have access to that channel');
|
|
|
|
}
|
|
|
|
// make sure the claim name is available
|
|
|
|
return checkClaimNameAvailability(name);
|
|
|
|
})
|
2017-09-28 19:51:02 +02:00
|
|
|
.then(result => {
|
|
|
|
if (!result) {
|
2017-12-14 21:32:20 +01:00
|
|
|
throw new Error('That name is already claimed by another user.');
|
2017-09-28 19:51:02 +02:00
|
|
|
}
|
2017-10-05 23:48:08 +02:00
|
|
|
// create publish parameters object
|
2017-10-10 03:29:40 +02:00
|
|
|
return createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName);
|
2017-09-28 19:51:02 +02:00
|
|
|
})
|
|
|
|
.then(publishParams => {
|
2018-01-23 21:08:53 +01:00
|
|
|
// set the timing event type for reporting
|
2018-01-24 00:05:43 +01:00
|
|
|
timingActionType = returnPublishTimingActionType(publishParams.channel_name);
|
2017-10-05 23:48:08 +02:00
|
|
|
// publish the asset
|
2017-09-28 19:51:02 +02:00
|
|
|
return publish(publishParams, fileName, fileType);
|
|
|
|
})
|
2017-08-16 20:00:17 +02:00
|
|
|
.then(result => {
|
2017-10-05 23:48:08 +02:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
message: {
|
2017-11-02 23:12:26 +01:00
|
|
|
name,
|
2017-12-14 21:32:20 +01:00
|
|
|
url : `${site.host}/${result.claim_id}/${name}`,
|
2017-10-05 23:48:08 +02:00
|
|
|
lbryTx: result,
|
|
|
|
},
|
|
|
|
});
|
2018-01-23 21:08:53 +01:00
|
|
|
// log the publish end time
|
2018-01-23 05:09:28 +01:00
|
|
|
const publishEndTime = Date.now();
|
|
|
|
logger.debug('publish request completed @', publishEndTime);
|
2018-01-23 21:08:53 +01:00
|
|
|
sendGoogleAnalyticsTiming(timingActionType, headers, ip, originalUrl, publishStartTime, publishEndTime);
|
2017-08-16 20:00:17 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-12-07 03:49:05 +01:00
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
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
|
2017-12-05 19:18:49 +01:00
|
|
|
app.get('/api/claim-shorten-id/:longId/:name', ({ params }, res) => {
|
2017-11-04 01:10:08 +01:00
|
|
|
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
2017-09-28 20:42:29 +02:00
|
|
|
.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
|
2017-12-05 19:18:49 +01:00
|
|
|
app.get('/api/channel-shorten-id/:longId/:name', ({ ip, originalUrl, params }, res) => {
|
2017-10-31 18:05:15 +01:00
|
|
|
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
2017-09-28 20:42:29 +02:00
|
|
|
.then(shortId => {
|
2017-10-30 05:01:45 +01:00
|
|
|
logger.debug('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);
|
2017-12-07 03:49:05 +01:00
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
2017-09-28 20:42:29 +02:00
|
|
|
});
|
|
|
|
});
|
2018-02-01 01:00:11 +01:00
|
|
|
app.get('/api/channel-data/:channelName/:channelClaimId', ({ ip, originalUrl, body, params }, res) => {
|
|
|
|
const channelName = params.channelName;
|
|
|
|
let channelClaimId = params.channelClaimId;
|
|
|
|
if (channelClaimId === 'none') channelClaimId = null;
|
|
|
|
getChannelData(channelName, channelClaimId, 0) // getChannelViewData(channelName, channelId, 0)
|
|
|
|
.then(data => {
|
|
|
|
if (data === NO_CHANNEL) {
|
|
|
|
return res.status(200).json({success: false, message: 'No matching channel was found'});
|
|
|
|
}
|
|
|
|
res.status(200).json({success: true, data});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('api error getting channel contents', error);
|
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
app.get('/api/channel-claims/:channelName/:channelClaimId/:page', ({ ip, originalUrl, body, params }, res) => {
|
|
|
|
const channelName = params.channelName;
|
|
|
|
let channelClaimId = params.channelClaimId;
|
2018-01-30 20:46:22 +01:00
|
|
|
if (channelClaimId === 'none') channelClaimId = null;
|
|
|
|
const page = params.page;
|
2018-02-01 01:00:11 +01:00
|
|
|
getChannelClaims(channelName, channelClaimId, page)// getChannelViewData(channelName, channelClaimId, page)
|
2018-01-30 20:46:22 +01:00
|
|
|
.then(data => {
|
|
|
|
if (data === NO_CHANNEL) {
|
|
|
|
return res.status(200).json({success: false, message: 'No matching channel was found'});
|
|
|
|
}
|
|
|
|
res.status(200).json({success: true, data});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('api error getting channel contents', error);
|
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
});
|
2018-01-31 02:15:23 +01:00
|
|
|
app.post('/api/claim-get-long-id', ({ ip, originalUrl, body, params }, res) => {
|
|
|
|
logger.debug('body:', body);
|
|
|
|
const channelName = body.channelName;
|
|
|
|
const channelClaimId = body.channelClaimId;
|
|
|
|
const claimName = body.claimName;
|
|
|
|
const claimId = body.claimId;
|
|
|
|
getClaimId(channelName, channelClaimId, claimName, claimId)
|
|
|
|
.then(result => {
|
|
|
|
if (result === NO_CHANNEL) {
|
|
|
|
return res.status(200).json({success: false, message: 'No matching channel could be found'});
|
|
|
|
}
|
|
|
|
if (result === NO_CLAIM) {
|
2018-01-31 00:32:42 +01:00
|
|
|
return res.status(200).json({success: false, message: 'No matching claim id could be found'});
|
|
|
|
}
|
2018-01-31 02:15:23 +01:00
|
|
|
res.status(200).json({success: true, message: result});
|
2018-01-31 00:32:42 +01:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('api error getting long claim id', error);
|
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
app.get('/api/claim-get-data/:claimName/:claimId', ({ ip, originalUrl, body, params }, res) => {
|
|
|
|
const claimName = params.claimName;
|
|
|
|
let claimId = params.claimId;
|
|
|
|
if (claimId === 'none') claimId = null;
|
|
|
|
db.Claim.resolveClaim(claimName, claimId)
|
|
|
|
.then(claimInfo => {
|
|
|
|
if (!claimInfo) {
|
|
|
|
return res.status(200).json({success: false, message: 'No claim could be found'});
|
|
|
|
}
|
|
|
|
res.status(200).json({success: true, message: claimInfo});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('api error getting long claim id', error);
|
|
|
|
errorHandlers.handleApiError(originalUrl, ip, error, res);
|
|
|
|
});
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|