1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Zip3 = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var debug_ = require("debug");
|
6 | var request = require("request");
|
7 | var unzipper = require("unzipper");
|
8 | var UrlUtils_1 = require("../http/UrlUtils");
|
9 | var zip_1 = require("./zip");
|
10 | var debug = debug_("r2:utils#zip/zip3");
|
11 | var Zip3 = (function (_super) {
|
12 | tslib_1.__extends(Zip3, _super);
|
13 | function Zip3(filePath, zip) {
|
14 | var _this = _super.call(this) || this;
|
15 | _this.filePath = filePath;
|
16 | _this.zip = zip;
|
17 | _this.entries = {};
|
18 | _this.zip.files.forEach(function (file) {
|
19 | _this.entries[file.path] = file;
|
20 | });
|
21 | return _this;
|
22 | }
|
23 | Zip3.loadPromise = function (filePath) {
|
24 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
25 | var _this = this;
|
26 | return tslib_1.__generator(this, function (_a) {
|
27 | if ((0, UrlUtils_1.isHTTP)(filePath)) {
|
28 | return [2, Zip3.loadPromiseHTTP(filePath)];
|
29 | }
|
30 | return [2, new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
31 | var zip, err_1;
|
32 | return tslib_1.__generator(this, function (_a) {
|
33 | switch (_a.label) {
|
34 | case 0:
|
35 | _a.trys.push([0, 2, , 3]);
|
36 | return [4, unzipper.Open.file(filePath)];
|
37 | case 1:
|
38 | zip = _a.sent();
|
39 | return [3, 3];
|
40 | case 2:
|
41 | err_1 = _a.sent();
|
42 | debug(err_1);
|
43 | reject(err_1);
|
44 | return [2];
|
45 | case 3:
|
46 | debug(zip);
|
47 | resolve(new Zip3(filePath, zip));
|
48 | return [2];
|
49 | }
|
50 | });
|
51 | }); })];
|
52 | });
|
53 | });
|
54 | };
|
55 | Zip3.loadPromiseHTTP = function (filePath) {
|
56 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
57 | var _this = this;
|
58 | return tslib_1.__generator(this, function (_a) {
|
59 | return [2, new Promise(function (resolve, reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
60 | var zip, err_2;
|
61 | return tslib_1.__generator(this, function (_a) {
|
62 | switch (_a.label) {
|
63 | case 0:
|
64 | _a.trys.push([0, 2, , 3]);
|
65 | return [4, unzipper.Open.url(request.get, {
|
66 | headers: {},
|
67 | method: "GET",
|
68 | uri: filePath,
|
69 | url: filePath,
|
70 | })];
|
71 | case 1:
|
72 | zip = _a.sent();
|
73 | return [3, 3];
|
74 | case 2:
|
75 | err_2 = _a.sent();
|
76 | debug(err_2);
|
77 | reject(err_2);
|
78 | return [2];
|
79 | case 3:
|
80 | debug(zip);
|
81 | resolve(new Zip3(filePath, zip));
|
82 | return [2];
|
83 | }
|
84 | });
|
85 | }); })];
|
86 | });
|
87 | });
|
88 | };
|
89 | Zip3.prototype.freeDestroy = function () {
|
90 | debug("freeDestroy: Zip3 -- " + this.filePath);
|
91 | if (this.zip) {
|
92 | }
|
93 | };
|
94 | Zip3.prototype.entriesCount = function () {
|
95 | return this.zip.files.length;
|
96 | };
|
97 | Zip3.prototype.hasEntries = function () {
|
98 | return this.entriesCount() > 0;
|
99 | };
|
100 | Zip3.prototype.hasEntry = function (entryPath) {
|
101 | return this.hasEntries() && this.entries[entryPath];
|
102 | };
|
103 | Zip3.prototype.getEntries = function () {
|
104 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
105 | return tslib_1.__generator(this, function (_a) {
|
106 | if (!this.hasEntries()) {
|
107 | return [2, Promise.resolve([])];
|
108 | }
|
109 | return [2, Promise.resolve(Object.keys(this.entries))];
|
110 | });
|
111 | });
|
112 | };
|
113 | Zip3.prototype.entryStreamPromise = function (entryPath) {
|
114 | return tslib_1.__awaiter(this, void 0, void 0, function () {
|
115 | var _this = this;
|
116 | return tslib_1.__generator(this, function (_a) {
|
117 | if (!this.hasEntries() || !this.hasEntry(entryPath)) {
|
118 | return [2, Promise.reject("no such path in zip: " + entryPath)];
|
119 | }
|
120 | return [2, new Promise(function (resolve, _reject) {
|
121 | var entry = _this.entries[entryPath];
|
122 | debug(entry);
|
123 | var stream = entry.stream();
|
124 | var streamAndLength = {
|
125 | length: entry.size,
|
126 | reset: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
127 | return tslib_1.__generator(this, function (_a) {
|
128 | return [2, this.entryStreamPromise(entryPath)];
|
129 | });
|
130 | }); },
|
131 | stream: stream,
|
132 | };
|
133 | resolve(streamAndLength);
|
134 | })];
|
135 | });
|
136 | });
|
137 | };
|
138 | return Zip3;
|
139 | }(zip_1.Zip));
|
140 | exports.Zip3 = Zip3;
|
141 |
|
\ | No newline at end of file |