overleaf/services/project-history/app/js/LargeFileManager.js
Antoine Clausse 7f48c67512 Add prefer-node-protocol ESLint rule (#21532)
* Add `unicorn/prefer-node-protocol`

* Fix `unicorn/prefer-node-protocol` ESLint errors

* Run `npm run format:fix`

* Add sandboxed-module sourceTransformers in mocha setups

Fix `no such file or directory, open 'node:fs'` in `sandboxed-module`

* Remove `node:` in the SandboxedModule requires

* Fix new linting errors with `node:`

GitOrigin-RevId: 68f6e31e2191fcff4cb8058dd0a6914c14f59926
2024-11-11 09:04:51 +00:00

88 lines
2.6 KiB
JavaScript

/* eslint-disable
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
import fs from 'node:fs'
import { randomUUID } from 'node:crypto'
import Path from 'node:path'
import logger from '@overleaf/logger'
import OError from '@overleaf/o-error'
import metrics from '@overleaf/metrics'
import Settings from '@overleaf/settings'
import _ from 'lodash'
import * as HistoryStoreManager from './HistoryStoreManager.js'
import * as HashManager from './HashManager.js'
export function createStub(fsPath, fileId, fileSize, fileHash, callback) {
if (callback == null) {
callback = function () {}
}
callback = _.once(callback)
const newFsPath = Path.join(
Settings.path.uploadFolder,
randomUUID() + `-${fileId}-stub`
)
const writeStream = fs.createWriteStream(newFsPath)
writeStream.on('error', function (error) {
OError.tag(error, 'error writing stub file', { fsPath, newFsPath })
return fs.unlink(newFsPath, () => callback(error))
})
writeStream.on('finish', function () {
logger.debug(
{ fsPath, fileId, fileSize, fileHash },
'replaced large file with stub'
)
return callback(null, newFsPath)
}) // let the consumer unlink the file
const stubLines = [
'FileTooLargeError v1',
'File too large to be stored in history service',
`id ${fileId}`,
`size ${fileSize} bytes`,
`hash ${fileHash}`,
'\0', // null byte to make this a binary file
]
writeStream.write(stubLines.join('\n'))
return writeStream.end()
}
export function replaceWithStubIfNeeded(fsPath, fileId, fileSize, callback) {
if (callback == null) {
callback = function () {}
}
if (
Settings.maxFileSizeInBytes != null &&
fileSize > Settings.maxFileSizeInBytes
) {
logger.error(
{ fsPath, fileId, maxFileSizeInBytes: Settings.maxFileSizeInBytes },
'file too large, will use stub'
)
return HashManager._getBlobHash(fsPath, function (error, fileHash) {
if (error != null) {
return callback(error)
}
return createStub(
fsPath,
fileId,
fileSize,
fileHash,
function (error, newFsPath) {
if (error != null) {
return callback(error)
}
return callback(null, newFsPath)
}
)
})
} else {
return callback(null, fsPath)
}
}