2017-06-19 13:10:06 -07:00
|
|
|
const logger = require('winston');
|
2017-06-26 10:02:14 -07:00
|
|
|
const multipart = require('connect-multiparty');
|
2018-03-16 10:34:26 -07:00
|
|
|
const { publishing: { uploadDirectory }, details: { host } } = require('../../config/siteConfig.js');
|
2018-03-09 18:23:19 -08:00
|
|
|
const multipartMiddleware = multipart({uploadDir: uploadDirectory});
|
2018-03-16 10:34:26 -07:00
|
|
|
const db = require('../models/index');
|
2018-03-05 15:38:26 -08:00
|
|
|
const { claimNameIsAvailable, checkChannelAvailability, publish } = require('../controllers/publishController.js');
|
2017-11-20 15:51:05 -08:00
|
|
|
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
|
2018-03-05 20:30:16 -08:00
|
|
|
const { addGetResultsToFileData, createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, createFileData } = require('../helpers/publishHelpers.js');
|
2017-08-02 17:13:02 -07:00
|
|
|
const errorHandlers = require('../helpers/errorHandlers.js');
|
2018-03-06 09:02:42 -08:00
|
|
|
const { sendGATimingEvent } = require('../helpers/googleAnalytics.js');
|
2018-03-05 14:44:37 -08:00
|
|
|
const { authenticateUser } = require('../auth/authentication.js');
|
2018-01-31 16:00:11 -08:00
|
|
|
const { getChannelData, getChannelClaims, getClaimId } = require('../controllers/serveController.js');
|
2018-01-30 11:46:22 -08:00
|
|
|
|
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2018-01-30 17:15:23 -08:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
2017-05-24 11:07:43 -07:00
|
|
|
|
2017-09-20 14:39:20 -07:00
|
|
|
module.exports = (app) => {
|
2018-02-06 21:55:04 -08:00
|
|
|
// route to check whether site has published to a channel
|
2018-03-07 19:39:22 -08:00
|
|
|
app.get('/api/channel/availability/:name', ({ ip, originalUrl, params: { name } }, res) => {
|
|
|
|
const gaStartTime = Date.now();
|
|
|
|
checkChannelAvailability(name)
|
|
|
|
.then(availableName => {
|
|
|
|
res.status(200).json(availableName);
|
|
|
|
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
|
2018-02-06 21:55:04 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-02-06 21:55:04 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// route to get a short channel id from long channel Id
|
|
|
|
app.get('/api/channel/short-id/:longId/:name', ({ ip, originalUrl, params }, res) => {
|
|
|
|
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
|
|
|
.then(shortId => {
|
|
|
|
res.status(200).json(shortId);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-02-06 21:55:04 -08: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;
|
2018-02-13 12:19:57 -08:00
|
|
|
getChannelData(channelName, channelClaimId, 0)
|
2018-02-06 21:55:04 -08:00
|
|
|
.then(data => {
|
|
|
|
if (data === NO_CHANNEL) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(404).json({success: false, message: 'No matching channel was found'});
|
2018-02-06 21:55:04 -08:00
|
|
|
}
|
|
|
|
res.status(200).json({success: true, data});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-02-06 21:55:04 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
app.get('/api/channel/claims/:channelName/:channelClaimId/:page', ({ ip, originalUrl, body, params }, res) => {
|
|
|
|
const channelName = params.channelName;
|
|
|
|
let channelClaimId = params.channelClaimId;
|
|
|
|
if (channelClaimId === 'none') channelClaimId = null;
|
|
|
|
const page = params.page;
|
2018-02-13 12:19:57 -08:00
|
|
|
getChannelClaims(channelName, channelClaimId, page)
|
2018-02-06 21:55:04 -08:00
|
|
|
.then(data => {
|
|
|
|
if (data === NO_CHANNEL) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(404).json({success: false, message: 'No matching channel was found'});
|
2018-02-06 21:55:04 -08:00
|
|
|
}
|
|
|
|
res.status(200).json({success: true, data});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-02-06 21:55:04 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
// route to run a claim_list request on the daemon
|
|
|
|
app.get('/api/claim/list/:name', ({ ip, originalUrl, params }, res) => {
|
|
|
|
getClaimList(params.name)
|
|
|
|
.then(claimsList => {
|
|
|
|
res.status(200).json(claimsList);
|
2017-11-21 15:44:27 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2017-11-21 15:44:27 -08:00
|
|
|
});
|
|
|
|
});
|
2017-11-20 15:51:05 -08:00
|
|
|
// route to get an asset
|
2018-02-06 21:55:04 -08:00
|
|
|
app.get('/api/claim/get/:name/:claimId', ({ ip, originalUrl, params }, res) => {
|
2017-11-29 15:36:23 -08:00
|
|
|
const name = params.name;
|
|
|
|
const claimId = params.claimId;
|
2017-11-28 12:17:22 -08:00
|
|
|
// resolve the claim
|
2017-11-29 15:36:23 -08:00
|
|
|
db.Claim.resolveClaim(name, claimId)
|
2017-11-21 12:53:43 -08:00
|
|
|
.then(resolveResult => {
|
2017-11-28 12:17:22 -08:00
|
|
|
// make sure a claim actually exists at that uri
|
2017-11-21 12:53:43 -08:00
|
|
|
if (!resolveResult) {
|
|
|
|
throw new Error('No matching uri found in Claim table');
|
|
|
|
}
|
2017-11-28 12:17:22 -08:00
|
|
|
let fileData = createFileData(resolveResult);
|
2017-11-21 15:44:27 -08:00
|
|
|
// get the claim
|
2017-11-29 15:36:23 -08:00
|
|
|
return Promise.all([fileData, getClaim(`${name}#${claimId}`)]);
|
2017-11-21 12:53:43 -08:00
|
|
|
})
|
2017-11-28 12:17:22 -08:00
|
|
|
.then(([ fileData, getResult ]) => {
|
|
|
|
fileData = addGetResultsToFileData(fileData, getResult);
|
2017-11-29 15:36:23 -08:00
|
|
|
return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]);
|
2017-11-21 12:53:43 -08:00
|
|
|
})
|
2017-11-28 12:17:22 -08:00
|
|
|
.then(([ fileRecord, {message, completed} ]) => {
|
2018-02-02 12:46:18 -08:00
|
|
|
res.status(200).json({ success: true, message, completed });
|
2017-11-20 15:51:05 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2017-11-20 15:51:05 -08:00
|
|
|
});
|
|
|
|
});
|
2017-12-14 12:32:20 -08:00
|
|
|
// route to check whether this site published to a claim
|
2018-03-07 18:41:58 -08:00
|
|
|
app.get('/api/claim/availability/:name', ({ ip, originalUrl, params: { name } }, res) => {
|
|
|
|
const gaStartTime = Date.now();
|
|
|
|
claimNameIsAvailable(name)
|
2017-09-19 08:47:24 -07:00
|
|
|
.then(result => {
|
2018-03-05 14:44:37 -08:00
|
|
|
res.status(200).json(result);
|
2018-03-07 18:41:58 -08:00
|
|
|
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
|
2017-09-19 08:47:24 -07:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2017-09-19 08:47:24 -07:00
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
// route to run a resolve request on the daemon
|
2018-02-06 21:55:04 -08:00
|
|
|
app.get('/api/claim/resolve/:name/:claimId', ({ headers, ip, originalUrl, params }, res) => {
|
2018-01-30 15:32:42 -08:00
|
|
|
resolveUri(`${params.name}#${params.claimId}`)
|
2018-02-06 21:55:04 -08:00
|
|
|
.then(resolvedUri => {
|
|
|
|
res.status(200).json(resolvedUri);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-02-06 21:55:04 -08:00
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-26 10:02:14 -07:00
|
|
|
// route to run a publish request on the daemon
|
2018-02-06 21:55:04 -08:00
|
|
|
app.post('/api/claim/publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl, user }, res) => {
|
2018-01-23 12:08:53 -08:00
|
|
|
// define variables
|
2018-03-06 09:02:42 -08:00
|
|
|
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
|
2018-02-15 11:00:51 -08:00
|
|
|
// record the start time of the request
|
2018-03-06 09:02:42 -08:00
|
|
|
gaStartTime = Date.now();
|
2017-12-08 17:50:47 -08:00
|
|
|
// validate the body and files of the request
|
2017-07-07 16:08:35 -07:00
|
|
|
try {
|
2017-12-08 17:50:47 -08:00
|
|
|
// validateApiPublishRequest(body, files);
|
|
|
|
({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body));
|
2018-03-02 17:24:18 -08:00
|
|
|
({fileName, filePath, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files));
|
2018-03-05 14:44:37 -08:00
|
|
|
({channelName, channelId, channelPassword} = body);
|
2017-07-07 16:08:35 -07:00
|
|
|
} catch (error) {
|
2017-12-08 17:50:47 -08:00
|
|
|
return res.status(400).json({success: false, message: error.message});
|
2017-06-26 19:26:37 -07:00
|
|
|
}
|
2017-10-05 14:48:08 -07:00
|
|
|
// check channel authorization
|
2018-03-05 14:44:37 -08:00
|
|
|
Promise.all([
|
|
|
|
authenticateUser(channelName, channelId, channelPassword, user),
|
2018-03-05 15:38:26 -08:00
|
|
|
claimNameIsAvailable(name),
|
|
|
|
createBasicPublishParams(filePath, name, title, description, license, nsfw, thumbnail),
|
2018-03-05 20:30:16 -08:00
|
|
|
createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw),
|
2018-03-05 14:44:37 -08:00
|
|
|
])
|
2018-03-05 20:30:16 -08:00
|
|
|
.then(([{channelName, channelClaimId}, validatedClaimName, publishParams, thumbnailPublishParams]) => {
|
2018-03-05 14:44:37 -08:00
|
|
|
// add channel details to the publish params
|
|
|
|
if (channelName && channelClaimId) {
|
|
|
|
publishParams['channel_name'] = channelName;
|
|
|
|
publishParams['channel_id'] = channelClaimId;
|
2018-02-06 21:55:04 -08:00
|
|
|
}
|
2018-03-02 17:24:18 -08:00
|
|
|
// publish the thumbnail
|
|
|
|
if (thumbnailPublishParams) {
|
|
|
|
publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType);
|
|
|
|
}
|
2018-02-06 21:55:04 -08:00
|
|
|
// publish the asset
|
|
|
|
return publish(publishParams, fileName, fileType);
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
message: 'publish completed successfully',
|
|
|
|
data : {
|
|
|
|
name,
|
|
|
|
claimId: result.claim_id,
|
2018-03-09 18:23:19 -08:00
|
|
|
url : `${host}/${result.claim_id}/${name}`,
|
2018-02-06 21:55:04 -08:00
|
|
|
lbryTx : result,
|
|
|
|
},
|
|
|
|
});
|
2018-02-15 11:00:51 -08:00
|
|
|
// record the publish end time and send to google analytics
|
2018-03-06 09:39:22 -08:00
|
|
|
sendGATimingEvent('end-to-end', 'publish', fileType, gaStartTime, Date.now());
|
2018-02-06 21:55:04 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2017-10-05 14:48:08 -07:00
|
|
|
});
|
2017-06-26 10:02:14 -07:00
|
|
|
});
|
2017-09-28 11:42:29 -07:00
|
|
|
// route to get a short claim id from long claim Id
|
2018-02-13 12:19:57 -08:00
|
|
|
app.get('/api/claim/short-id/:longId/:name', ({ ip, originalUrl, body, params }, res) => {
|
2017-11-03 17:10:08 -07:00
|
|
|
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
2017-09-28 11:42:29 -07:00
|
|
|
.then(shortId => {
|
2018-02-04 17:43:02 -08:00
|
|
|
res.status(200).json({success: true, data: shortId});
|
2017-09-28 11:42:29 -07:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2017-09-28 11:42:29 -07:00
|
|
|
});
|
|
|
|
});
|
2018-02-07 18:01:51 -08:00
|
|
|
app.post('/api/claim/long-id', ({ ip, originalUrl, body, params }, res) => {
|
2018-01-30 17:15:23 -08:00
|
|
|
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) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(404).json({success: false, message: 'No matching channel could be found'});
|
2018-01-30 17:15:23 -08:00
|
|
|
}
|
|
|
|
if (result === NO_CLAIM) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(404).json({success: false, message: 'No matching claim id could be found'});
|
2018-01-30 15:32:42 -08:00
|
|
|
}
|
2018-02-04 17:43:02 -08:00
|
|
|
res.status(200).json({success: true, data: result});
|
2018-01-30 15:32:42 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-01-30 15:32:42 -08:00
|
|
|
});
|
|
|
|
});
|
2018-02-06 21:55:04 -08:00
|
|
|
app.get('/api/claim/data/:claimName/:claimId', ({ ip, originalUrl, body, params }, res) => {
|
2018-01-30 15:32:42 -08:00
|
|
|
const claimName = params.claimName;
|
|
|
|
let claimId = params.claimId;
|
|
|
|
if (claimId === 'none') claimId = null;
|
|
|
|
db.Claim.resolveClaim(claimName, claimId)
|
|
|
|
.then(claimInfo => {
|
|
|
|
if (!claimInfo) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(404).json({success: false, message: 'No claim could be found'});
|
2018-01-30 15:32:42 -08:00
|
|
|
}
|
2018-02-07 18:01:51 -08:00
|
|
|
res.status(200).json({success: true, data: claimInfo});
|
2018-01-30 15:32:42 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-01-30 15:32:42 -08:00
|
|
|
});
|
|
|
|
});
|
2018-02-06 21:55:04 -08:00
|
|
|
// route to see if asset is available locally
|
|
|
|
app.get('/api/file/availability/:name/:claimId', ({ ip, originalUrl, params }, res) => {
|
|
|
|
const name = params.name;
|
|
|
|
const claimId = params.claimId;
|
|
|
|
db.File.findOne({where: {name, claimId}})
|
|
|
|
.then(result => {
|
|
|
|
if (result) {
|
2018-02-13 12:19:57 -08:00
|
|
|
return res.status(200).json({success: true, data: true});
|
2018-02-06 21:55:04 -08:00
|
|
|
}
|
2018-02-13 12:19:57 -08:00
|
|
|
res.status(200).json({success: true, data: false});
|
2018-02-06 21:55:04 -08:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-13 12:19:57 -08:00
|
|
|
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
2018-02-06 21:55:04 -08:00
|
|
|
});
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|