overleaf/services/web/app/src/Features/FileStore/FileHashManager.js
Jakob Ackermann f0bd6dda23 Merge pull request #7986 from overleaf/jpa-eslint-8
[misc] upgrade eslint packages to the latest version everywhere

GitOrigin-RevId: f1480d4a171acef82fb26c4aa54be3a6088b0ab3
2022-05-17 08:05:59 +00:00

61 lines
1.7 KiB
JavaScript

/* 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:
* 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
*/
let FileHashManager
const crypto = require('crypto')
const logger = require('@overleaf/logger')
const fs = require('fs')
const _ = require('underscore')
module.exports = FileHashManager = {
computeHash(filePath, callback) {
if (callback == null) {
callback = function () {}
}
callback = _.once(callback) // avoid double callbacks
// taken from v1/history/storage/lib/blob_hash.js
const getGitBlobHeader = byteLength => `blob ${byteLength}` + '\x00'
const getByteLengthOfFile = cb =>
fs.stat(filePath, function (err, stats) {
if (err != null) {
return cb(err)
}
return cb(null, stats.size)
})
return getByteLengthOfFile(function (err, byteLength) {
if (err != null) {
return callback(err)
}
const input = fs.createReadStream(filePath)
input.on('error', function (err) {
logger.warn({ filePath, err }, 'error opening file in computeHash')
return callback(err)
})
const hash = crypto.createHash('sha1')
hash.setEncoding('hex')
hash.update(getGitBlobHeader(byteLength))
hash.on('readable', function () {
const result = hash.read()
if (result != null) {
return callback(null, result.toString('hex'))
}
})
return input.pipe(hash)
})
},
}