overleaf/services/web/app/src/Features/Project/ProjectOptionsHandler.js
Jakob Ackermann 5e773ce950 Merge pull request #4101 from overleaf/ae-settings-module
Migrate from `settings-sharelatex` to `@overleaf/settings`

GitOrigin-RevId: 9a298ba26382180c1351683c5fddc9004418c1e6
2021-07-08 02:08:28 +00:00

69 lines
2.3 KiB
JavaScript

const { Project } = require('../../models/Project')
const settings = require('@overleaf/settings')
const { promisifyAll } = require('../../util/promises')
const safeCompilers = ['xelatex', 'pdflatex', 'latex', 'lualatex']
const ProjectOptionsHandler = {
setCompiler(projectId, compiler, callback) {
if (!compiler) {
return callback()
}
compiler = compiler.toLowerCase()
if (!safeCompilers.includes(compiler)) {
return callback(new Error(`invalid compiler: ${compiler}`))
}
const conditions = { _id: projectId }
const update = { compiler }
Project.updateOne(conditions, update, {}, callback)
},
setImageName(projectId, imageName, callback) {
if (!imageName || !Array.isArray(settings.allowedImageNames)) {
return callback()
}
imageName = imageName.toLowerCase()
const isAllowed = settings.allowedImageNames.find(
allowed => imageName === allowed.imageName
)
if (!isAllowed) {
return callback(new Error(`invalid imageName: ${imageName}`))
}
const conditions = { _id: projectId }
const update = { imageName: settings.imageRoot + '/' + imageName }
Project.updateOne(conditions, update, {}, callback)
},
setSpellCheckLanguage(projectId, languageCode, callback) {
if (!Array.isArray(settings.languages)) {
return callback()
}
const language = settings.languages.find(
language => language.code === languageCode
)
if (languageCode && !language) {
return callback(new Error(`invalid languageCode: ${languageCode}`))
}
const conditions = { _id: projectId }
const update = { spellCheckLanguage: languageCode }
Project.updateOne(conditions, update, {}, callback)
},
setBrandVariationId(projectId, brandVariationId, callback) {
if (!brandVariationId) {
return callback()
}
const conditions = { _id: projectId }
const update = { brandVariationId }
Project.updateOne(conditions, update, {}, callback)
},
unsetBrandVariationId(projectId, callback) {
const conditions = { _id: projectId }
const update = { $unset: { brandVariationId: 1 } }
Project.updateOne(conditions, update, {}, callback)
},
}
ProjectOptionsHandler.promises = promisifyAll(ProjectOptionsHandler)
module.exports = ProjectOptionsHandler