mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
07b7566466
compute filestore hash GitOrigin-RevId: 23a909d2c2ea7aba3abfdb8f0c060e414a17e52e
35 lines
No EOL
1,010 B
CoffeeScript
35 lines
No EOL
1,010 B
CoffeeScript
crypto = require "crypto"
|
|
logger = require("logger-sharelatex")
|
|
fs = require("fs")
|
|
_ = require("underscore")
|
|
|
|
module.exports = FileHashManager =
|
|
|
|
computeHash: (filePath, callback = (error, hashValue) ->) ->
|
|
callback = _.once(callback) # avoid double callbacks
|
|
|
|
# taken from v1/history/storage/lib/blob_hash.js
|
|
getGitBlobHeader = (byteLength) ->
|
|
return 'blob ' + byteLength + '\x00'
|
|
|
|
getByteLengthOfFile = (cb) ->
|
|
fs.stat filePath, (err, stats) ->
|
|
return cb(err) if err?
|
|
cb(null, stats.size)
|
|
|
|
getByteLengthOfFile (err, byteLength) ->
|
|
return callback(err) if err?
|
|
|
|
input = fs.createReadStream(filePath)
|
|
input.on 'error', (err) ->
|
|
logger.err {filePath: filePath, err:err}, "error opening file in computeHash"
|
|
return callback(err)
|
|
|
|
hash = crypto.createHash("sha1")
|
|
hash.setEncoding('hex')
|
|
hash.update(getGitBlobHeader(byteLength))
|
|
hash.on 'readable', () ->
|
|
result = hash.read()
|
|
if result?
|
|
callback(null, result.toString('hex'))
|
|
input.pipe(hash) |