unpackzip.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var JSZip = require("jszip");
  2. var async = require("async");
  3. var fileType = require("file-type");
  4. function unpackzip(emitter) {
  5. return function(f) {
  6. if (!f) {
  7. var p = new Promise(function(resolve, reject) {
  8. reject("no file");
  9. })
  10. return p
  11. }
  12. var p = new Promise(function(resolve, reject) {
  13. JSZip.loadAsync(f) // 1) read the Blob
  14. .then(function(zip) {
  15. var nass;
  16. var obj = {};
  17. if (zip.files["namespace.manifest"]) {
  18. zip.file("namespace.manifest").async("string")
  19. .then(function(data) {
  20. nass= JSON.parse(data);
  21. emitter.emit("namespace", nass, cont)
  22. })
  23. } else {
  24. emitter.emit("nonamespace", cont)
  25. }
  26. function cont() {
  27. async.mapSeries(zip.files, function(z, next) {
  28. if (z.dir) {
  29. next()
  30. } else {
  31. zip.file(z.name).async("uint8array")
  32. .then(function(data) {
  33. var mimen = fileType(data);
  34. if (z.name.indexOf(".json") > -1 || z.name.indexOf(".link") > -1) {
  35. zip.file(z.name).async("string")
  36. .then(function(data) {
  37. if (z.name.indexOf(".json") > -1) {
  38. emitter.emit("json", z.name, JSON.parse(data))
  39. } else {
  40. if (z.name !== "namespace.manifest") {
  41. emitter.emit("file", z.name, (data))
  42. }
  43. }
  44. next()
  45. })
  46. } else {
  47. if (z.name.indexOf(".svg") > -1) {
  48. zip.file(z.name).async("string")
  49. .then(function(data) {
  50. if (z.name !== "namespace.manifest") {
  51. emitter.emit("file", z.name, (data))
  52. }
  53. next()
  54. })
  55. } else {
  56. if (z.name.indexOf(".js")===z.name.length-3) {
  57. zip.file(z.name).async("string")
  58. .then(function(data) {
  59. emitter.emit("jsfile", z.name, data, nass);
  60. next()
  61. })
  62. } else {
  63. zip.file(z.name).async("base64")
  64. .then(function(data) {
  65. if (z.name !== "namespace.manifest") {
  66. emitter.emit("file", z.name, (data), mimen)
  67. }
  68. next()
  69. })
  70. }
  71. }
  72. }
  73. })
  74. }
  75. }, function(err, done) {
  76. emitter.emit("done")
  77. resolve(obj);
  78. })
  79. }
  80. }, function(e) {
  81. reject(e)
  82. });
  83. })
  84. return p
  85. }
  86. }
  87. module.exports = unpackzip;