123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- var JSZip = require("jszip");
- var async = require("async");
- var fileType = require("file-type");
- function unpackzip(emitter) {
- return function(f) {
- if (!f) {
- var p = new Promise(function(resolve, reject) {
- reject("no file");
- })
- return p
- }
- var p = new Promise(function(resolve, reject) {
- JSZip.loadAsync(f) // 1) read the Blob
- .then(function(zip) {
- var nass;
- var obj = {};
- if (zip.files["namespace.manifest"]) {
- zip.file("namespace.manifest").async("string")
- .then(function(data) {
- nass= JSON.parse(data);
- emitter.emit("namespace", nass, cont)
- })
- } else {
- emitter.emit("nonamespace", cont)
- }
- function cont() {
- async.mapSeries(zip.files, function(z, next) {
- if (z.dir) {
- next()
- } else {
- zip.file(z.name).async("uint8array")
- .then(function(data) {
- var mimen = fileType(data);
- if (z.name.indexOf(".json") > -1 || z.name.indexOf(".link") > -1) {
- zip.file(z.name).async("string")
- .then(function(data) {
- if (z.name.indexOf(".json") > -1) {
- emitter.emit("json", z.name, JSON.parse(data))
- } else {
- if (z.name !== "namespace.manifest") {
- emitter.emit("file", z.name, (data))
- }
- }
- next()
- })
- } else {
- if (z.name.indexOf(".svg") > -1) {
- zip.file(z.name).async("string")
- .then(function(data) {
- if (z.name !== "namespace.manifest") {
- emitter.emit("file", z.name, (data))
- }
- next()
- })
- } else {
- if (z.name.indexOf(".js")===z.name.length-3) {
- zip.file(z.name).async("string")
- .then(function(data) {
- emitter.emit("jsfile", z.name, data, nass);
-
- next()
- })
- } else {
- zip.file(z.name).async("base64")
- .then(function(data) {
- if (z.name !== "namespace.manifest") {
- emitter.emit("file", z.name, (data), mimen)
- }
- next()
- })
- }
- }
- }
- })
- }
- }, function(err, done) {
- emitter.emit("done")
- resolve(obj);
- })
- }
- }, function(e) {
- reject(e)
- });
- })
- return p
- }
- }
- module.exports = unpackzip;
|