workspace-ionic-app/src/app/storagehub.service.ts

139 lines
5.2 KiB
TypeScript

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { catchError, map, retry, switchMap } from 'rxjs/operators';
import { HttpErrorResponse, HttpHeaders } from 'node-angular-http-client';
import { Item } from './model/item.model';
import { ItemList } from './model/itemlist.model';
import { ItemWrapper } from './model/item-wrapper.model';
const token = "b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548";
//const shURL ="https://workspace-repository.dev.d4science.org/storagehub/workspace"
const shURL = "/shub/storagehub/workspace";
@Injectable()
export class StoragehubService {
constructor(private http: HttpClient) {
}
getWsRoot(): Observable<Item> {
const getWsURL = `${shURL}?gcube-token=${token}&exclude=hl:accounting`;
return this.http.get<ItemWrapper>(getWsURL, {observe: 'body', responseType: 'json'}).pipe(
map(value => value.item),
catchError(this.error)
);
}
getTrashRoot(): Observable<Item> {
const getTrashURL = `${shURL}/trash/?gcube-token=${token}&exclude=hl:accounting`;
return this.http.get<ItemWrapper>(getTrashURL, {observe: "body", responseType: "json"}).pipe(
map(value => value.item),
catchError(this.error)
);
}
getVres(): Observable<Item[]> {
const getVresURL = `${shURL}/vrefolders/?gcube-token=${token}&exclude=hl:accounting`;
return this.http.get<ItemList>(getVresURL, {observe: 'body', responseType: 'json'}).pipe(
map(value => value.itemlist),
catchError(this.error)
);
}
getItem(id: string): Observable<Item> {
const getWsURL = `${shURL}/items/${id}?gcube-token=${token}&exclude=hl:accounting`;
return this.http.get<ItemWrapper>(getWsURL, {observe: 'body', responseType: 'json'}).pipe(
map(value => value.item),
catchError(this.error)
);
}
getChildren(id:string, onlyFolder?:boolean): Observable<Item[]>{
let getWsURL = `${shURL}/items/${id}/children?gcube-token=${token}&exclude=hl:accounting`;
if (onlyFolder)
getWsURL += '&onlyType=nthl:workspaceItem';
return this.http.get<ItemList>(getWsURL).pipe(
map(values => values.itemlist),
catchError(this.error)
);
}
getAnchestor(id:string): Observable<Item[]>{
let getWsURL = `${shURL}/items/${id}/ancherstors?gcube-token=${token}&exclude=hl:accounting`;
console.log("ws children "+getWsURL);
return this.http.get<ItemList>(getWsURL, {observe: 'body', responseType: 'json'}).pipe(
map(value => value.itemlist),
catchError(this.error)
);
}
uploadFile(parentId: string, name: string, file: File){
let uploadURL = `${shURL}/items/${parentId}/create/FILE?gcube-token=${token}`;
const formData = new FormData();
formData.append("name", name);
formData.append("description", "");
formData.append("file",file);
return this.http.post(uploadURL, formData, {observe: 'events', responseType: 'text', reportProgress: true}).pipe(
catchError(this.error)
);
}
createFolder(parentId: string, name: string, description: string) : Observable<any> {
let createFolderURL = `${shURL}/items/${parentId}/create/FOLDER?gcube-token=${token}`;
const formData = new FormData();
let body = `name=${name}&description=${description}`;
return this.http.post(createFolderURL, body, {observe: 'body', responseType: 'text' , headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }}).pipe(
catchError(this.error)
);
}
deleteItem(itemId: string) : Observable<any> {
let deleteItemUrl = `${shURL}/items/${itemId}?gcube-token=${token}`;
return this.http.delete( deleteItemUrl).pipe(
catchError(this.error)
);
}
renameItem(itemId: string, newName: string) : Observable<any> {
let renameItemUrl = `${shURL}/items/${itemId}/rename?gcube-token=${token}`;
let body = `newName=${newName}`;
return this.http.put(renameItemUrl,body,{observe: 'body', responseType: 'text' ,headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }}).pipe(
catchError(this.error)
);
}
copyItem(destinationId: string, itemId: string, name:string) : Observable<any> {
let copyItemUrl = `${shURL}/items/${itemId}/copy?gcube-token=${token}`;
let body = `destinationId=${destinationId}&fileName=${name}`;
return this.http.put(copyItemUrl,body,{observe: 'body', responseType: 'text' ,headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }}).pipe(
catchError(this.error)
);
}
moveItem(destinationId: string, itemId: string) : Observable<any> {
let copyItemUrl = `${shURL}/items/${itemId}/move?gcube-token=${token}`;
let body = `destinationId=${destinationId}`;
return this.http.put(copyItemUrl,body,{observe: 'body', responseType: 'text' ,headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }}).pipe(
catchError(this.error)
);
}
error(error: HttpErrorResponse) {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
errorMessage = error.error.message;
} else {
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
}
console.log(errorMessage);
return throwError(() => {
return errorMessage;
});
}
}