From a7007b2868b9354a2cd432539ef9906961b7a142 Mon Sep 17 00:00:00 2001 From: Juan Carlos Farah Date: Tue, 20 Aug 2019 16:22:56 +0200 Subject: [PATCH] fix: fall back on default language for proxied labs If a proxied lab (Amrita, PhET, Physics Aviary) is not available in the required language, fall back to the default (currently English). This prevents labs like the Amrita ones from breaking. closes #173 --- public/app/download.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/public/app/download.js b/public/app/download.js index cd87c088..1d103604 100644 --- a/public/app/download.js +++ b/public/app/download.js @@ -5,7 +5,7 @@ const download = require('download'); const providers = require('./config/providers'); const logger = require('./logger'); const mapping = require('./config/mapping'); -const { DEFAULT_PROTOCOL } = require('./config/config'); +const { DEFAULT_PROTOCOL, DEFAULT_LANG } = require('./config/config'); const { getExtension, isDownloadable, @@ -25,11 +25,20 @@ const getDownloadUrl = async ({ url, lang }) => { } const res = await request(url); const $ = cheerio.load(res); - const elem = $(`meta[name="download"][language="${lang}"]`); - if (elem) { - return elem.attr('value'); + + const proxyUrl = $(`meta[name="download"][language="${lang}"]`).attr('value'); + if (proxyUrl) { + logger.debug(`proxying from ${proxyUrl}`); + return proxyUrl; + } + // fall back on default language if requested is not available + const defaultProxyUrl = $( + `meta[name="download"][language="${DEFAULT_LANG}"]` + ).attr('value'); + if (defaultProxyUrl) { + logger.debug(`defaulting to proxying from ${defaultProxyUrl}`); + return defaultProxyUrl; } - // todo: fall back on another language if requested is not available? return false; };