spee.ch/server/routes/apiRoutes/otherRoutes.js

233 lines
9.5 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const { details: { host } } = require('../../../config/siteConfig.js');
const { db } = require('../../../config/mysqlConfig.js');
const { claimNameIsAvailable, checkChannelAvailability, publish } = require('../../controllers/publishController.js');
const { getClaimList, resolveUri, getClaim } = require('../../helpers/lbryApi.js');
const { addGetResultsToFileData, createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles, createFileData } = require('../../helpers/publishHelpers.js');
const errorHandlers = require('../../helpers/errorHandlers.js');
const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js');
const { authenticateUser } = require('../../auth/authentication.js');
const { getChannelData, getChannelClaims, getClaimId } = require('../../controllers/serveController.js');
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
const routes = {
2018-02-07 06:55:04 +01:00
// route to check whether site has published to a channel
channelAvailabilityRoute ({ 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-07 06:55:04 +01:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2018-02-07 06:55:04 +01:00
});
},
channelDataRoute ({ ip, originalUrl, body, params }, res) {
2018-02-07 06:55:04 +01:00
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
getChannelData(channelName, channelClaimId, 0)
2018-02-07 06:55:04 +01:00
.then(data => {
if (data === NO_CHANNEL) {
return res.status(404).json({success: false, message: 'No matching channel was found'});
2018-02-07 06:55:04 +01:00
}
res.status(200).json({success: true, data});
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2018-02-07 06:55:04 +01:00
});
},
channelClaimsRoute ({ ip, originalUrl, body, params }, res) {
2018-02-07 06:55:04 +01:00
const channelName = params.channelName;
let channelClaimId = params.channelClaimId;
if (channelClaimId === 'none') channelClaimId = null;
const page = params.page;
getChannelClaims(channelName, channelClaimId, page)
2018-02-07 06:55:04 +01:00
.then(data => {
if (data === NO_CHANNEL) {
return res.status(404).json({success: false, message: 'No matching channel was found'});
2018-02-07 06:55:04 +01:00
}
res.status(200).json({success: true, data});
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2018-02-07 06:55:04 +01:00
});
},
2018-02-07 06:55:04 +01:00
// route to run a claim_list request on the daemon
claimListRoute ({ ip, originalUrl, params }, res) {
2018-02-07 06:55:04 +01:00
getClaimList(params.name)
.then(claimsList => {
res.status(200).json(claimsList);
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
});
},
2017-11-21 00:51:05 +01:00
// route to get an asset
claimGetRoute ({ ip, originalUrl, params }, res) {
2017-11-30 00:36:23 +01:00
const name = params.name;
const claimId = params.claimId;
// 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 => {
// 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');
}
let fileData = createFileData(resolveResult);
// 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
})
.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
})
.then(([ fileRecord, {message, completed} ]) => {
res.status(200).json({ success: true, message, completed });
2017-11-21 00:51:05 +01:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2017-11-21 00:51:05 +01:00
});
},
// route to check whether this site published to a claim
claimAvailabilityRoute ({ ip, originalUrl, params: { name } }, res) {
const gaStartTime = Date.now();
claimNameIsAvailable(name)
2017-09-19 17:47:24 +02:00
.then(result => {
res.status(200).json(result);
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
2017-09-19 17:47:24 +02:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2017-09-19 17:47:24 +02:00
});
},
// route to run a resolve request on the daemon
claimResolveRoute ({ headers, ip, originalUrl, params }, res) {
2018-01-31 00:32:42 +01:00
resolveUri(`${params.name}#${params.claimId}`)
2018-02-07 06:55:04 +01:00
.then(resolvedUri => {
res.status(200).json(resolvedUri);
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2018-02-07 06:55:04 +01:00
});
},
// route to run a publish request on the daemon
claimPublishRoute ({ body, files, headers, ip, originalUrl, user }, res) {
2018-01-23 21:08:53 +01:00
// define variables
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
2018-02-15 20:00:51 +01:00
// record the start time of the request
gaStartTime = Date.now();
// validate the body and files of the request
2017-07-08 01:08:35 +02:00
try {
// validateApiPublishRequest(body, files);
({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body));
2018-03-03 02:24:18 +01:00
({fileName, filePath, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files));
({channelName, channelId, channelPassword} = body);
2017-07-08 01:08:35 +02:00
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// check channel authorization
Promise.all([
authenticateUser(channelName, channelId, channelPassword, user),
2018-03-06 00:38:26 +01:00
claimNameIsAvailable(name),
createBasicPublishParams(filePath, name, title, description, license, nsfw, thumbnail),
2018-03-06 05:30:16 +01:00
createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw),
])
2018-03-06 05:30:16 +01:00
.then(([{channelName, channelClaimId}, validatedClaimName, publishParams, thumbnailPublishParams]) => {
// add channel details to the publish params
if (channelName && channelClaimId) {
publishParams['channel_name'] = channelName;
publishParams['channel_id'] = channelClaimId;
2018-02-07 06:55:04 +01:00
}
2018-03-03 02:24:18 +01:00
// publish the thumbnail
if (thumbnailPublishParams) {
publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType);
}
2018-02-07 06:55:04 +01: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,
url : `${host}/${result.claim_id}/${name}`,
2018-02-07 06:55:04 +01:00
lbryTx : result,
},
});
2018-02-15 20:00:51 +01:00
// record the publish end time and send to google analytics
2018-03-06 18:39:22 +01:00
sendGATimingEvent('end-to-end', 'publish', fileType, gaStartTime, Date.now());
2018-02-07 06:55:04 +01:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
});
},
2017-09-28 20:42:29 +02:00
// route to get a short claim id from long claim Id
claimShortIdRoute ({ ip, originalUrl, body, 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 => {
2018-02-05 02:43:02 +01:00
res.status(200).json({success: true, data: shortId});
2017-09-28 20:42:29 +02:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2017-09-28 20:42:29 +02:00
});
},
claimLongIdRoute ({ 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(404).json({success: false, message: 'No matching channel could be found'});
}
if (result === NO_CLAIM) {
return res.status(404).json({success: false, message: 'No matching claim id could be found'});
2018-01-31 00:32:42 +01:00
}
2018-02-05 02:43:02 +01:00
res.status(200).json({success: true, data: result});
2018-01-31 00:32:42 +01:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2018-01-31 00:32:42 +01:00
});
},
claimDataRoute ({ ip, originalUrl, body, params }, res) {
2018-01-31 00:32:42 +01:00
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(404).json({success: false, message: 'No claim could be found'});
2018-01-31 00:32:42 +01:00
}
res.status(200).json({success: true, data: claimInfo});
2018-01-31 00:32:42 +01:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2018-01-31 00:32:42 +01:00
});
},
2018-02-07 06:55:04 +01:00
// route to see if asset is available locally
fileAvailabilityRoute ({ ip, originalUrl, params }, res) {
2018-02-07 06:55:04 +01:00
const name = params.name;
const claimId = params.claimId;
db.File.findOne({where: {name, claimId}})
.then(result => {
if (result) {
return res.status(200).json({success: true, data: true});
2018-02-07 06:55:04 +01:00
}
res.status(200).json({success: true, data: false});
2018-02-07 06:55:04 +01:00
})
.catch(error => {
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
2018-02-07 06:55:04 +01:00
});
},
};
2018-03-28 01:08:01 +02:00
module.exports = routes;