Speech as a package #416
23 changed files with 488 additions and 287 deletions
25
server/routes/api/channelAvailability.js
Normal file
25
server/routes/api/channelAvailability.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
const { checkChannelAvailability } = require('../../controllers/publishController.js');
|
||||||
|
const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to check whether site has published to a channel
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const channelAvailability = () => {
|
||||||
|
return ({ 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());
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = channelAvailability;
|
31
server/routes/api/channelClaims.js
Normal file
31
server/routes/api/channelClaims.js
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
const { getChannelClaims } = require('../../controllers/serveController.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to get all claims for channel
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const channelClaims = () => {
|
||||||
|
return ({ ip, originalUrl, body, params }, res) => {
|
||||||
|
const channelName = params.channelName;
|
||||||
|
let channelClaimId = params.channelClaimId;
|
||||||
|
if (channelClaimId === 'none') channelClaimId = null;
|
||||||
|
const page = params.page;
|
||||||
|
getChannelClaims(channelName, channelClaimId, page)
|
||||||
|
.then(data => {
|
||||||
|
if (data === NO_CHANNEL) {
|
||||||
|
return res.status(404).json({success: false, message: 'No matching channel was found'});
|
||||||
|
}
|
||||||
|
res.status(200).json({success: true, data});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = channelClaims;
|
30
server/routes/api/channelData.js
Normal file
30
server/routes/api/channelData.js
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
const { getChannelData } = require('../../controllers/serveController.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to get data for a channel
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const channelData = () => {
|
||||||
|
return ({ ip, originalUrl, body, params }, res) => {
|
||||||
|
const channelName = params.channelName;
|
||||||
|
let channelClaimId = params.channelClaimId;
|
||||||
|
if (channelClaimId === 'none') channelClaimId = null;
|
||||||
|
getChannelData(channelName, channelClaimId, 0)
|
||||||
|
.then(data => {
|
||||||
|
if (data === NO_CHANNEL) {
|
||||||
|
return res.status(404).json({success: false, message: 'No matching channel was found'});
|
||||||
|
}
|
||||||
|
res.status(200).json({success: true, data});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = channelData;
|
|
@ -1,6 +1,3 @@
|
||||||
const logger = require('winston');
|
|
||||||
// const { details: { host } } = require('siteConfig.js');
|
|
||||||
// const { db } = require('mysqlConfig.js');
|
|
||||||
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -9,10 +6,8 @@ route to get a short channel id from long channel Id
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const channelShortIdRoute = (db, host) => {
|
const channelShortIdRoute = (db) => {
|
||||||
return ({ ip, originalUrl, params }, res) => {
|
return ({ ip, originalUrl, params }, res) => {
|
||||||
console.log('hello from channelShortIdRoute');
|
|
||||||
logger.debug('host:', host);
|
|
||||||
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
|
||||||
.then(shortId => {
|
.then(shortId => {
|
||||||
res.status(200).json(shortId);
|
res.status(200).json(shortId);
|
25
server/routes/api/claimAvailability.js
Normal file
25
server/routes/api/claimAvailability.js
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
const { claimNameIsAvailable } = require('../../controllers/publishController.js');
|
||||||
|
const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to check whether this site published to a claim
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimAvailability = () => {
|
||||||
|
return ({ ip, originalUrl, params: { name } }, res) => {
|
||||||
|
const gaStartTime = Date.now();
|
||||||
|
claimNameIsAvailable(name)
|
||||||
|
.then(result => {
|
||||||
|
res.status(200).json(result);
|
||||||
|
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimAvailability;
|
27
server/routes/api/claimData.js
Normal file
27
server/routes/api/claimData.js
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to return data for a claim
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimData = (db) => {
|
||||||
|
return ({ 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(404).json({success: false, message: 'No claim could be found'});
|
||||||
|
}
|
||||||
|
res.status(200).json({success: true, data: claimInfo});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimData;
|
39
server/routes/api/claimGet.js
Normal file
39
server/routes/api/claimGet.js
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
const { getClaim } = require('../../helpers/lbryApi.js');
|
||||||
|
const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to get a claim
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimGet = (db) => {
|
||||||
|
return ({ ip, originalUrl, params }, res) => {
|
||||||
|
const name = params.name;
|
||||||
|
const claimId = params.claimId;
|
||||||
|
// resolve the claim
|
||||||
|
db.Claim.resolveClaim(name, claimId)
|
||||||
|
.then(resolveResult => {
|
||||||
|
// make sure a claim actually exists at that uri
|
||||||
|
if (!resolveResult) {
|
||||||
|
throw new Error('No matching uri found in Claim table');
|
||||||
|
}
|
||||||
|
let fileData = createFileData(resolveResult);
|
||||||
|
// get the claim
|
||||||
|
return Promise.all([fileData, getClaim(`${name}#${claimId}`)]);
|
||||||
|
})
|
||||||
|
.then(([ fileData, getResult ]) => {
|
||||||
|
fileData = addGetResultsToFileData(fileData, getResult);
|
||||||
|
return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]);
|
||||||
|
})
|
||||||
|
.then(([ fileRecord, {message, completed} ]) => {
|
||||||
|
res.status(200).json({ success: true, message, completed });
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimGet;
|
35
server/routes/api/claimLongId.js
Normal file
35
server/routes/api/claimLongId.js
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
const { getClaimId } = require('../../controllers/serveController.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
const NO_CHANNEL = 'NO_CHANNEL';
|
||||||
|
const NO_CLAIM = 'NO_CLAIM';
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to get a long claim id
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimLongId = () => {
|
||||||
|
return ({ ip, originalUrl, body, params }, res) => {
|
||||||
|
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'});
|
||||||
|
}
|
||||||
|
res.status(200).json({success: true, data: result});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimLongId;
|
69
server/routes/api/claimPublish.js
Normal file
69
server/routes/api/claimPublish.js
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
const { createBasicPublishParams, createThumbnailPublishParams, parsePublishApiRequestBody, parsePublishApiRequestFiles } = require('../../helpers/publishHelpers.js');
|
||||||
|
const { claimNameIsAvailable, publish } = require('../../controllers/publishController.js');
|
||||||
|
const { authenticateUser } = require('../../auth/authentication.js');
|
||||||
|
const { sendGATimingEvent } = require('../../helpers/googleAnalytics.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to publish a claim through the daemon
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimPublish = (db, host) => {
|
||||||
|
return ({ body, files, headers, ip, originalUrl, user }, res) => {
|
||||||
|
// define variables
|
||||||
|
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
|
||||||
|
// 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, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files));
|
||||||
|
({channelName, channelId, channelPassword} = body);
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(400).json({success: false, message: error.message});
|
||||||
|
}
|
||||||
|
// check channel authorization
|
||||||
|
Promise
|
||||||
|
.all([
|
||||||
|
authenticateUser(channelName, channelId, channelPassword, user),
|
||||||
|
claimNameIsAvailable(name),
|
||||||
|
createBasicPublishParams(filePath, name, title, description, license, nsfw, thumbnail),
|
||||||
|
createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw),
|
||||||
|
])
|
||||||
|
.then(([{channelName, channelClaimId}, validatedClaimName, publishParams, thumbnailPublishParams]) => {
|
||||||
|
// add channel details to the publish params
|
||||||
|
if (channelName && channelClaimId) {
|
||||||
|
publishParams['channel_name'] = channelName;
|
||||||
|
publishParams['channel_id'] = channelClaimId;
|
||||||
|
}
|
||||||
|
// publish the thumbnail
|
||||||
|
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,
|
||||||
|
claimId: result.claim_id,
|
||||||
|
url : `${host}/${result.claim_id}/${name}`,
|
||||||
|
lbryTx : result,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
// record the publish end time and send to google analytics
|
||||||
|
sendGATimingEvent('end-to-end', 'publish', fileType, gaStartTime, Date.now());
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimPublish;
|
22
server/routes/api/claimResolve.js
Normal file
22
server/routes/api/claimResolve.js
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
const { resolveUri } = require('../../helpers/lbryApi.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to run a resolve request on the daemon
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimResolve = () => {
|
||||||
|
return ({ headers, ip, originalUrl, params }, res) => {
|
||||||
|
resolveUri(`${params.name}#${params.claimId}`)
|
||||||
|
.then(resolvedUri => {
|
||||||
|
res.status(200).json(resolvedUri);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimResolve;
|
21
server/routes/api/claimShortId.js
Normal file
21
server/routes/api/claimShortId.js
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to get a short claim id from long claim Id
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimShortId = (db) => {
|
||||||
|
return ({ ip, originalUrl, body, params }, res) => {
|
||||||
|
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
||||||
|
.then(shortId => {
|
||||||
|
res.status(200).json({success: true, data: shortId});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimShortId;
|
22
server/routes/api/claimsList.js
Normal file
22
server/routes/api/claimsList.js
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
const { getClaimList } = require('../../helpers/lbryApi.js');
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to get list of claims
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claimList = (db) => {
|
||||||
|
return ({ ip, originalUrl, params }, res) => {
|
||||||
|
getClaimList(params.name)
|
||||||
|
.then(claimsList => {
|
||||||
|
res.status(200).json(claimsList);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claimList;
|
33
server/routes/api/fileAvailability.js
Normal file
33
server/routes/api/fileAvailability.js
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
|
||||||
|
const { handleErrorResponse } = require('helpers/errorHandlers.js');
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to see if asset is available locally
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const fileAvailability = (db) => {
|
||||||
|
return ({ ip, originalUrl, params }, res) => {
|
||||||
|
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});
|
||||||
|
}
|
||||||
|
res.status(200).json({success: true, data: false});
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
handleErrorResponse(originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = fileAvailability;
|
29
server/routes/api/index.js
Normal file
29
server/routes/api/index.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
const channelAvailability = require('./channelAvailability');
|
||||||
|
const channelClaims = require('./channelClaims');
|
||||||
|
const channelData = require('./channelData');
|
||||||
|
const channelShortId = require('./channelShortId');
|
||||||
|
const claimAvailability = require('./claimAvailability');
|
||||||
|
const claimData = require('./claimData');
|
||||||
|
const claimGet = require('./claimGet');
|
||||||
|
const claimLongId = require('./claimLongId');
|
||||||
|
const claimPublish = require('./claimPublish');
|
||||||
|
const claimResolve = require('./claimResolve');
|
||||||
|
const claimShortId = require('./claimShortId');
|
||||||
|
const claimsList = require('./claimsList');
|
||||||
|
const fileAvailability = require('./fileAvailability');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
channelAvailability,
|
||||||
|
channelClaims,
|
||||||
|
channelData,
|
||||||
|
channelShortId,
|
||||||
|
claimAvailability,
|
||||||
|
claimData,
|
||||||
|
claimGet,
|
||||||
|
claimLongId,
|
||||||
|
claimPublish,
|
||||||
|
claimResolve,
|
||||||
|
claimShortId,
|
||||||
|
claimsList,
|
||||||
|
fileAvailability,
|
||||||
|
};
|
|
@ -1,5 +0,0 @@
|
||||||
const channelShortId = require('./channelShortId');
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
channelShortId,
|
|
||||||
}
|
|
|
@ -1,232 +0,0 @@
|
||||||
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 = {
|
|
||||||
// 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());
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
channelDataRoute ({ ip, originalUrl, body, params }, res) {
|
|
||||||
const channelName = params.channelName;
|
|
||||||
let channelClaimId = params.channelClaimId;
|
|
||||||
if (channelClaimId === 'none') channelClaimId = null;
|
|
||||||
getChannelData(channelName, channelClaimId, 0)
|
|
||||||
.then(data => {
|
|
||||||
if (data === NO_CHANNEL) {
|
|
||||||
return res.status(404).json({success: false, message: 'No matching channel was found'});
|
|
||||||
}
|
|
||||||
res.status(200).json({success: true, data});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
channelClaimsRoute ({ ip, originalUrl, body, params }, res) {
|
|
||||||
const channelName = params.channelName;
|
|
||||||
let channelClaimId = params.channelClaimId;
|
|
||||||
if (channelClaimId === 'none') channelClaimId = null;
|
|
||||||
const page = params.page;
|
|
||||||
getChannelClaims(channelName, channelClaimId, page)
|
|
||||||
.then(data => {
|
|
||||||
if (data === NO_CHANNEL) {
|
|
||||||
return res.status(404).json({success: false, message: 'No matching channel was found'});
|
|
||||||
}
|
|
||||||
res.status(200).json({success: true, data});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// route to run a claim_list request on the daemon
|
|
||||||
claimListRoute ({ ip, originalUrl, params }, res) {
|
|
||||||
getClaimList(params.name)
|
|
||||||
.then(claimsList => {
|
|
||||||
res.status(200).json(claimsList);
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// route to get an asset
|
|
||||||
claimGetRoute ({ ip, originalUrl, params }, res) {
|
|
||||||
const name = params.name;
|
|
||||||
const claimId = params.claimId;
|
|
||||||
// resolve the claim
|
|
||||||
db.Claim.resolveClaim(name, claimId)
|
|
||||||
.then(resolveResult => {
|
|
||||||
// make sure a claim actually exists at that uri
|
|
||||||
if (!resolveResult) {
|
|
||||||
throw new Error('No matching uri found in Claim table');
|
|
||||||
}
|
|
||||||
let fileData = createFileData(resolveResult);
|
|
||||||
// get the claim
|
|
||||||
return Promise.all([fileData, getClaim(`${name}#${claimId}`)]);
|
|
||||||
})
|
|
||||||
.then(([ fileData, getResult ]) => {
|
|
||||||
fileData = addGetResultsToFileData(fileData, getResult);
|
|
||||||
return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]);
|
|
||||||
})
|
|
||||||
.then(([ fileRecord, {message, completed} ]) => {
|
|
||||||
res.status(200).json({ success: true, message, completed });
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// route to check whether this site published to a claim
|
|
||||||
claimAvailabilityRoute ({ ip, originalUrl, params: { name } }, res) {
|
|
||||||
const gaStartTime = Date.now();
|
|
||||||
claimNameIsAvailable(name)
|
|
||||||
.then(result => {
|
|
||||||
res.status(200).json(result);
|
|
||||||
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// route to run a resolve request on the daemon
|
|
||||||
claimResolveRoute ({ headers, ip, originalUrl, params }, res) {
|
|
||||||
resolveUri(`${params.name}#${params.claimId}`)
|
|
||||||
.then(resolvedUri => {
|
|
||||||
res.status(200).json(resolvedUri);
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// route to run a publish request on the daemon
|
|
||||||
claimPublishRoute ({ body, files, headers, ip, originalUrl, user }, res) {
|
|
||||||
// define variables
|
|
||||||
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
|
|
||||||
// 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, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files));
|
|
||||||
({channelName, channelId, channelPassword} = body);
|
|
||||||
} catch (error) {
|
|
||||||
return res.status(400).json({success: false, message: error.message});
|
|
||||||
}
|
|
||||||
// check channel authorization
|
|
||||||
Promise.all([
|
|
||||||
authenticateUser(channelName, channelId, channelPassword, user),
|
|
||||||
claimNameIsAvailable(name),
|
|
||||||
createBasicPublishParams(filePath, name, title, description, license, nsfw, thumbnail),
|
|
||||||
createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw),
|
|
||||||
])
|
|
||||||
.then(([{channelName, channelClaimId}, validatedClaimName, publishParams, thumbnailPublishParams]) => {
|
|
||||||
// add channel details to the publish params
|
|
||||||
if (channelName && channelClaimId) {
|
|
||||||
publishParams['channel_name'] = channelName;
|
|
||||||
publishParams['channel_id'] = channelClaimId;
|
|
||||||
}
|
|
||||||
// publish the thumbnail
|
|
||||||
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,
|
|
||||||
claimId: result.claim_id,
|
|
||||||
url : `${host}/${result.claim_id}/${name}`,
|
|
||||||
lbryTx : result,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
// record the publish end time and send to google analytics
|
|
||||||
sendGATimingEvent('end-to-end', 'publish', fileType, gaStartTime, Date.now());
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// route to get a short claim id from long claim Id
|
|
||||||
claimShortIdRoute ({ ip, originalUrl, body, params }, res) {
|
|
||||||
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
|
|
||||||
.then(shortId => {
|
|
||||||
res.status(200).json({success: true, data: shortId});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
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'});
|
|
||||||
}
|
|
||||||
res.status(200).json({success: true, data: result});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
claimDataRoute ({ 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(404).json({success: false, message: 'No claim could be found'});
|
|
||||||
}
|
|
||||||
res.status(200).json({success: true, data: claimInfo});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
// route to see if asset is available locally
|
|
||||||
fileAvailabilityRoute ({ ip, originalUrl, params }, res) {
|
|
||||||
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});
|
|
||||||
}
|
|
||||||
res.status(200).json({success: true, data: false});
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleErrorResponse(originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = routes;
|
|
8
server/routes/asset/index.js
Normal file
8
server/routes/asset/index.js
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
const serveAssetByClaim = require('./serveAssetByClaim');
|
||||||
|
const serveAssetByIdentifierAndClaim = require('./serveAssetByIdentifierAndClaim');
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
serveAssetByClaim,
|
||||||
|
serveAssetByIdentifierAndClaim,
|
||||||
|
};
|
44
server/routes/asset/serveAssetByClaim.js
Normal file
44
server/routes/asset/serveAssetByClaim.js
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
const { sendGAServeEvent } = require('../../helpers/googleAnalytics');
|
||||||
|
const { determineResponseType, logRequestData, getClaimIdAndServeAsset } = require('../../helpers/serveHelpers.js');
|
||||||
|
const lbryUri = require('../../helpers/lbryUri.js');
|
||||||
|
const handleShowRender = require('../../helpers/handleShowRender.jsx');
|
||||||
|
const SERVE = 'SERVE';
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
route to serve an asset or the react app via the claim name only
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const claim = () => {
|
||||||
|
return (req, res) => {
|
||||||
|
const { headers, ip, originalUrl, params } = req;
|
||||||
|
// decide if this is a show request
|
||||||
|
let hasFileExtension;
|
||||||
|
try {
|
||||||
|
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(400).json({success: false, message: error.message});
|
||||||
|
}
|
||||||
|
let responseType = determineResponseType(hasFileExtension, headers);
|
||||||
|
if (responseType !== SERVE) {
|
||||||
|
return handleShowRender(req, res);
|
||||||
|
}
|
||||||
|
// handle serve request
|
||||||
|
// send google analytics
|
||||||
|
sendGAServeEvent(headers, ip, originalUrl);
|
||||||
|
// parse the claim
|
||||||
|
let claimName;
|
||||||
|
try {
|
||||||
|
({claimName} = lbryUri.parseClaim(params.claim));
|
||||||
|
} catch (error) {
|
||||||
|
return res.status(400).json({success: false, message: error.message});
|
||||||
|
}
|
||||||
|
// log the request data for debugging
|
||||||
|
logRequestData(responseType, claimName, null, null);
|
||||||
|
// get the claim Id and then serve the asset
|
||||||
|
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = claim;
|
|
@ -1,12 +1,18 @@
|
||||||
const { sendGAServeEvent } = require('../helpers/googleAnalytics');
|
const { sendGAServeEvent } = require('../../helpers/googleAnalytics');
|
||||||
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset } = require('../helpers/serveHelpers.js');
|
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, logRequestData, getClaimIdAndServeAsset } = require('../../helpers/serveHelpers.js');
|
||||||
const lbryUri = require('../helpers/lbryUri.js');
|
const lbryUri = require('../../helpers/lbryUri.js');
|
||||||
const handleShowRender = require('../helpers/handleShowRender.jsx');
|
const handleShowRender = require('../../helpers/handleShowRender.jsx');
|
||||||
|
|
||||||
const SERVE = 'SERVE';
|
const SERVE = 'SERVE';
|
||||||
|
|
||||||
module.exports = (app) => {
|
/*
|
||||||
// route to serve a specific asset using the channel or claim id
|
|
||||||
app.get('/:identifier/:claim', (req, res) => {
|
route to serve an asset or the react app via the claim name and an identifier
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
const identifierClaim = () => {
|
||||||
|
return (req, res) => {
|
||||||
const { headers, ip, originalUrl, params } = req;
|
const { headers, ip, originalUrl, params } = req;
|
||||||
// decide if this is a show request
|
// decide if this is a show request
|
||||||
let hasFileExtension;
|
let hasFileExtension;
|
||||||
|
@ -43,34 +49,7 @@ module.exports = (app) => {
|
||||||
logRequestData(responseType, claimName, channelName, claimId);
|
logRequestData(responseType, claimName, channelName, claimId);
|
||||||
// get the claim Id and then serve the asset
|
// get the claim Id and then serve the asset
|
||||||
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
|
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
|
||||||
});
|
|
||||||
// route to serve the winning asset at a claim or a channel page
|
|
||||||
app.get('/:claim', (req, res) => {
|
|
||||||
const { headers, ip, originalUrl, params } = req;
|
|
||||||
// decide if this is a show request
|
|
||||||
let hasFileExtension;
|
|
||||||
try {
|
|
||||||
({ hasFileExtension } = lbryUri.parseModifier(params.claim));
|
|
||||||
} catch (error) {
|
|
||||||
return res.status(400).json({success: false, message: error.message});
|
|
||||||
}
|
|
||||||
let responseType = determineResponseType(hasFileExtension, headers);
|
|
||||||
if (responseType !== SERVE) {
|
|
||||||
return handleShowRender(req, res);
|
|
||||||
}
|
|
||||||
// handle serve request
|
|
||||||
// send google analytics
|
|
||||||
sendGAServeEvent(headers, ip, originalUrl);
|
|
||||||
// parse the claim
|
|
||||||
let claimName;
|
|
||||||
try {
|
|
||||||
({claimName} = lbryUri.parseClaim(params.claim));
|
|
||||||
} catch (error) {
|
|
||||||
return res.status(400).json({success: false, message: error.message});
|
|
||||||
}
|
|
||||||
// log the request data for debugging
|
|
||||||
logRequestData(responseType, claimName, null, null);
|
|
||||||
// get the claim Id and then serve the asset
|
|
||||||
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = identifierClaim;
|
|
@ -1,4 +1,4 @@
|
||||||
const handlePageRender = require('../helpers/handlePageRender.jsx');
|
const handlePageRender = require('../../helpers/handlePageRender.jsx');
|
||||||
|
|
||||||
module.exports = app => {
|
module.exports = app => {
|
||||||
// a catch-all route if someone visits a page that does not exist
|
// a catch-all route if someone visits a page that does not exist
|
|
@ -1,5 +1,5 @@
|
||||||
const { details: host } = require('../../config/siteConfig.js');
|
const { details: host } = require('../../../config/siteConfig.js');
|
||||||
const handlePageRender = require('../helpers/handlePageRender.jsx');
|
const handlePageRender = require('../../helpers/handlePageRender.jsx');
|
||||||
|
|
||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
// route for the home page
|
// route for the home page
|
12
speech.js
12
speech.js
|
@ -1,4 +1,5 @@
|
||||||
const apiRoutes = require('./server/routes/apiRoutes/');
|
const api = require('./server/routes/api/');
|
||||||
|
const asset = require('./server/routes/asset/');
|
||||||
const logger = require('./config/loggerConfig.js');
|
const logger = require('./config/loggerConfig.js');
|
||||||
const mysql = require('./config/mysqlConfig');
|
const mysql = require('./config/mysqlConfig');
|
||||||
const site = require('./config/siteConfig');
|
const site = require('./config/siteConfig');
|
||||||
|
@ -8,13 +9,16 @@ const models = require('./server/models/');
|
||||||
// const Components = require('./client/components');
|
// const Components = require('./client/components');
|
||||||
|
|
||||||
const exports = {
|
const exports = {
|
||||||
apiRoutes,
|
|
||||||
logger,
|
logger,
|
||||||
|
models,
|
||||||
mysql,
|
mysql,
|
||||||
|
passport,
|
||||||
site,
|
site,
|
||||||
slack,
|
slack,
|
||||||
passport,
|
routes: {
|
||||||
models,
|
api,
|
||||||
|
asset,
|
||||||
|
},
|
||||||
// Components,
|
// Components,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue