consolidated code in serve-routes
This commit is contained in:
parent
0fd7dd6b08
commit
4d2cf09f37
1 changed files with 39 additions and 60 deletions
|
@ -156,13 +156,6 @@ function determineResponseType (uri, headers) {
|
||||||
return responseType;
|
return responseType;
|
||||||
}
|
}
|
||||||
|
|
||||||
function determineFileExtension (uri) {
|
|
||||||
if (uri.indexOf('.') !== -1) {
|
|
||||||
return uri.substring(uri.indexOf('.') + 1);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function determineName (uri) {
|
function determineName (uri) {
|
||||||
/* patch because twitter player preview adds '>' before file extension. Note: put this inside determineName()? */
|
/* patch because twitter player preview adds '>' before file extension. Note: put this inside determineName()? */
|
||||||
if (uri.indexOf('>') !== -1) {
|
if (uri.indexOf('>') !== -1) {
|
||||||
|
@ -217,61 +210,64 @@ function serveAssetToClient (claimId, name, res) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function showOrServeAsset (responseType, claimId, claimName, res) {
|
||||||
|
switch (responseType) {
|
||||||
|
case SHOW:
|
||||||
|
return showAssetToClient(claimId, claimName, res);
|
||||||
|
case SHOWLITE:
|
||||||
|
return showPlainAssetToClient(claimId, claimName, res);
|
||||||
|
case SERVE:
|
||||||
|
return serveAssetToClient(claimId, claimName, res);
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function flipClaimNameAndIdForBackwardsCompatibility (identifier, name) {
|
||||||
|
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
||||||
|
const tempName = name;
|
||||||
|
name = identifier;
|
||||||
|
identifier = tempName;
|
||||||
|
}
|
||||||
|
return [identifier, name];
|
||||||
|
}
|
||||||
|
|
||||||
|
function logRequestData (responseType, claimName, channelName, claimId) {
|
||||||
|
logger.debug('responseType ===', responseType);
|
||||||
|
logger.debug('claim name === ', claimName);
|
||||||
|
logger.debug('channel name ===', channelName);
|
||||||
|
logger.debug('claim id ===', claimId);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = (app) => {
|
module.exports = (app) => {
|
||||||
// route to serve a specific asset
|
// route to serve a specific asset
|
||||||
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
|
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
|
||||||
let identifier = params.identifier; // identifier will either be a @channel, @channel:channelId, or claimId
|
let identifier = params.identifier; // identifier will either be a @channel, @channel:channelId, or claimId
|
||||||
let name = params.name; // name will be example or example.ext
|
let name = params.name; // name will be example or example.ext
|
||||||
|
[identifier, name] = flipClaimNameAndIdForBackwardsCompatibility(identifier, name); // patch for backwards compatability with spee.ch/name/claim_id
|
||||||
let channelName = null;
|
let channelName = null;
|
||||||
let claimId = null;
|
let claimId = null;
|
||||||
let channelId = null;
|
let channelId = null;
|
||||||
/* patch for backwards compatability with spee.ch/name/claim_id */
|
|
||||||
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
|
||||||
let tempName = name;
|
|
||||||
name = identifier;
|
|
||||||
identifier = tempName;
|
|
||||||
}
|
|
||||||
/* end patch */
|
|
||||||
let responseType = determineResponseType(name, headers);
|
let responseType = determineResponseType(name, headers);
|
||||||
logger.debug('responseType ==', responseType);
|
|
||||||
let fileExtension = determineFileExtension(name);
|
|
||||||
logger.debug('file extension ==', fileExtension);
|
|
||||||
let claimName = determineName(name);
|
let claimName = determineName(name);
|
||||||
logger.debug('claim name == ', claimName);
|
|
||||||
// parse identifier for whether it is a channel, short url, or claim_id
|
// parse identifier for whether it is a channel, short url, or claim_id
|
||||||
if (isUriAChannel(identifier)) {
|
if (isUriAChannel(identifier)) {
|
||||||
channelName = returnChannelNameFromUri(identifier);
|
channelName = returnChannelNameFromUri(identifier);
|
||||||
channelId = returnChannelIdFromUri(identifier);
|
channelId = returnChannelIdFromUri(identifier);
|
||||||
logger.debug('channel name =', channelName);
|
|
||||||
} else {
|
} else {
|
||||||
claimId = identifier;
|
claimId = identifier;
|
||||||
logger.debug('claim id =', claimId);
|
|
||||||
}
|
}
|
||||||
|
// consolidated logging
|
||||||
|
logRequestData(responseType, claimName, channelName, claimId);
|
||||||
// get the claim id
|
// get the claim id
|
||||||
getClaimId(channelName, channelId, claimName, claimId)
|
getClaimId(channelName, channelId, claimName, claimId)
|
||||||
.then(fullClaimId => {
|
.then(fullClaimId => {
|
||||||
if (fullClaimId === NO_CLAIM) {
|
if (fullClaimId === NO_CLAIM) {
|
||||||
res.status(200).render('noClaim');
|
return res.status(200).render('noClaim');
|
||||||
return;
|
|
||||||
} else if (fullClaimId === NO_CHANNEL) {
|
} else if (fullClaimId === NO_CHANNEL) {
|
||||||
res.status(200).render('noChannel');
|
return res.status(200).render('noChannel');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// show, showlite, or serve
|
showOrServeAsset(responseType, fullClaimId, claimName, res);
|
||||||
switch (responseType) {
|
|
||||||
case SHOW:
|
|
||||||
return showAssetToClient(fullClaimId, claimName, res);
|
|
||||||
case SHOWLITE:
|
|
||||||
return showPlainAssetToClient(fullClaimId, claimName, res);
|
|
||||||
case SERVE:
|
|
||||||
return serveAssetToClient(fullClaimId, claimName, res);
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// 3. update the file
|
|
||||||
.then(fileInfoForUpdate => {
|
|
||||||
// if needed, this is where we would update the file
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
handleRequestError('serve', originalUrl, ip, error, res);
|
handleRequestError('serve', originalUrl, ip, error, res);
|
||||||
|
@ -287,33 +283,16 @@ module.exports = (app) => {
|
||||||
// (b) handle stream requests
|
// (b) handle stream requests
|
||||||
} else {
|
} else {
|
||||||
let responseType = determineResponseType(uri, headers);
|
let responseType = determineResponseType(uri, headers);
|
||||||
logger.debug('responseType ==', responseType);
|
|
||||||
let fileExtension = determineFileExtension(uri);
|
|
||||||
logger.debug('file extension ==', fileExtension);
|
|
||||||
let claimName = determineName(uri);
|
let claimName = determineName(uri);
|
||||||
logger.debug('claim name == ', claimName);
|
// consolidated logging
|
||||||
|
logRequestData(responseType, claimName, null, null);
|
||||||
// get the claim id
|
// get the claim id
|
||||||
getClaimId(null, null, claimName, null)
|
getClaimId(null, null, claimName, null)
|
||||||
.then(fullClaimId => {
|
.then(fullClaimId => {
|
||||||
// if no claim id found, skip
|
|
||||||
if (fullClaimId === NO_CLAIM) {
|
if (fullClaimId === NO_CLAIM) {
|
||||||
res.status(200).render('noClaim');
|
return res.status(200).render('noClaim');
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
// show, showlite, or serve
|
showOrServeAsset(responseType, fullClaimId, claimName, res);
|
||||||
switch (responseType) {
|
|
||||||
case SHOW:
|
|
||||||
return showAssetToClient(fullClaimId, claimName, res);
|
|
||||||
case SHOWLITE:
|
|
||||||
return showPlainAssetToClient(fullClaimId, claimName, res);
|
|
||||||
case SERVE:
|
|
||||||
return serveAssetToClient(fullClaimId, claimName, res);
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.then(fileInfoForUpdate => {
|
|
||||||
// if needed, this is where we would update the file
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
handleRequestError(responseType, originalUrl, ip, error, res);
|
handleRequestError(responseType, originalUrl, ip, error, res);
|
||||||
|
|
Loading…
Reference in a new issue