ProjectOptionsHandler.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const { Project } = require('../../models/Project')
  2. const settings = require('settings-sharelatex')
  3. const { promisifyAll } = require('../../util/promises')
  4. const safeCompilers = ['xelatex', 'pdflatex', 'latex', 'lualatex']
  5. const ProjectOptionsHandler = {
  6. setCompiler(projectId, compiler, callback) {
  7. if (!compiler) {
  8. return callback()
  9. }
  10. compiler = compiler.toLowerCase()
  11. if (!safeCompilers.includes(compiler)) {
  12. return callback(new Error(`invalid compiler: ${compiler}`))
  13. }
  14. const conditions = { _id: projectId }
  15. const update = { compiler }
  16. Project.updateOne(conditions, update, {}, callback)
  17. },
  18. setImageName(projectId, imageName, callback) {
  19. if (!imageName || !Array.isArray(settings.allowedImageNames)) {
  20. return callback()
  21. }
  22. imageName = imageName.toLowerCase()
  23. const isAllowed = settings.allowedImageNames.find(
  24. allowed => imageName === allowed.imageName
  25. )
  26. if (!isAllowed) {
  27. return callback(new Error(`invalid imageName: ${imageName}`))
  28. }
  29. const conditions = { _id: projectId }
  30. const update = { imageName: settings.imageRoot + '/' + imageName }
  31. Project.updateOne(conditions, update, {}, callback)
  32. },
  33. setSpellCheckLanguage(projectId, languageCode, callback) {
  34. if (!Array.isArray(settings.languages)) {
  35. return callback()
  36. }
  37. const language = settings.languages.find(
  38. language => language.code === languageCode
  39. )
  40. if (languageCode && !language) {
  41. return callback(new Error(`invalid languageCode: ${languageCode}`))
  42. }
  43. const conditions = { _id: projectId }
  44. const update = { spellCheckLanguage: languageCode }
  45. Project.updateOne(conditions, update, {}, callback)
  46. },
  47. setBrandVariationId(projectId, brandVariationId, callback) {
  48. if (!brandVariationId) {
  49. return callback()
  50. }
  51. const conditions = { _id: projectId }
  52. const update = { brandVariationId }
  53. Project.updateOne(conditions, update, {}, callback)
  54. },
  55. unsetBrandVariationId(projectId, callback) {
  56. const conditions = { _id: projectId }
  57. const update = { $unset: { brandVariationId: 1 } }
  58. Project.updateOne(conditions, update, {}, callback)
  59. }
  60. }
  61. ProjectOptionsHandler.promises = promisifyAll(ProjectOptionsHandler)
  62. module.exports = ProjectOptionsHandler