import {Component, Input, ViewChild} from '@angular/core'; import {Subscriber} from "rxjs"; import {HttpClient, HttpHeaders} from "@angular/common/http"; import {AbstractControl, ValidatorFn, Validators} from "@angular/forms"; import {Location} from '@angular/common'; import {COOKIE} from "../../login/utils/helper.class"; import {Router} from "@angular/router"; import {properties} from "../../../../environments/environment"; declare var UIkit; @Component({ selector: 'egi-transfer-data', template: `
In order to send data to a Cloud Storage, you would need to be authenticated, please login via EGI check-in.
You have requested to send the data corresponding to the DOI {{selectedSourceUrl.split(doiPrefix)[1]}} to a cloud storage using the EOSC Data Transfer service

Available Zenodo DOI URLs:

{{this.downloadElements.length}} files found:
  • {{ element.name}}
-

Please select the Destination Storage type and provide the corresponding storage destination path.

` }) export class EGIDataTransferComponent { subscriptions = []; accessToken = null; @Input() dois; loginURL = properties.environment == "development"? "http://rudie.di.uoa.gr:8580/openid_connect_login":"https://explore.eosc-portal.eu/egi-login-service/openid_connect_login" sourceUrls = [] selectedSourceUrl = null; destinationPath = ""; destinationOptions = [{label: "EGI dCache (dcache-demo.desy.de)", value: { url: "https://dcache-demo.desy.de:2443", id: "dcache" , webpage:"https://dcache-demo.desy.de"} }]; selectedDestination = null; downloadElements = null; @Input() isOpen = false; @ViewChild('egiTransferModal') egiTransferModal; APIURL = "https://eosc-data-transfer.vm.fedcloud.eu" // APIURL = "https://virtserver.swaggerhub.com/thebe14/eosc-future-data-transfer/1.0.0"; status: "loading" | "success" | "errorParser" | "errorUser" | "errorTransfer" | "init" = "init"; message; doiPrefix = properties.doiURL; validators = [Validators.required, this.pathValidator() /*StringUtils.urlValidator()*/]; constructor(private http: HttpClient, private location: Location, private _router: Router) { } ngAfterViewInit() { if(this.isOpen){ this.open(); } } ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { subscription.unsubscribe(); } }); } open(){ this.accessToken = COOKIE.getCookie("EGIAccessToken"); if(this.accessToken) { for (let doi of this.dois) { if (doi.indexOf("zenodo.") != -1) { this.sourceUrls.push(this.doiPrefix + doi); } } try { this.sourceUrls.sort(function (a, b) { return Number(b.split("zenodo.")[1]) - Number(a.split("zenodo.")[1]); }); }catch (e){} this.selectedSourceUrl = this.sourceUrls[0]; this.selectedDestination = this.destinationOptions[0].value; this.parse(); } this.isOpen = true; this.egiTransferModal.cancelButton = false; this.egiTransferModal.okButton = false; this.egiTransferModal.alertTitle = "EOSC data transfer service [demo]"; this.destinationPath = ""; this.selectedDestination = this.destinationOptions[0].value; this.selectedSourceUrl = this.sourceUrls[0]; this.message = null; this.status = "init"; this.egiTransferModal.open(); } close(){ if(this.isOpen) { this.isOpen = false; this.egiTransferModal.cancel(); } // 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 + (window.location.href.indexOf("&egiTransfer=t")!=-1?"":"&egiTransfer=t")); } parse(){ this.status = "loading"; this.message = null; this.downloadElements = []; this.subscriptions.push(this.http.get(this.APIURL + "/parser/zenodo?doi=" + encodeURIComponent(this.selectedSourceUrl)).subscribe( res => { this.downloadElements= res['elements'] // console.log(this.downloadElements) this.status = "init"; }, 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: 3000, 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": [element['downloadUrl']], "destinations": [this.selectedDestination.url + this.destinationPath + element.name], }; //TODO priority? checksum? filesize? // "filesize": element['size'] 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]; } pathValidator(): ValidatorFn { return (control: AbstractControl): { [key: string]: string } | null => { if (!this.validatePath()) { return {'error': 'Path should start and end with "/" e.g /path/'}; } return null; } } validatePath():boolean { let exp1 = /^\/([A-z0-9-_+]+\/)*$/g; return (this.destinationPath.length > 0 && this.destinationPath.match(exp1) != null) } }