ProjectOptionsHandler.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const { Project } = require('../../models/Project')
  2. const settings = require('@overleaf/settings')
  3. const { callbackify } = require('util')
  4. const { db, ObjectId } = require('../../infrastructure/mongodb')
  5. const safeCompilers = ['xelatex', 'pdflatex', 'latex', 'lualatex']
  6. const ProjectOptionsHandler = {
  7. async setCompiler(projectId, compiler) {
  8. if (!compiler) {
  9. return
  10. }
  11. compiler = compiler.toLowerCase()
  12. if (!safeCompilers.includes(compiler)) {
  13. throw new Error(`invalid compiler: ${compiler}`)
  14. }
  15. const conditions = { _id: projectId }
  16. const update = { compiler }
  17. return Project.updateOne(conditions, update, {})
  18. },
  19. async setImageName(projectId, imageName) {
  20. if (!imageName || !Array.isArray(settings.allowedImageNames)) {
  21. return
  22. }
  23. imageName = imageName.toLowerCase()
  24. const isAllowed = settings.allowedImageNames.find(
  25. allowed => imageName === allowed.imageName
  26. )
  27. if (!isAllowed) {
  28. throw new Error(`invalid imageName: ${imageName}`)
  29. }
  30. const conditions = { _id: projectId }
  31. const update = { imageName: settings.imageRoot + '/' + imageName }
  32. return Project.updateOne(conditions, update, {})
  33. },
  34. async setSpellCheckLanguage(projectId, languageCode) {
  35. if (!Array.isArray(settings.languages)) {
  36. return
  37. }
  38. const language = settings.languages.find(
  39. language => language.code === languageCode
  40. )
  41. if (languageCode && !language) {
  42. throw new Error(`invalid languageCode: ${languageCode}`)
  43. }
  44. const conditions = { _id: projectId }
  45. const update = { spellCheckLanguage: languageCode }
  46. return Project.updateOne(conditions, update, {})
  47. },
  48. async setBrandVariationId(projectId, brandVariationId) {
  49. if (!brandVariationId) {
  50. return
  51. }
  52. const conditions = { _id: projectId }
  53. const update = { brandVariationId }
  54. return Project.updateOne(conditions, update, {})
  55. },
  56. async unsetBrandVariationId(projectId) {
  57. const conditions = { _id: projectId }
  58. const update = { $unset: { brandVariationId: 1 } }
  59. return Project.updateOne(conditions, update, {})
  60. },
  61. async setHistoryRangesSupport(projectId, enabled) {
  62. const conditions = { _id: new ObjectId(projectId) }
  63. const update = {
  64. $set: { 'overleaf.history.rangesSupportEnabled': enabled },
  65. }
  66. // NOTE: Updating the Mongoose model with the same query doesn't work. Maybe
  67. // because rangesSupportEnabled is not part of the schema?
  68. return db.projects.updateOne(conditions, update)
  69. },
  70. }
  71. module.exports = {
  72. setCompiler: callbackify(ProjectOptionsHandler.setCompiler),
  73. setImageName: callbackify(ProjectOptionsHandler.setImageName),
  74. setSpellCheckLanguage: callbackify(
  75. ProjectOptionsHandler.setSpellCheckLanguage
  76. ),
  77. setBrandVariationId: callbackify(ProjectOptionsHandler.setBrandVariationId),
  78. unsetBrandVariationId: callbackify(
  79. ProjectOptionsHandler.unsetBrandVariationId
  80. ),
  81. setHistoryRangesSupport: callbackify(
  82. ProjectOptionsHandler.setHistoryRangesSupport
  83. ),
  84. promises: ProjectOptionsHandler,
  85. }