2019-05-29 05:21:06 -04:00
|
|
|
const path = require('path')
|
|
|
|
const DocstoreManager = require('../Docstore/DocstoreManager')
|
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
const ProjectGetter = require('./ProjectGetter')
|
2019-09-09 07:52:25 -04:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-09 07:52:25 -04:00
|
|
|
const ProjectEntityHandler = {
|
2019-11-26 08:11:19 -05:00
|
|
|
getAllDocs(projectId, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// We get the path and name info from the project, and the lines and
|
|
|
|
// version info from the doc store.
|
2019-11-26 08:11:19 -05:00
|
|
|
DocstoreManager.getAllDocs(projectId, (error, docContentsArray) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn array from docstore into a dictionary based on doc id
|
|
|
|
const docContents = {}
|
2019-11-26 08:11:19 -05:00
|
|
|
for (let docContent of docContentsArray) {
|
2019-05-29 05:21:06 -04:00
|
|
|
docContents[docContent._id] = docContent
|
|
|
|
}
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
ProjectEntityHandler._getAllFolders(projectId, (error, folders) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (folders == null) {
|
|
|
|
folders = {}
|
|
|
|
}
|
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const docs = {}
|
|
|
|
for (let folderPath in folders) {
|
|
|
|
const folder = folders[folderPath]
|
2019-11-26 08:11:19 -05:00
|
|
|
for (let doc of folder.docs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
const content = docContents[doc._id.toString()]
|
|
|
|
if (content != null) {
|
|
|
|
docs[path.join(folderPath, doc.name)] = {
|
|
|
|
_id: doc._id,
|
|
|
|
name: doc.name,
|
|
|
|
lines: content.lines,
|
|
|
|
rev: content.rev
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 09:19:08 -05:00
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
callback(null, docs)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
getAllFiles(projectId, callback) {
|
|
|
|
ProjectEntityHandler._getAllFolders(projectId, (err, folders) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (folders == null) {
|
|
|
|
folders = {}
|
|
|
|
}
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const files = {}
|
|
|
|
for (let folderPath in folders) {
|
|
|
|
const folder = folders[folderPath]
|
2019-11-26 08:11:19 -05:00
|
|
|
for (let file of folder.fileRefs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (file != null) {
|
|
|
|
files[path.join(folderPath, file.name)] = file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
callback(null, files)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
getAllEntities(projectId, callback) {
|
|
|
|
ProjectGetter.getProject(projectId, (err, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2019-09-11 07:05:30 -04:00
|
|
|
if (project == null) {
|
|
|
|
return callback(new Errors.NotFoundError('project not found'))
|
|
|
|
}
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
ProjectEntityHandler.getAllEntitiesFromProject(project, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllEntitiesFromProject(project, callback) {
|
2019-11-26 08:11:19 -05:00
|
|
|
ProjectEntityHandler._getAllFoldersFromProject(project, (err, folders) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (folders == null) {
|
|
|
|
folders = {}
|
|
|
|
}
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const docs = []
|
|
|
|
const files = []
|
|
|
|
for (let folderPath in folders) {
|
|
|
|
const folder = folders[folderPath]
|
2019-11-26 08:11:19 -05:00
|
|
|
for (let doc of folder.docs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (doc != null) {
|
|
|
|
docs.push({ path: path.join(folderPath, doc.name), doc })
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
for (let file of folder.fileRefs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (file != null) {
|
|
|
|
files.push({ path: path.join(folderPath, file.name), file })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
callback(null, docs, files)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
getAllDocPathsFromProjectById(projectId, callback) {
|
|
|
|
ProjectGetter.getProjectWithoutDocLines(projectId, (err, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
return callback(Errors.NotFoundError('no project'))
|
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
ProjectEntityHandler.getAllDocPathsFromProject(project, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllDocPathsFromProject(project, callback) {
|
2019-11-26 08:11:19 -05:00
|
|
|
ProjectEntityHandler._getAllFoldersFromProject(project, (err, folders) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (folders == null) {
|
|
|
|
folders = {}
|
|
|
|
}
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const docPath = {}
|
|
|
|
for (let folderPath in folders) {
|
|
|
|
const folder = folders[folderPath]
|
2019-11-26 08:11:19 -05:00
|
|
|
for (let doc of folder.docs || []) {
|
2019-05-29 05:21:06 -04:00
|
|
|
docPath[doc._id] = path.join(folderPath, doc.name)
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
callback(null, docPath)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
getDoc(projectId, docId, options, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
if (typeof options === 'function') {
|
|
|
|
callback = options
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
DocstoreManager.getDoc(projectId, docId, options, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
getDocPathByProjectIdAndDocId(projectId, docId, callback) {
|
|
|
|
ProjectGetter.getProjectWithoutDocLines(projectId, (err, project) => {
|
2019-10-03 10:10:00 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
|
|
|
return callback(new Errors.NotFoundError('no project'))
|
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
function recursivelyFindDocInFolder(basePath, docId, folder) {
|
|
|
|
let docInCurrentFolder = (folder.docs || []).find(
|
|
|
|
currentDoc => currentDoc._id.toString() === docId.toString()
|
2019-10-03 10:10:00 -04:00
|
|
|
)
|
|
|
|
if (docInCurrentFolder != null) {
|
|
|
|
return path.join(basePath, docInCurrentFolder.name)
|
|
|
|
} else {
|
|
|
|
let docPath, childFolder
|
2019-11-26 08:11:19 -05:00
|
|
|
for (childFolder of folder.folders || []) {
|
2019-10-03 10:10:00 -04:00
|
|
|
docPath = recursivelyFindDocInFolder(
|
|
|
|
path.join(basePath, childFolder.name),
|
2019-11-26 08:11:19 -05:00
|
|
|
docId,
|
2019-10-03 10:10:00 -04:00
|
|
|
childFolder
|
|
|
|
)
|
|
|
|
if (docPath != null) {
|
|
|
|
return docPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const docPath = recursivelyFindDocInFolder(
|
|
|
|
'/',
|
2019-11-26 08:11:19 -05:00
|
|
|
docId,
|
2019-10-03 10:10:00 -04:00
|
|
|
project.rootFolder[0]
|
|
|
|
)
|
|
|
|
if (docPath == null) {
|
|
|
|
return callback(new Errors.NotFoundError('no doc'))
|
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
callback(null, docPath)
|
2019-10-03 10:10:00 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2019-11-26 08:11:19 -05:00
|
|
|
_getAllFolders(projectId, callback) {
|
|
|
|
ProjectGetter.getProjectWithoutDocLines(projectId, (err, project) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
if (project == null) {
|
2019-10-07 11:21:04 -04:00
|
|
|
return callback(new Errors.NotFoundError('no project'))
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
ProjectEntityHandler._getAllFoldersFromProject(project, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_getAllFoldersFromProject(project, callback) {
|
|
|
|
const folders = {}
|
2019-11-26 08:11:19 -05:00
|
|
|
function processFolder(basePath, folder) {
|
2019-05-29 05:21:06 -04:00
|
|
|
folders[basePath] = folder
|
2019-11-26 08:11:19 -05:00
|
|
|
for (let childFolder of folder.folders || []) {
|
|
|
|
if (childFolder.name != null) {
|
|
|
|
processFolder(path.join(basePath, childFolder.name), childFolder)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-26 08:11:19 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
processFolder('/', project.rootFolder[0])
|
2019-11-26 08:11:19 -05:00
|
|
|
callback(null, folders)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 07:52:25 -04:00
|
|
|
|
|
|
|
module.exports = ProjectEntityHandler
|
2019-11-12 03:56:58 -05:00
|
|
|
module.exports.promises = promisifyAll(ProjectEntityHandler, {
|
|
|
|
multiResult: {
|
|
|
|
getAllEntities: ['docs', 'files'],
|
2020-03-04 04:37:43 -05:00
|
|
|
getAllEntitiesFromProject: ['docs', 'files'],
|
|
|
|
getDoc: ['lines', 'rev', 'version', 'ranges']
|
2019-11-12 03:56:58 -05:00
|
|
|
}
|
|
|
|
})
|