2683ff5f495a04c00df60dad5ba244c87a237ca3
[ccsdk/cds.git] /
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 IBM Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21 import { Component, OnInit } from '@angular/core';
22 import * as JSZip from 'jszip';
23 import { SortPipe } from '../../../../common/shared/pipes/sort.pipe';
24 import { LoaderService } from '../../../../common/core/services/loader.service';
25
26 @Component({
27   selector: 'app-zipfile-extraction',
28   templateUrl: './zipfile-extraction.component.html',
29   styleUrls: ['./zipfile-extraction.component.scss']
30 })
31 export class ZipfileExtractionComponent implements OnInit {
32   private paths = [];
33   private tree;
34   private zipFile: JSZip = new JSZip();
35   private fileObject: any;
36   private activationBlueprint: any;
37   private tocsaMetadaData: any;
38   private blueprintName: string;
39   private entryDefinition: string;
40   validfile: boolean = false;
41   uploadedFileName: string;
42
43   constructor(private loader: LoaderService) { }
44
45   ngOnInit() {
46   }
47   async buildFileViewData(zip) {
48     this.validfile = false;
49     this.paths = [];
50     console.log(zip.files);
51     for (var file in zip.files) {
52       console.log("name: " + zip.files[file].name);
53       this.fileObject = {
54         // nameForUIDisplay: this.uploadedFileName + '/' + zip.files[file].name,
55         // name: zip.files[file].name,
56         name: this.uploadedFileName + '/' + zip.files[file].name,
57         data: ''
58       };
59       const value = <any>await zip.files[file].async('string');
60       this.fileObject.data = value;
61       this.paths.push(this.fileObject);
62     }
63
64     if (this.paths) {
65       this.paths.forEach(path => {
66         if (path.name.includes("TOSCA.meta")) {
67           this.validfile = true
68         }
69       });
70     } else {
71       alert('Please update proper file');
72     }
73
74     if (this.validfile) {
75       this.fetchTOSACAMetadata();
76       this.paths = new SortPipe().transform(this.paths, 'asc', 'name');
77       this.tree = this.arrangeTreeData(this.paths);
78     } else {
79       alert('Please update proper file with TOSCA metadata');
80     }
81   }
82
83   arrangeTreeData(paths) {
84     const tree = [];
85
86     paths.forEach((path) => {
87
88       const pathParts = path.name.split('/');
89       // pathParts.shift();
90       let currentLevel = tree;
91
92       pathParts.forEach((part) => {
93         const existingPath = currentLevel.filter(level => level.name === part);
94
95         if (existingPath.length > 0) {
96           currentLevel = existingPath[0].children;
97         } else {
98           const newPart = {
99             name: part,
100             children: [],
101             data: path.data,
102             path: path.name
103           };
104           if (part.trim() == this.blueprintName.trim()) {
105             this.activationBlueprint = path.data;
106             newPart.data = JSON.parse(this.activationBlueprint.toString());
107             console.log('newpart', newPart);
108             this.entryDefinition = path.name.trim();
109           }
110           if (newPart.name !== '') {
111             currentLevel.push(newPart);
112             currentLevel = newPart.children;
113           }
114         }
115       });
116     });
117     this.loader.hideLoader();
118     return tree;
119   }
120
121   fetchTOSACAMetadata() {
122     let toscaData = {};
123     this.paths.forEach(file => {
124       if (file.name.includes('TOSCA.meta')) {
125         let keys = file.data.split("\n");
126         keys.forEach((key) => {
127           let propertyData = key.split(':');
128           toscaData[propertyData[0]] = propertyData[1];
129         });
130       }
131     });
132     this.blueprintName = (((toscaData['Entry-Definitions']).split('/'))[1]).toString();;
133     console.log(toscaData);
134   }
135
136 }