ArchiveManager.coffee 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. child = require "child_process"
  2. logger = require "logger-sharelatex"
  3. metrics = require "../../infrastructure/Metrics"
  4. module.exports = ArchiveManager =
  5. extractZipArchive: (source, destination, _callback = (err) ->) ->
  6. callback = (args...) ->
  7. _callback(args...)
  8. _callback = () ->
  9. timer = new metrics.Timer("unzipDirectory")
  10. logger.log source: source, destination: destination, "unzipping file"
  11. unzip = child.spawn("unzip", [source, "-d", destination])
  12. # don't remove this line, some zips need
  13. # us to listen on this for some unknow reason
  14. unzip.stdout.on "data", (d)->
  15. error = null
  16. unzip.stderr.on "data", (chunk) ->
  17. error ||= ""
  18. error += chunk
  19. unzip.on "error", (err) ->
  20. logger.error {err, source, destination}, "unzip failed"
  21. if err.code == "ENOENT"
  22. logger.error "unzip command not found. Please check the unzip command is installed"
  23. callback(err)
  24. unzip.on "exit", () ->
  25. timer.done()
  26. if error?
  27. error = new Error(error)
  28. logger.error err:error, source: source, destination: destination, "error unzipping file"
  29. callback(error)