Files
Verso/services/web/app/src/Features/Project/FolderStructureBuilder.js
T
Eric Mc Sween 27504d7b9d Merge pull request #2361 from overleaf/em-project-imports-2
Import full folder structure in a single Mongo update

GitOrigin-RevId: 623d2a098b2084fdd0193e1593c1c55c08a2d92d
2019-11-18 14:20:38 +00:00

74 lines
1.9 KiB
JavaScript

const Path = require('path')
const OError = require('@overleaf/o-error')
const { ObjectId } = require('mongodb')
module.exports = { buildFolderStructure }
function buildFolderStructure(docUploads, fileUploads) {
const builder = new FolderStructureBuilder()
for (const docUpload of docUploads) {
builder.addDocUpload(docUpload)
}
for (const fileUpload of fileUploads) {
builder.addFileUpload(fileUpload)
}
return builder.rootFolder
}
class FolderStructureBuilder {
constructor() {
this.foldersByPath = new Map()
this.entityPaths = new Set()
this.rootFolder = this.createFolder('rootFolder')
this.foldersByPath.set('/', this.rootFolder)
this.entityPaths.add('/')
}
addDocUpload(docUpload) {
this.recordEntityPath(Path.join(docUpload.dirname, docUpload.doc.name))
const folder = this.mkdirp(docUpload.dirname)
folder.docs.push(docUpload.doc)
}
addFileUpload(fileUpload) {
this.recordEntityPath(
Path.join(fileUpload.dirname, fileUpload.fileRef.name)
)
const folder = this.mkdirp(fileUpload.dirname)
folder.fileRefs.push(fileUpload.fileRef)
}
mkdirp(path) {
const existingFolder = this.foldersByPath.get(path)
if (existingFolder != null) {
return existingFolder
}
// Folder not found, create it.
this.recordEntityPath(path)
const dirname = Path.dirname(path)
const basename = Path.basename(path)
const parentFolder = this.mkdirp(dirname)
const newFolder = this.createFolder(basename)
parentFolder.folders.push(newFolder)
this.foldersByPath.set(path, newFolder)
return newFolder
}
recordEntityPath(path) {
if (this.entityPaths.has(path)) {
throw new OError({ message: 'entity already exists', info: { path } })
}
this.entityPaths.add(path)
}
createFolder(name) {
return {
_id: ObjectId(),
name,
folders: [],
docs: [],
fileRefs: []
}
}
}