updated claimid/example route
This commit is contained in:
parent
cc4728e0ba
commit
369399ca98
2 changed files with 90 additions and 64 deletions
|
@ -37,7 +37,7 @@ module.exports = {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
serveFile ({ filePath }, { claimId, name, contentType }, shortId, res) {
|
serveFile ({ filePath }, { claimId, name, contentType }, res) {
|
||||||
logger.verbose(`serving ${name}#${claimId}`);
|
logger.verbose(`serving ${name}#${claimId}`);
|
||||||
// set response options
|
// set response options
|
||||||
const headerContentType = contentType || 'image/jpeg';
|
const headerContentType = contentType || 'image/jpeg';
|
||||||
|
@ -55,11 +55,11 @@ module.exports = {
|
||||||
res.status(400).json({success: false, message: 'that claim is not hosted locally by Spee<ch yet'});
|
res.status(400).json({success: false, message: 'that claim is not hosted locally by Spee<ch yet'});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
showFile (fileInfo, claimInfo, shortId, res) {
|
showFile (claimInfo, shortId, res) {
|
||||||
const openGraphInfo = createOpenGraphInfo(claimInfo);
|
const openGraphInfo = createOpenGraphInfo(claimInfo);
|
||||||
res.status(200).render('show', { layout: 'show', claimInfo, shortId, openGraphInfo });
|
res.status(200).render('show', { layout: 'show', claimInfo, shortId, openGraphInfo });
|
||||||
},
|
},
|
||||||
showFileLite (fileInfo, claimInfo, shortId, res) {
|
showFileLite (claimInfo, shortId, res) {
|
||||||
const openGraphInfo = createOpenGraphInfo(claimInfo);
|
const openGraphInfo = createOpenGraphInfo(claimInfo);
|
||||||
res.status(200).render('showLite', { layout: 'showlite', claimInfo, shortId, openGraphInfo });
|
res.status(200).render('showLite', { layout: 'showlite', claimInfo, shortId, openGraphInfo });
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
const logger = require('winston');
|
const logger = require('winston');
|
||||||
const { getClaimId, getChannelContents, getLocalFileRecord, getClaimRecord } = require('../controllers/serveController.js');
|
const { getClaimId, getChannelContents, getLocalFileRecord, getClaimRecord } = require('../controllers/serveController.js');
|
||||||
const { serveOrShowAsset } = require('../helpers/serveHelpers.js');
|
const serveHelpers = require('../helpers/serveHelpers.js');
|
||||||
const { handleRequestError } = require('../helpers/errorHandlers.js');
|
const { handleRequestError } = require('../helpers/errorHandlers.js');
|
||||||
const db = require('../models');
|
const db = require('../models');
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ function isValidClaimId (claimId) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidShortId (claimId) {
|
function isValidShortId (claimId) {
|
||||||
return claimId.length === 1; // really it should evaluate the short url itself
|
return claimId.length === 1; // it should really evaluate the short url itself
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidShortIdOrClaimId (input) {
|
function isValidShortIdOrClaimId (input) {
|
||||||
|
@ -127,7 +127,7 @@ function sendChannelContentsToClient (result, query, res) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function respondWithChannelPageToClient (uri, originalUrl, ip, query, res) {
|
function showChannelPageToClient (uri, originalUrl, ip, query, res) {
|
||||||
let channelName = returnChannelNameFromUri(uri);
|
let channelName = returnChannelNameFromUri(uri);
|
||||||
logger.debug('channel name =', channelName);
|
logger.debug('channel name =', channelName);
|
||||||
let channelId = returnChannelIdFromUri(uri);
|
let channelId = returnChannelIdFromUri(uri);
|
||||||
|
@ -163,41 +163,67 @@ function determineFileExtension (uri) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function determineName (uri) {
|
function determineName (uri) {
|
||||||
|
/* patch because twitter player preview adds '>' before file extension. Note: put this inside determineName()? */
|
||||||
|
if (uri.indexOf('>') !== -1) {
|
||||||
|
return uri.substring(0, uri.indexOf('>'));
|
||||||
|
}
|
||||||
|
/* end patch */
|
||||||
if (uri.indexOf('.') !== -1) {
|
if (uri.indexOf('.') !== -1) {
|
||||||
return uri.substring(0, uri.indexOf('.'));
|
return uri.substring(0, uri.indexOf('.'));
|
||||||
}
|
}
|
||||||
return uri;
|
return uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function showAssetToClient (claimId, name, res) {
|
||||||
|
// check for local file info, resolve the claim, and get the short
|
||||||
|
return Promise
|
||||||
|
.all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
|
||||||
|
.then(([claimInfo, shortClaimId]) => {
|
||||||
|
logger.debug('claimInfo:', claimInfo);
|
||||||
|
logger.debug('shortClaimId:', shortClaimId);
|
||||||
|
return serveHelpers.showFile(claimInfo, shortClaimId, res);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function showPlainAssetToClient (claimId, name, res) {
|
||||||
|
return Promise
|
||||||
|
.all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
|
||||||
|
.then(([claimInfo, shortClaimId]) => {
|
||||||
|
logger.debug('claimInfo:', claimInfo);
|
||||||
|
logger.debug('shortClaimId:', shortClaimId);
|
||||||
|
return serveHelpers.showFileLite(claimInfo, shortClaimId, res);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function serveAssetToClient (claimId, name, res) {
|
||||||
|
return getLocalFileRecord(claimId, name)
|
||||||
|
.then(fileInfo => {
|
||||||
|
logger.debug('fileInfo:', fileInfo);
|
||||||
|
if (fileInfo) {
|
||||||
|
return serveHelpers.serveFile(fileInfo, res);
|
||||||
|
} else {
|
||||||
|
return res.status(307).json({status: 'success', message: 'resource temporarily unavailable'});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
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 or claim id
|
let identifier = params.identifier; // identifier will either be a @channel, @channel:channelId, or claimId
|
||||||
let name = params.name;
|
let name = params.name; // name will be example or example.ext
|
||||||
let channelName = null;
|
let channelName = null;
|
||||||
let claimId = null;
|
let claimId = null;
|
||||||
let channelId = null;
|
let channelId = null;
|
||||||
let method;
|
|
||||||
let fileExtension;
|
|
||||||
// parse the name
|
|
||||||
const positionOfExtension = name.indexOf('.');
|
|
||||||
if (positionOfExtension >= 0) {
|
|
||||||
fileExtension = name.substring(positionOfExtension + 1);
|
|
||||||
name = name.substring(0, positionOfExtension);
|
|
||||||
/* patch because twitter player preview adds '>' before file extension */
|
|
||||||
if (name.indexOf('>') >= 0) {
|
|
||||||
name = name.substring(0, name.indexOf('>'));
|
|
||||||
}
|
|
||||||
/* end patch */
|
|
||||||
logger.debug('file extension =', fileExtension);
|
|
||||||
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
|
||||||
method = SHOWLITE;
|
|
||||||
} else {
|
|
||||||
method = SERVE;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
method = SHOW;
|
|
||||||
}
|
|
||||||
/* patch for backwards compatability with spee.ch/name/claim_id */
|
/* patch for backwards compatability with spee.ch/name/claim_id */
|
||||||
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
if (isValidShortIdOrClaimId(name) && !isValidShortIdOrClaimId(identifier)) {
|
||||||
let tempName = name;
|
let tempName = name;
|
||||||
|
@ -205,8 +231,12 @@ module.exports = (app) => {
|
||||||
identifier = tempName;
|
identifier = tempName;
|
||||||
}
|
}
|
||||||
/* end patch */
|
/* end patch */
|
||||||
logger.debug('claim name =', name);
|
let responseType = determineResponseType(name, headers);
|
||||||
logger.debug('method =', method);
|
logger.debug('responseType ==', responseType);
|
||||||
|
let fileExtension = determineFileExtension(name);
|
||||||
|
logger.debug('file extension ==', fileExtension);
|
||||||
|
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);
|
||||||
|
@ -227,15 +257,17 @@ module.exports = (app) => {
|
||||||
res.status(200).render('noChannel');
|
res.status(200).render('noChannel');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// check for local file info and resolve the claim
|
// show, showlite, or serve
|
||||||
return Promise.all([getLocalFileRecord(result, name), getClaimRecord(result, name), db.Claim.getShortClaimIdFromLongClaimId(result, name)]);
|
switch (responseType) {
|
||||||
})
|
case SERVE:
|
||||||
.then(([fileInfo, claimInfo, shortClaimId]) => {
|
return showAssetToClient(claimId, name, res);
|
||||||
logger.debug(`file record:`, fileInfo);
|
case SHOWLITE:
|
||||||
logger.debug('claim record:', claimInfo);
|
return showPlainAssetToClient(claimId, name, res);
|
||||||
logger.debug('short url:', shortClaimId);
|
case SHOW:
|
||||||
// serve or show
|
return serveAssetToClient(claimId, name, res);
|
||||||
return serveOrShowAsset(method, fileInfo, claimInfo, shortClaimId, res);
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
// 3. update the file
|
// 3. update the file
|
||||||
.then(fileInfoForUpdate => {
|
.then(fileInfoForUpdate => {
|
||||||
|
@ -251,7 +283,7 @@ module.exports = (app) => {
|
||||||
let uri = params.uri;
|
let uri = params.uri;
|
||||||
// (a) handle channel requests
|
// (a) handle channel requests
|
||||||
if (isUriAChannel(uri)) {
|
if (isUriAChannel(uri)) {
|
||||||
respondWithChannelPageToClient(uri, originalUrl, ip, query, res);
|
showChannelPageToClient(uri, originalUrl, ip, query, res);
|
||||||
// (b) handle stream requests
|
// (b) handle stream requests
|
||||||
} else {
|
} else {
|
||||||
let responseType = determineResponseType(uri, headers);
|
let responseType = determineResponseType(uri, headers);
|
||||||
|
@ -262,36 +294,30 @@ module.exports = (app) => {
|
||||||
logger.debug('claim name == ', name);
|
logger.debug('claim name == ', name);
|
||||||
// get the claim id
|
// get the claim id
|
||||||
getClaimId(null, null, name, null)
|
getClaimId(null, null, name, null)
|
||||||
.then(result => {
|
.then(claimId => {
|
||||||
logger.debug('getClaimId result:', result);
|
logger.debug('getClaimId result:', claimId);
|
||||||
if (result === NO_CLAIM || result === NO_CHANNEL) {
|
// if no claim id found, skip
|
||||||
return result;
|
if (claimId === NO_CLAIM) {
|
||||||
}
|
|
||||||
// check for local file info and resolve the claim
|
|
||||||
return Promise.all([getLocalFileRecord(result, name), getClaimRecord(result, name), db.Claim.getShortClaimIdFromLongClaimId(result, name)]);
|
|
||||||
})
|
|
||||||
.then(result => {
|
|
||||||
if (result === NO_CLAIM) {
|
|
||||||
res.status(200).render('noClaim');
|
res.status(200).render('noClaim');
|
||||||
return;
|
return;
|
||||||
} else if (result === NO_CHANNEL) {
|
|
||||||
res.status(200).render('noChannel');
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
let fileInfo, claimInfo, shortClaimId;
|
// show, showlite, or serve
|
||||||
[fileInfo, claimInfo, shortClaimId] = result;
|
switch (responseType) {
|
||||||
logger.debug(`fileInfo:`, fileInfo);
|
case SERVE:
|
||||||
logger.debug('claimInfo:', claimInfo);
|
return showAssetToClient(claimId, name, res);
|
||||||
logger.debug('shortClaimId:', shortClaimId);
|
case SHOWLITE:
|
||||||
// serve or show
|
return showPlainAssetToClient(claimId, name, res);
|
||||||
return serveOrShowAsset(responseType, fileInfo, claimInfo, shortClaimId, res);
|
case SHOW:
|
||||||
|
return serveAssetToClient(claimId, name, res);
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
})
|
})
|
||||||
// 3. update the file
|
|
||||||
.then(fileInfoForUpdate => {
|
.then(fileInfoForUpdate => {
|
||||||
// if needed, this is where we would update the file
|
// if needed, this is where we would update the file
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
handleRequestError('serve', originalUrl, ip, error, res);
|
handleRequestError(responseType, originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue