overleaf/services/web/app/src/Features/Project/FolderStructureBuilder.js
Eric Mc Sween 27941dc8af Merge pull request #2760 from overleaf/em-faster-uploads
Make a single Mongo update when uploading projects

GitOrigin-RevId: de102d3e112c9014ca5885f963e35971e4db6cee
2020-04-24 03:30:55 +00:00

73 lines
1.9 KiB
JavaScript

const Path = require('path')
const OError = require('@overleaf/o-error')
const { ObjectId } = require('mongodb')
module.exports = { buildFolderStructure }
function buildFolderStructure(docEntries, fileEntries) {
const builder = new FolderStructureBuilder()
for (const docEntry of docEntries) {
builder.addDocEntry(docEntry)
}
for (const fileEntry of fileEntries) {
builder.addFileEntry(fileEntry)
}
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('/')
}
addDocEntry(docEntry) {
this.recordEntityPath(docEntry.path)
const folderPath = Path.dirname(docEntry.path)
const folder = this.mkdirp(folderPath)
folder.docs.push(docEntry.doc)
}
addFileEntry(fileEntry) {
this.recordEntityPath(fileEntry.path)
const folderPath = Path.dirname(fileEntry.path)
const folder = this.mkdirp(folderPath)
folder.fileRefs.push(fileEntry.file)
}
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: []
}
}
}