2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2020-06-30 08:04:59 -04:00
|
|
|
const _ = require('lodash')
|
|
|
|
const { promisify } = require('util')
|
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const ENGINE_TO_COMPILER_MAP = {
|
|
|
|
latex_dvipdf: 'latex',
|
|
|
|
pdflatex: 'pdflatex',
|
|
|
|
xelatex: 'xelatex',
|
|
|
|
lualatex: 'lualatex'
|
|
|
|
}
|
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
module.exports = {
|
|
|
|
compilerFromV1Engine,
|
|
|
|
isArchived,
|
|
|
|
isTrashed,
|
|
|
|
isArchivedOrTrashed,
|
|
|
|
calculateArchivedArray,
|
|
|
|
ensureNameIsUnique,
|
|
|
|
getAllowedImagesForUser,
|
|
|
|
promises: {
|
|
|
|
ensureNameIsUnique: promisify(ensureNameIsUnique)
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function compilerFromV1Engine(engine) {
|
|
|
|
return ENGINE_TO_COMPILER_MAP[engine]
|
|
|
|
}
|
2019-08-09 05:40:11 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function isArchived(project, userId) {
|
|
|
|
userId = ObjectId(userId)
|
2019-08-09 05:40:11 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
if (Array.isArray(project.archived)) {
|
|
|
|
return project.archived.some(id => id.equals(userId))
|
|
|
|
} else {
|
|
|
|
return !!project.archived
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 06:38:17 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function isTrashed(project, userId) {
|
|
|
|
userId = ObjectId(userId)
|
2019-10-02 10:06:57 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
if (project.trashed) {
|
|
|
|
return project.trashed.some(id => id.equals(userId))
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 10:06:57 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function isArchivedOrTrashed(project, userId) {
|
|
|
|
return isArchived(project, userId) || isTrashed(project, userId)
|
|
|
|
}
|
2019-10-02 10:06:57 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function _allCollaborators(project) {
|
|
|
|
return _.unionWith(
|
|
|
|
[project.owner_ref],
|
|
|
|
project.collaberator_refs,
|
|
|
|
project.readOnly_refs,
|
|
|
|
project.tokenAccessReadAndWrite_refs,
|
|
|
|
project.tokenAccessReadOnly_refs,
|
|
|
|
_objectIdEquals
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function calculateArchivedArray(project, userId, action) {
|
|
|
|
let archived = project.archived
|
|
|
|
userId = ObjectId(userId)
|
2019-10-02 10:06:57 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
if (archived === true) {
|
|
|
|
archived = _allCollaborators(project)
|
|
|
|
} else if (!archived) {
|
|
|
|
archived = []
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
if (action === 'ARCHIVE') {
|
|
|
|
archived = _.unionWith(archived, [userId], _objectIdEquals)
|
|
|
|
} else if (action === 'UNARCHIVE') {
|
|
|
|
archived = archived.filter(id => !_objectIdEquals(id, userId))
|
|
|
|
} else {
|
|
|
|
throw new Error('Unrecognised action')
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
return archived
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function ensureNameIsUnique(nameList, name, suffixes, maxLength, callback) {
|
|
|
|
// create a set of all project names
|
|
|
|
if (suffixes == null) {
|
|
|
|
suffixes = []
|
|
|
|
}
|
|
|
|
const allNames = new Set(nameList)
|
|
|
|
const isUnique = x => !allNames.has(x)
|
|
|
|
// check if the supplied name is already unique
|
|
|
|
if (isUnique(name)) {
|
|
|
|
return callback(null, name)
|
|
|
|
}
|
|
|
|
// the name already exists, try adding the user-supplied suffixes to generate a unique name
|
|
|
|
for (const suffix of suffixes) {
|
|
|
|
const candidateName = _addSuffixToProjectName(name, suffix, maxLength)
|
|
|
|
if (isUnique(candidateName)) {
|
|
|
|
return callback(null, candidateName)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-06-30 08:04:59 -04:00
|
|
|
}
|
|
|
|
// if there are no (more) suffixes, use a numeric one
|
|
|
|
const uniqueName = _addNumericSuffixToProjectName(name, allNames, maxLength)
|
|
|
|
if (uniqueName != null) {
|
|
|
|
callback(null, uniqueName)
|
|
|
|
} else {
|
|
|
|
callback(new Error(`Failed to generate a unique name for: ${name}`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _objectIdEquals(firstVal, secondVal) {
|
|
|
|
// For use as a comparator for unionWith
|
|
|
|
return firstVal.toString() === secondVal.toString()
|
|
|
|
}
|
|
|
|
|
|
|
|
function _addSuffixToProjectName(name, suffix, maxLength) {
|
|
|
|
// append the suffix and truncate the project title if needed
|
|
|
|
if (suffix == null) {
|
|
|
|
suffix = ''
|
|
|
|
}
|
|
|
|
const truncatedLength = maxLength - suffix.length
|
|
|
|
return name.substr(0, truncatedLength) + suffix
|
|
|
|
}
|
|
|
|
|
|
|
|
function _addNumericSuffixToProjectName(name, allProjectNames, maxLength) {
|
|
|
|
const NUMERIC_SUFFIX_MATCH = / \((\d+)\)$/
|
2021-04-14 09:17:21 -04:00
|
|
|
const suffixedName = function (basename, number) {
|
2020-06-30 08:04:59 -04:00
|
|
|
const suffix = ` (${number})`
|
|
|
|
return basename.substr(0, maxLength - suffix.length) + suffix
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
const match = name.match(NUMERIC_SUFFIX_MATCH)
|
|
|
|
let basename = name
|
|
|
|
let n = 1
|
|
|
|
const last = allProjectNames.size + n
|
|
|
|
|
|
|
|
if (match != null) {
|
|
|
|
basename = name.replace(NUMERIC_SUFFIX_MATCH, '')
|
|
|
|
n = parseInt(match[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n <= last) {
|
|
|
|
const candidate = suffixedName(basename, n)
|
|
|
|
if (!allProjectNames.has(candidate)) {
|
|
|
|
return candidate
|
|
|
|
}
|
|
|
|
n += 1
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-06-30 08:04:59 -04:00
|
|
|
|
|
|
|
return null
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
|
2020-06-30 08:04:59 -04:00
|
|
|
function getAllowedImagesForUser(sessionUser) {
|
|
|
|
const images = Settings.allowedImageNames || []
|
|
|
|
if (sessionUser && sessionUser.isAdmin) {
|
|
|
|
return images
|
|
|
|
} else {
|
|
|
|
return images.filter(image => !image.adminOnly)
|
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
}
|