lbry-desktop/web/src/routes.js
infinite-persistence de206162f9
getHtml/routes/rss: fetch streaming_url instead of generating it.
## Issues from the initial attempt
- There are 2 versions of `Lbry` and `buildURI` -- the app and web-server version.  There are subtle differences between them, and for the app case, importing the web-server version results in a query into an invalid URL.
- It changed the function from returning a string to returning a promise.

## Changes
- Since the new function (renamed to `fetchStreamUrl` for clarity) is currently only needed for web-server, I moved it into the `web` folder to avoid misuse in app.
- Await on the promise. Unfortunately, this also means the entire chain of function calls need to be adjusted to be `async`.
2022-05-13 20:23:17 +08:00

70 lines
2 KiB
JavaScript

const { fetchStreamUrl } = require('./fetchStreamUrl');
const { getHtml } = require('./html');
const { getOEmbed } = require('./oEmbed');
const { getRss } = require('./rss');
const { getTempFile } = require('./tempfile');
const fetch = require('node-fetch');
const Router = require('@koa/router');
const { getHomepage } = require('./homepageApi');
// So any code from 'lbry-redux'/'lbryinc' that uses `fetch` can be run on the server
global.fetch = fetch;
const router = new Router();
async function getStreamUrl(ctx) {
const { claimName, claimId } = ctx.params;
return await fetchStreamUrl(claimName, claimId);
}
const rssMiddleware = async (ctx) => {
const rss = await getRss(ctx);
if (rss.startsWith('<?xml')) {
ctx.set('Content-Type', 'application/xml');
}
ctx.body = rss;
};
const oEmbedMiddleware = async (ctx) => {
const oEmbed = await getOEmbed(ctx);
ctx.body = oEmbed;
};
const tempfileMiddleware = async (ctx) => {
const temp = await getTempFile(ctx);
ctx.body = temp;
};
router.get(`/$/api/content/v1/get`, async (ctx) => getHomepage(ctx, 1));
router.get(`/$/api/content/v2/get`, async (ctx) => getHomepage(ctx, 2));
router.get(`/$/download/:claimName/:claimId`, async (ctx) => {
const streamUrl = await getStreamUrl(ctx);
const downloadUrl = `${streamUrl}?download=1`;
ctx.redirect(downloadUrl);
});
router.get(`/$/stream/:claimName/:claimId`, async (ctx) => {
const streamUrl = getStreamUrl(ctx);
ctx.redirect(streamUrl);
});
router.get(`/$/activate`, async (ctx) => {
ctx.redirect(`https://sso.odysee.com/auth/realms/Users/device`);
});
// to add a path for a temp file on the server, customize this path
router.get('/.well-known/:filename', tempfileMiddleware);
router.get(`/$/rss/:claimName/:claimId`, rssMiddleware);
router.get(`/$/rss/:claimName::claimId`, rssMiddleware);
router.get(`/$/oembed`, oEmbedMiddleware);
router.get('*', async (ctx) => {
const html = await getHtml(ctx);
ctx.body = html;
});
module.exports = router;