From 99cdfc0c8d7ee4162edfefad9aad8acb70f7647b Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 27 Jul 2018 15:36:55 -0700 Subject: [PATCH 01/15] rebuilt client --- client/build/containers/SEO/view.js | 13 +++++++---- client/build/utils/oEmbed.js | 36 +++++++++++++++++++++++++++++ client/src/containers/SEO/view.jsx | 17 +++++++++++--- client/src/utils/oEmbed.js | 29 +++++++++++++++++++++++ 4 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 client/build/utils/oEmbed.js create mode 100644 client/src/utils/oEmbed.js diff --git a/client/build/containers/SEO/view.js b/client/build/containers/SEO/view.js index 16b25365..d2153976 100644 --- a/client/build/containers/SEO/view.js +++ b/client/build/containers/SEO/view.js @@ -11,10 +11,14 @@ var _reactHelmet = _interopRequireDefault(require("react-helmet")); var _propTypes = _interopRequireDefault(require("prop-types")); +var _siteConfig = _interopRequireDefault(require("@config/siteConfig.json")); + var _createPageTitle = _interopRequireDefault(require("../../utils/createPageTitle")); var _createMetaTags = _interopRequireDefault(require("../../utils/createMetaTags")); +var _oEmbed = _interopRequireDefault(require("../../utils/oEmbed.js")); + var _createCanonicalLink = _interopRequireDefault(require("../../utils/createCanonicalLink")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } @@ -37,6 +41,8 @@ function _assertThisInitialized(self) { if (self === void 0) { throw new Referen function _getPrototypeOf(o) { _getPrototypeOf = Object.getPrototypeOf || function _getPrototypeOf(o) { return o.__proto__; }; return _getPrototypeOf(o); } +var host = _siteConfig.default.details.host; + var SEO = /*#__PURE__*/ function (_React$Component) { @@ -61,15 +67,15 @@ function (_React$Component) { asset: asset, channel: channel }); - var canonicalLink = (0, _createCanonicalLink.default)(asset, channel, pageUri); // render results + var cannonicalLink = (0, _createCanonicalLink.default)(asset, channel, pageUri); // render results return _react.default.createElement(_reactHelmet.default, { title: pageTitle, meta: metaTags, link: [{ rel: 'canonical', - href: canonicalLink - }] + href: cannonicalLink + }, _oEmbed.default.json(host, cannonicalLink), _oEmbed.default.xml(host, cannonicalLink)] }); } }]); @@ -79,7 +85,6 @@ function (_React$Component) { return SEO; }(_react.default.Component); -; SEO.propTypes = { pageTitle: _propTypes.default.string, pageUri: _propTypes.default.string, diff --git a/client/build/utils/oEmbed.js b/client/build/utils/oEmbed.js new file mode 100644 index 00000000..a1d7c1ab --- /dev/null +++ b/client/build/utils/oEmbed.js @@ -0,0 +1,36 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; +var rel = 'alternate'; +var title = 'spee.ch oEmbed profile'; + +var formatUrlForQuery = function formatUrlForQuery(url) { + return url.replace(/\/+/, '%2F').replace(/:/, '%3A'); +}; + +var createJsonLinkData = function createJsonLinkData(host, canonicalUrl) { + return { + rel: rel, + type: 'application/json+oembed', + href: "".concat(host, "/api/oembed?url=").concat(formatUrlForQuery(canonicalUrl), "%2F&format=json"), + title: title + }; +}; + +var createXmlLinkData = function createXmlLinkData(host, canonicalUrl) { + return { + rel: rel, + type: 'application/xml+oembed', + href: "".concat(host, "/api/oembed?").concat(formatUrlForQuery(canonicalUrl), "%2F&format=xml"), + title: title + }; +}; + +var _default = { + json: createJsonLinkData, + xml: createXmlLinkData +}; +exports.default = _default; \ No newline at end of file diff --git a/client/src/containers/SEO/view.jsx b/client/src/containers/SEO/view.jsx index 85574695..5ca6dcf9 100644 --- a/client/src/containers/SEO/view.jsx +++ b/client/src/containers/SEO/view.jsx @@ -2,10 +2,14 @@ import React from 'react'; import Helmet from 'react-helmet'; import PropTypes from 'prop-types'; +import siteConfig from '@config/siteConfig.json'; import createPageTitle from '../../utils/createPageTitle'; import createMetaTags from '../../utils/createMetaTags'; +import oEmbed from '../../utils/oEmbed.js'; import createCanonicalLink from '../../utils/createCanonicalLink'; +const { details: { host } } = siteConfig; + class SEO extends React.Component { render () { // props from parent @@ -17,17 +21,24 @@ class SEO extends React.Component { asset, channel, }); - const canonicalLink = createCanonicalLink(asset, channel, pageUri); + const cannonicalLink = createCanonicalLink(asset, channel, pageUri) // render results return ( ); } -}; +} SEO.propTypes = { pageTitle: PropTypes.string, diff --git a/client/src/utils/oEmbed.js b/client/src/utils/oEmbed.js new file mode 100644 index 00000000..9d8b2296 --- /dev/null +++ b/client/src/utils/oEmbed.js @@ -0,0 +1,29 @@ +const rel = 'alternate'; +const title = 'spee.ch oEmbed profile'; + +const formatUrlForQuery = (url) => { + return url.replace(/\/+/, '%2F').replace(/:/, '%3A'); +}; + +const createJsonLinkData = (host, canonicalUrl) => { + return { + rel, + type: 'application/json+oembed', + href: `${host}/api/oembed?url=${formatUrlForQuery(canonicalUrl)}%2F&format=json`, + title, + }; +}; + +const createXmlLinkData = (host, canonicalUrl) => { + return { + rel, + type: 'application/xml+oembed', + href: `${host}/api/oembed?${formatUrlForQuery(canonicalUrl)}%2F&format=xml`, + title, + }; +}; + +export default { + json: createJsonLinkData, + xml : createXmlLinkData, +}; From 67e7a6f5b9a97628289b72ee50ba0c238789bf75 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 27 Jul 2018 15:56:56 -0700 Subject: [PATCH 02/15] fixed regex and oebed url creator --- client/build/utils/oEmbed.js | 4 ++-- client/src/utils/oEmbed.js | 4 ++-- server/controllers/api/oEmbed/index.js | 28 ++++++++++++++++++++++++++ server/routes/api/index.js | 3 +++ 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 server/controllers/api/oEmbed/index.js diff --git a/client/build/utils/oEmbed.js b/client/build/utils/oEmbed.js index a1d7c1ab..fc276f25 100644 --- a/client/build/utils/oEmbed.js +++ b/client/build/utils/oEmbed.js @@ -8,7 +8,7 @@ var rel = 'alternate'; var title = 'spee.ch oEmbed profile'; var formatUrlForQuery = function formatUrlForQuery(url) { - return url.replace(/\/+/, '%2F').replace(/:/, '%3A'); + return url.replace(/\//g, '%2F').replace(/:/g, '%3A'); }; var createJsonLinkData = function createJsonLinkData(host, canonicalUrl) { @@ -24,7 +24,7 @@ var createXmlLinkData = function createXmlLinkData(host, canonicalUrl) { return { rel: rel, type: 'application/xml+oembed', - href: "".concat(host, "/api/oembed?").concat(formatUrlForQuery(canonicalUrl), "%2F&format=xml"), + href: "".concat(host, "/api/oembed?url=").concat(formatUrlForQuery(canonicalUrl), "%2F&format=xml"), title: title }; }; diff --git a/client/src/utils/oEmbed.js b/client/src/utils/oEmbed.js index 9d8b2296..796df5e6 100644 --- a/client/src/utils/oEmbed.js +++ b/client/src/utils/oEmbed.js @@ -2,7 +2,7 @@ const rel = 'alternate'; const title = 'spee.ch oEmbed profile'; const formatUrlForQuery = (url) => { - return url.replace(/\/+/, '%2F').replace(/:/, '%3A'); + return url.replace(/\//g, '%2F').replace(/:/g, '%3A'); }; const createJsonLinkData = (host, canonicalUrl) => { @@ -18,7 +18,7 @@ const createXmlLinkData = (host, canonicalUrl) => { return { rel, type: 'application/xml+oembed', - href: `${host}/api/oembed?${formatUrlForQuery(canonicalUrl)}%2F&format=xml`, + href: `${host}/api/oembed?url=${formatUrlForQuery(canonicalUrl)}%2F&format=xml`, title, }; }; diff --git a/server/controllers/api/oEmbed/index.js b/server/controllers/api/oEmbed/index.js new file mode 100644 index 00000000..35182912 --- /dev/null +++ b/server/controllers/api/oEmbed/index.js @@ -0,0 +1,28 @@ +const logger = require('winston'); +const db = require('../../../models'); + +const getOEmbedData = (req, res) => { + logger.debug('req', req.query); + res.status(200).json({ + success: true, + message: 'hello', + data: req.query, + }); + // db.Blocked.refreshTable() + // .then(data => { + // logger.info('finished updating blocked content list'); + // res.status(200).json({ + // success: true, + // data, + // }); + // }) + // .catch(error => { + // logger.error(error); + // res.status(500).json({ + // success: false, + // error, + // }); + // }); +}; + +module.exports = getOEmbedData; diff --git a/server/routes/api/index.js b/server/routes/api/index.js index bb24e5b2..349e2ff3 100644 --- a/server/routes/api/index.js +++ b/server/routes/api/index.js @@ -19,6 +19,7 @@ const userPassword = require('../../controllers/api/user/password'); const publishingConfig = require('../../controllers/api/config/site/publishing'); const getTorList = require('../../controllers/api/tor'); const getBlockedList = require('../../controllers/api/blocked'); +const getOEmbedData = require('../../controllers/api/oEmbed'); module.exports = (app) => { @@ -46,4 +47,6 @@ module.exports = (app) => { app.get('/api/tor', torCheckMiddleware, getTorList); // blocked app.get('/api/blocked', torCheckMiddleware, getBlockedList); + // open embed + app.get('/api/oembed', torCheckMiddleware, getOEmbedData) }; From 3d7fa83e78ce1ccdd56f29da379622d43d9d8005 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Fri, 27 Jul 2018 18:40:57 -0700 Subject: [PATCH 03/15] working on regex --- README.md | 18 +++++++ server/controllers/api/oEmbed/index.js | 68 +++++++++++++++++++------- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c883e6b8..2162aaf3 100644 --- a/README.md +++ b/README.md @@ -80,3 +80,21 @@ Issues with lbry (e.g. the spee.ch wallet, lbrynet configuration, etc.) that req * client * [react](https://reactjs.org/) +### URL formats +Below is a list of all possible urls for the content on spee.ch +* controlling, free `LBRY` claim + * spee.ch/claim (show) + * spee.ch/claim.ext (serve) +* specific `LBRY` claim + * spee.ch/claim_id/claim + * spee.ch/claim_id/claim.ext +* all free contents for the controlling `LBRY` channel + * spee.ch/@channel +* a specific `LBRY` channel + * spee.ch/@channel:channel_id +* a specific claim within the controlling `LBRY` channel + * spee.ch/@channel/claim (show) + * spee.ch/@channel/claim.ext (serve) +* a specific claim within a specific `LBRY` channel + * spee.ch/@channel:channel_id/claim + * spee.ch/@channel:channel_id/claim.ext diff --git a/server/controllers/api/oEmbed/index.js b/server/controllers/api/oEmbed/index.js index 35182912..606e1026 100644 --- a/server/controllers/api/oEmbed/index.js +++ b/server/controllers/api/oEmbed/index.js @@ -2,27 +2,61 @@ const logger = require('winston'); const db = require('../../../models'); const getOEmbedData = (req, res) => { - logger.debug('req', req.query); + + const CHANNEL_CHAR = '@'; + const { query: { url, format } } = req; + logger.debug('req url', url); + logger.debug('req format', format); + + // parse the request url + const componentsRegex = new RegExp( + '([^:/?#]+:\/\/)'+ + '([^/?#]*)' + + '(\/)' + + '([^?#]*)' + + '(\/)' + + '([^?#]*)' + ); + 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? + + // 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? + + // get the data + + const data = { + version: 1.0 , + author_name: 'Spee.ch', + author_url: 'https://spee.ch', + provider_name: 'Spee.ch', + provider_url: 'https://spee.ch', + cache_age: 86400, // one day in seconds + }; + + // set type + // if (fileType.substring(0, fileType.indexOf('/') === 'video')) { + // data['type'] = 'video'; + // } else { + // data['type'] = 'picture'; + // } + + data['title'] = ''; + data['thumbnail_url'] = ''; + data['thumbnail_width'] = ''; + data['thumbnail_height'] = ''; + res.status(200).json({ success: true, message: 'hello', - data: req.query, + data, }); - // db.Blocked.refreshTable() - // .then(data => { - // logger.info('finished updating blocked content list'); - // res.status(200).json({ - // success: true, - // data, - // }); - // }) - // .catch(error => { - // logger.error(error); - // res.status(500).json({ - // success: false, - // error, - // }); - // }); }; module.exports = getOEmbedData; From 7237899e77609a25d9c60128a3a7676a794f9d92 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 10:02:08 -0700 Subject: [PATCH 04/15] fixed regex --- server/controllers/api/oEmbed/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/controllers/api/oEmbed/index.js b/server/controllers/api/oEmbed/index.js index 606e1026..7f314b96 100644 --- a/server/controllers/api/oEmbed/index.js +++ b/server/controllers/api/oEmbed/index.js @@ -13,9 +13,9 @@ const getOEmbedData = (req, res) => { '([^:/?#]+:\/\/)'+ '([^/?#]*)' + '(\/)' + - '([^?#]*)' + + '([^/?#]*)' + '(\/)' + - '([^?#]*)' + '([^/?#]*)' ); const [proto, protocol, domain, slashOne, paramOne, slashTwo, paramTwo] = componentsRegex .exec(url) From 523097070564d0437c0e9ae6f3d3ef42453a42b8 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 13:48:47 -0700 Subject: [PATCH 05/15] updated process for getting file record in serve logic --- server/controllers/api/claim/longId/index.js | 2 +- server/controllers/api/oEmbed/index.js | 108 +++++++++++++----- .../controllers/assets/serveByClaim/index.js | 8 +- .../assets/serveByIdentifierAndClaim/index.js | 2 +- .../assets/utils/getClaimIdAndServeAsset.js | 17 ++- .../{api/claim/longId => utils}/getClaimId.js | 2 +- .../controllers/{assets => }/utils/lbryUri.js | 0 7 files changed, 102 insertions(+), 37 deletions(-) rename server/controllers/{api/claim/longId => utils}/getClaimId.js (95%) rename server/controllers/{assets => }/utils/lbryUri.js (100%) diff --git a/server/controllers/api/claim/longId/index.js b/server/controllers/api/claim/longId/index.js index 27f66b1c..c2c91d04 100644 --- a/server/controllers/api/claim/longId/index.js +++ b/server/controllers/api/claim/longId/index.js @@ -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'; diff --git a/server/controllers/api/oEmbed/index.js b/server/controllers/api/oEmbed/index.js index 7f314b96..a7f9fbde 100644 --- a/server/controllers/api/oEmbed/index.js +++ b/server/controllers/api/oEmbed/index.js @@ -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; diff --git a/server/controllers/assets/serveByClaim/index.js b/server/controllers/assets/serveByClaim/index.js index 1ca0be13..2d154152 100644 --- a/server/controllers/assets/serveByClaim/index.js +++ b/server/controllers/assets/serveByClaim/index.js @@ -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}); } diff --git a/server/controllers/assets/serveByIdentifierAndClaim/index.js b/server/controllers/assets/serveByIdentifierAndClaim/index.js index 34434f5b..c377b0d1 100644 --- a/server/controllers/assets/serveByIdentifierAndClaim/index.js +++ b/server/controllers/assets/serveByIdentifierAndClaim/index.js @@ -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'); diff --git a/server/controllers/assets/utils/getClaimIdAndServeAsset.js b/server/controllers/assets/utils/getClaimIdAndServeAsset.js index 6603d046..a4173cbf 100644 --- a/server/controllers/assets/utils/getClaimIdAndServeAsset.js +++ b/server/controllers/assets/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); diff --git a/server/controllers/api/claim/longId/getClaimId.js b/server/controllers/utils/getClaimId.js similarity index 95% rename from server/controllers/api/claim/longId/getClaimId.js rename to server/controllers/utils/getClaimId.js index d1a3a212..f692bd2e 100644 --- a/server/controllers/api/claim/longId/getClaimId.js +++ b/server/controllers/utils/getClaimId.js @@ -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) => { diff --git a/server/controllers/assets/utils/lbryUri.js b/server/controllers/utils/lbryUri.js similarity index 100% rename from server/controllers/assets/utils/lbryUri.js rename to server/controllers/utils/lbryUri.js From 79109cce6986804ed84885223150a75a059f733f Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 14:22:02 -0700 Subject: [PATCH 06/15] replaced oembed route logic with two files --- .../api/oEmbed/getOEmbedDataForAsset.js | 58 ++++++++++++ .../api/oEmbed/getOEmbedDataForChannel.js | 17 ++++ server/controllers/api/oEmbed/index.js | 90 +++++++------------ 3 files changed, 105 insertions(+), 60 deletions(-) create mode 100644 server/controllers/api/oEmbed/getOEmbedDataForAsset.js create mode 100644 server/controllers/api/oEmbed/getOEmbedDataForChannel.js diff --git a/server/controllers/api/oEmbed/getOEmbedDataForAsset.js b/server/controllers/api/oEmbed/getOEmbedDataForAsset.js new file mode 100644 index 00000000..a8f5a3f4 --- /dev/null +++ b/server/controllers/api/oEmbed/getOEmbedDataForAsset.js @@ -0,0 +1,58 @@ +const logger = require('winston'); +const db = require('../../../models'); +const getClaimId = require('../../utils/getClaimId'); + +const getOEmbedDataForAsset = (channelName, channelClaimId, claimName, claimId) => { + let fileData, claimData; + let data = { + version : 1.0, + author_name : 'Spee.ch', + author_url : 'https://spee.ch', + provider_name: 'Spee.ch', + provider_url : 'https://spee.ch', + cache_age : 86400, // one day in seconds + }; + + return 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); + // set the resource type + if (fileData.fileType === 'video/mp4') { + data['type'] = 'video'; + } else { + data['type'] = 'picture'; + } + // get the data + data['title'] = claimData.title; + data['thumbnail_url'] = `https://dev1.spee.ch/${fileData.claimId}/${fileData.name}.${fileData.fileType.substring('/')}`; + data['thumbnail_width'] = fileData.width || 600; + data['thumbnail_height'] = fileData.height || 400; + }) + .then(() => { + return data; + }); +}; + +module.exports = getOEmbedDataForAsset; diff --git a/server/controllers/api/oEmbed/getOEmbedDataForChannel.js b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js new file mode 100644 index 00000000..47241353 --- /dev/null +++ b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js @@ -0,0 +1,17 @@ +const logger = require('winston'); + +const getOEmbedDataForChannel = (channelName, channelClaimId) => { + logger.debug('get oembed for channel:', `${channelName}:${channelClaimId}`); + return new Promise((resolve, reject) => { + resolve({ + version : 1.0, + author_name : 'Spee.ch', + author_url : 'https://spee.ch', + provider_name: 'Spee.ch', + provider_url : 'https://spee.ch', + cache_age : 86400, // one day in seconds + }); + }); +}; + +module.exports = getOEmbedDataForChannel; diff --git a/server/controllers/api/oEmbed/index.js b/server/controllers/api/oEmbed/index.js index a7f9fbde..9e005d8a 100644 --- a/server/controllers/api/oEmbed/index.js +++ b/server/controllers/api/oEmbed/index.js @@ -1,8 +1,9 @@ const logger = require('winston'); -const db = require('../../../models'); - const lbryUri = require('../../utils/lbryUri'); -const getClaimId = require('../../utils/getClaimId'); + +const getOEmbedDataForChannel = require('./getOEmbedDataForChannel'); +const getOEmbedDataForAsset = require('./getOEmbedDataForAsset'); + const parseSpeechUrl = (url) => { // parse the request url @@ -32,82 +33,51 @@ const getOEmbedData = (req, res) => { logger.debug('req url', url); logger.debug('req format', format); - let data = { - version: 1.0 , - author_name: 'Spee.ch', - author_url: 'https://spee.ch', - provider_name: 'Spee.ch', - provider_url: 'https://spee.ch', - cache_age: 86400, // one day in seconds - }; - const { paramOne, paramTwo } = parseSpeechUrl(url); - let fileData, claimData; + let claimName, isChannel, channelName, channelClaimId, claimId; if (paramTwo) { ({ isChannel, channelName, channelClaimId, claimId } = lbryUri.parseIdentifier(paramOne)); ({ claimName } = lbryUri.parseClaim(paramTwo)); } else { - ({ isChannel } = lbryUri.parseIdentifier(paramOne)); + ({ isChannel, channelName, channelClaimId } = lbryUri.parseIdentifier(paramOne)); if (!isChannel ) { ({ claimName } = lbryUri.parseClaim(paramOne)); } } - 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 + getOEmbedDataForChannel(channelName, channelClaimId) + .then(data => { return res.status(200).json({ success: true, message: 'hello', data, }); }) + .catch((error) => { + return res.status(404).json({ + success: false, + message: error, + }); + }) + + } else { + getOEmbedDataForAsset(channelName, channelClaimId, claimName, claimId) + .then(data => { + return res.status(200).json({ + success: true, + message: 'hello', + data, + }); + }) + .catch((error) => { + return res.status(404).json({ + success: false, + message: error, + }); + }) } }; From abe405c1b551ad5ee9c5139b49192d85f3cae958 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 14:50:10 -0700 Subject: [PATCH 07/15] inserted check for xml requests --- server/controllers/api/oEmbed/index.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/server/controllers/api/oEmbed/index.js b/server/controllers/api/oEmbed/index.js index 9e005d8a..0c4b108a 100644 --- a/server/controllers/api/oEmbed/index.js +++ b/server/controllers/api/oEmbed/index.js @@ -50,11 +50,14 @@ const getOEmbedData = (req, res) => { if (isChannel && !paramTwo) { getOEmbedDataForChannel(channelName, channelClaimId) .then(data => { - return res.status(200).json({ - success: true, - message: 'hello', - data, - }); + if (format === 'xml'){ + return res.status(503).json({ + success: false, + message: 'xml format is not implemented yet', + }) + } else { + return res.status(200).json(data); + } }) .catch((error) => { return res.status(404).json({ @@ -66,11 +69,14 @@ const getOEmbedData = (req, res) => { } else { getOEmbedDataForAsset(channelName, channelClaimId, claimName, claimId) .then(data => { - return res.status(200).json({ - success: true, - message: 'hello', - data, - }); + if (format === 'xml'){ + return res.status(503).json({ + success: false, + message: 'xml format is not implemented yet', + }) + } else { + return res.status(200).json(data); + } }) .catch((error) => { return res.status(404).json({ From b401919bb41a7232d7dd7c9fa0bd3f4673b412cd Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 15:23:26 -0700 Subject: [PATCH 08/15] updated serve url --- .../api/oEmbed/getOEmbedDataForAsset.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/server/controllers/api/oEmbed/getOEmbedDataForAsset.js b/server/controllers/api/oEmbed/getOEmbedDataForAsset.js index a8f5a3f4..9389a537 100644 --- a/server/controllers/api/oEmbed/getOEmbedDataForAsset.js +++ b/server/controllers/api/oEmbed/getOEmbedDataForAsset.js @@ -5,9 +5,7 @@ const getClaimId = require('../../utils/getClaimId'); const getOEmbedDataForAsset = (channelName, channelClaimId, claimName, claimId) => { let fileData, claimData; let data = { - version : 1.0, - author_name : 'Spee.ch', - author_url : 'https://spee.ch', + version : '1.0', provider_name: 'Spee.ch', provider_url : 'https://spee.ch', cache_age : 86400, // one day in seconds @@ -38,17 +36,21 @@ const getOEmbedDataForAsset = (channelName, channelClaimId, claimName, claimId) .then(fileRecord => { fileData = fileRecord.dataValues; logger.debug('file data:', fileData); + const serveUrl = `https://dev1.spee.ch/${fileData.claimId}/${fileData.name}.${fileData.fileType.substring(fileData.fileType.indexOf('/') + 1)}`; // set the resource type if (fileData.fileType === 'video/mp4') { data['type'] = 'video'; + data['html'] = ``; } else { data['type'] = 'picture'; + data['url'] = serveUrl; } // get the data data['title'] = claimData.title; - data['thumbnail_url'] = `https://dev1.spee.ch/${fileData.claimId}/${fileData.name}.${fileData.fileType.substring('/')}`; - data['thumbnail_width'] = fileData.width || 600; - data['thumbnail_height'] = fileData.height || 400; + data['width'] = fileData.width || 600; + data['height'] = fileData.height || 400; + data['author_name'] = 'Spee.ch'; + data['author_url'] = 'https://spee.ch'; }) .then(() => { return data; From e22b6765818732c3a2a571c6b466256d14dd98b5 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 15:51:01 -0700 Subject: [PATCH 09/15] added channel oEmbed route --- .../api/oEmbed/getOEmbedDataForChannel.js | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/server/controllers/api/oEmbed/getOEmbedDataForChannel.js b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js index 47241353..1cd9d4a4 100644 --- a/server/controllers/api/oEmbed/getOEmbedDataForChannel.js +++ b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js @@ -1,17 +1,28 @@ const logger = require('winston'); +const db = require('../../../models'); const getOEmbedDataForChannel = (channelName, channelClaimId) => { logger.debug('get oembed for channel:', `${channelName}:${channelClaimId}`); - return new Promise((resolve, reject) => { - resolve({ - version : 1.0, - author_name : 'Spee.ch', - author_url : 'https://spee.ch', - provider_name: 'Spee.ch', - provider_url : 'https://spee.ch', - cache_age : 86400, // one day in seconds + return db.Certificate + .findOne({ + where: { + name : channelName, + claimId: channelClaimId, + }, + }) + .then(certificateRecord => { + const certificateData = certificateRecord.dataValues; + return { + version : 1.0, + provider_name: 'Spee.ch', + provider_url : 'https://spee.ch', + type : 'link', + author_name : certificateData.name, + title : `${certificateData.name}'s channel on Spee.ch`, + author_url : `https://spee.ch/${certificateData.name}:${certificateData.claimId}`, + cache_age : 86400, // one day in seconds + }; }); - }); }; module.exports = getOEmbedDataForChannel; From 76a3af45228b62bd3539e9f65f7002dba42eb09b Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 16:50:36 -0700 Subject: [PATCH 10/15] moved speech url parser to own file --- .../api/oEmbed/getOEmbedDataForChannel.js | 2 -- server/controllers/api/oEmbed/index.js | 24 +------------------ .../controllers/api/oEmbed/parseSpeechUrl.js | 24 +++++++++++++++++++ 3 files changed, 25 insertions(+), 25 deletions(-) create mode 100644 server/controllers/api/oEmbed/parseSpeechUrl.js diff --git a/server/controllers/api/oEmbed/getOEmbedDataForChannel.js b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js index 1cd9d4a4..f6908dac 100644 --- a/server/controllers/api/oEmbed/getOEmbedDataForChannel.js +++ b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js @@ -1,8 +1,6 @@ -const logger = require('winston'); const db = require('../../../models'); const getOEmbedDataForChannel = (channelName, channelClaimId) => { - logger.debug('get oembed for channel:', `${channelName}:${channelClaimId}`); return db.Certificate .findOne({ where: { diff --git a/server/controllers/api/oEmbed/index.js b/server/controllers/api/oEmbed/index.js index 0c4b108a..22a684f8 100644 --- a/server/controllers/api/oEmbed/index.js +++ b/server/controllers/api/oEmbed/index.js @@ -3,29 +3,7 @@ const lbryUri = require('../../utils/lbryUri'); const getOEmbedDataForChannel = require('./getOEmbedDataForChannel'); const getOEmbedDataForAsset = require('./getOEmbedDataForAsset'); - - -const parseSpeechUrl = (url) => { - // parse the request url - const componentsRegex = new RegExp( - '([^:/?#]+:\/\/)'+ - '([^/?#]*)' + - '(\/)' + - '([^/?#]*)' + - '(\/)' + - '([^/?#]*)' - ); - const [proto, protocol, domain, slashOne, paramOne, slashTwo, paramTwo] = componentsRegex - .exec(url) - .map(match => match || null); - - logger.debug(`${protocol}, ${domain}, ${slashOne}, ${paramOne}, ${slashTwo}, ${paramTwo}`); - - return { - paramOne, - paramTwo, - } -}; +const parseSpeechUrl = require('./parseSpeechUrl'); const getOEmbedData = (req, res) => { diff --git a/server/controllers/api/oEmbed/parseSpeechUrl.js b/server/controllers/api/oEmbed/parseSpeechUrl.js new file mode 100644 index 00000000..9b330f17 --- /dev/null +++ b/server/controllers/api/oEmbed/parseSpeechUrl.js @@ -0,0 +1,24 @@ +const logger = require('winston'); + +const parseSpeechUrl = (url) => { + const componentsRegex = new RegExp( + '([^:/?#]+://)' + + '([^/?#]*)' + + '(/)' + + '([^/?#]*)' + + '(/)' + + '([^/?#]*)' + ); + const [, , , , paramOne, , paramTwo] = componentsRegex + .exec(url) + .map(match => match || null); + + logger.debug(`params from speech url: ${paramOne} ${paramTwo}`); + + return { + paramOne, + paramTwo, + }; +}; + +module.exports = parseSpeechUrl; From a91be45938669d303002484ba3eff13bc52e366e Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 30 Jul 2018 17:02:18 -0700 Subject: [PATCH 11/15] removed comments in serve route handlers --- server/controllers/assets/serveByClaim/index.js | 7 +------ .../controllers/assets/serveByIdentifierAndClaim/index.js | 1 + 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/server/controllers/assets/serveByClaim/index.js b/server/controllers/assets/serveByClaim/index.js index 2d154152..b1b64722 100644 --- a/server/controllers/assets/serveByClaim/index.js +++ b/server/controllers/assets/serveByClaim/index.js @@ -36,17 +36,12 @@ const serveByClaim = (req, res) => { ({ claimName } = lbryUri.parseClaim(params.claim)); logger.debug('serve request:', { headers, ip, originalUrl, params }); - getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res); - // get claim id - // serve asset + getClaimIdAndServeAsset(null, null, claimName, null, originalUrl, ip, res); sendGAServeEvent(headers, ip, originalUrl); } catch (error) { - // check for different bubbled up error messages - - // fallback return res.status(400).json({success: false, message: error.message}); } diff --git a/server/controllers/assets/serveByIdentifierAndClaim/index.js b/server/controllers/assets/serveByIdentifierAndClaim/index.js index c377b0d1..3ab64d8d 100644 --- a/server/controllers/assets/serveByIdentifierAndClaim/index.js +++ b/server/controllers/assets/serveByIdentifierAndClaim/index.js @@ -37,6 +37,7 @@ const serverByIdentifierAndClaim = (req, res) => { } logger.debug('serve request:', { headers, ip, originalUrl, params }); + getClaimIdAndServeAsset(channelName, channelClaimId, claimName, claimId, originalUrl, ip, res); sendGAServeEvent(headers, ip, originalUrl); From f42cc51d122395602dbdef45aa23de75bdaa40f8 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Jul 2018 09:20:56 -0700 Subject: [PATCH 12/15] switched hard coding for configs --- .../api/oEmbed/getOEmbedDataForAsset.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/server/controllers/api/oEmbed/getOEmbedDataForAsset.js b/server/controllers/api/oEmbed/getOEmbedDataForAsset.js index 9389a537..d55f9232 100644 --- a/server/controllers/api/oEmbed/getOEmbedDataForAsset.js +++ b/server/controllers/api/oEmbed/getOEmbedDataForAsset.js @@ -2,12 +2,19 @@ const logger = require('winston'); const db = require('../../../models'); const getClaimId = require('../../utils/getClaimId'); +const { + details: { + host, + title: siteTitle, + }, +} = require('@config/siteConfig'); + const getOEmbedDataForAsset = (channelName, channelClaimId, claimName, claimId) => { let fileData, claimData; let data = { version : '1.0', - provider_name: 'Spee.ch', - provider_url : 'https://spee.ch', + provider_name: siteTitle, + provider_url : host, cache_age : 86400, // one day in seconds }; @@ -36,7 +43,7 @@ const getOEmbedDataForAsset = (channelName, channelClaimId, claimName, claimId) .then(fileRecord => { fileData = fileRecord.dataValues; logger.debug('file data:', fileData); - const serveUrl = `https://dev1.spee.ch/${fileData.claimId}/${fileData.name}.${fileData.fileType.substring(fileData.fileType.indexOf('/') + 1)}`; + const serveUrl = `${host}/${fileData.claimId}/${fileData.name}.${fileData.fileType.substring(fileData.fileType.indexOf('/') + 1)}`; // set the resource type if (fileData.fileType === 'video/mp4') { data['type'] = 'video'; @@ -49,8 +56,8 @@ const getOEmbedDataForAsset = (channelName, channelClaimId, claimName, claimId) data['title'] = claimData.title; data['width'] = fileData.width || 600; data['height'] = fileData.height || 400; - data['author_name'] = 'Spee.ch'; - data['author_url'] = 'https://spee.ch'; + data['author_name'] = siteTitle; + data['author_url'] = host; }) .then(() => { return data; From ece07b12d1976765702e67586208309ba186a498 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Jul 2018 09:24:03 -0700 Subject: [PATCH 13/15] switched hard coding for configs on channel logic --- .../api/oEmbed/getOEmbedDataForChannel.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/server/controllers/api/oEmbed/getOEmbedDataForChannel.js b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js index f6908dac..8f2afab3 100644 --- a/server/controllers/api/oEmbed/getOEmbedDataForChannel.js +++ b/server/controllers/api/oEmbed/getOEmbedDataForChannel.js @@ -1,5 +1,12 @@ const db = require('../../../models'); +const { + details: { + host, + title: siteTitle, + }, +} = require('@config/siteConfig'); + const getOEmbedDataForChannel = (channelName, channelClaimId) => { return db.Certificate .findOne({ @@ -12,12 +19,12 @@ const getOEmbedDataForChannel = (channelName, channelClaimId) => { const certificateData = certificateRecord.dataValues; return { version : 1.0, - provider_name: 'Spee.ch', - provider_url : 'https://spee.ch', + provider_name: siteTitle, + provider_url : host, type : 'link', author_name : certificateData.name, title : `${certificateData.name}'s channel on Spee.ch`, - author_url : `https://spee.ch/${certificateData.name}:${certificateData.claimId}`, + author_url : `${host}/${certificateData.name}:${certificateData.claimId}`, cache_age : 86400, // one day in seconds }; }); From 78cadc8301f4faadcd1f6adeacaa02c3a5c3c723 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Jul 2018 09:28:12 -0700 Subject: [PATCH 14/15] removed xml from tags --- client/src/containers/SEO/view.jsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/src/containers/SEO/view.jsx b/client/src/containers/SEO/view.jsx index 5ca6dcf9..4f64f9a3 100644 --- a/client/src/containers/SEO/view.jsx +++ b/client/src/containers/SEO/view.jsx @@ -21,7 +21,7 @@ class SEO extends React.Component { asset, channel, }); - const cannonicalLink = createCanonicalLink(asset, channel, pageUri) + const cannonicalLink = createCanonicalLink(asset, channel, pageUri); // render results return ( ); From a0c1f3553cc8e6eef95eb52de9d8138fae58bdb8 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Tue, 31 Jul 2018 10:17:45 -0700 Subject: [PATCH 15/15] rebuilt SEO component --- client/build/containers/SEO/view.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/build/containers/SEO/view.js b/client/build/containers/SEO/view.js index d2153976..ba89e49a 100644 --- a/client/build/containers/SEO/view.js +++ b/client/build/containers/SEO/view.js @@ -75,7 +75,7 @@ function (_React$Component) { link: [{ rel: 'canonical', href: cannonicalLink - }, _oEmbed.default.json(host, cannonicalLink), _oEmbed.default.xml(host, cannonicalLink)] + }, _oEmbed.default.json(host, cannonicalLink)] }); } }]);