mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
ee85d948e2
GitOrigin-RevId: ef2ef77e26df59d1af3df6dc664e284d3c70102d
63 lines
1.7 KiB
JavaScript
63 lines
1.7 KiB
JavaScript
/* eslint-disable
|
|
no-undef,
|
|
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 'fs'
|
|
import crypto from 'crypto'
|
|
import _ from 'lodash'
|
|
import logger from '@overleaf/logger'
|
|
import OError from '@overleaf/o-error'
|
|
|
|
export function _getBlobHashFromString(string) {
|
|
const byteLength = Buffer.byteLength(string)
|
|
const hash = crypto.createHash('sha1')
|
|
hash.setEncoding('hex')
|
|
hash.update('blob ' + byteLength + '\x00')
|
|
hash.update(string, 'utf8')
|
|
hash.end()
|
|
return hash.read()
|
|
}
|
|
|
|
export function _getBlobHash(fsPath, _callback) {
|
|
if (_callback == null) {
|
|
_callback = function () {}
|
|
}
|
|
const callback = _.once(_callback)
|
|
|
|
return fs.stat(fsPath, function (err, stats) {
|
|
if (err != null) {
|
|
OError.tag(err, 'failed to stat file in _getBlobHash', { fsPath })
|
|
return callback(err)
|
|
}
|
|
const byteLength = stats.size
|
|
const hash = crypto.createHash('sha1')
|
|
hash.setEncoding('hex')
|
|
hash.update('blob ' + byteLength + '\x00')
|
|
|
|
const stream = fs.createReadStream(fsPath)
|
|
|
|
stream.on('error', function (err) {
|
|
return callback(
|
|
OError.tag(err, 'error streaming file from disk', {
|
|
fsPath,
|
|
byteLength,
|
|
})
|
|
)
|
|
})
|
|
|
|
stream.on('end', function () {
|
|
hash.end()
|
|
return callback(null, hash.read(), byteLength)
|
|
})
|
|
|
|
return stream.pipe(hash)
|
|
})
|
|
}
|