mirror of
https://github.com/overleaf/overleaf.git
synced 2025-02-17 10:53:03 +00:00
check size of zip files
This commit is contained in:
parent
da242d90e6
commit
6664b67fba
2 changed files with 54 additions and 21 deletions
|
@ -4,38 +4,57 @@ metrics = require "../../infrastructure/Metrics"
|
||||||
fs = require "fs"
|
fs = require "fs"
|
||||||
Path = require "path"
|
Path = require "path"
|
||||||
|
|
||||||
|
ONE_MEG = 1024 * 1024
|
||||||
|
|
||||||
module.exports = ArchiveManager =
|
module.exports = ArchiveManager =
|
||||||
extractZipArchive: (source, destination, _callback = (err) ->) ->
|
extractZipArchive: (source, destination, _callback = (err) ->) ->
|
||||||
callback = (args...) ->
|
callback = (args...) ->
|
||||||
_callback(args...)
|
_callback(args...)
|
||||||
_callback = () ->
|
_callback = () ->
|
||||||
|
|
||||||
timer = new metrics.Timer("unzipDirectory")
|
child.exec "unzip -l #{source} | tail -n 1", (err, result)->
|
||||||
logger.log source: source, destination: destination, "unzipping file"
|
if err?
|
||||||
|
logger.err err:err, "error checking size of zip file"
|
||||||
|
return callback(err)
|
||||||
|
|
||||||
unzip = child.spawn("unzip", [source, "-d", destination])
|
totalSizeInBytes = result.trim()?.split(" ")?[0]
|
||||||
|
|
||||||
|
if !totalSizeInBytes?
|
||||||
|
logger.err source:source, "error getting bytes of zip"
|
||||||
|
return callback(new Error("something went wrong"))
|
||||||
|
|
||||||
# don't remove this line, some zips need
|
if totalSizeInBytes > ONE_MEG * 300
|
||||||
# us to listen on this for some unknow reason
|
logger.log source:source, totalSizeInBytes:totalSizeInBytes, "zip file too large"
|
||||||
unzip.stdout.on "data", (d)->
|
return callback(new Error("zip_too_large"))
|
||||||
|
|
||||||
|
|
||||||
error = null
|
|
||||||
unzip.stderr.on "data", (chunk) ->
|
|
||||||
error ||= ""
|
|
||||||
error += chunk
|
|
||||||
|
|
||||||
unzip.on "error", (err) ->
|
timer = new metrics.Timer("unzipDirectory")
|
||||||
logger.error {err, source, destination}, "unzip failed"
|
logger.log source: source, destination: destination, "unzipping file"
|
||||||
if err.code == "ENOENT"
|
|
||||||
logger.error "unzip command not found. Please check the unzip command is installed"
|
|
||||||
callback(err)
|
|
||||||
|
|
||||||
unzip.on "exit", () ->
|
unzip = child.spawn("unzip", [source, "-d", destination])
|
||||||
timer.done()
|
|
||||||
if error?
|
# don't remove this line, some zips need
|
||||||
error = new Error(error)
|
# us to listen on this for some unknow reason
|
||||||
logger.error err:error, source: source, destination: destination, "error unzipping file"
|
unzip.stdout.on "data", (d)->
|
||||||
callback(error)
|
|
||||||
|
error = null
|
||||||
|
unzip.stderr.on "data", (chunk) ->
|
||||||
|
error ||= ""
|
||||||
|
error += chunk
|
||||||
|
|
||||||
|
unzip.on "error", (err) ->
|
||||||
|
logger.error {err, source, destination}, "unzip failed"
|
||||||
|
if err.code == "ENOENT"
|
||||||
|
logger.error "unzip command not found. Please check the unzip command is installed"
|
||||||
|
callback(err)
|
||||||
|
|
||||||
|
unzip.on "exit", () ->
|
||||||
|
timer.done()
|
||||||
|
if error?
|
||||||
|
error = new Error(error)
|
||||||
|
logger.error err:error, source: source, destination: destination, "error unzipping file"
|
||||||
|
callback(error)
|
||||||
|
|
||||||
findTopLevelDirectory: (directory, callback = (error, topLevelDir) ->) ->
|
findTopLevelDirectory: (directory, callback = (error, topLevelDir) ->) ->
|
||||||
fs.readdir directory, (error, files) ->
|
fs.readdir directory, (error, files) ->
|
||||||
|
|
|
@ -15,6 +15,7 @@ describe "ArchiveManager", ->
|
||||||
@process.stderr = new events.EventEmitter
|
@process.stderr = new events.EventEmitter
|
||||||
@child =
|
@child =
|
||||||
spawn: sinon.stub().returns(@process)
|
spawn: sinon.stub().returns(@process)
|
||||||
|
exec: sinon.stub().callsArgWith(1, null, " 109042 2 files")
|
||||||
@metrics =
|
@metrics =
|
||||||
Timer: class Timer
|
Timer: class Timer
|
||||||
done: sinon.stub()
|
done: sinon.stub()
|
||||||
|
@ -58,6 +59,19 @@ describe "ArchiveManager", ->
|
||||||
it "should log out the error", ->
|
it "should log out the error", ->
|
||||||
@logger.error.called.should.equal true
|
@logger.error.called.should.equal true
|
||||||
|
|
||||||
|
describe "with a zip that is too large", ->
|
||||||
|
beforeEach (done) ->
|
||||||
|
@child.exec = sinon.stub().callsArgWith(1, null, " 10000000000009042 2 files")
|
||||||
|
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
|
||||||
|
@callback(error)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "should return the callback with an error", ->
|
||||||
|
@callback.calledWithExactly(new Error("zip_too_large")).should.equal true
|
||||||
|
|
||||||
|
it "should not call spawn", ->
|
||||||
|
@child.spawn.called.should.equal false
|
||||||
|
|
||||||
describe "with an error on the process", ->
|
describe "with an error on the process", ->
|
||||||
beforeEach (done) ->
|
beforeEach (done) ->
|
||||||
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
|
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
|
||||||
|
|
Loading…
Reference in a new issue