diff --git a/package.json b/package.json index 0a25737d..119c5251 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "@sentry/browser": "5.1.1", "@sentry/electron": "0.17.1", "archiver": "3.0.0", + "cheerio": "1.0.0-rc.3", "classnames": "2.2.6", "connected-react-router": "6.4.0", "electron-devtools-installer": "2.2.4", @@ -83,6 +84,7 @@ "redux-devtools-extension": "2.13.8", "redux-promise": "0.6.0", "redux-thunk": "2.3.0", + "request-promise": "4.2.4", "rimraf": "2.6.3", "universal-analytics": "0.4.20", "wait-on": "3.2.0" diff --git a/public/app/config/mapping.js b/public/app/config/mapping.js index a21653fb..f053ebf7 100644 --- a/public/app/config/mapping.js +++ b/public/app/config/mapping.js @@ -1,4 +1 @@ -module.exports = { - 'https://gateway.golabz.eu/os/pub/phet/http%25253A%25252F%25252Fphet.colorado.edu%25252Fen%25252Fsimulation%25252Fwave-on-a-string/w_default.html': - 'https://apps.graasp.eu/5acb589d0d5d9464081c2d46/5ccae1f6203454853b0aabcb/latest/index.html', -}; +module.exports = {}; diff --git a/public/app/config/providers.js b/public/app/config/providers.js new file mode 100644 index 00000000..4ec0490c --- /dev/null +++ b/public/app/config/providers.js @@ -0,0 +1 @@ +module.exports = ['gateway.golabz.eu']; diff --git a/public/app/download.js b/public/app/download.js new file mode 100644 index 00000000..edf67d1d --- /dev/null +++ b/public/app/download.js @@ -0,0 +1,27 @@ +const request = require('request-promise'); +const cheerio = require('cheerio'); +const providers = require('./config/providers'); + +const getDownloadUrl = async ({ url, lang }) => { + let proxied = false; + providers.forEach(provider => { + if (url.includes(provider)) { + proxied = true; + } + }); + if (!proxied) { + return false; + } + const res = await request(url); + const $ = cheerio.load(res); + const elem = $(`meta[name="download"][language="${lang}"]`); + if (elem) { + return elem.attr('value'); + } + // todo: fall back on another language if requested is not available? + return false; +}; + +module.exports = { + getDownloadUrl, +}; diff --git a/public/electron.js b/public/electron.js index b9ec2378..fc8295ff 100644 --- a/public/electron.js +++ b/public/electron.js @@ -20,6 +20,7 @@ const Sentry = require('@sentry/electron'); const ua = require('universal-analytics'); const { machineIdSync } = require('node-machine-id'); const logger = require('./app/logger'); +const { getDownloadUrl } = require('./app/download'); const { getExtension, isDownloadable, @@ -252,6 +253,7 @@ app.on('ready', async () => { try { // get handle to spaces collection const spaces = db.get(SPACES_COLLECTION); + const lang = db.get('user.lang').value(); const { id } = space; const existingSpace = spaces.find({ id }).value(); @@ -323,6 +325,13 @@ app.on('ready', async () => { url = mapping[url]; } + // download from proxy url if available + // eslint-disable-next-line no-await-in-loop + const downloadUrl = await getDownloadUrl({ url, lang }); + if (downloadUrl) { + url = downloadUrl; + } + // generate hash and get extension to save file const hash = generateHash(resource); const ext = getExtension(resource); diff --git a/src/config/i18n.js b/src/config/i18n.js index 4b489004..60f87562 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -5,10 +5,10 @@ import fr from '../langs/fr'; i18n.use(initReactI18next).init({ resources: { - en_all: en, - fr_all: fr, + en_ALL: en, + fr_ALL: fr, }, - fallbackLng: 'en_all', + fallbackLng: 'en_ALL', // debug only when not in production debug: process.env.NODE_ENV !== 'production', ns: ['translations'], @@ -24,34 +24,34 @@ i18n.use(initReactI18next).init({ }); const langs = { - // bg_all: "български", - // ca_all: "Català", - // cs_all: "čeština", - // de_all: "Deutsch", - // el_all: "Ελληνικά", - en_all: 'English', - // es_all: "Español", - // et_all: "Eesti", - // fi_all: "Suomi", - fr_all: 'Français', - // hu_all: "Magyar", - // it_all: "Italiano", - // ja_all: "日本語", - // ka_all: "ქართული", - // lt_all: "lietuvių kalba", - // lv_all: "Latviešu", - // nl_all: "Nederlands", - // pt_all: "Português", - // ro_all: "Română", - // ru_all: "Русский", - // sk_all: "Slovenský", - // sl_all: "Slovenščina", - // sr_all: "српски језик", - // sw_all: "Kiswahili", - // tr_all: "Türkçe", - // uk_all: "Українська", - // vi_all: "Tiếng Việt", - // zh_all: "简体中文", + // bg_ALL: "български", + // ca_ALL: "Català", + // cs_ALL: "čeština", + // de_ALL: "Deutsch", + // el_ALL: "Ελληνικά", + en_ALL: 'English', + // es_ALL: "Español", + // et_ALL: "Eesti", + // fi_ALL: "Suomi", + fr_ALL: 'Français', + // hu_ALL: "Magyar", + // it_ALL: "Italiano", + // ja_ALL: "日本語", + // ka_ALL: "ქართული", + // lt_ALL: "lietuvių kalba", + // lv_ALL: "Latviešu", + // nl_ALL: "Nederlands", + // pt_ALL: "Português", + // ro_ALL: "Română", + // ru_ALL: "Русский", + // sk_ALL: "Slovenský", + // sl_ALL: "Slovenščina", + // sr_ALL: "српски језик", + // sw_ALL: "Kiswahili", + // tr_ALL: "Türkçe", + // uk_ALL: "Українська", + // vi_ALL: "Tiếng Việt", + // zh_ALL: "简体中文", // zh_tw: "繁體中文", }; diff --git a/yarn.lock b/yarn.lock index fbc34241..849a8c61 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3205,7 +3205,7 @@ charenc@~0.0.1: resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667" integrity sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc= -cheerio@^1.0.0-rc.2: +cheerio@1.0.0-rc.3, cheerio@^1.0.0-rc.2: version "1.0.0-rc.3" resolved "https://registry.yarnpkg.com/cheerio/-/cheerio-1.0.0-rc.3.tgz#094636d425b2e9c0f4eb91a46c05630c9a1a8bf6" integrity sha512-0td5ijfUPuubwLUu0OBoe98gZj8C/AA+RW3v67GPlGOrvxWjZmBXiBCRU+I8VEiNyJzjth40POfHiz2RB3gImA== @@ -11317,7 +11317,7 @@ request-promise-native@^1.0.5: stealthy-require "^1.1.1" tough-cookie "^2.3.3" -request-promise@^4.x: +request-promise@4.2.4, request-promise@^4.x: version "4.2.4" resolved "https://registry.yarnpkg.com/request-promise/-/request-promise-4.2.4.tgz#1c5ed0d71441e38ad58c7ce4ea4ea5b06d54b310" integrity sha512-8wgMrvE546PzbR5WbYxUQogUnUDfM0S7QIFZMID+J73vdFARkFy+HElj4T+MWYhpXwlLp0EQ8Zoj8xUA0he4Vg==