updated process for getting file record in serve logic
This commit is contained in:
parent
7237899e77
commit
5230970705
7 changed files with 102 additions and 37 deletions
|
@ -2,7 +2,7 @@ const db = require('../../../../models');
|
|||
|
||||
const { handleErrorResponse } = require('../../../utils/errorHandlers.js');
|
||||
|
||||
const getClaimId = require('./getClaimId.js');
|
||||
const getClaimId = require('../../../utils/getClaimId.js');
|
||||
|
||||
const NO_CHANNEL = 'NO_CHANNEL';
|
||||
const NO_CLAIM = 'NO_CLAIM';
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
const logger = require('winston');
|
||||
const db = require('../../../models');
|
||||
|
||||
const getOEmbedData = (req, res) => {
|
||||
|
||||
const CHANNEL_CHAR = '@';
|
||||
const { query: { url, format } } = req;
|
||||
logger.debug('req url', url);
|
||||
logger.debug('req format', format);
|
||||
const lbryUri = require('../../utils/lbryUri');
|
||||
const getClaimId = require('../../utils/getClaimId');
|
||||
|
||||
const parseSpeechUrl = (url) => {
|
||||
// parse the request url
|
||||
const componentsRegex = new RegExp(
|
||||
'([^:/?#]+:\/\/)'+
|
||||
|
@ -20,18 +17,22 @@ const getOEmbedData = (req, res) => {
|
|||
const [proto, protocol, domain, slashOne, paramOne, slashTwo, paramTwo] = componentsRegex
|
||||
.exec(url)
|
||||
.map(match => match || null);
|
||||
|
||||
logger.debug(`${protocol}, ${domain}, ${slashOne}, ${paramOne}, ${slashTwo}, ${paramTwo}`);
|
||||
|
||||
// parse the request url's pieces
|
||||
// is there an identifier?
|
||||
return {
|
||||
paramOne,
|
||||
paramTwo,
|
||||
}
|
||||
};
|
||||
|
||||
// if there is an identifier, is it a channel or a claim_id?
|
||||
// if it is a channel, does it have a channel id?
|
||||
// if no identifier, is the claim a channel?
|
||||
const getOEmbedData = (req, res) => {
|
||||
|
||||
// get the data
|
||||
const { query: { url, format } } = req;
|
||||
logger.debug('req url', url);
|
||||
logger.debug('req format', format);
|
||||
|
||||
const data = {
|
||||
let data = {
|
||||
version: 1.0 ,
|
||||
author_name: 'Spee.ch',
|
||||
author_url: 'https://spee.ch',
|
||||
|
@ -40,23 +41,74 @@ const getOEmbedData = (req, res) => {
|
|||
cache_age: 86400, // one day in seconds
|
||||
};
|
||||
|
||||
// set type
|
||||
// if (fileType.substring(0, fileType.indexOf('/') === 'video')) {
|
||||
// data['type'] = 'video';
|
||||
// } else {
|
||||
// data['type'] = 'picture';
|
||||
// }
|
||||
const { paramOne, paramTwo } = parseSpeechUrl(url);
|
||||
let fileData, claimData;
|
||||
let claimName, isChannel, channelName, channelClaimId, claimId;
|
||||
|
||||
data['title'] = '';
|
||||
data['thumbnail_url'] = '';
|
||||
data['thumbnail_width'] = '';
|
||||
data['thumbnail_height'] = '';
|
||||
if (paramTwo) {
|
||||
({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(paramOne));
|
||||
({ claimName } = lbryUri.parseClaim(paramTwo));
|
||||
} else {
|
||||
({ isChannel } = lbryUri.parseIdentifier(paramOne));
|
||||
if (!isChannel ) {
|
||||
({ claimName } = lbryUri.parseClaim(paramOne));
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
message: 'hello',
|
||||
data,
|
||||
});
|
||||
logger.debug('ischannel:', isChannel);
|
||||
|
||||
if (isChannel && !paramTwo) {
|
||||
data['title'] = paramOne;
|
||||
data['thumbnail_url'] = 'test';
|
||||
data['thumbnail_width'] = 'test';
|
||||
data['thumbnail_height'] = 'test';
|
||||
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: 'hello',
|
||||
data,
|
||||
});
|
||||
|
||||
} else {
|
||||
// return claim info
|
||||
getClaimId(channelName, channelClaimId, claimName, claimId)
|
||||
.then(fullClaimId => {
|
||||
claimId = fullClaimId;
|
||||
return db.Claim.findOne({
|
||||
where: {
|
||||
name : claimName,
|
||||
claimId: fullClaimId
|
||||
}
|
||||
});
|
||||
})
|
||||
.then(claimRecord => {
|
||||
claimData = claimRecord.dataValues;
|
||||
return db.Blocked.isNotBlocked(claimData.outpoint);
|
||||
})
|
||||
.then(() => {
|
||||
return db.File.findOne({
|
||||
where: {
|
||||
name: claimName,
|
||||
claimId,
|
||||
},
|
||||
});
|
||||
})
|
||||
.then(fileRecord => {
|
||||
fileData = fileRecord.dataValues;
|
||||
logger.debug('file data:', fileData);
|
||||
// get channel data
|
||||
data['title'] = claimData.title;
|
||||
data['thumbnail_url'] = `https://dev1.spee.ch/${claimId}/${claimName}.ext`;
|
||||
data['thumbnail_width'] = fileData.width || 600;
|
||||
data['thumbnail_height'] = fileData.height || 400;
|
||||
// send response
|
||||
return res.status(200).json({
|
||||
success: true,
|
||||
message: 'hello',
|
||||
data,
|
||||
});
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = getOEmbedData;
|
||||
|
|
|
@ -3,7 +3,7 @@ const logger = require('winston');
|
|||
const { sendGAServeEvent } = require('../../../utils/googleAnalytics');
|
||||
const handleShowRender = require('../../../render/build/handleShowRender.js');
|
||||
|
||||
const lbryUri = require('../utils/lbryUri.js');
|
||||
const lbryUri = require('../../utils/lbryUri.js');
|
||||
|
||||
const determineRequestType = require('../utils/determineRequestType.js');
|
||||
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
||||
|
@ -38,9 +38,15 @@ const serveByClaim = (req, res) => {
|
|||
logger.debug('serve request:', { headers, ip, originalUrl, params });
|
||||
getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res);
|
||||
|
||||
// get claim id
|
||||
// serve asset
|
||||
|
||||
sendGAServeEvent(headers, ip, originalUrl);
|
||||
|
||||
} catch (error) {
|
||||
// check for different bubbled up error messages
|
||||
|
||||
// fallback
|
||||
return res.status(400).json({success: false, message: error.message});
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ const logger = require('winston');
|
|||
const { sendGAServeEvent } = require('../../../utils/googleAnalytics');
|
||||
const handleShowRender = require('../../../render/build/handleShowRender.js');
|
||||
|
||||
const lbryUri = require('../utils/lbryUri.js');
|
||||
const lbryUri = require('../../utils/lbryUri.js');
|
||||
|
||||
const determineRequestType = require('../utils/determineRequestType.js');
|
||||
const getClaimIdAndServeAsset = require('../utils/getClaimIdAndServeAsset.js');
|
||||
|
|
|
@ -2,10 +2,9 @@ const logger = require('winston');
|
|||
|
||||
const db = require('../../../models');
|
||||
|
||||
const getClaimId = require('../../api/claim/longId/getClaimId.js');
|
||||
const getClaimId = require('../../utils/getClaimId.js');
|
||||
const { handleErrorResponse } = require('../../utils/errorHandlers.js');
|
||||
|
||||
const getLocalFileRecord = require('./getLocalFileRecord.js');
|
||||
const serveFile = require('./serveFile.js');
|
||||
|
||||
const NO_CHANNEL = 'NO_CHANNEL';
|
||||
|
@ -25,10 +24,18 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId
|
|||
return db.Blocked.isNotBlocked(outpoint);
|
||||
})
|
||||
.then(() => {
|
||||
return getLocalFileRecord(claimId, claimName);
|
||||
return db.File.findOne({
|
||||
where: {
|
||||
claimId,
|
||||
name: claimName,
|
||||
},
|
||||
});
|
||||
})
|
||||
.then(fileRecord => {
|
||||
serveFile(fileRecord, res);
|
||||
if (!fileRecord) {
|
||||
throw NO_FILE;
|
||||
}
|
||||
serveFile(fileRecord.dataValues, res);
|
||||
})
|
||||
.catch(error => {
|
||||
if (error === NO_CLAIM) {
|
||||
|
@ -53,7 +60,7 @@ const getClaimIdAndServeAsset = (channelName, channelClaimId, claimName, claimId
|
|||
});
|
||||
}
|
||||
if (error === NO_FILE) {
|
||||
logger.debug('claim was blocked');
|
||||
logger.debug('no file available');
|
||||
return res.status(307).redirect(`/api/claim/get/${name}/${claimId}`);
|
||||
}
|
||||
handleErrorResponse(originalUrl, ip, error, res);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const logger = require('winston');
|
||||
|
||||
const db = require('../../../../models');
|
||||
const db = require('../../models/index');
|
||||
|
||||
const getClaimIdByChannel = (channelName, channelClaimId, claimName) => {
|
||||
return new Promise((resolve, reject) => {
|
Loading…
Add table
Reference in a new issue