2018-06-28 03:10:47 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
|
2019-02-23 06:52:31 +01:00
|
|
|
const {
|
|
|
|
details: { host },
|
|
|
|
publishing: { disabled, disabledMessage },
|
|
|
|
} = require('@config/siteConfig');
|
2018-04-27 23:29:00 +02:00
|
|
|
|
2019-02-24 07:23:56 +01:00
|
|
|
const { sendGATimingEvent } = require('server/utils/googleAnalytics.js');
|
2019-01-14 07:27:23 +01:00
|
|
|
const isApprovedChannel = require('@globalutils/isApprovedChannel');
|
2019-02-23 06:52:31 +01:00
|
|
|
const {
|
|
|
|
publishing: { publishOnlyApproved, approvedChannels },
|
|
|
|
} = require('@config/siteConfig');
|
2018-04-27 23:29:00 +02:00
|
|
|
|
2018-04-27 18:54:36 +02:00
|
|
|
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
|
2018-04-27 23:29:00 +02:00
|
|
|
|
2018-04-29 21:17:23 +02:00
|
|
|
const checkClaimAvailability = require('../availability/checkClaimAvailability.js');
|
2018-04-27 23:29:00 +02:00
|
|
|
|
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');
|
2018-10-17 15:32:21 +02:00
|
|
|
const parsePublishApiRequestFiles = require('./parsePublishApiRequestFiles.js');
|
2018-04-27 23:29:00 +02:00
|
|
|
const authenticateUser = require('./authentication.js');
|
2018-03-29 02:35:41 +02:00
|
|
|
|
2018-12-14 18:42:37 +01:00
|
|
|
const chainquery = require('chainquery').default;
|
2019-01-14 07:27:23 +01:00
|
|
|
const createCanonicalLink = require('@globalutils/createCanonicalLink');
|
2018-11-06 23:40:38 +01:00
|
|
|
|
2018-07-27 20:05:10 +02:00
|
|
|
const CLAIM_TAKEN = 'CLAIM_TAKEN';
|
2018-09-19 19:27:55 +02:00
|
|
|
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,
|
2018-07-17 21:23:14 +02:00
|
|
|
files,
|
2018-06-28 21:30:32 +02:00
|
|
|
});
|
2018-06-23 22:19:58 +02:00
|
|
|
// check for disabled publishing
|
|
|
|
if (disabled) {
|
2018-06-23 22:53:35 +02:00
|
|
|
return res.status(503).json({
|
2018-06-23 22:19:58 +02:00
|
|
|
success: false,
|
2018-08-01 01:43:08 +02:00
|
|
|
message: disabledMessage,
|
2018-06-23 22:19:58 +02:00
|
|
|
});
|
|
|
|
}
|
2018-03-29 20:24:52 +02:00
|
|
|
// define variables
|
2018-11-06 23:40:38 +01:00
|
|
|
let channelName,
|
|
|
|
channelId,
|
|
|
|
channelPassword,
|
|
|
|
description,
|
|
|
|
fileName,
|
|
|
|
filePath,
|
|
|
|
fileExtension,
|
|
|
|
fileType,
|
|
|
|
gaStartTime,
|
|
|
|
license,
|
2019-02-23 06:52:31 +01:00
|
|
|
licenseUrl,
|
2018-11-06 23:40:38 +01:00
|
|
|
name,
|
|
|
|
nsfw,
|
|
|
|
thumbnail,
|
|
|
|
thumbnailFileName,
|
|
|
|
thumbnailFilePath,
|
|
|
|
thumbnailFileType,
|
|
|
|
title,
|
|
|
|
claimData,
|
|
|
|
claimId;
|
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);
|
2019-02-23 06:52:31 +01:00
|
|
|
({
|
|
|
|
name,
|
|
|
|
nsfw,
|
|
|
|
license,
|
|
|
|
licenseUrl,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
thumbnail,
|
|
|
|
} = parsePublishApiRequestBody(body));
|
|
|
|
({
|
|
|
|
fileName,
|
|
|
|
filePath,
|
|
|
|
fileExtension,
|
|
|
|
fileType,
|
|
|
|
thumbnailFileName,
|
|
|
|
thumbnailFilePath,
|
|
|
|
thumbnailFileType,
|
|
|
|
} = parsePublishApiRequestFiles(files));
|
|
|
|
({ channelName, channelId, channelPassword } = body);
|
2018-03-29 20:24:52 +02:00
|
|
|
} catch (error) {
|
2019-02-23 06:52:31 +01:00
|
|
|
return res.status(400).json({ success: false, message: error.message });
|
2018-03-29 20:24:52 +02:00
|
|
|
}
|
|
|
|
// check channel authorization
|
2018-07-27 20:05:10 +02:00
|
|
|
authenticateUser(channelName, channelId, channelPassword, user)
|
|
|
|
.then(({ channelName, channelClaimId }) => {
|
2018-09-21 22:16:01 +02:00
|
|
|
if (publishOnlyApproved && !isApprovedChannel({ longId: channelClaimId }, approvedChannels)) {
|
2018-09-19 19:27:55 +02:00
|
|
|
const error = {
|
2019-02-23 06:52:31 +01:00
|
|
|
name: UNAPPROVED_CHANNEL,
|
2018-09-19 19:27:55 +02:00
|
|
|
message: 'This spee.ch instance only allows publishing to approved channels',
|
|
|
|
};
|
|
|
|
throw error;
|
|
|
|
}
|
2018-11-06 23:40:38 +01:00
|
|
|
|
2018-07-27 20:05:10 +02:00
|
|
|
return Promise.all([
|
|
|
|
checkClaimAvailability(name),
|
2019-02-23 06:52:31 +01:00
|
|
|
createPublishParams(
|
|
|
|
filePath,
|
|
|
|
name,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
license,
|
|
|
|
licenseUrl,
|
|
|
|
nsfw,
|
|
|
|
thumbnail,
|
|
|
|
channelName,
|
|
|
|
channelClaimId
|
|
|
|
),
|
|
|
|
createThumbnailPublishParams(thumbnailFilePath, name, license, licenseUrl, nsfw),
|
2018-08-01 01:43:08 +02:00
|
|
|
]);
|
2018-07-27 20:05:10 +02:00
|
|
|
})
|
2019-02-23 06:52:31 +01:00
|
|
|
.then(([claimAvailable, publishParams, thumbnailPublishParams]) => {
|
2018-07-27 21:31:08 +02:00
|
|
|
if (!claimAvailable) {
|
2018-08-01 01:43:08 +02:00
|
|
|
const error = {
|
2019-02-23 06:52:31 +01:00
|
|
|
name: CLAIM_TAKEN,
|
2018-08-01 01:43:08 +02:00
|
|
|
message: 'That claim name is already taken',
|
2018-07-27 20:05:10 +02:00
|
|
|
};
|
2018-08-01 01:43:08 +02:00
|
|
|
throw error;
|
2018-03-29 20:24:52 +02:00
|
|
|
}
|
2018-04-27 08:42:22 +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
|
2018-10-04 16:26:42 +02:00
|
|
|
return publish(publishParams, fileName, fileType, filePath);
|
2018-03-29 20:24:52 +02:00
|
|
|
})
|
2018-11-06 23:40:38 +01:00
|
|
|
.then(publishResults => {
|
2019-04-19 18:14:03 +02:00
|
|
|
logger.debug('Publish success >', publishResults);
|
2018-11-06 23:40:38 +01:00
|
|
|
claimData = publishResults;
|
2019-02-23 06:52:31 +01:00
|
|
|
({ claimId } = claimData);
|
2018-11-06 23:40:38 +01:00
|
|
|
|
|
|
|
if (channelName) {
|
2019-04-19 18:14:03 +02:00
|
|
|
logger.debug(`api/claim/publish: claimData.certificateId ${claimData.certificateId}`);
|
2019-02-23 06:52:31 +01:00
|
|
|
return chainquery.claim.queries.getShortClaimIdFromLongClaimId(
|
|
|
|
claimData.certificateId,
|
|
|
|
channelName
|
|
|
|
);
|
2018-11-06 23:40:38 +01:00
|
|
|
} else {
|
2019-02-23 06:52:31 +01:00
|
|
|
return chainquery.claim.queries
|
|
|
|
.getShortClaimIdFromLongClaimId(claimId, name, claimData)
|
|
|
|
.catch(() => {
|
|
|
|
return claimId.slice(0, 1);
|
|
|
|
});
|
2018-11-06 23:40:38 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(shortId => {
|
|
|
|
let canonicalUrl;
|
|
|
|
if (channelName) {
|
|
|
|
canonicalUrl = createCanonicalLink({ asset: { ...claimData, channelShortId: shortId } });
|
|
|
|
} else {
|
2018-11-11 01:11:12 +01:00
|
|
|
canonicalUrl = createCanonicalLink({ asset: { ...claimData, shortId } });
|
2018-11-06 23:40:38 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 20:24:52 +02:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
message: 'publish completed successfully',
|
2019-02-23 06:52:31 +01:00
|
|
|
data: {
|
2018-03-29 20:24:52 +02:00
|
|
|
name,
|
2018-11-06 23:40:38 +01:00
|
|
|
claimId,
|
2019-02-23 06:52:31 +01:00
|
|
|
url: `${host}${canonicalUrl}`, // for backwards compatability with app
|
|
|
|
showUrl: `${host}${canonicalUrl}`,
|
2018-11-06 23:40:38 +01:00
|
|
|
serveUrl: `${host}${canonicalUrl}${fileExtension}`,
|
2019-02-23 06:52:31 +01:00
|
|
|
pushTo: canonicalUrl,
|
2018-08-21 14:53:17 +02:00
|
|
|
claimData,
|
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)) {
|
2018-09-19 19:27:55 +02:00
|
|
|
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;
|