From 1a7571593e5d64faefe1322184d231267e3acf51 Mon Sep 17 00:00:00 2001 From: argirok Date: Thu, 19 May 2022 12:15:52 +0300 Subject: [PATCH] move datatransfer modules to library --- landingPages/result/resultLanding.module.ts | 2 +- utils/dataTransfer/transferData.component.ts | 292 +++++++++++++++++++ utils/dataTransfer/transferData.module.ts | 20 ++ 3 files changed, 313 insertions(+), 1 deletion(-) create mode 100644 utils/dataTransfer/transferData.component.ts create mode 100644 utils/dataTransfer/transferData.module.ts diff --git a/landingPages/result/resultLanding.module.ts b/landingPages/result/resultLanding.module.ts index f1b2f0d9..964aa01a 100644 --- a/landingPages/result/resultLanding.module.ts +++ b/landingPages/result/resultLanding.module.ts @@ -35,7 +35,7 @@ import {IconsModule} from "../../utils/icons/icons.module"; import {IconsService} from "../../utils/icons/icons.service"; import {graph, link, quotes} from "../../utils/icons/icons"; import {InputModule} from "../../sharedComponents/input/input.module"; -import {EGIDataTransferModule} from "../../../utils/transferData.module"; +import {EGIDataTransferModule} from "../../utils/dataTransfer/transferData.module"; @NgModule({ imports: [ diff --git a/utils/dataTransfer/transferData.component.ts b/utils/dataTransfer/transferData.component.ts new file mode 100644 index 00000000..f0fcb2ec --- /dev/null +++ b/utils/dataTransfer/transferData.component.ts @@ -0,0 +1,292 @@ +import {Component, Input} from '@angular/core'; +import {Subscriber} from "rxjs"; +import {HttpClient, HttpHeaders} from "@angular/common/http"; +import {Validators} from "@angular/forms"; +import {Location} from '@angular/common'; +import {StringUtils} from "../string-utils.class"; +import {COOKIE} from "../../login/utils/helper.class"; +import {Router} from "@angular/router"; +declare var UIkit; + +@Component({ + selector: 'egi-transfer-data', + template: ` + + + + + + + + + + + + ` +}) +export class EGIDataTransferComponent { + subscriptions = []; + accessToken = null; + @Input() dois; + loginURL = "http://rudie.di.uoa.gr:8580/openid_connect_login" + sourceUrls = [] + selectedSourceUrl = null; + destinationPath = ""; + destinationOptions = [{label: "EGI storage", value: { url: "https://egi.storage.eu", id: "egi" } }]; + selectedDestination = null; + + downloadElements = null; + @Input() isOpen = false; + APIURL = "https://virtserver.swaggerhub.com/thebe14/eosc-future-data-transfer/1.0.0"; + status: "loading" | "success" | "errorParser" | "errorUser" | "errorTransfer" | "init" = "init"; + message; + validators = [Validators.required, StringUtils.urlValidator()]; + constructor(private http: HttpClient, private location: Location, private _router: Router) { + + } + + ngOnInit() { + if(this.isOpen){ + this.open(); + } + } + + ngOnDestroy() { + this.subscriptions.forEach(subscription => { + if (subscription instanceof Subscriber) { + subscription.unsubscribe(); + } + }); + + } + open(){ + this.accessToken = COOKIE.getCookie("EGIAccessToken"); + for(let doi of this.dois){ + if(doi.indexOf("zenodo.")!=-1){ + this.sourceUrls.push("https://doi.org/" + doi); + } + } + this.selectedSourceUrl = this.sourceUrls[0]; + this.selectedDestination = this.destinationOptions[0].value; + this.parse(); + this.isOpen = true; + } + close(){ + this.isOpen = false; + // this.downloadElements = []; + this.destinationPath = ""; + this.selectedDestination = this.destinationOptions[0].value; + this.selectedSourceUrl = this.sourceUrls[0]; + this.message = null; + this.status = "init"; + if(this._router.url.indexOf("&egiTransfer")){ + this.location.go(this._router.url.split("&egiTransfer")[0]); + } + + } + checkin(){ + window.location.href = this.loginURL+"?redirect="+ encodeURIComponent(window.location.href + "&egiTransfer=t"); + + } + parse(){ + + this.subscriptions.push(this.http.get(this.APIURL + "/parser/zenodo?source=" + this.selectedSourceUrl ).subscribe( + res => { + console.log(res) + this.downloadElements = []; + for( let element of res['elements']){ + //TODO remove + element.downloadUrl = "https://zenodo.org/record/6354460/files/preprocessed_data/weights/atlas_EUCP_ICTP_CMIP6_REA_tas_weights.nc?download=1"; + //TODO can we use element.name instead? + element.filename = this.parseFilename(element.downloadUrl); + console.log(element.filename) + this.downloadElements.push(element) + } + + console.log(this.downloadElements) + // this.transfer(); + + }, error => { + this.status = "errorParser"; + this.message = "Couldn't get download URLs from zenodo"; + UIkit.notification("Couldn't get download URLs from zenodo", { + status: 'error', + timeout: 6000, + pos: 'bottom-right' + }); + + } + )); + + } + + transfer() { + console.log(this.selectedDestination) + this.status = "loading"; + let headers = new HttpHeaders({'Authorization': 'Bearer '+this.accessToken}); + this.subscriptions.push(this.http.get(this.APIURL + "/user/info", {headers: headers}).subscribe( + res => { + console.log(res) + let body = { + "files": [], + "params": { + "priority": 0, + "overwrite": true, + "retry": 3 + } + }; + console.log(this.selectedDestination) + for (let element of this.downloadElements) { + + let file = { + "sources": [ + { + "url": element['downloadUrl'] + } + ], + "destinations": [ + { + "url": this.selectedDestination.url + this.destinationPath + element.filename + } + ], + "filesize": element['size'] + }; + //TODO priority? checksum? + body.files.push(file); + } + + let headers = new HttpHeaders({'Authorization': 'Bearer '+this.accessToken}); + this.subscriptions.push(this.http.post(this.APIURL + "/transfer" ,body, {headers: headers}).subscribe( + res => { + console.log(res) + UIkit.notification('Data transfer has began! ', { + status: 'success', + timeout: 6000, + pos: 'bottom-right' + }); + + this.status = "success" + this.message = ` +
Data transfer has began!
+
Transfering ` + this.downloadElements.length + ` files to EGI Storage: + +
` + + }, error => { + this.status = "errorTransfer"; + this.message = "Couldn't transfer files"; + UIkit.notification("Couldn't transfer files", { + status: 'error', + timeout: 6000, + pos: 'bottom-right' + }); + + } + )); + }, error => { + this.status = "errorUser"; + this.message = "User can't be authenticated!"; + UIkit.notification("User can't be authenticated!", { + status: 'error', + timeout: 6000, + pos: 'bottom-right' + }); + + } + )); + } + private parseFilename(url){ + let filename = url.split("/")[url.split("/").length - 1]; + return filename.split("?")[0]; + } + +} diff --git a/utils/dataTransfer/transferData.module.ts b/utils/dataTransfer/transferData.module.ts new file mode 100644 index 00000000..64aa6695 --- /dev/null +++ b/utils/dataTransfer/transferData.module.ts @@ -0,0 +1,20 @@ +import {NgModule} from '@angular/core'; +import {CommonModule} from '@angular/common'; +import {FormsModule} from '@angular/forms'; +import {EGIDataTransferComponent} from "./transferData.component"; +import {InputModule} from "../../sharedComponents/input/input.module"; + +@NgModule({ + imports: [ + CommonModule, FormsModule, InputModule + + ], + declarations: [ + EGIDataTransferComponent + ], + + exports: [ + EGIDataTransferComponent + ] +}) +export class EGIDataTransferModule { }