2021-10-25 09:57:16 -04:00
|
|
|
const { callbackify } = require('util')
|
|
|
|
const Path = require('path')
|
|
|
|
const ProjectEntityHandler = require('./ProjectEntityHandler')
|
2022-03-31 03:55:51 -04:00
|
|
|
const EditorController = require('../Editor/EditorController')
|
2021-10-25 09:57:16 -04:00
|
|
|
|
2021-12-08 05:03:35 -05:00
|
|
|
// generate a new name based on the original, with an optional label.
|
|
|
|
// e.g. origname-20210101-122345.tex (default)
|
|
|
|
// origname-restored-20210101-122345.tex (label="restored")
|
|
|
|
function generateRestoredName(docName, label) {
|
|
|
|
const formattedTimestamp = new Date()
|
|
|
|
.toISOString()
|
|
|
|
.replace('T', '-')
|
|
|
|
.replace(/[^0-9-]/g, '')
|
|
|
|
const extension = Path.extname(docName)
|
|
|
|
const basename =
|
|
|
|
Path.basename(docName, extension) + (label ? `-${label}` : '')
|
|
|
|
return `${basename}-${formattedTimestamp}${extension}`
|
|
|
|
}
|
|
|
|
|
2021-10-25 09:57:16 -04:00
|
|
|
async function restoreDeletedDoc(projectId, docId, docName, userId) {
|
|
|
|
const deletedDoc = await ProjectEntityHandler.promises.getDoc(
|
|
|
|
projectId,
|
|
|
|
docId,
|
|
|
|
{ include_deleted: true }
|
|
|
|
)
|
2021-12-08 05:03:35 -05:00
|
|
|
const deletedDocName = generateRestoredName(docName)
|
2022-03-31 03:55:51 -04:00
|
|
|
// Create the doc and emit a websocket message.
|
|
|
|
return await EditorController.promises.addDocWithRanges(
|
2021-10-25 09:57:16 -04:00
|
|
|
projectId,
|
|
|
|
null,
|
|
|
|
`${deletedDocName}`,
|
|
|
|
deletedDoc.lines,
|
|
|
|
deletedDoc.ranges,
|
2022-03-31 03:55:51 -04:00
|
|
|
null,
|
2021-10-25 09:57:16 -04:00
|
|
|
userId
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
restoreDeletedDoc: callbackify(restoreDeletedDoc),
|
2021-12-08 05:03:35 -05:00
|
|
|
generateRestoredName,
|
2021-10-25 09:57:16 -04:00
|
|
|
promises: {
|
|
|
|
restoreDeletedDoc,
|
|
|
|
},
|
|
|
|
}
|