@yelon/abc
Version:
Common business components of ng-yunzai.
126 lines (122 loc) • 4.39 kB
JavaScript
import { __decorate } from 'tslib';
import { HttpClient } from '@angular/common/http';
import * as i0 from '@angular/core';
import { inject, NgZone, Injectable } from '@angular/core';
import { saveAs } from 'file-saver';
import { YunzaiConfigService } from '@yelon/util/config';
import { ZoneOutside } from '@yelon/util/decorator';
import { LazyService } from '@yelon/util/other';
class ZipService {
http = inject(HttpClient);
lazy = inject(LazyService);
ngZone = inject(NgZone);
cogSrv = inject(YunzaiConfigService);
cog;
constructor() {
this.cog = this.cogSrv.merge('zip', {
url: 'https://cdn.jsdelivr.net/npm/jszip@3/dist/jszip.min.js',
utils: []
});
}
init() {
return this.lazy.load([this.cog.url].concat(this.cog.utils));
}
check(zip) {
if (!zip)
throw new Error('get instance via `ZipService.create()`');
}
/** 解压 */
read(fileOrUrl, options) {
return new Promise((resolve, reject) => {
const resolveCallback = (data) => {
this.ngZone.run(() => resolve(data));
};
this.init().then(() => {
// from url
if (typeof fileOrUrl === 'string') {
this.http.request('GET', fileOrUrl, { responseType: 'arraybuffer' }).subscribe({
next: (res) => {
JSZip.loadAsync(res, options).then((ret) => resolveCallback(ret));
},
error: err => {
reject(err);
}
});
return;
}
// from file
const reader = new FileReader();
reader.onload = (e) => {
JSZip.loadAsync(e.target.result, options).then((ret) => resolveCallback(ret));
};
reader.readAsBinaryString(fileOrUrl);
});
});
}
/** 创建 Zip 实例,用于创建压缩文件 */
create() {
return new Promise(resolve => {
this.init()
.then(() => {
const zipFile = new JSZip();
resolve(zipFile);
})
.catch(() => resolve(null));
});
}
/**
* 下载URL资源并写入 zip
*
* @param zip Zip 实例
* @param path Zip 路径,例如: `text.txt`、`txt/hi.txt`
* @param url URL 地址
*/
pushUrl(zip, path, url) {
this.check(zip);
return new Promise((resolve, reject) => {
this.http.request('GET', url, { responseType: 'arraybuffer' }).subscribe({
next: (res) => {
zip.file(path, res);
resolve();
},
error: error => {
reject({ url, error });
}
});
});
}
/**
* 保存Zip并执行打开保存对话框
*
* @param zip zip 对象,务必通过 `create()` 构建
* @param options 额外参数,
*/
save(zip, options) {
this.check(zip);
const opt = { filename: 'download.zip', ...options };
return new Promise((resolve, reject) => {
zip.generateAsync({ type: 'blob', ...opt.options }, opt.update).then(data => {
if (opt.callback)
opt.callback(data);
saveAs(data, opt.filename);
resolve();
}, err => {
reject(err);
});
});
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: ZipService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: ZipService, providedIn: 'root' });
}
__decorate([
ZoneOutside()
], ZipService.prototype, "read", null);
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.3", ngImport: i0, type: ZipService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}], ctorParameters: () => [], propDecorators: { read: [] } });
/**
* Generated bundle index. Do not edit.
*/
export { ZipService };
//# sourceMappingURL=zip.mjs.map