From b2b9a05e3c7a19a5968f57e39b45eef6049572b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Mon, 29 Mar 2021 11:19:54 +0200 Subject: [PATCH] Merge pull request #3706 from overleaf/msm-jlm-configurable-text-extensions New ADDITIONAL_TEXT_EXTENSIONS env to support extra editable files GitOrigin-RevId: 03bec0a1a9dbd303754f0b90377d1b7d1ec3f528 --- .../src/Features/Uploads/FileTypeManager.js | 32 +-------------- .../app/src/infrastructure/ExpressLocals.js | 1 + services/web/config/settings.defaults.coffee | 39 +++++++++++++++++++ .../controllers/BinaryFileController.js | 31 +-------------- .../test/unit/src/Settings/SettingsTests.js | 23 +++++++++++ 5 files changed, 66 insertions(+), 60 deletions(-) create mode 100644 services/web/test/unit/src/Settings/SettingsTests.js diff --git a/services/web/app/src/Features/Uploads/FileTypeManager.js b/services/web/app/src/Features/Uploads/FileTypeManager.js index b67b00a5d1..6616be0037 100644 --- a/services/web/app/src/Features/Uploads/FileTypeManager.js +++ b/services/web/app/src/Features/Uploads/FileTypeManager.js @@ -2,38 +2,10 @@ const fs = require('fs') const Path = require('path') const isUtf8 = require('utf-8-validate') const { promisifyAll } = require('../../util/promises') +const Settings = require('settings-sharelatex') const FileTypeManager = { - TEXT_EXTENSIONS: [ - '.tex', - '.latex', - '.sty', - '.cls', - '.bst', - '.bib', - '.bibtex', - '.txt', - '.tikz', - '.mtx', - '.rtex', - '.md', - '.asy', - '.latexmkrc', - '.lbx', - '.bbx', - '.cbx', - '.m', - '.lco', - '.dtx', - '.ins', - '.ist', - '.def', - '.clo', - '.ldf', - '.rmd', - '.lua', - '.gv' - ], + TEXT_EXTENSIONS: Settings.textExtensions.map(ext => `.${ext}`), IGNORE_EXTENSIONS: [ '.dvi', diff --git a/services/web/app/src/infrastructure/ExpressLocals.js b/services/web/app/src/infrastructure/ExpressLocals.js index d3b76fdc9f..ae035ccd98 100644 --- a/services/web/app/src/infrastructure/ExpressLocals.js +++ b/services/web/app/src/infrastructure/ExpressLocals.js @@ -360,6 +360,7 @@ module.exports = function(webRouter, privateApiRouter, publicApiRouter) { Settings.recaptcha != null ? Settings.recaptcha.siteKeyV3 : undefined, recaptchaDisabled: Settings.recaptcha != null ? Settings.recaptcha.disabled : undefined, + textExtensions: Settings.textExtensions, validRootDocExtensions: Settings.validRootDocExtensions, sentryAllowedOriginRegex: Settings.sentry.allowedOriginRegex, sentryDsn: Settings.sentry.publicDSN, diff --git a/services/web/config/settings.defaults.coffee b/services/web/config/settings.defaults.coffee index 8908b45afd..01743bc69a 100644 --- a/services/web/config/settings.defaults.coffee +++ b/services/web/config/settings.defaults.coffee @@ -32,6 +32,43 @@ intFromEnv = (name, defaultValue) -> throw new Error("Bad default integer value for setting: #{name}, #{defaultValue}") parseInt(process.env[name], 10) || defaultValue +defaultTextExtensions = [ + 'tex', + 'latex', + 'sty', + 'cls', + 'bst', + 'bib', + 'bibtex', + 'txt', + 'tikz', + 'mtx', + 'rtex', + 'md', + 'asy', + 'latexmkrc', + 'lbx', + 'bbx', + 'cbx', + 'm', + 'lco', + 'dtx', + 'ins', + 'ist', + 'def', + 'clo', + 'ldf', + 'rmd', + 'lua', + 'gv' +] + +parseTextExtensions = (extensions) -> + if extensions + extensions.split(',').map((ext) => ext.trim()) + else + [] + module.exports = settings = allowAnonymousReadAndWriteSharing: @@ -641,6 +678,8 @@ module.exports = settings = compileBodySizeLimitMb: process.env['COMPILE_BODY_SIZE_LIMIT_MB'] or 5 + textExtensions: defaultTextExtensions.concat(parseTextExtensions(process.env["ADDITIONAL_TEXT_EXTENSIONS"])) + validRootDocExtensions: ['tex', 'Rtex', 'ltx'] emailConfirmationDisabled: (process.env['EMAIL_CONFIRMATION_DISABLED'] == "true") or false diff --git a/services/web/frontend/js/ide/binary-files/controllers/BinaryFileController.js b/services/web/frontend/js/ide/binary-files/controllers/BinaryFileController.js index 1075a862b1..3d77857809 100644 --- a/services/web/frontend/js/ide/binary-files/controllers/BinaryFileController.js +++ b/services/web/frontend/js/ide/binary-files/controllers/BinaryFileController.js @@ -16,36 +16,7 @@ export default App.controller('BinaryFileController', function( const TAIL_OF_URL_LENGTH = MAX_URL_LENGTH - FRONT_OF_URL_LENGTH - FILLER.length - const textExtensions = [ - 'tex', - 'latex', - 'sty', - 'cls', - 'bst', - 'bib', - 'bibtex', - 'txt', - 'tikz', - 'mtx', - 'rtex', - 'md', - 'asy', - 'latexmkrc', - 'lbx', - 'bbx', - 'cbx', - 'm', - 'lco', - 'dtx', - 'ins', - 'ist', - 'def', - 'clo', - 'ldf', - 'rmd', - 'lua', - 'gv' - ] + const textExtensions = window.ExposedSettings.textExtensions const imageExtensions = ['png', 'jpg', 'jpeg', 'gif'] const previewableExtensions = [] diff --git a/services/web/test/unit/src/Settings/SettingsTests.js b/services/web/test/unit/src/Settings/SettingsTests.js new file mode 100644 index 0000000000..a995e6fa67 --- /dev/null +++ b/services/web/test/unit/src/Settings/SettingsTests.js @@ -0,0 +1,23 @@ +const chai = require('chai') +const { expect } = chai + +function clearSettingsCache() { + delete require.cache[ + require.resolve('../../../../config/settings.defaults.coffee') + ] + const settingsDeps = Object.keys(require.cache).filter(x => + x.includes('/settings-sharelatex/') + ) + settingsDeps.forEach(dep => delete require.cache[dep]) +} + +describe('settings.defaults', function() { + it('additional text extensions can be added via config', function() { + clearSettingsCache() + process.env.ADDITIONAL_TEXT_EXTENSIONS = 'abc, xyz' + const settings = require('settings-sharelatex') + expect(settings.textExtensions).to.include('tex') // from the default list + expect(settings.textExtensions).to.include('abc') + expect(settings.textExtensions).to.include('xyz') + }) +})