1 import {Component, OnInit} from '@angular/core';
2 import {FileSystemFileEntry, NgxFileDropEntry} from 'ngx-file-drop';
3 import {PackageCreationExtractionService} from '../../package-creation/package-creation-extraction.service';
4 import {Router} from '@angular/router';
5 import {PackageCreationStore} from '../../package-creation/package-creation.store';
6 import * as JSZip from 'jszip';
7 import {PackageCreationService} from '../../package-creation/package-creation.service';
8 import {ToastrService} from 'ngx-toastr';
9 import {PackagesStore} from '../../packages.store';
12 selector: 'app-import-package',
13 templateUrl: './import-package.component.html',
14 styleUrls: ['./import-package.component.css']
16 export class ImportPackageComponent implements OnInit {
18 public uploadedFiles: FileSystemFileEntry[] = [];
19 private fileNames: Set<string> = new Set();
20 fileToDelete: any = {};
21 zipFile: JSZip = new JSZip();
22 public files: NgxFileDropEntry[] = [];
24 constructor(private packageCreationExtractionService: PackageCreationExtractionService,
25 private packageCreationStore: PackageCreationStore,
26 private packageCreationService: PackageCreationService,
27 private toastService: ToastrService,
28 private packagesStore: PackagesStore,
29 private router: Router) {
37 removeInitFile(index) {
38 this.uploadedFiles.splice(index, 1);
41 public dropped(files: NgxFileDropEntry[]) {
43 for (const droppedFile of files) {
44 // Is it a file? & Not added before
45 if (droppedFile.fileEntry.isFile) {
46 const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
47 this.uploadedFiles.push(fileEntry);
48 console.log(fileEntry.name);
49 this.fileNames.add(fileEntry.name);
57 this.fileToDelete = file;
61 const filename = this.fileToDelete.key;
62 for (let i = 0; i < this.uploadedFiles.length; i++) {
63 console.log(this.uploadedFiles[i]);
64 if (this.uploadedFiles[i].name === filename) {
65 this.uploadedFiles.splice(i, 1);
71 resetTheUploadedFiles() {
72 this.uploadedFiles = [];
76 public fileOver(event) {
80 public fileLeave(event) {
84 importPackageAndViewIt() {
85 this.openFilesInCreationPackage();
86 this.saveFileToStore();
90 console.log(this.uploadedFiles.length);
91 const file = this.getFile(this.uploadedFiles[this.uploadedFiles.length - 1]);
92 this.packageCreationStore.clear();
93 this.packageCreationExtractionService.extractBlobToStore(file);
96 openFilesInCreationPackage() {
97 this.router.navigate(['/packages/createPackage/']);
100 async getFile(fileEntry) {
102 return await new Promise((resolve, reject) => fileEntry.file(resolve, reject));
109 const file = this.getFile(this.uploadedFiles[this.uploadedFiles.length - 1]);
110 this.zipFile = new JSZip();
111 this.zipFile.loadAsync(file).then(zip => {
113 console.log(this.zipFile);
114 this.resetTheUploadedFiles();
115 this.zipFile.generateAsync({type: 'blob'}).then(blob => {
116 this.packageCreationService.savePackage(blob).subscribe(
117 bluePrintDetailModels => {
118 this.toastService.info('package is imported and saved successfully ');
119 this.router.navigate(['/packages']);
120 this.packagesStore.getAll();
122 this.toastService.error('there is an error happened ' + error));
128 const file = this.getFile(this.uploadedFiles[this.uploadedFiles.length - 1]);
129 this.zipFile = new JSZip();
130 this.zipFile.loadAsync(file).then(zip => {
132 console.log(this.zipFile);
133 this.resetTheUploadedFiles();
134 this.zipFile.generateAsync({type: 'blob'}).then(blob => {
135 this.packageCreationService.deploy(blob).subscribe(
136 bluePrintDetailModels => {
137 this.toastService.info('package is imported and deployed successfully ');
138 this.router.navigate(['/packages']);
139 this.packagesStore.getAll();
141 this.toastService.error('there is an error happened ' + error));