spee.ch/server/controllers/api/claim/publish/index.js

116 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-06-28 03:10:47 +02:00
const logger = require('winston');
const { details: { host }, publishing: { disabled, disabledMessage } } = require('@config/siteConfig');
2018-04-27 18:54:36 +02:00
const { sendGATimingEvent } = require('../../../../utils/googleAnalytics.js');
const isApprovedChannel = require('../../../../../utils/isApprovedChannel');
const { publishing: { publishOnlyApproved, approvedChannels } } = require('@config/siteConfig');
2018-04-27 18:54:36 +02:00
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
const checkClaimAvailability = require('../availability/checkClaimAvailability.js');
2018-04-27 18:54:36 +02:00
const publish = require('./publish.js');
2018-07-27 20:05:10 +02:00
const createPublishParams = require('./createPublishParams.js');
2018-04-27 18:54:36 +02:00
const createThumbnailPublishParams = require('./createThumbnailPublishParams.js');
const parsePublishApiRequestBody = require('./parsePublishApiRequestBody.js');
const parsePublishApiRequestFiles = require('./parsePublishApiRequestFiles.js');
const authenticateUser = require('./authentication.js');
2018-03-29 02:35:41 +02:00
2018-07-27 20:05:10 +02:00
const CLAIM_TAKEN = 'CLAIM_TAKEN';
const UNAPPROVED_CHANNEL = 'UNAPPROVED_CHANNEL';
2018-07-27 20:05:10 +02:00
2018-03-29 02:35:41 +02:00
/*
route to publish a claim through the daemon
*/
2018-06-28 03:10:47 +02:00
const claimPublish = ({ body, files, headers, ip, originalUrl, user, tor }, res) => {
2018-06-28 21:30:32 +02:00
// logging
logger.info('Publish request:', {
ip,
headers,
2018-06-28 22:22:52 +02:00
body,
files,
2018-06-28 21:30:32 +02:00
});
// check for disabled publishing
if (disabled) {
return res.status(503).json({
success: false,
message: disabledMessage,
});
}
2018-03-29 20:24:52 +02:00
// define variables
let channelName, channelId, channelPassword, description, fileName, filePath, fileExtension, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
2018-03-29 20:24:52 +02:00
// record the start time of the request
gaStartTime = Date.now();
// validate the body and files of the request
try {
// validateApiPublishRequest(body, files);
({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body));
({fileName, filePath, fileExtension, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files));
2018-03-29 20:24:52 +02:00
({channelName, channelId, channelPassword} = body);
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
// check channel authorization
2018-07-27 20:05:10 +02:00
authenticateUser(channelName, channelId, channelPassword, user)
.then(({ channelName, channelClaimId }) => {
if (publishOnlyApproved && !isApprovedChannel({ longId: channelClaimId }, approvedChannels)) {
const error = {
name : UNAPPROVED_CHANNEL,
message: 'This spee.ch instance only allows publishing to approved channels',
};
throw error;
}
2018-07-27 20:05:10 +02:00
return Promise.all([
checkClaimAvailability(name),
createPublishParams(filePath, name, title, description, license, nsfw, thumbnail, channelName, channelClaimId),
createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw),
]);
2018-07-27 20:05:10 +02:00
})
.then(([ claimAvailable, publishParams, thumbnailPublishParams ]) => {
if (!claimAvailable) {
const error = {
name : CLAIM_TAKEN,
message: 'That claim name is already taken',
2018-07-27 20:05:10 +02:00
};
throw error;
2018-03-29 20:24:52 +02:00
}
// publish the thumbnail, if one exists
2018-03-29 20:24:52 +02:00
if (thumbnailPublishParams) {
publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType);
}
// publish the asset
return publish(publishParams, fileName, fileType);
})
.then(result => {
res.status(200).json({
success: true,
message: 'publish completed successfully',
data : {
name,
2018-07-16 23:57:26 +02:00
claimId : result.claim_id,
url : `${host}/${result.claim_id}/${name}`, // for backwards compatability with app
showUrl : `${host}/${result.claim_id}/${name}`,
serveUrl: `${host}/${result.claim_id}/${name}${fileExtension}`,
2018-07-16 23:57:26 +02:00
lbryTx : result,
2018-03-29 20:24:52 +02:00
},
2018-03-29 02:35:41 +02:00
});
2018-03-29 20:24:52 +02:00
// record the publish end time and send to google analytics
sendGATimingEvent('end-to-end', 'publish', fileType, gaStartTime, Date.now());
})
.catch(error => {
2018-09-24 15:26:16 +02:00
if ([CLAIM_TAKEN, UNAPPROVED_CHANNEL].includes(error.name)) {
res.status(400).json({
success: false,
message: error.message,
});
}
2018-03-29 20:24:52 +02:00
handleErrorResponse(originalUrl, ip, error, res);
});
2018-03-29 02:35:41 +02:00
};
module.exports = claimPublish;