| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 TATA ELXSI |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | Author: KUMARAN M (kumaran.m@tataelxsi.co.in), RAJESH S (rajesh.s@tataelxsi.co.in), BARATH KUMAR R (barath.r@tataelxsi.co.in) |
| 17 | */ |
| 18 | /** |
| 19 | * @file Provider for Shared Service |
| 20 | */ |
| 21 | import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; |
| 22 | import { EventEmitter, Injectable, Output } from '@angular/core'; |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame] | 23 | import { FormArray, FormControl, FormGroup } from '@angular/forms'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 24 | import { Router } from '@angular/router'; |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 25 | import { TranslateService } from '@ngx-translate/core'; |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 26 | import { |
| 27 | CONSTANTNUMBER, |
| 28 | DOMAINS, |
| 29 | ERRORDATA, |
| 30 | FILESETTINGS, |
| 31 | GETAPIURLHEADER, |
| 32 | PACKAGEINFO, |
| 33 | PAGERSMARTTABLE, |
| 34 | SMARTTABLECLASS, |
| 35 | TARSETTINGS, |
| 36 | TYPESECTION |
| 37 | } from 'CommonModel'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 38 | import { environment } from 'environment'; |
| 39 | import * as HttpStatus from 'http-status-codes'; |
| 40 | import * as untar from 'js-untar'; |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 41 | import { ActiveToast, ToastrService } from 'ngx-toastr'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 42 | import * as pako from 'pako'; |
| 43 | import { RestService } from 'RestService'; |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 44 | import { Observable } from 'rxjs'; |
| 45 | import { map } from 'rxjs/operators'; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 46 | |
| 47 | /** This is added globally by the tar.js library */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 48 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 49 | declare const Tar: any; |
| 50 | |
| 51 | /** |
| 52 | * An Injectable is a class adorned with the @Injectable decorator function. |
| 53 | * @Injectable takes a metadata object that tells Angular how to compile and run module code |
| 54 | */ |
| 55 | @Injectable({ |
| 56 | providedIn: 'root' |
| 57 | }) |
| 58 | /** Exporting a class @exports SharedService */ |
| 59 | export class SharedService { |
| 60 | /** call the parent using event information @private */ |
| 61 | @Output() public dataEvent: EventEmitter<{}> = new EventEmitter<{}>(); |
| 62 | |
| 63 | /** Variables to hold regexp pattern for URL */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 64 | public REGX_URL_PATTERN: RegExp = new RegExp(/^(http?|ftp|https):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.[a-zA-Z0-9]{2,15})(:((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4})))*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/); |
| 65 | |
| 66 | /** Variables to hold regexp pattern for IP Address */ |
| 67 | public REGX_IP_PATTERN: RegExp = new RegExp(/^(?:(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(?!$)|$)){4}$/); |
| 68 | |
| 69 | /** Variables to hold regexp pattern for Port Number */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 70 | public REGX_PORT_PATTERN: RegExp = new RegExp(/^((6553[0-5])|(655[0-2][0-9])|(65[0-4][0-9]{2})|(6[0-4][0-9]{3})|([1-5][0-9]{4})|([0-5]{0,5})|([0-9]{1,4}))$/); |
| 71 | |
| 72 | /** Variables to hold regexp pattern for DPID */ |
| 73 | public REGX_DPID_PATTERN: RegExp = new RegExp(/^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$/); |
| 74 | |
| 75 | /** Variable to hold regexp pattern for password */ |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 76 | public REGX_PASSWORD_PATTERN: RegExp = new RegExp(/^.*(?=.{8,})((?=.*[!@#$%^&*()\-_=+{};:,<.>]){1})(?=.*\d)((?=.*[a-z]){1})((?=.*[A-Z]){1}).*$/); |
| 77 | |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 78 | /** Variables to hold regexp pattern for Latitude */ |
| 79 | public REGX_LAT_PATTERN: RegExp = new RegExp(/^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,15})?))$/); |
| 80 | |
| 81 | /** Variables to hold regexp pattern for Longitude */ |
| Barath Kumar R | 1245fc8 | 2021-04-16 13:34:06 +0530 | [diff] [blame] | 82 | public REGX_LONG_PATTERN: RegExp = new RegExp(/^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,15})?))$/); |
| 83 | |
| SANDHYA.JS | 6c68608 | 2024-06-10 21:39:41 +0530 | [diff] [blame] | 84 | /** Variable to hold regexp pattern for EMAIL */ |
| 85 | public REGX_EMAIL_PATTERN: RegExp = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; |
| 86 | |
| Barath Kumar R | 403234e | 2020-07-07 15:48:58 +0530 | [diff] [blame] | 87 | /** Variables to hold maxlength for the description @public */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 88 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| Barath Kumar R | 403234e | 2020-07-07 15:48:58 +0530 | [diff] [blame] | 89 | public MAX_LENGTH_DESCRIPTION: number = 500; |
| 90 | |
| 91 | /** Variables to hold maxlength for the name @public */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 92 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| Barath Kumar R | 403234e | 2020-07-07 15:48:58 +0530 | [diff] [blame] | 93 | public MAX_LENGTH_NAME: number = 50; |
| 94 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 95 | /** FormGroup instance added to the form @ html @public */ |
| 96 | public formGroup: FormGroup; |
| 97 | |
| 98 | /** Controls the go to top button on scroll @public */ |
| 99 | public showGotoTop: boolean; |
| 100 | |
| 101 | /** Holds OSM Version value @public */ |
| 102 | public osmVersion: string; |
| 103 | |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 104 | /** Holds Last Login Toaster Message @public */ |
| 105 | public lastLoginMessage: string; |
| 106 | |
| 107 | /** Holds Failed Attempts Toaster Message @public */ |
| 108 | public failedAttemptsMessage: string; |
| 109 | |
| 110 | /** Holds No Of Days Toaster Message @public */ |
| 111 | public daysMessage: string; |
| 112 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 113 | /** express number for time manupulation -2 */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 114 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 115 | private epochTimeMinus2: number = -2; |
| 116 | |
| 117 | /** express number for time manupulation 1000 */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 118 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 119 | private epochTime1000: number = 1000; |
| 120 | |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 121 | /** express number for time manupulation 60 */ |
| 122 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 123 | private epochTime60: number = 60; |
| 124 | |
| 125 | /** express number for time manupulation 24 */ |
| 126 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 127 | private epochTime24: number = 24; |
| 128 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 129 | /** Random string generator length */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 130 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 131 | private randomStringLength: number = 4; |
| 132 | |
| SANDHYA.JS | faf168f | 2024-03-07 23:08:48 +0530 | [diff] [blame] | 133 | /** express number for rgb manipulation */ |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 134 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| SANDHYA.JS | faf168f | 2024-03-07 23:08:48 +0530 | [diff] [blame] | 135 | private colourHour: number = 10; |
| 136 | |
| 137 | /** express number for rgb manipulation*/ |
| 138 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 139 | private colourMin: number = 5; |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 140 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 141 | /** Instance of the rest service @private */ |
| 142 | private restService: RestService; |
| 143 | |
| 144 | /** Service holds the router information @private */ |
| 145 | private router: Router; |
| 146 | |
| 147 | /** Check for the root directory @private */ |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 148 | // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 149 | private directoryCount: number = 2; |
| 150 | |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 151 | /** express number for time manupulation 1000 */ |
| 152 | private toasterSettings: {} = { |
| 153 | enableHtml: true, |
| 154 | closeButton: true, |
| 155 | timeOut: 2000 |
| 156 | }; |
| 157 | |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 158 | /** Contains tranlsate instance @private */ |
| 159 | private translateService: TranslateService; |
| 160 | |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 161 | /** Contains toaster instance @private */ |
| 162 | private toaster: ToastrService; |
| 163 | |
| 164 | constructor(restService: RestService, router: Router, translateService: TranslateService, toaster: ToastrService) { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 165 | this.restService = restService; |
| 166 | this.router = router; |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 167 | this.translateService = translateService; |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 168 | this.toaster = toaster; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** convert epoch time function @public */ |
| 172 | public convertEpochTime(unixtimestamp: number): string { |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 173 | if (!isNullOrUndefined(unixtimestamp)) { |
| 174 | const monthsArr: string[] = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
| 175 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; |
| 176 | const date: Date = new Date(unixtimestamp * this.epochTime1000); |
| 177 | const year: number = date.getFullYear(); |
| 178 | const month: string = monthsArr[date.getMonth()]; |
| 179 | const day: number = date.getDate(); |
| 180 | const hours: number = date.getHours(); |
| 181 | const minutes: string = '0' + date.getMinutes(); |
| 182 | const seconds: string = '0' + date.getSeconds(); |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 183 | // eslint-disable-next-line deprecation/deprecation |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 184 | return month + '-' + day + '-' + year + ' ' + hours + ':' + minutes.substr(this.epochTimeMinus2) + ':' |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 185 | // eslint-disable-next-line deprecation/deprecation |
| Barath Kumar R | 09cd4ec | 2020-07-07 16:12:32 +0530 | [diff] [blame] | 186 | + seconds.substr(this.epochTimeMinus2); |
| 187 | } |
| 188 | return this.translateService.instant('NODATE'); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 189 | } |
| 190 | |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 191 | /** convert epoch time function to No of days @public */ |
| 192 | public converEpochToDays(date: string): number { |
| 193 | if (!isNullOrUndefined(date)) { |
| 194 | const today: Date = new Date(); |
| 195 | const accountDate: Date = new Date(date); |
| SANDHYA.JS | c6c72e3 | 2023-10-12 11:29:52 +0530 | [diff] [blame] | 196 | const toasterDate: number = (accountDate.getTime() - |
| 197 | today.getTime()) / this.epochTime1000 / this.epochTime60 / this.epochTime60 / this.epochTime24; |
| 198 | if (toasterDate >= 0 || toasterDate < 1) { |
| 199 | return Math.round(toasterDate); |
| 200 | } |
| 201 | return Math.floor(toasterDate); |
| SANDHYA.JS | 1b17c43 | 2023-04-26 17:54:57 +0530 | [diff] [blame] | 202 | } |
| 203 | return this.translateService.instant('N/A'); |
| 204 | } |
| 205 | |
| 206 | /** show toaster for password & account expiry @public */ |
| 207 | public showToaster(lastLogin: string, failedAttempts: string, passwordNoOfDays: string, |
| 208 | accountNoOfDays: string, passwordExpireMessage: string, accountExpireMessage: string, |
| 209 | passwordMessage: string, accountMessage: string): ActiveToast<string> { |
| 210 | this.lastLoginMessage = this.translateService.instant('PAGE.LOGIN.LASTACCESS'); |
| 211 | this.failedAttemptsMessage = this.translateService.instant('PAGE.LOGIN.FAILED'); |
| 212 | return this.toaster.info(this.lastLoginMessage + ':' + ' ' + lastLogin + |
| 213 | '</br>' + this.failedAttemptsMessage + ':' + ' ' + failedAttempts + |
| 214 | '</br>' + passwordExpireMessage + ' ' + passwordNoOfDays + ' ' + passwordMessage + |
| 215 | '</br>' + accountExpireMessage + ' ' + accountNoOfDays + ' ' + accountMessage, |
| 216 | this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings); |
| 217 | } |
| 218 | |
| 219 | /** show toaster for password expiry @public */ |
| 220 | public passwordToaster(lastLogin: string, failedAttempts: string, passwordNoOfDays: string, |
| 221 | passwordExpireMessage: string, passwordMessage: string): ActiveToast<string> { |
| 222 | this.lastLoginMessage = this.translateService.instant('PAGE.LOGIN.LASTACCESS'); |
| 223 | this.failedAttemptsMessage = this.translateService.instant('PAGE.LOGIN.FAILED'); |
| 224 | return this.toaster.info(this.lastLoginMessage + ':' + ' ' + lastLogin + |
| 225 | '</br>' + this.failedAttemptsMessage + ':' + ' ' + failedAttempts + |
| 226 | '</br>' + passwordExpireMessage + ' ' + passwordNoOfDays + ' ' + passwordMessage, |
| 227 | this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings); |
| 228 | } |
| 229 | |
| 230 | /** show toaster for account expiry @public */ |
| 231 | public accountToaster(lastLogin: string, failedAttempts: string, |
| 232 | accountNoOfDays: string, accountExpireMessage: string, accountMessage: string): ActiveToast<string> { |
| 233 | this.lastLoginMessage = this.translateService.instant('PAGE.LOGIN.LASTACCESS'); |
| 234 | this.failedAttemptsMessage = this.translateService.instant('PAGE.LOGIN.FAILED'); |
| 235 | return this.toaster.info(this.lastLoginMessage + ':' + ' ' + lastLogin + |
| 236 | '</br>' + this.failedAttemptsMessage + ':' + ' ' + failedAttempts + |
| 237 | '</br>' + accountExpireMessage + ' ' + accountNoOfDays + ' ' + accountMessage, |
| 238 | this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings); |
| 239 | } |
| 240 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 241 | /** Download Files function @public */ |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 242 | public downloadFiles(name: string, binaryData: Blob[], filetype: string): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 243 | const downloadLink: HTMLAnchorElement = document.createElement('a'); |
| 244 | downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: filetype })); |
| Barath Kumar R | 063a3f1 | 2020-12-29 16:35:09 +0530 | [diff] [blame] | 245 | if (name !== undefined) { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 246 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 247 | const newVariable: any = window.navigator; |
| 248 | if (newVariable.msSaveOrOpenBlob) { |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame] | 249 | if (filetype === 'text/plain') { |
| 250 | newVariable.msSaveBlob(new Blob(binaryData, { type: 'text/yaml' }), 'OSM_Export_' + name + '.yaml'); |
| 251 | } else { |
| 252 | newVariable.msSaveBlob(new Blob(binaryData, { type: filetype }), 'OSM_Export_' + name + '.tar.gz'); |
| 253 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 254 | } else { |
| SANDHYA.JS | b534715 | 2024-10-15 11:41:45 +0530 | [diff] [blame] | 255 | if (filetype === 'text/plain') { |
| 256 | downloadLink.setAttribute('download', 'OSM_Export_' + name + '.yaml'); |
| 257 | } else { |
| 258 | downloadLink.setAttribute('download', 'OSM_Export_' + name + '.tar.gz'); |
| 259 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 260 | document.body.appendChild(downloadLink); |
| 261 | downloadLink.click(); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /** Call this method after delete perform action is completed in the ng-smart-table data @public */ |
| 267 | public callData(): void { |
| 268 | this.dataEvent.emit(); |
| 269 | } |
| 270 | |
| 271 | /** Generate random string @public */ |
| 272 | public randomString(): string { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 273 | let result: string = ''; |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 274 | for (let randomStringRef: number = this.randomStringLength; randomStringRef > 0; --randomStringRef) { |
| SANDHYA.JS | faf168f | 2024-03-07 23:08:48 +0530 | [diff] [blame] | 275 | result += new Date().getSeconds(); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 276 | } |
| 277 | return result; |
| 278 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 279 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 280 | /** Function to read uploaded file String @public */ |
| 281 | public async getFileString(files: FileList, fileType: string): Promise<string | ArrayBuffer> { |
| 282 | const reader: FileReader = new FileReader(); |
| 283 | return new Promise<string | ArrayBuffer>((resolve: Function, reject: Function): void => { |
| 284 | if (this.vaildataFileInfo(files[0], fileType)) { |
| 285 | this.readFileContent(reader, files[0], fileType); |
| 286 | } else { |
| 287 | reject('typeError'); |
| 288 | } |
| 289 | reader.onload = (): void => { |
| 290 | if (reader.result === null) { |
| 291 | reject('contentError'); |
| 292 | } |
| 293 | resolve(reader.result); |
| 294 | }; |
| 295 | reader.onerror = (event: Event): void => { |
| 296 | reject('contentError'); |
| 297 | }; |
| 298 | }); |
| 299 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 300 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 301 | /** Method to handle tar and tar.gz file for shared YAML file content @public */ |
| 302 | public async targzFile(packageInfo: PACKAGEINFO): Promise<string | ArrayBuffer> { |
| 303 | return new Promise<string | ArrayBuffer>((resolve: Function, reject: Function): void => { |
| 304 | const httpOptions: GETAPIURLHEADER = this.getHttpOptions(); |
| 305 | let apiUrl: string = ''; |
| 306 | apiUrl = packageInfo.packageType === 'nsd' ? environment.NSDESCRIPTORS_URL + '/' + packageInfo.id + '/nsd_content' : |
| 307 | environment.VNFPACKAGES_URL + '/' + packageInfo.id + '/package_content'; |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 308 | this.restService.getResource(apiUrl, httpOptions).subscribe((response: ArrayBuffer): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 309 | try { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 310 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 311 | const tar: any = new Tar(); |
| 312 | const originalInput: Uint8Array = pako.inflate(response, { to: 'Uint8Array' }); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 313 | untar(originalInput.buffer).then((extractedFiles: TARSETTINGS[]): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 314 | const getFoldersFiles: {}[] = extractedFiles; |
| 315 | const folderNameStr: string = extractedFiles[0].name; |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 316 | getFoldersFiles.forEach((value: TARSETTINGS): void => { |
| Barath Kumar R | db1aeb0 | 2020-10-13 18:14:16 +0530 | [diff] [blame] | 317 | const fileValueObj: FILESETTINGS = this.createFileValueObject(value); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 318 | const getRootFolder: string[] = value.name.split('/'); |
| 319 | if (value.name.startsWith(folderNameStr) && |
| 320 | (value.name.endsWith('.yaml') || value.name.endsWith('.yml')) && |
| 321 | getRootFolder.length === this.directoryCount) { |
| Barath Kumar R | db1aeb0 | 2020-10-13 18:14:16 +0530 | [diff] [blame] | 322 | tar.append(value.name, packageInfo.descriptor, fileValueObj); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 323 | } else { |
| 324 | if (value.type !== 'L') { |
| Barath Kumar R | db1aeb0 | 2020-10-13 18:14:16 +0530 | [diff] [blame] | 325 | tar.append(value.name, new Uint8Array(value.buffer), fileValueObj); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | }); |
| 329 | const out: Uint8Array = tar.out; |
| 330 | const originalOutput: Uint8Array = pako.gzip(out); |
| 331 | resolve(originalOutput.buffer); |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 332 | }, (err: string): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 333 | reject(''); |
| 334 | }); |
| 335 | } catch (e) { |
| 336 | reject(''); |
| 337 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 338 | }, (error: HttpErrorResponse): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 339 | if (error.status === HttpStatus.NOT_FOUND || error.status === HttpStatus.UNAUTHORIZED) { |
| SANDHYA.JS | 0a34dfa | 2023-04-25 23:59:41 +0530 | [diff] [blame] | 340 | this.router.navigateByUrl('404', { skipLocationChange: true }).catch((): void => { |
| 341 | // Catch Navigation Error |
| 342 | }); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 343 | } else { |
| 344 | this.restService.handleError(error, 'get'); |
| 345 | reject(''); |
| 346 | } |
| 347 | }); |
| 348 | }); |
| 349 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 350 | |
| Barath Kumar R | db1aeb0 | 2020-10-13 18:14:16 +0530 | [diff] [blame] | 351 | /** Method to return the file information @public */ |
| 352 | public createFileValueObject(value: TARSETTINGS): FILESETTINGS { |
| 353 | return { |
| 354 | type: value.type, |
| 355 | linkname: value.linkname, |
| 356 | owner: value.uname, |
| 357 | group: value.gname |
| 358 | }; |
| 359 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 360 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 361 | /** Method to check given string is JSON or not @public */ |
| 362 | public checkJson(jsonString: string): boolean { |
| 363 | jsonString = jsonString.replace(/'/g, '"'); |
| 364 | try { |
| 365 | JSON.parse(jsonString); |
| 366 | } catch (e) { |
| 367 | return false; |
| 368 | } |
| 369 | return true; |
| 370 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 371 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 372 | /** Clean the form before submit @public */ |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame] | 373 | public cleanForm(formGroup: FormGroup | FormArray, formName?: string): void { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 374 | Object.keys(formGroup.controls).forEach((key: string) => { |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame] | 375 | const control = formGroup.get(key); |
| 376 | if (control instanceof FormArray && key !== 'config') { |
| 377 | control.controls.forEach((ctrl, index) => { |
| 378 | if (ctrl instanceof FormGroup) { |
| 379 | this.cleanForm(ctrl); |
| 380 | } else if (ctrl instanceof FormControl) { |
| 381 | if (typeof ctrl.value === 'string') { |
| 382 | ctrl.setValue(ctrl.value.trim()); |
| 383 | } |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 384 | } |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame] | 385 | }); |
| 386 | } else if (control instanceof FormGroup && key !== 'config') { |
| 387 | this.cleanForm(control); |
| 388 | } else if (control instanceof FormControl && control.value !== undefined && control.value !== null) { |
| 389 | if (typeof control.value === 'string') { |
| 390 | control.setValue(control.value.trim()); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 391 | } |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 392 | } else if (key === 'config' && formName === 'vim') { |
| SANDHYA.JS | 92379ec | 2025-06-13 17:29:35 +0530 | [diff] [blame] | 393 | this.cleanForm(control as FormGroup); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 394 | } |
| 395 | }); |
| 396 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 397 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 398 | /** Method to return the config of pager value for ngSmarttable @public */ |
| 399 | public paginationPagerConfig(): PAGERSMARTTABLE { |
| 400 | return { |
| 401 | display: true, |
| 402 | perPage: environment.paginationNumber |
| 403 | }; |
| 404 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 405 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 406 | /** Method to return the class for the table for ngSmarttable @public */ |
| 407 | public tableClassConfig(): SMARTTABLECLASS { |
| 408 | return { |
| 409 | class: 'table list-data' |
| 410 | }; |
| 411 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 412 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 413 | /** Method to return all languages name and its code @public */ |
| 414 | public languageCodeList(): {}[] { |
| 415 | return [ |
| 416 | { code: 'en', language: 'English' }, |
| 417 | { code: 'es', language: 'Spanish' }, |
| 418 | { code: 'pt', language: 'Portuguese' }, |
| 419 | { code: 'de', language: 'German' } |
| 420 | ]; |
| 421 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 422 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 423 | /** Fetch OSM Version @public */ |
| 424 | public fetchOSMVersion(): void { |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 425 | this.restService.getResource(environment.OSM_VERSION_URL).subscribe((res: { version: string }): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 426 | const version: string[] = res.version.split('+'); |
| 427 | if (!isNullOrUndefined(version[0])) { |
| 428 | this.osmVersion = version[0]; |
| SANDHYA.JS | 995c672 | 2024-03-08 12:14:11 +0530 | [diff] [blame] | 429 | sessionStorage.setItem('version', version[0]); |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 430 | } else { |
| 431 | this.osmVersion = null; |
| 432 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 433 | }, (error: ERRORDATA): void => { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 434 | this.osmVersion = null; |
| 435 | this.restService.handleError(error, 'get'); |
| 436 | }); |
| 437 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 438 | |
| Barath Kumar R | 208bef2 | 2020-07-07 12:28:04 +0530 | [diff] [blame] | 439 | /** Random RGB color code generator @public */ |
| 440 | public generateColor(): string { |
| SANDHYA.JS | faf168f | 2024-03-07 23:08:48 +0530 | [diff] [blame] | 441 | const x: number = Math.floor((new Date().getHours()) * this.colourHour); |
| 442 | const y: number = Math.floor((new Date().getMinutes()) * this.colourMin); |
| 443 | const z: number = Math.floor((new Date().getSeconds()) * this.colourMin); |
| Barath Kumar R | 208bef2 | 2020-07-07 12:28:04 +0530 | [diff] [blame] | 444 | return 'rgb(' + x + ',' + y + ',' + z + ')'; |
| 445 | } |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 446 | |
| 447 | /** Add custom name/tag to the dropdown @public */ |
| 448 | public addCustomTag(tag: string): string { |
| 449 | return tag; |
| 450 | } |
| 451 | |
| Barath Kumar R | d22b094 | 2020-07-14 11:05:24 +0530 | [diff] [blame] | 452 | /** Fetch file extension @public */ |
| 453 | public fetchFileExtension(fileInfo: FileList): string { |
| 454 | return fileInfo[0].name.substring(fileInfo[0].name.lastIndexOf('.') + 1); |
| 455 | } |
| 456 | |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 457 | /** Get domain name @private */ |
| 458 | public getDomainName(): Observable<TYPESECTION[]> { |
| 459 | return this.restService.getResource(environment.DOMAIN_URL).pipe(map((domains: DOMAINS): TYPESECTION[] => { |
| 460 | const domainList: TYPESECTION[] = []; |
| 461 | try { |
| 462 | let domainNames: string[] = []; |
| 463 | if (!isNullOrUndefined(domains.project_domain_name)) { |
| 464 | domainNames = domainNames.concat(domains.project_domain_name.split(',')); |
| 465 | } |
| 466 | if (!isNullOrUndefined(domains.user_domain_name)) { |
| 467 | domainNames = domainNames.concat(domains.user_domain_name.split(',')); |
| 468 | } |
| 469 | domainNames = Array.from(new Set(domainNames)); |
| 470 | if (domainNames.length > 0) { |
| 471 | domainNames.forEach((domainName: string): void => { |
| 472 | if (!domainName.endsWith(':ro')) { |
| 473 | domainList.push({ title: domainName, value: domainName }); |
| 474 | } |
| 475 | }); |
| 476 | } |
| 477 | return domainList; |
| 478 | } catch (e) { |
| 479 | return domainList; |
| 480 | } |
| 481 | })); |
| 482 | } |
| 483 | |
| SANDHYA.JS | 5f8c802 | 2023-10-13 11:45:25 +0530 | [diff] [blame] | 484 | /** Sorting the list based on date @public */ |
| 485 | public compareFunction = (dir: number, a: string, b: string): number => { |
| 486 | const first: number = new Date(a).getTime(); |
| 487 | const second: number = new Date(b).getTime(); |
| 488 | if (first < second) { |
| 489 | return -1 * dir; |
| 490 | } |
| 491 | if (first > second) { |
| 492 | return dir; |
| 493 | } |
| 494 | return 0; |
| 495 | }; |
| 496 | |
| SANDHYA.JS | 2657011 | 2024-07-05 21:35:46 +0530 | [diff] [blame] | 497 | /** Method to validate file extension and size @public */ |
| 498 | public vaildataFileInfo(fileInfo: File, fileType: string): boolean { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 499 | const extension: string = fileInfo.name.substring(fileInfo.name.lastIndexOf('.') + 1); |
| 500 | const packageSize: number = CONSTANTNUMBER.oneMB * environment.packageSize; |
| Barath Kumar R | d477b85 | 2020-07-07 15:24:05 +0530 | [diff] [blame] | 501 | if (fileType === 'yaml' && (extension.toLowerCase() === 'yaml' || extension.toLowerCase() === 'yml') |
| 502 | && fileInfo.size <= packageSize) { |
| 503 | return true; |
| 504 | } else if (extension.toLowerCase() === fileType && fileInfo.size <= packageSize) { |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 505 | return true; |
| 506 | } |
| 507 | return false; |
| 508 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 509 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 510 | /** Method to read file content based on type @private */ |
| 511 | private readFileContent(reader: FileReader, fileInfo: File, fileType: string): void { |
| 512 | if (fileType === 'gz') { |
| 513 | reader.readAsArrayBuffer(fileInfo); |
| 514 | } else { |
| 515 | reader.readAsText(fileInfo); |
| 516 | } |
| 517 | } |
| Barath Kumar R | 1607058 | 2021-02-08 18:19:35 +0530 | [diff] [blame] | 518 | |
| kumaran.m | 3b4814a | 2020-05-01 19:48:54 +0530 | [diff] [blame] | 519 | /** Method to handle http options @public */ |
| 520 | private getHttpOptions(): GETAPIURLHEADER { |
| 521 | return { |
| 522 | headers: new HttpHeaders({ |
| 523 | Accept: 'application/gzip, application/json', |
| 524 | 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0' |
| 525 | }), |
| 526 | responseType: 'arraybuffer' |
| 527 | }; |
| 528 | } |
| 529 | } |
| SANDHYA.JS | c84f112 | 2024-06-04 21:50:03 +0530 | [diff] [blame] | 530 | |
| 531 | /** Method to handle null or undefined @public */ |
| 532 | // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types |
| 533 | export const isNullOrUndefined = (data: any): boolean => data === null || data === undefined; |
| 534 | |