mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
1be43911b4
Set Prettier's "trailingComma" setting to "es5" GitOrigin-RevId: 9f14150511929a855b27467ad17be6ab262fe5d5
61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
/* eslint-disable
|
|
node/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('logger-sharelatex')
|
|
const fs = require('fs')
|
|
const _ = require('underscore')
|
|
|
|
module.exports = FileHashManager = {
|
|
computeHash(filePath, callback) {
|
|
if (callback == null) {
|
|
callback = function (error, hashValue) {}
|
|
}
|
|
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)
|
|
})
|
|
},
|
|
}
|