FileStoreHandler.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import logger from '@overleaf/logger'
  2. import fs from 'node:fs'
  3. import Async from 'async'
  4. import FileHashManager from './FileHashManager.js'
  5. import HistoryManager from '../History/HistoryManager.mjs'
  6. import ProjectDetailsHandler from '../Project/ProjectDetailsHandler.mjs'
  7. import { File } from '../../models/File.js'
  8. import OError from '@overleaf/o-error'
  9. import { promisifyAll } from '@overleaf/promise-utils'
  10. import Modules from '../../infrastructure/Modules.js'
  11. const FileStoreHandler = {
  12. RETRY_ATTEMPTS: 3,
  13. uploadFileFromDisk(projectId, fileArgs, fsPath, callback) {
  14. // Look up the history id for the project if we don't have it already
  15. ProjectDetailsHandler.getDetails(projectId, function (err, project) {
  16. if (err) {
  17. return callback(err)
  18. }
  19. const historyId = project.overleaf?.history?.id
  20. if (!historyId) {
  21. return callback(new OError('missing history id'))
  22. }
  23. FileStoreHandler.uploadFileFromDiskWithHistoryId(
  24. projectId,
  25. historyId,
  26. fileArgs,
  27. fsPath,
  28. callback
  29. )
  30. })
  31. },
  32. _uploadToHistory(historyId, hash, size, fsPath, callback) {
  33. Async.retry(
  34. FileStoreHandler.RETRY_ATTEMPTS,
  35. cb =>
  36. HistoryManager.uploadBlobFromDisk(historyId, hash, size, fsPath, cb),
  37. error => {
  38. if (error) return callback(error, false)
  39. callback(null, true)
  40. }
  41. )
  42. },
  43. uploadFileFromDiskWithHistoryId(
  44. projectId,
  45. historyId,
  46. fileArgs,
  47. fsPath,
  48. callback
  49. ) {
  50. fs.lstat(fsPath, function (err, stat) {
  51. if (err) {
  52. logger.warn({ err, projectId, fileArgs, fsPath }, 'error stating file')
  53. callback(err)
  54. }
  55. if (!stat) {
  56. logger.warn(
  57. { projectId, fileArgs, fsPath },
  58. 'stat is not available, can not check file from disk'
  59. )
  60. return callback(new Error('error getting stat, not available'))
  61. }
  62. if (!stat.isFile()) {
  63. logger.debug(
  64. { projectId, fileArgs, fsPath },
  65. 'tried to upload symlink, not continuing'
  66. )
  67. return callback(new Error('can not upload symlink'))
  68. }
  69. const size = stat.size
  70. Modules.hooks.fire(
  71. 'preUploadFile',
  72. { projectId, historyId, fileArgs, fsPath, size },
  73. preUploadErr => {
  74. if (preUploadErr) {
  75. return callback(preUploadErr)
  76. }
  77. FileHashManager.computeHash(fsPath, function (err, hash) {
  78. if (err) {
  79. return callback(err)
  80. }
  81. FileStoreHandler._uploadToHistory(
  82. historyId,
  83. hash,
  84. stat.size,
  85. fsPath,
  86. function (err) {
  87. if (err) {
  88. return callback(err)
  89. }
  90. const fileRef = new File({ ...fileArgs, hash })
  91. Modules.hooks.fire(
  92. 'postUploadFile',
  93. {
  94. projectId,
  95. fileRef,
  96. size,
  97. },
  98. postUploadErr => {
  99. if (postUploadErr) {
  100. return callback(postUploadErr)
  101. }
  102. callback(err, fileRef, true, size)
  103. }
  104. )
  105. }
  106. )
  107. })
  108. }
  109. )
  110. })
  111. },
  112. }
  113. FileStoreHandler.promises = promisifyAll(FileStoreHandler, {
  114. multiResult: {
  115. uploadFileFromDisk: ['fileRef', 'createdBlob', 'size'],
  116. uploadFileFromDiskWithHistoryId: ['fileRef', 'createdBlob', 'size'],
  117. },
  118. })
  119. export default FileStoreHandler