ProjectEntityHandler.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. const path = require('path')
  2. const DocstoreManager = require('../Docstore/DocstoreManager')
  3. const Errors = require('../Errors/Errors')
  4. const ProjectGetter = require('./ProjectGetter')
  5. const { promisifyAll } = require('../../util/promises')
  6. const ProjectEntityHandler = {
  7. getAllDocs(projectId, callback) {
  8. // We get the path and name info from the project, and the lines and
  9. // version info from the doc store.
  10. DocstoreManager.getAllDocs(projectId, (error, docContentsArray) => {
  11. if (error != null) {
  12. return callback(error)
  13. }
  14. // Turn array from docstore into a dictionary based on doc id
  15. const docContents = {}
  16. for (let docContent of docContentsArray) {
  17. docContents[docContent._id] = docContent
  18. }
  19. ProjectEntityHandler._getAllFolders(projectId, (error, folders) => {
  20. if (folders == null) {
  21. folders = {}
  22. }
  23. if (error != null) {
  24. return callback(error)
  25. }
  26. const docs = {}
  27. for (let folderPath in folders) {
  28. const folder = folders[folderPath]
  29. for (let doc of folder.docs || []) {
  30. const content = docContents[doc._id.toString()]
  31. if (content != null) {
  32. docs[path.join(folderPath, doc.name)] = {
  33. _id: doc._id,
  34. name: doc.name,
  35. lines: content.lines,
  36. rev: content.rev
  37. }
  38. }
  39. }
  40. }
  41. callback(null, docs)
  42. })
  43. })
  44. },
  45. getAllFiles(projectId, callback) {
  46. ProjectEntityHandler._getAllFolders(projectId, (err, folders) => {
  47. if (folders == null) {
  48. folders = {}
  49. }
  50. if (err != null) {
  51. return callback(err)
  52. }
  53. const files = {}
  54. for (let folderPath in folders) {
  55. const folder = folders[folderPath]
  56. for (let file of folder.fileRefs || []) {
  57. if (file != null) {
  58. files[path.join(folderPath, file.name)] = file
  59. }
  60. }
  61. }
  62. callback(null, files)
  63. })
  64. },
  65. getAllEntities(projectId, callback) {
  66. ProjectGetter.getProject(projectId, (err, project) => {
  67. if (err != null) {
  68. return callback(err)
  69. }
  70. if (project == null) {
  71. return callback(new Errors.NotFoundError('project not found'))
  72. }
  73. ProjectEntityHandler.getAllEntitiesFromProject(project, callback)
  74. })
  75. },
  76. getAllEntitiesFromProject(project, callback) {
  77. ProjectEntityHandler._getAllFoldersFromProject(project, (err, folders) => {
  78. if (folders == null) {
  79. folders = {}
  80. }
  81. if (err != null) {
  82. return callback(err)
  83. }
  84. const docs = []
  85. const files = []
  86. for (let folderPath in folders) {
  87. const folder = folders[folderPath]
  88. for (let doc of folder.docs || []) {
  89. if (doc != null) {
  90. docs.push({ path: path.join(folderPath, doc.name), doc })
  91. }
  92. }
  93. for (let file of folder.fileRefs || []) {
  94. if (file != null) {
  95. files.push({ path: path.join(folderPath, file.name), file })
  96. }
  97. }
  98. }
  99. callback(null, docs, files)
  100. })
  101. },
  102. getAllDocPathsFromProjectById(projectId, callback) {
  103. ProjectGetter.getProjectWithoutDocLines(projectId, (err, project) => {
  104. if (err != null) {
  105. return callback(err)
  106. }
  107. if (project == null) {
  108. return callback(Errors.NotFoundError('no project'))
  109. }
  110. ProjectEntityHandler.getAllDocPathsFromProject(project, callback)
  111. })
  112. },
  113. getAllDocPathsFromProject(project, callback) {
  114. ProjectEntityHandler._getAllFoldersFromProject(project, (err, folders) => {
  115. if (folders == null) {
  116. folders = {}
  117. }
  118. if (err != null) {
  119. return callback(err)
  120. }
  121. const docPath = {}
  122. for (let folderPath in folders) {
  123. const folder = folders[folderPath]
  124. for (let doc of folder.docs || []) {
  125. docPath[doc._id] = path.join(folderPath, doc.name)
  126. }
  127. }
  128. callback(null, docPath)
  129. })
  130. },
  131. getDoc(projectId, docId, options, callback) {
  132. if (options == null) {
  133. options = {}
  134. }
  135. if (typeof options === 'function') {
  136. callback = options
  137. options = {}
  138. }
  139. DocstoreManager.getDoc(projectId, docId, options, callback)
  140. },
  141. getDocPathByProjectIdAndDocId(projectId, docId, callback) {
  142. ProjectGetter.getProjectWithoutDocLines(projectId, (err, project) => {
  143. if (err != null) {
  144. return callback(err)
  145. }
  146. if (project == null) {
  147. return callback(new Errors.NotFoundError('no project'))
  148. }
  149. function recursivelyFindDocInFolder(basePath, docId, folder) {
  150. let docInCurrentFolder = (folder.docs || []).find(
  151. currentDoc => currentDoc._id.toString() === docId.toString()
  152. )
  153. if (docInCurrentFolder != null) {
  154. return path.join(basePath, docInCurrentFolder.name)
  155. } else {
  156. let docPath, childFolder
  157. for (childFolder of folder.folders || []) {
  158. docPath = recursivelyFindDocInFolder(
  159. path.join(basePath, childFolder.name),
  160. docId,
  161. childFolder
  162. )
  163. if (docPath != null) {
  164. return docPath
  165. }
  166. }
  167. return null
  168. }
  169. }
  170. const docPath = recursivelyFindDocInFolder(
  171. '/',
  172. docId,
  173. project.rootFolder[0]
  174. )
  175. if (docPath == null) {
  176. return callback(new Errors.NotFoundError('no doc'))
  177. }
  178. callback(null, docPath)
  179. })
  180. },
  181. _getAllFolders(projectId, callback) {
  182. ProjectGetter.getProjectWithoutDocLines(projectId, (err, project) => {
  183. if (err != null) {
  184. return callback(err)
  185. }
  186. if (project == null) {
  187. return callback(new Errors.NotFoundError('no project'))
  188. }
  189. ProjectEntityHandler._getAllFoldersFromProject(project, callback)
  190. })
  191. },
  192. _getAllFoldersFromProject(project, callback) {
  193. const folders = {}
  194. function processFolder(basePath, folder) {
  195. folders[basePath] = folder
  196. for (let childFolder of folder.folders || []) {
  197. if (childFolder.name != null) {
  198. processFolder(path.join(basePath, childFolder.name), childFolder)
  199. }
  200. }
  201. }
  202. processFolder('/', project.rootFolder[0])
  203. callback(null, folders)
  204. }
  205. }
  206. module.exports = ProjectEntityHandler
  207. module.exports.promises = promisifyAll(ProjectEntityHandler, {
  208. multiResult: {
  209. getAllEntities: ['docs', 'files'],
  210. getAllEntitiesFromProject: ['docs', 'files']
  211. }
  212. })