From 2f32fd705bd2191ecc4a434beb6059eea997983a Mon Sep 17 00:00:00 2001 From: argirok Date: Mon, 9 May 2022 13:16:52 +0300 Subject: [PATCH] Initial version of data service component --- src/app/utils/transferData.component.ts | 237 ++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 src/app/utils/transferData.component.ts diff --git a/src/app/utils/transferData.component.ts b/src/app/utils/transferData.component.ts new file mode 100644 index 0000000..74a678d --- /dev/null +++ b/src/app/utils/transferData.component.ts @@ -0,0 +1,237 @@ +import {Component} from '@angular/core'; +import {Subscriber} from "rxjs"; +import {HttpClient} from "@angular/common/http"; +import {Validators} from "@angular/forms"; +import {StringUtils} from "../openaireLibrary/utils/string-utils.class"; +declare var UIkit; + +@Component({ + selector: 'egi-transfer-data', + template: ` + Transfer data to EGI storage + + + + + + + ` +}) +export class EGIDataTransfer { + subscriptions = []; + loggedIn = false; + sourceUrl = "https://doi.org/10.5281/zenodo.6507535"; + destinationUrl = "https://egi.storage.eu/my-path/"; + downloadElements = null; + 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) { + + } + + ngOnInit() { + + } + + ngOnDestroy() { + this.subscriptions.forEach(subscription => { + if (subscription instanceof Subscriber) { + subscription.unsubscribe(); + } + }); + + } + open(){ + this.isOpen = true; + } + close(){ + this.isOpen = false; + this.downloadElements = null; + this.destinationUrl = ""; + this.message = null; + this.status = "init"; + } + checkin(){ + this.loggedIn = true; + } + parse(){ + // check get user info + // .pipe(catch(error: any) => { + // return Observable.throw(new Error(error.status)); + // })) + this.status = "loading"; + this.subscriptions.push(this.http.get(this.APIURL + "/user/info").subscribe( + res => { + console.log(res) + this.subscriptions.push(this.http.get(this.APIURL + "/parser/zenodo?source=" + this.sourceUrl ).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' + }); + + } + )); + }, 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' + }); + + } + )); + } + + transfer() { + + let body = { + "files": [], + "params": { + "priority": 0, + "overwrite": true, + "retry": 3 + } + }; + for (let element of this.downloadElements) { + + let file = { + "sources": [ + { + "url": element['downloadUrl'] + } + ], + "destinations": [ + { + "url":this.destinationUrl + element.filename + } + ], + "filesize": element['size'], + }; + //TODO priority? checksum? + body.files.push(file); + } + + + + this.subscriptions.push(this.http.post(this.APIURL + "/transfer" ,body ).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: + +
` + + }, error => { + this.status = "errorTransfer"; + this.message = "Couldn't transfer files"; + UIkit.notification("Couldn't transfer files", { + status: 'error', + timeout: 6000, + pos: 'bottom-right' + }); + + } + )); + + } + private parseFilename(url){ + let filename = url.split("/")[url.split("/").length - 1]; + return filename.split("?")[0]; + } + +}