import {Component, Input} from '@angular/core';
import {Subscriber} from "rxjs";
import {HttpClient} from "@angular/common/http";
import {Validators} from "@angular/forms";
import {Location} from '@angular/common';
import {StringUtils} from "../openaireLibrary/utils/string-utils.class";
import {COOKIE, Session} from "../openaireLibrary/login/utils/helper.class";
import {UserManagementService} from "../openaireLibrary/services/user-management.service";
import {Router} from "@angular/router";
declare var UIkit;
@Component({
selector: 'egi-transfer-data',
template: `
Title
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
EGI
checkin
Title
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
Lorem ipsum ....
>> Transfer to EGI Storage
Close
`
})
export class EGIDataTransferComponent {
subscriptions = [];
accessToken = null;
@Input() dois;
loginURL = "http://rudie.di.uoa.gr:8580/openid_connect_login"
sourceUrl = null;
destinationPath = "";
destinationURL = "https://egi.storage.eu";
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 userManagementsService: UserManagementService, private _router: Router) {
}
ngOnInit() {
console.log(COOKIE.getCookie("EGISession"))
this.accessToken = COOKIE.getCookie("EGISession");
for(let doi of this.dois){
console.log(doi)
if(doi.indexOf("zenodo.")!=-1){
this.sourceUrl = "https://doi.org/" + doi;
break;
}
}
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
open(){
this.isOpen = true;
}
close(){
this.isOpen = false;
this.downloadElements = null;
this.destinationPath = "";
this.message = null;
this.status = "init";
if(this._router.url.indexOf("&egiTransfer")){
this.location.go(this._router.url.split("&egiTransfer")[0]);
}
}
checkin(){
// this.loggedIn = true;
console.log(COOKIE.getCookie("EGISession"))
// this.accessToken = COOKIE.getCookie("EGISession");
COOKIE.setCookie("EGISession", "test!!!!!!",10,"/" );
this.userManagementsService.setRedirectUrl(this._router.url+"&egiTransfer=t");
// this.location.go("/reload");
window.location.href = "/reload";
// window.location.href = this.loginURL;
}
parse(){
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 + this.destinationPath + 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 to
EGI Storage :
`;
// TODO LATER we can call status for each file and see if the transfer has been complete
for(let element of this.downloadElements){
console.log(element)
this.message += ` `+ element.filename+ ` `;
}
this.message += `
`
}, 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];
}
}