ArchiveManager.coffee 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. child = require "child_process"
  2. logger = require "logger-sharelatex"
  3. metrics = require "../../infrastructure/Metrics"
  4. fs = require "fs"
  5. Path = require "path"
  6. module.exports = ArchiveManager =
  7. extractZipArchive: (source, destination, _callback = (err) ->) ->
  8. callback = (args...) ->
  9. _callback(args...)
  10. _callback = () ->
  11. timer = new metrics.Timer("unzipDirectory")
  12. logger.log source: source, destination: destination, "unzipping file"
  13. unzip = child.spawn("unzip", [source, "-d", destination])
  14. # don't remove this line, some zips need
  15. # us to listen on this for some unknow reason
  16. unzip.stdout.on "data", (d)->
  17. error = null
  18. unzip.stderr.on "data", (chunk) ->
  19. error ||= ""
  20. error += chunk
  21. unzip.on "error", (err) ->
  22. logger.error {err, source, destination}, "unzip failed"
  23. if err.code == "ENOENT"
  24. logger.error "unzip command not found. Please check the unzip command is installed"
  25. callback(err)
  26. unzip.on "exit", () ->
  27. timer.done()
  28. if error?
  29. error = new Error(error)
  30. logger.error err:error, source: source, destination: destination, "error unzipping file"
  31. callback(error)
  32. findTopLevelDirectory: (directory, callback = (error, topLevelDir) ->) ->
  33. fs.readdir directory, (error, files) ->
  34. return callback(error) if error?
  35. if files.length == 1
  36. childPath = Path.join(directory, files[0])
  37. fs.stat childPath, (error, stat) ->
  38. return callback(error) if error?
  39. if stat.isDirectory()
  40. return callback(null, childPath)
  41. else
  42. return callback(null, directory)
  43. else
  44. return callback(null, directory)