Преглед изворни кода

Merge pull request #17069 from overleaf/dp-mongoose-callback-linked-files-handler

Promisify LinkedFilesHandler

GitOrigin-RevId: 9c996dfd721b0a5dced989c9403ac3a5d9cbfc87
David пре 2 година
родитељ
комит
62b4a19788
1 измењених фајлова са 57 додато и 109 уклоњено
  1. 57 109
      services/web/app/src/Features/LinkedFiles/LinkedFilesHandler.js

+ 57 - 109
services/web/app/src/Features/LinkedFiles/LinkedFilesHandler.js

@@ -1,160 +1,108 @@
-/* eslint-disable
-    n/handle-callback-err,
-    max-len,
-    no-unused-vars,
-*/
-// TODO: This file was created by bulk-decaffeinate.
-// Fix any style issues and re-enable lint.
-/*
- * decaffeinate suggestions:
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
 const FileWriter = require('../../infrastructure/FileWriter')
 const FileWriter = require('../../infrastructure/FileWriter')
 const EditorController = require('../Editor/EditorController')
 const EditorController = require('../Editor/EditorController')
 const ProjectLocator = require('../Project/ProjectLocator')
 const ProjectLocator = require('../Project/ProjectLocator')
 const { Project } = require('../../models/Project')
 const { Project } = require('../../models/Project')
 const ProjectGetter = require('../Project/ProjectGetter')
 const ProjectGetter = require('../Project/ProjectGetter')
-const _ = require('lodash')
 const {
 const {
   ProjectNotFoundError,
   ProjectNotFoundError,
   V1ProjectNotFoundError,
   V1ProjectNotFoundError,
   BadDataError,
   BadDataError,
 } = require('./LinkedFilesErrors')
 } = require('./LinkedFilesErrors')
-const { promisifyAll } = require('@overleaf/promise-utils')
+const { callbackifyAll } = require('@overleaf/promise-utils')
 
 
 const LinkedFilesHandler = {
 const LinkedFilesHandler = {
-  getFileById(projectId, fileId, callback) {
-    if (callback == null) {
-      callback = function () {}
-    }
-    ProjectLocator.findElement(
+  async getFileById(projectId, fileId) {
+    const { element, path, folder } = await ProjectLocator.promises.findElement(
       {
       {
         project_id: projectId,
         project_id: projectId,
         element_id: fileId,
         element_id: fileId,
         type: 'file',
         type: 'file',
-      },
-      function (err, file, path, parentFolder) {
-        if (err != null) {
-          return callback(err)
-        }
-        callback(null, file, path, parentFolder)
       }
       }
     )
     )
+    return { file: element, path, parentFolder: folder }
   },
   },
 
 
-  getSourceProject(data, callback) {
-    if (callback == null) {
-      callback = function () {}
-    }
+  async getSourceProject(data) {
     const projection = { _id: 1, name: 1 }
     const projection = { _id: 1, name: 1 }
     if (data.v1_source_doc_id != null) {
     if (data.v1_source_doc_id != null) {
-      Project.findOne(
+      const project = await Project.findOne(
         { 'overleaf.id': data.v1_source_doc_id },
         { 'overleaf.id': data.v1_source_doc_id },
-        projection,
-        function (err, project) {
-          if (err != null) {
-            return callback(err)
-          }
-          if (project == null) {
-            return callback(new V1ProjectNotFoundError())
-          }
-          callback(null, project)
-        }
-      )
+        projection
+      ).exec()
+
+      if (project == null) {
+        throw new V1ProjectNotFoundError()
+      }
+
+      return project
     } else if (data.source_project_id != null) {
     } else if (data.source_project_id != null) {
-      ProjectGetter.getProject(
+      const project = await ProjectGetter.promises.getProject(
         data.source_project_id,
         data.source_project_id,
-        projection,
-        function (err, project) {
-          if (err != null) {
-            return callback(err)
-          }
-          if (project == null) {
-            return callback(new ProjectNotFoundError())
-          }
-          callback(null, project)
-        }
+        projection
       )
       )
+
+      if (project == null) {
+        throw new ProjectNotFoundError()
+      }
+
+      return project
     } else {
     } else {
-      callback(new BadDataError('neither v1 nor v2 id present'))
+      throw new BadDataError('neither v1 nor v2 id present')
     }
     }
   },
   },
 
 
-  importFromStream(
+  async importFromStream(
     projectId,
     projectId,
     readStream,
     readStream,
     linkedFileData,
     linkedFileData,
     name,
     name,
     parentFolderId,
     parentFolderId,
-    userId,
-    callback
+    userId
   ) {
   ) {
-    if (callback == null) {
-      callback = function () {}
-    }
-    callback = _.once(callback)
-    FileWriter.writeStreamToDisk(projectId, readStream, function (err, fsPath) {
-      if (err != null) {
-        return callback(err)
-      }
-      EditorController.upsertFile(
-        projectId,
-        parentFolderId,
-        name,
-        fsPath,
-        linkedFileData,
-        'upload',
-        userId,
-        (err, file) => {
-          if (err != null) {
-            return callback(err)
-          }
-          callback(null, file)
-        }
-      )
-    })
+    const fsPath = await FileWriter.promises.writeStreamToDisk(
+      projectId,
+      readStream
+    )
+
+    return await EditorController.promises.upsertFile(
+      projectId,
+      parentFolderId,
+      name,
+      fsPath,
+      linkedFileData,
+      'upload',
+      userId
+    )
   },
   },
 
 
-  importContent(
+  async importContent(
     projectId,
     projectId,
     content,
     content,
     linkedFileData,
     linkedFileData,
     name,
     name,
     parentFolderId,
     parentFolderId,
-    userId,
-    callback
+    userId
   ) {
   ) {
-    if (callback == null) {
-      callback = function () {}
-    }
-    callback = _.once(callback)
-    FileWriter.writeContentToDisk(projectId, content, function (err, fsPath) {
-      if (err != null) {
-        return callback(err)
-      }
-      EditorController.upsertFile(
-        projectId,
-        parentFolderId,
-        name,
-        fsPath,
-        linkedFileData,
-        'upload',
-        userId,
-        (err, file) => {
-          if (err != null) {
-            return callback(err)
-          }
-          callback(null, file)
-        }
-      )
-    })
+    const fsPath = await FileWriter.promises.writeContentToDisk(
+      projectId,
+      content
+    )
+
+    return await EditorController.promises.upsertFile(
+      projectId,
+      parentFolderId,
+      name,
+      fsPath,
+      linkedFileData,
+      'upload',
+      userId
+    )
   },
   },
 }
 }
 
 
 module.exports = {
 module.exports = {
-  ...LinkedFilesHandler,
-  promises: promisifyAll(LinkedFilesHandler, {
+  promises: LinkedFilesHandler,
+  ...callbackifyAll(LinkedFilesHandler, {
     multiResult: { getFileById: ['file', 'path', 'parentFolder'] },
     multiResult: { getFileById: ['file', 'path', 'parentFolder'] },
   }),
   }),
 }
 }