X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=src%2Fservices%2FSharedService.ts;h=d5c561d0ccfae5d68767ed3ad527226e053620d6;hb=1f07bd03d6016fb2c48336f0b3322ea91bc7437f;hp=41f3d76ada7aac5f32a1eea2f4bd2cafd0accad8;hpb=403234ed6120ba4bc01578b3ec237fa5fd4c0f6d;p=osm%2FNG-UI.git diff --git a/src/services/SharedService.ts b/src/services/SharedService.ts index 41f3d76..d5c561d 100644 --- a/src/services/SharedService.ts +++ b/src/services/SharedService.ts @@ -22,12 +22,26 @@ import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { EventEmitter, Injectable, Output } from '@angular/core'; import { FormArray, FormGroup } from '@angular/forms'; import { Router } from '@angular/router'; -import { CONSTANTNUMBER, ERRORDATA, GETAPIURLHEADER, PACKAGEINFO, PAGERSMARTTABLE, SMARTTABLECLASS, TARSETTINGS } from 'CommonModel'; +import { TranslateService } from '@ngx-translate/core'; +import { + CONSTANTNUMBER, + DOMAINS, + ERRORDATA, + FILESETTINGS, + GETAPIURLHEADER, + PACKAGEINFO, + PAGERSMARTTABLE, + SMARTTABLECLASS, + TARSETTINGS, + TYPESECTION +} from 'CommonModel'; import { environment } from 'environment'; import * as HttpStatus from 'http-status-codes'; import * as untar from 'js-untar'; import * as pako from 'pako'; import { RestService } from 'RestService'; +import { Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; import { isNullOrUndefined } from 'util'; /** This is added globally by the tar.js library */ @@ -64,6 +78,13 @@ export class SharedService { // tslint:disable-next-line: max-line-length public REGX_PASSWORD_PATTERN: RegExp = new RegExp(/^.*(?=.{8,})((?=.*[!@#$%^&*()\-_=+{};:,<.>]){1})(?=.*\d)((?=.*[a-z]){1})((?=.*[A-Z]){1}).*$/); + /** Variables to hold regexp pattern for Latitude */ + public REGX_LAT_PATTERN: RegExp = new RegExp(/^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,15})?))$/); + + /** Variables to hold regexp pattern for Longitude */ + // tslint:disable-next-line: max-line-length + 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})?))$/); + /** Variables to hold maxlength for the description @public */ public MAX_LENGTH_DESCRIPTION: number = 500; @@ -100,35 +121,42 @@ export class SharedService { /** Check for the root directory @private */ private directoryCount: number = 2; - constructor(restService: RestService, router: Router) { + /** Contains tranlsate instance @private */ + private translateService: TranslateService; + + constructor(restService: RestService, router: Router, translateService: TranslateService) { this.restService = restService; this.router = router; + this.translateService = translateService; } /** convert epoch time function @public */ public convertEpochTime(unixtimestamp: number): string { - const monthsArr: string[] = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', - 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; - const date: Date = new Date(unixtimestamp * this.epochTime1000); - const year: number = date.getFullYear(); - const month: string = monthsArr[date.getMonth()]; - const day: number = date.getDate(); - const hours: number = date.getHours(); - const minutes: string = '0' + date.getMinutes(); - const seconds: string = '0' + date.getSeconds(); - return month + '-' + day + '-' + year + ' ' + hours + ':' + minutes.substr(this.epochTimeMinus2) + ':' - + seconds.substr(this.epochTimeMinus2); + if (!isNullOrUndefined(unixtimestamp)) { + const monthsArr: string[] = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; + const date: Date = new Date(unixtimestamp * this.epochTime1000); + const year: number = date.getFullYear(); + const month: string = monthsArr[date.getMonth()]; + const day: number = date.getDate(); + const hours: number = date.getHours(); + const minutes: string = '0' + date.getMinutes(); + const seconds: string = '0' + date.getSeconds(); + return month + '-' + day + '-' + year + ' ' + hours + ':' + minutes.substr(this.epochTimeMinus2) + ':' + + seconds.substr(this.epochTimeMinus2); + } + return this.translateService.instant('NODATE'); } /** Download Files function @public */ - public downloadFiles(shortName: string, binaryData: Blob[], filetype: string): void { + public downloadFiles(name: string, binaryData: Blob[], filetype: string): void { const downloadLink: HTMLAnchorElement = document.createElement('a'); downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, { type: filetype })); - if (shortName !== undefined) { + if (name !== undefined) { if (window.navigator.msSaveOrOpenBlob) { - window.navigator.msSaveBlob(new Blob(binaryData, { type: filetype }), 'OSM_Export_' + shortName + '.tar.gz'); + window.navigator.msSaveBlob(new Blob(binaryData, { type: filetype }), 'OSM_Export_' + name + '.tar.gz'); } else { - downloadLink.setAttribute('download', 'OSM_Export_' + shortName + '.tar.gz'); + downloadLink.setAttribute('download', 'OSM_Export_' + name + '.tar.gz'); document.body.appendChild(downloadLink); downloadLink.click(); } @@ -150,6 +178,7 @@ export class SharedService { } return result; } + /** Function to read uploaded file String @public */ public async getFileString(files: FileList, fileType: string): Promise { const reader: FileReader = new FileReader(); @@ -170,6 +199,7 @@ export class SharedService { }; }); } + /** Method to handle tar and tar.gz file for shared YAML file content @public */ public async targzFile(packageInfo: PACKAGEINFO): Promise { return new Promise((resolve: Function, reject: Function): void => { @@ -177,36 +207,37 @@ export class SharedService { let apiUrl: string = ''; apiUrl = packageInfo.packageType === 'nsd' ? environment.NSDESCRIPTORS_URL + '/' + packageInfo.id + '/nsd_content' : environment.VNFPACKAGES_URL + '/' + packageInfo.id + '/package_content'; - this.restService.getResource(apiUrl, httpOptions).subscribe((response: ArrayBuffer) => { + this.restService.getResource(apiUrl, httpOptions).subscribe((response: ArrayBuffer): void => { try { // tslint:disable-next-line: no-any const tar: any = new Tar(); const originalInput: Uint8Array = pako.inflate(response, { to: 'Uint8Array' }); - untar(originalInput.buffer).then((extractedFiles: TARSETTINGS[]) => { + untar(originalInput.buffer).then((extractedFiles: TARSETTINGS[]): void => { const getFoldersFiles: {}[] = extractedFiles; const folderNameStr: string = extractedFiles[0].name; - getFoldersFiles.forEach((value: TARSETTINGS) => { + getFoldersFiles.forEach((value: TARSETTINGS): void => { + const fileValueObj: FILESETTINGS = this.createFileValueObject(value); const getRootFolder: string[] = value.name.split('/'); if (value.name.startsWith(folderNameStr) && (value.name.endsWith('.yaml') || value.name.endsWith('.yml')) && getRootFolder.length === this.directoryCount) { - tar.append(value.name, packageInfo.descriptor, { type: value.type }); + tar.append(value.name, packageInfo.descriptor, fileValueObj); } else { if (value.type !== 'L') { - tar.append(value.name, new Uint8Array(value.buffer), { type: value.type }); + tar.append(value.name, new Uint8Array(value.buffer), fileValueObj); } } }); const out: Uint8Array = tar.out; const originalOutput: Uint8Array = pako.gzip(out); resolve(originalOutput.buffer); - }, (err: string) => { + }, (err: string): void => { reject(''); }); } catch (e) { reject(''); } - }, (error: HttpErrorResponse) => { + }, (error: HttpErrorResponse): void => { if (error.status === HttpStatus.NOT_FOUND || error.status === HttpStatus.UNAUTHORIZED) { this.router.navigateByUrl('404', { skipLocationChange: true }).catch(); } else { @@ -216,6 +247,17 @@ export class SharedService { }); }); } + + /** Method to return the file information @public */ + public createFileValueObject(value: TARSETTINGS): FILESETTINGS { + return { + type: value.type, + linkname: value.linkname, + owner: value.uname, + group: value.gname + }; + } + /** Method to check given string is JSON or not @public */ public checkJson(jsonString: string): boolean { jsonString = jsonString.replace(/'/g, '"'); @@ -226,6 +268,7 @@ export class SharedService { } return true; } + /** Clean the form before submit @public */ public cleanForm(formGroup: FormGroup, formName?: String): void { Object.keys(formGroup.controls).forEach((key: string) => { @@ -247,6 +290,7 @@ export class SharedService { } }); } + /** Method to return the config of pager value for ngSmarttable @public */ public paginationPagerConfig(): PAGERSMARTTABLE { return { @@ -254,12 +298,14 @@ export class SharedService { perPage: environment.paginationNumber }; } + /** Method to return the class for the table for ngSmarttable @public */ public tableClassConfig(): SMARTTABLECLASS { return { class: 'table list-data' }; } + /** Method to return all languages name and its code @public */ public languageCodeList(): {}[] { return [ @@ -269,20 +315,22 @@ export class SharedService { { code: 'de', language: 'German' } ]; } + /** Fetch OSM Version @public */ public fetchOSMVersion(): void { - this.restService.getResource(environment.OSM_VERSION_URL).subscribe((res: { version: string }) => { + this.restService.getResource(environment.OSM_VERSION_URL).subscribe((res: { version: string }): void => { const version: string[] = res.version.split('+'); if (!isNullOrUndefined(version[0])) { this.osmVersion = version[0]; } else { this.osmVersion = null; } - }, (error: ERRORDATA) => { + }, (error: ERRORDATA): void => { this.osmVersion = null; this.restService.handleError(error, 'get'); }); } + /** Random RGB color code generator @public */ public generateColor(): string { const x: number = Math.floor(Math.random() * this.colorStringLength); @@ -296,6 +344,38 @@ export class SharedService { return tag; } + /** Fetch file extension @public */ + public fetchFileExtension(fileInfo: FileList): string { + return fileInfo[0].name.substring(fileInfo[0].name.lastIndexOf('.') + 1); + } + + /** Get domain name @private */ + public getDomainName(): Observable { + return this.restService.getResource(environment.DOMAIN_URL).pipe(map((domains: DOMAINS): TYPESECTION[] => { + const domainList: TYPESECTION[] = []; + try { + let domainNames: string[] = []; + if (!isNullOrUndefined(domains.project_domain_name)) { + domainNames = domainNames.concat(domains.project_domain_name.split(',')); + } + if (!isNullOrUndefined(domains.user_domain_name)) { + domainNames = domainNames.concat(domains.user_domain_name.split(',')); + } + domainNames = Array.from(new Set(domainNames)); + if (domainNames.length > 0) { + domainNames.forEach((domainName: string): void => { + if (!domainName.endsWith(':ro')) { + domainList.push({ title: domainName, value: domainName }); + } + }); + } + return domainList; + } catch (e) { + return domainList; + } + })); + } + /** Method to validate file extension and size @private */ private vaildataFileInfo(fileInfo: File, fileType: string): boolean { const extension: string = fileInfo.name.substring(fileInfo.name.lastIndexOf('.') + 1); @@ -308,6 +388,7 @@ export class SharedService { } return false; } + /** Method to read file content based on type @private */ private readFileContent(reader: FileReader, fileInfo: File, fileType: string): void { if (fileType === 'gz') { @@ -316,6 +397,7 @@ export class SharedService { reader.readAsText(fileInfo); } } + /** Method to handle http options @public */ private getHttpOptions(): GETAPIURLHEADER { return {