mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-29 20:11:32 +02:00
35 lines
1010 B
CoffeeScript
35 lines
1010 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) |