Speech as a package #416

Merged
bones7242 merged 89 commits from speech-as-a-package into master 2018-04-18 21:47:34 +02:00
26 changed files with 439 additions and 353 deletions
Showing only changes of commit 9523890b65 - Show all commits

View file

@ -0,0 +1,5 @@
const multipart = require('connect-multiparty');
const { publishing: { uploadDirectory } } = require('siteConfig.js');
const multipartMiddleware = multipart({uploadDir: uploadDirectory});
module.exports = multipartMiddleware;

View file

@ -8,18 +8,16 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const channelAvailability = () => { const channelAvailability = ({ ip, originalUrl, params: { name } }, res) => {
return ({ ip, originalUrl, params: { name } }, res) => { const gaStartTime = Date.now();
const gaStartTime = Date.now(); checkChannelAvailability(name)
checkChannelAvailability(name) .then(availableName => {
.then(availableName => { res.status(200).json(availableName);
res.status(200).json(availableName); sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = channelAvailability; module.exports = channelAvailability;

View file

@ -9,23 +9,21 @@ const NO_CHANNEL = 'NO_CHANNEL';
*/ */
const channelClaims = () => { const channelClaims = ({ ip, originalUrl, body, params }, res) => {
return ({ ip, originalUrl, body, params }, res) => { const channelName = params.channelName;
const channelName = params.channelName; let channelClaimId = params.channelClaimId;
let channelClaimId = params.channelClaimId; if (channelClaimId === 'none') channelClaimId = null;
if (channelClaimId === 'none') channelClaimId = null; const page = params.page;
const page = params.page; getChannelClaims(channelName, channelClaimId, page)
getChannelClaims(channelName, channelClaimId, page) .then(data => {
.then(data => { if (data === NO_CHANNEL) {
if (data === NO_CHANNEL) { return res.status(404).json({success: false, message: 'No matching channel was found'});
return res.status(404).json({success: false, message: 'No matching channel was found'}); }
} res.status(200).json({success: true, data});
res.status(200).json({success: true, data}); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = channelClaims; module.exports = channelClaims;

View file

@ -9,22 +9,20 @@ const NO_CHANNEL = 'NO_CHANNEL';
*/ */
const channelData = () => { const channelData = ({ ip, originalUrl, body, params }, res) => {
return ({ ip, originalUrl, body, params }, res) => { const channelName = params.channelName;
const channelName = params.channelName; let channelClaimId = params.channelClaimId;
let channelClaimId = params.channelClaimId; if (channelClaimId === 'none') channelClaimId = null;
if (channelClaimId === 'none') channelClaimId = null; getChannelData(channelName, channelClaimId, 0)
getChannelData(channelName, channelClaimId, 0) .then(data => {
.then(data => { if (data === NO_CHANNEL) {
if (data === NO_CHANNEL) { return res.status(404).json({success: false, message: 'No matching channel was found'});
return res.status(404).json({success: false, message: 'No matching channel was found'}); }
} res.status(200).json({success: true, data});
res.status(200).json({success: true, data}); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = channelData; module.exports = channelData;

View file

@ -1,4 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js'); const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/* /*
@ -6,16 +7,14 @@ route to get a short channel id from long channel Id
*/ */
const channelShortIdRoute = (db) => { const channelShortIdRoute = ({ ip, originalUrl, params }, res) => {
return ({ ip, originalUrl, params }, res) => { 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); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = channelShortIdRoute; module.exports = channelShortIdRoute;

View file

@ -8,18 +8,16 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const claimAvailability = () => { const claimAvailability = ({ ip, originalUrl, params: { name } }, res) => {
return ({ ip, originalUrl, params: { name } }, res) => { const gaStartTime = Date.now();
const gaStartTime = Date.now(); claimNameIsAvailable(name)
claimNameIsAvailable(name) .then(result => {
.then(result => { res.status(200).json(result);
res.status(200).json(result); sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now());
sendGATimingEvent('end-to-end', 'claim name availability', name, gaStartTime, Date.now()); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = claimAvailability; module.exports = claimAvailability;

View file

@ -1,4 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js'); const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/* /*
@ -6,22 +7,20 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const claimData = (db) => { const claimData = ({ ip, originalUrl, body, params }, res) => {
return ({ ip, originalUrl, body, params }, res) => { const claimName = params.claimName;
const claimName = params.claimName; let claimId = params.claimId;
let claimId = params.claimId; if (claimId === 'none') claimId = null;
if (claimId === 'none') claimId = null; db.Claim.resolveClaim(claimName, claimId)
db.Claim.resolveClaim(claimName, claimId) .then(claimInfo => {
.then(claimInfo => { if (!claimInfo) {
if (!claimInfo) { return res.status(404).json({success: false, message: 'No claim could be found'});
return res.status(404).json({success: false, message: 'No claim could be found'}); }
} res.status(200).json({success: true, data: claimInfo});
res.status(200).json({success: true, data: claimInfo}); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = claimData; module.exports = claimData;

View file

@ -1,6 +1,7 @@
const { getClaim } = require('helpers/lbryApi.js'); const { getClaim } = require('helpers/lbryApi.js');
const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js'); const { addGetResultsToFileData, createFileData } = require('../../helpers/publishHelpers.js');
const { handleErrorResponse } = require('helpers/errorHandlers.js'); const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/* /*
@ -8,32 +9,30 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const claimGet = (db) => { const claimGet = ({ ip, originalUrl, params }, res) => {
return ({ ip, originalUrl, params }, res) => { const name = params.name;
const name = params.name; const claimId = params.claimId;
const claimId = params.claimId; // resolve the claim
// resolve the claim db.Claim.resolveClaim(name, claimId)
db.Claim.resolveClaim(name, claimId) .then(resolveResult => {
.then(resolveResult => { // make sure a claim actually exists at that uri
// make sure a claim actually exists at that uri if (!resolveResult) {
if (!resolveResult) { throw new Error('No matching uri found in Claim table');
throw new Error('No matching uri found in Claim table'); }
} let fileData = createFileData(resolveResult);
let fileData = createFileData(resolveResult); // get the claim
// get the claim return Promise.all([fileData, getClaim(`${name}#${claimId}`)]);
return Promise.all([fileData, getClaim(`${name}#${claimId}`)]); })
}) .then(([ fileData, getResult ]) => {
.then(([ fileData, getResult ]) => { fileData = addGetResultsToFileData(fileData, getResult);
fileData = addGetResultsToFileData(fileData, getResult); return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]);
return Promise.all([db.upsert(db.File, fileData, {name, claimId}, 'File'), getResult]); })
}) .then(([ fileRecord, {message, completed} ]) => {
.then(([ fileRecord, {message, completed} ]) => { res.status(200).json({ success: true, message, completed });
res.status(200).json({ success: true, message, completed }); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = claimGet; module.exports = claimGet;

View file

@ -7,16 +7,14 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const claimList = (db) => { const claimList = ({ ip, originalUrl, params }, res) => {
return ({ ip, originalUrl, params }, res) => { getClaimList(params.name)
getClaimList(params.name) .then(claimsList => {
.then(claimsList => { res.status(200).json(claimsList);
res.status(200).json(claimsList); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = claimList; module.exports = claimList;

View file

@ -10,26 +10,24 @@ const NO_CLAIM = 'NO_CLAIM';
*/ */
const claimLongId = () => { const claimLongId = ({ ip, originalUrl, body, params }, res) => {
return ({ ip, originalUrl, body, params }, res) => { const channelName = body.channelName;
const channelName = body.channelName; const channelClaimId = body.channelClaimId;
const channelClaimId = body.channelClaimId; const claimName = body.claimName;
const claimName = body.claimName; const claimId = body.claimId;
const claimId = body.claimId; getClaimId(channelName, channelClaimId, claimName, claimId)
getClaimId(channelName, channelClaimId, claimName, claimId) .then(result => {
.then(result => { if (result === NO_CHANNEL) {
if (result === NO_CHANNEL) { return res.status(404).json({success: false, message: 'No matching channel could be found'});
return res.status(404).json({success: false, message: 'No matching channel could be found'}); }
} if (result === NO_CLAIM) {
if (result === NO_CLAIM) { return res.status(404).json({success: false, message: 'No matching claim id could be found'});
return res.status(404).json({success: false, message: 'No matching claim id could be found'}); }
} res.status(200).json({success: true, data: result});
res.status(200).json({success: true, data: result}); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = claimLongId; module.exports = claimLongId;

View file

@ -11,60 +11,58 @@ const { details: { host } } = require('siteConfig.js');
*/ */
const claimPublish = (db) => { const claimPublish = ({ body, files, headers, ip, originalUrl, user }, res) => {
return ({ body, files, headers, ip, originalUrl, user }, res) => { // define variables
// define variables let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title;
let channelName, channelId, channelPassword, description, fileName, filePath, fileType, gaStartTime, license, name, nsfw, thumbnail, thumbnailFileName, thumbnailFilePath, thumbnailFileType, title; // record the start time of the request
// record the start time of the request gaStartTime = Date.now();
gaStartTime = Date.now(); // validate the body and files of the request
// validate the body and files of the request try {
try { // validateApiPublishRequest(body, files);
// validateApiPublishRequest(body, files); ({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body));
({name, nsfw, license, title, description, thumbnail} = parsePublishApiRequestBody(body)); ({fileName, filePath, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files));
({fileName, filePath, fileType, thumbnailFileName, thumbnailFilePath, thumbnailFileType} = parsePublishApiRequestFiles(files)); ({channelName, channelId, channelPassword} = body);
({channelName, channelId, channelPassword} = body); } catch (error) {
} catch (error) { return res.status(400).json({success: false, message: error.message});
return res.status(400).json({success: false, message: error.message}); }
} // check channel authorization
// check channel authorization Promise
Promise .all([
.all([ authenticateUser(channelName, channelId, channelPassword, user),
authenticateUser(channelName, channelId, channelPassword, user), claimNameIsAvailable(name),
claimNameIsAvailable(name), createBasicPublishParams(filePath, name, title, description, license, nsfw, thumbnail),
createBasicPublishParams(filePath, name, title, description, license, nsfw, thumbnail), createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw),
createThumbnailPublishParams(thumbnailFilePath, name, license, nsfw), ])
]) .then(([{channelName, channelClaimId}, validatedClaimName, publishParams, thumbnailPublishParams]) => {
.then(([{channelName, channelClaimId}, validatedClaimName, publishParams, thumbnailPublishParams]) => { // add channel details to the publish params
// add channel details to the publish params if (channelName && channelClaimId) {
if (channelName && channelClaimId) { publishParams['channel_name'] = channelName;
publishParams['channel_name'] = channelName; publishParams['channel_id'] = channelClaimId;
publishParams['channel_id'] = channelClaimId; }
} // publish the thumbnail
// publish the thumbnail if (thumbnailPublishParams) {
if (thumbnailPublishParams) { publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType);
publish(thumbnailPublishParams, thumbnailFileName, thumbnailFileType); }
} // publish the asset
// publish the asset return publish(publishParams, fileName, fileType);
return publish(publishParams, fileName, fileType); })
}) .then(result => {
.then(result => { res.status(200).json({
res.status(200).json({ success: true,
success: true, message: 'publish completed successfully',
message: 'publish completed successfully', data : {
data : { name,
name, claimId: result.claim_id,
claimId: result.claim_id, url : `${host}/${result.claim_id}/${name}`,
url : `${host}/${result.claim_id}/${name}`, lbryTx : result,
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);
}); });
}; // 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; module.exports = claimPublish;

View file

@ -7,16 +7,14 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const claimResolve = () => { const claimResolve = ({ headers, ip, originalUrl, params }, res) => {
return ({ headers, ip, originalUrl, params }, res) => { resolveUri(`${params.name}#${params.claimId}`)
resolveUri(`${params.name}#${params.claimId}`) .then(resolvedUri => {
.then(resolvedUri => { res.status(200).json(resolvedUri);
res.status(200).json(resolvedUri); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = claimResolve; module.exports = claimResolve;

View file

@ -1,4 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js'); const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/* /*
@ -6,16 +7,14 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const claimShortId = (db) => { const claimShortId = ({ ip, originalUrl, body, params }, res) => {
return ({ ip, originalUrl, body, params }, res) => { db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name) .then(shortId => {
.then(shortId => { res.status(200).json({success: true, data: shortId});
res.status(200).json({success: true, data: shortId}); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = claimShortId; module.exports = claimShortId;

View file

@ -1,5 +1,5 @@
const { handleErrorResponse } = require('helpers/errorHandlers.js'); const { handleErrorResponse } = require('helpers/errorHandlers.js');
const db = require('models');
/* /*
@ -7,27 +7,25 @@ const { handleErrorResponse } = require('helpers/errorHandlers.js');
*/ */
const fileAvailability = (db) => { const fileAvailability = ({ ip, originalUrl, params }, res) => {
return ({ ip, originalUrl, params }, res) => { const name = params.name;
const name = params.name; const claimId = params.claimId;
const claimId = params.claimId; db.File
db.File .findOne({
.findOne({ where: {
where: { name,
name, claimId,
claimId, },
}, })
}) .then(result => {
.then(result => { if (result) {
if (result) { return res.status(200).json({success: true, data: true});
return res.status(200).json({success: true, data: true}); }
} res.status(200).json({success: true, data: false});
res.status(200).json({success: true, data: false}); })
}) .catch(error => {
.catch(error => { handleErrorResponse(originalUrl, ip, error, res);
handleErrorResponse(originalUrl, ip, error, res); });
});
};
}; };
module.exports = fileAvailability; module.exports = fileAvailability;

View file

@ -12,18 +12,23 @@ const claimShortId = require('./claimShortId');
const claimList = require('./claimList'); const claimList = require('./claimList');
const fileAvailability = require('./fileAvailability'); const fileAvailability = require('./fileAvailability');
module.exports = { const multipartMiddleware = require('helpers/multipartMiddleware');
channelAvailability,
channelClaims, module.exports = (app) => {
channelData, // channel routes
channelShortId, app.get('/api/channel/availability/:name', channelAvailability);
claimAvailability, app.get('/api/channel/short-id/:longId/:name', channelShortId);
claimData, app.get('/api/channel/data/:channelName/:channelClaimId', channelData);
claimGet, app.get('/api/channel/claims/:channelName/:channelClaimId/:page', channelClaims);
claimLongId, // claim routes
claimPublish, app.get('/api/claim/list/:name', claimList);
claimResolve, app.get('/api/claim/get/:name/:claimId', claimGet);
claimShortId, app.get('/api/claim/availability/:name', claimAvailability);
claimList, app.get('/api/claim/resolve/:name/:claimId', claimResolve);
fileAvailability, app.post('/api/claim/publish', multipartMiddleware, claimPublish);
app.get('/api/claim/short-id/:longId/:name', claimShortId);
app.post('/api/claim/long-id', claimLongId);
app.get('/api/claim/data/:claimName/:claimId', claimData);
// file routes
app.get('/api/file/availability/:name/:claimId', fileAvailability);
}; };

View file

@ -1,44 +0,0 @@
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;

View file

@ -1,55 +0,0 @@
const { sendGAServeEvent } = require('helpers/googleAnalytics');
const { determineResponseType, flipClaimNameAndIdForBackwardsCompatibility, 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 and an identifier
*/
const identifierClaim = () => {
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});
}
// parse the identifier
let isChannel, channelName, channelClaimId, claimId;
try {
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
if (!isChannel) {
[claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName);
}
// log the request data for debugging
logRequestData(responseType, claimName, channelName, claimId);
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
};
};
module.exports = identifierClaim;

View file

@ -1,8 +1,7 @@
const serveAssetByClaim = require('./serveAssetByClaim'); const serveAssetByClaim = require('./serveAssetByClaim');
const serveAssetByIdentifierAndClaim = require('./serveAssetByIdentifierAndClaim'); const serveAssetByIdentifierAndClaim = require('./serveAssetByIdentifierAndClaim');
module.exports = (app, db) => {
module.exports = { app.get('/:identifier/:claim', serveAssetByIdentifierAndClaim);
serveAssetByClaim, app.get('/:claim', serveAssetByClaim);
serveAssetByIdentifierAndClaim,
}; };

View file

@ -0,0 +1,42 @@
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 serverAssetByClaim = (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 = serverAssetByClaim;

View file

@ -0,0 +1,58 @@
const { sendGAServeEvent } = require('helpers/googleAnalytics');
const {
determineResponseType,
flipClaimNameAndIdForBackwardsCompatibility,
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 and an identifier
*/
const serverAssetByIdentifierAndClaim = (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});
}
// parse the identifier
let isChannel, channelName, channelClaimId, claimId;
try {
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(params.identifier));
} catch (error) {
return res.status(400).json({success: false, message: error.message});
}
if (!isChannel) {
[claimId, claimName] = flipClaimNameAndIdForBackwardsCompatibility(claimId, claimName);
}
// log the request data for debugging
logRequestData(responseType, claimName, channelName, claimId);
// get the claim Id and then serve the asset
getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res);
};
module.exports = serverAssetByIdentifierAndClaim;

98
server/server.js Normal file
View file

@ -0,0 +1,98 @@
// app dependencies
const express = require('express');
const bodyParser = require('body-parser');
const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars');
const helmet = require('helmet');
const passport = require('passport');
const { serializeSpeechUser, deserializeSpeechUser } = require('./helpers/authHelpers.js');
const cookieSession = require('cookie-session');
const http = require('http');
// logging dependencies
const logger = require('winston');
function Server () {
this.configureMysql = (mysqlConfig) => {
require('../config/mysqlConfig.js').configure(mysqlConfig);
};
this.configureSite = (siteConfig) => {
require('../config/siteConfig.js').configure(siteConfig);
this.sessionKey = siteConfig.auth.sessionKey;
this.PORT = siteConfig.details.port;
};
this.configureSlack = (slackConfig) => {
require('../config/slackConfig.js').configure(slackConfig);
};
this.createApp = () => {
// create an Express application
const app = express();
// trust the proxy to get ip address for us
app.enable('trust proxy');
// add middleware
app.use(helmet()); // set HTTP headers to protect against well-known web vulnerabilties
app.use(express.static(`${__dirname}/public`)); // 'express.static' to serve static files from public directory
app.use(bodyParser.json()); // 'body parser' for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // 'body parser' for parsing application/x-www-form-urlencoded
app.use((req, res, next) => { // custom logging middleware to log all incoming http requests
logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
next();
});
// configure passport
passport.serializeUser(serializeSpeechUser);
passport.deserializeUser(deserializeSpeechUser);
const localSignupStrategy = require('./passport/local-signup.js');
const localLoginStrategy = require('./passport/local-login.js');
passport.use('local-signup', localSignupStrategy);
passport.use('local-login', localLoginStrategy);
// initialize passport
app.use(cookieSession({
name : 'session',
keys : [this.sessionKey],
maxAge: 24 * 60 * 60 * 1000, // i.e. 24 hours
}));
app.use(passport.initialize());
app.use(passport.session());
// configure handlebars & register it with express app
const hbs = expressHandlebars.create({
defaultLayout: 'embed',
handlebars : Handlebars,
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// set the routes on the app
require('./routes/auth.js')(app);
require('./routes/api.js')(app);
require('./routes/pages.js')(app);
require('./routes/assets.js')(app);
require('./routes/fallback.js')(app);
this.app = app;
};
this.initialize = () => {
require('./helpers/configureLogger.js')(logger);
require('./helpers/configureSlack.js')(logger);
this.createApp();
this.server = http.Server(this.app);
};
this.start = () => {
const db = require('./models/index');
// sync sequelize
db.sequelize.sync()
// start the server
.then(() => {
this.server.listen(this.PORT, () => {
logger.info(`Server is listening on PORT ${this.PORT}`);
});
})
.catch((error) => {
logger.error(`Startup Error:`, error);
});
};
};
module.exports = Server;

View file

@ -4,9 +4,9 @@ import rootSaga from 'sagas';
import GAListener from 'components/GAListener'; import GAListener from 'components/GAListener';
const api = require('./server/routes/api/'); const api = require('./server/routes/api/');
const asset = require('./server/routes/asset/'); const asset = require('./server/routes/assets/');
const auth = require('./server/routes/auth/'); const auth = require('./server/routes/auth/');
const page = require('./server/routes/page/'); const page = require('./server/routes/pages/');
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');