mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
392e96b81d
prevent unnecessary logging when looking at headers of files where hitting the end of the file is expected.
25 lines
827 B
CoffeeScript
25 lines
827 B
CoffeeScript
fs = require "fs"
|
|
logger = require "logger-sharelatex"
|
|
|
|
module.exports = SafeReader =
|
|
|
|
# safely read up to size bytes from a file and return result as a
|
|
# string
|
|
|
|
readFile: (file, size, encoding, callback = (error, result) ->) ->
|
|
fs.open file, 'r', (err, fd) ->
|
|
return callback() if err? and err.code is 'ENOENT'
|
|
return callback(err) if err?
|
|
|
|
# safely return always closing the file
|
|
callbackWithClose = (err, result...) ->
|
|
fs.close fd, (err1) ->
|
|
return callback(err) if err?
|
|
return callback(err1) if err1?
|
|
callback(null, result...)
|
|
|
|
buff = new Buffer(size, 0) # fill with zeros
|
|
fs.read fd, buff, 0, buff.length, 0, (err, bytesRead, buffer) ->
|
|
return callbackWithClose(err) if err?
|
|
result = buffer.toString(encoding, 0, bytesRead)
|
|
callbackWithClose(null, result, bytesRead)
|