From 86b7c21ebb443d9a430b92145eac31cf73e717bd Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Fri, 24 Feb 2023 17:28:40 +0100 Subject: [PATCH] added proxy --- angular.json | 4 + src/app/model/actions.ts | 76 +++++++++++++++++++ src/app/model/ws-item.ts | 35 +-------- .../show-folder/show-folder.component.html | 2 +- src/app/show-folder/show-folder.component.ts | 51 ++++++++----- src/app/storagehub.service.ts | 24 ++++-- src/app/trash/trash.page.ts | 11 ++- src/app/vres/vres.page.ts | 28 ++++--- src/app/ws/ws.page.ts | 26 ++++--- src/proxy.conf.json | 9 +++ 10 files changed, 177 insertions(+), 89 deletions(-) create mode 100644 src/app/model/actions.ts create mode 100644 src/proxy.conf.json diff --git a/angular.json b/angular.json index 62e5120..459f742 100644 --- a/angular.json +++ b/angular.json @@ -72,6 +72,10 @@ }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", + "options": { + "browserTarget": "app:build", + "proxyConfig": "src/proxy.conf.json" + }, "configurations": { "production": { "browserTarget": "app:build:production" diff --git a/src/app/model/actions.ts b/src/app/model/actions.ts new file mode 100644 index 0000000..44cd712 --- /dev/null +++ b/src/app/model/actions.ts @@ -0,0 +1,76 @@ +import { AlertOptions } from "@ionic/angular"; +import { title } from "process"; +import { StoragehubService } from "../storagehub.service"; +import { Item } from "./item.model"; +import { WSItem } from "./ws-item"; + +export interface Action { + + getAlertOptions(item: WSItem, storagehub: StoragehubService): AlertOptions; + + actionHandler(data: any, storagehub: StoragehubService): any; + + getName(): string; + + getActionType(): string | undefined; + + mustReload(): boolean; +} + +export class DeleteAction implements Action { + + getAlertOptions(item: WSItem, storagehub: StoragehubService): AlertOptions { + var title = item.getTitle(); + var options: AlertOptions = { + header: `Are you sure to delete item ${title} ?`, + buttons: [{ + text: 'No', + role: 'cancel' + }, + { + text: 'Yes', + handler: () => { + this.actionHandler(item, storagehub); + } + }] + }; + return options; + + } + actionHandler(data: WSItem, storagehub: StoragehubService) { + storagehub.deleteItem(data.item.id).subscribe((res) =>console.log(`item ${data.getTitle()} deleted`) ); + } + + getName(): string { + return "Delete"; + } + getActionType(): string | undefined { + return "destructive"; + } + + mustReload(): boolean { + return true; + } +} + +export class Actions { + + private static actions: Action[] = [new DeleteAction()]; + public static getActionsPerType(type: string): Action[] { + /*switch (type) { + case 'SharedFolder': + break; + case 'FolderItem': + break; + case 'PDFFileItem': + break; + case 'ImageFile': + break; + case 'ExternalLink': + break; + default: + }*/ + return Actions.actions; + } +} + diff --git a/src/app/model/ws-item.ts b/src/app/model/ws-item.ts index 673aa5f..cde109d 100644 --- a/src/app/model/ws-item.ts +++ b/src/app/model/ws-item.ts @@ -1,15 +1,14 @@ import { StoragehubService } from "../storagehub.service"; +import { Action, Actions } from "./actions"; import { Item } from "./item.model"; export class WSItem { item: Item; - storagehubService: StoragehubService; type: string; - constructor(item: Item, storagehubService: StoragehubService) { + constructor(item: Item) { this.item = item; - this.storagehubService = storagehubService; const tempType$ = this.getType(item); this.type = tempType$ ? tempType$ :""; } @@ -52,36 +51,6 @@ export class WSItem { return this.item.vreFolder? this.item.displayName: this.item.title; } - - getActions(): Action[] { - return Actions.getActionsPerType(this.type); - } - } -export interface Action { - - execute(): void; - getName(): string; -} - -class Actions{ - public static getActionsPerType(type:string): Action[] { - let actions: Action[] = []; - /*switch (type) { - case 'SharedFolder': - break; - case 'FolderItem': - break; - case 'PDFFileItem': - break; - case 'ImageFile': - break; - case 'ExternalLink': - break; - default: - }*/ - return actions; - } -} diff --git a/src/app/show-folder/show-folder.component.html b/src/app/show-folder/show-folder.component.html index 9f7f149..3f93e8b 100644 --- a/src/app/show-folder/show-folder.component.html +++ b/src/app/show-folder/show-folder.component.html @@ -14,7 +14,7 @@ - + {{ i.getIconInfo() }} {{ i.getTitle() }} diff --git a/src/app/show-folder/show-folder.component.ts b/src/app/show-folder/show-folder.component.ts index 7b63a30..af6d059 100644 --- a/src/app/show-folder/show-folder.component.ts +++ b/src/app/show-folder/show-folder.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit, Input, CUSTOM_ELEMENTS_SCHEMA, Output, EventEmitter, ElementRef, ViewChild } from '@angular/core'; -import { Platform, AlertController, ToastController, IonicModule, ActionSheetController, ActionSheetButton } from '@ionic/angular'; +import { Platform, AlertController, ToastController, IonicModule, ActionSheetController, ActionSheetButton, AlertOptions } from '@ionic/angular'; import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx'; import { Router, ActivatedRoute } from '@angular/router'; @@ -10,6 +10,7 @@ import { CommonModule } from '@angular/common'; import { Item } from '../model/item.model'; import { MatIconModule } from '@angular/material/icon'; import { WSItem } from '../model/ws-item'; +import { Action, Actions } from '../model/actions'; @Component({ standalone: true, @@ -24,11 +25,9 @@ import { WSItem } from '../model/ws-item'; }) export class ShowFolderComponent implements OnInit { - @Output() folderClickedEvent = new EventEmitter(); + @Output() folderClickedEvent = new EventEmitter(); - @Input() items: Item[] = []; - - wsItems: WSItem[] = []; + @Input() items: WSItem[] = []; @Input() parentItem: WSItem | undefined; @@ -45,23 +44,22 @@ export class ShowFolderComponent implements OnInit { ngOnInit(): void { } - ngOnChanges() { - this.items.forEach(i => this.wsItems.push(new WSItem(i, this.storagehub)) ); - } - + loadDocuments() { + console.log("loadDoc called"); if (this.parentItem) this.storagehub.getChildren(this.parentItem.item.id, false).subscribe( (res) => { - this.items = res; - this.items.forEach(i => this.wsItems.push(new WSItem(i, this.storagehub)) ); - } + const tmpItems$ : WSItem[] = [] + res.forEach(i => tmpItems$.push(new WSItem(i))); + this.items = tmpItems$; + } ) } itemClicked(item: WSItem) { if (item.isFolder()) { - this.folderClickedEvent.emit(item.item); + this.folderClickedEvent.emit(item); } } @@ -83,7 +81,7 @@ export class ShowFolderComponent implements OnInit { } async createFolder() { - let alert = await this.alertCtrl.create({ + this.presentAlertControl({ header: 'Create folder', message: 'Please specify the name of the new folder', inputs: [ @@ -111,18 +109,17 @@ export class ShowFolderComponent implements OnInit { } ] }); - await alert.present(); } async presentActionSheet(item: WSItem) { - var buttons :ActionSheetButton[] = []; - item.getActions().forEach( action => buttons.push({ - text: action.name, + Actions.getActionsPerType(item.type).forEach( action => buttons.push({ + text: action.getName(), data: { - action: action.name, + action: action.getName(), }, - handler: () => action.execute() + role: action.getActionType(), + handler: () => this.actionHandler(action, item) })); buttons.push({ @@ -143,9 +140,21 @@ export class ShowFolderComponent implements OnInit { }); await actionSheet.present(); - const result = await actionSheet.onDidDismiss(); + } + async presentAlertControl(options: AlertOptions){ + let alert = await this.alertCtrl.create(options); + await alert.present(); + } + + async actionHandler(action: Action, item: WSItem){ + var options: AlertOptions = action.getAlertOptions(item, this.storagehub); + if (options){ + await this.presentAlertControl(options); + }else + await action.actionHandler(item, this.storagehub); + } } diff --git a/src/app/storagehub.service.ts b/src/app/storagehub.service.ts index 6f2a5f6..d873655 100644 --- a/src/app/storagehub.service.ts +++ b/src/app/storagehub.service.ts @@ -10,7 +10,7 @@ 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() @@ -28,7 +28,7 @@ export class StoragehubService { } getTrashRoot(): Observable { - const getTrashURL = `${shURL}trash/?gcube-token=${token}&exclude=hl:accounting`; + const getTrashURL = `${shURL}/trash/?gcube-token=${token}&exclude=hl:accounting`; return this.http.get(getTrashURL, {observe: "body", responseType: "json"}).pipe( map(value => value.item), catchError(this.error) @@ -36,7 +36,7 @@ export class StoragehubService { } getVres(): Observable { - const getVresURL = `${shURL}vrefolders/?gcube-token=${token}&exclude=hl:accounting`; + const getVresURL = `${shURL}/vrefolders/?gcube-token=${token}&exclude=hl:accounting`; return this.http.get(getVresURL, {observe: 'body', responseType: 'json'}).pipe( map(value => value.itemlist), catchError(this.error) @@ -44,7 +44,7 @@ export class StoragehubService { } getItem(id: string): Observable { - const getWsURL = `${shURL}items/${id}?gcube-token=${token}&exclude=hl:accounting`; + const getWsURL = `${shURL}/items/${id}?gcube-token=${token}&exclude=hl:accounting`; return this.http.get(getWsURL, {observe: 'body', responseType: 'json'}).pipe( map(value => value.item), catchError(this.error) @@ -52,7 +52,7 @@ export class StoragehubService { } getChildren(id:string, onlyFolder?:boolean): Observable{ - let getWsURL = `${shURL}items/${id}/children?gcube-token=${token}&exclude=hl:accounting`; + let getWsURL = `${shURL}/items/${id}/children?gcube-token=${token}&exclude=hl:accounting`; if (onlyFolder) getWsURL += '&onlyType=nthl:workspaceItem'; return this.http.get(getWsURL).pipe( @@ -62,7 +62,7 @@ export class StoragehubService { } getAnchestor(id:string): Observable{ - let getWsURL = `${shURL}items/${id}/ancherstors?gcube-token=${token}&exclude=hl:accounting`; + let getWsURL = `${shURL}/items/${id}/ancherstors?gcube-token=${token}&exclude=hl:accounting`; console.log("ws children "+getWsURL); return this.http.get(getWsURL, {observe: 'body', responseType: 'json'}).pipe( map(value => value.itemlist), @@ -71,7 +71,7 @@ export class StoragehubService { } uploadFile(parentId: string, name: string, file: File){ - let uploadURL = `${shURL}items/${parentId}/create/FILE?gcube-token=${token}`; + let uploadURL = `${shURL}/items/${parentId}/create/FILE?gcube-token=${token}`; const formData = new FormData(); formData.append("name", name); formData.append("description", ""); @@ -82,7 +82,7 @@ export class StoragehubService { } createFolder(parentId: string, name: string, description: string) : Observable { - let createFolderURL = `${shURL}items/${parentId}/create/FOLDER?gcube-token=${token}`; + 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( @@ -90,6 +90,14 @@ export class StoragehubService { ); } + deleteItem(itemId: string) : Observable { + let deleteItemUrl = `${shURL}/items/${itemId}?gcube-token=${token}`; + console.log("delete item done with url "+deleteItemUrl); + return this.http.delete( deleteItemUrl).pipe( + catchError(this.error) + ); + } + error(error: HttpErrorResponse) { let errorMessage = ''; if (error.error instanceof ErrorEvent) { diff --git a/src/app/trash/trash.page.ts b/src/app/trash/trash.page.ts index 92b00f0..5b834ce 100644 --- a/src/app/trash/trash.page.ts +++ b/src/app/trash/trash.page.ts @@ -1,6 +1,7 @@ import { Component, OnInit } from '@angular/core'; import { lastValueFrom } from 'rxjs'; import { Item } from '../model/item.model'; +import { WSItem } from '../model/ws-item'; import { StoragehubService } from '../storagehub.service'; @Component({ @@ -13,7 +14,7 @@ import { StoragehubService } from '../storagehub.service'; export class TrashPage implements OnInit { - values: Item[] = []; + values: WSItem[] = []; constructor(private storagehub: StoragehubService) { } @@ -26,11 +27,15 @@ export class TrashPage implements OnInit { private onSuccess(res: Item) { this.storagehub.getChildren(res.id, false).subscribe( - (res) => this.values = res + (res) => { + const tmpItems$ : WSItem[] = [] + res.forEach(i => tmpItems$.push(new WSItem(i))); + this.values = tmpItems$; + } ) } - public getValues(): Item[] { + public getValues(): WSItem[] { return this.values; } } diff --git a/src/app/vres/vres.page.ts b/src/app/vres/vres.page.ts index f64c04c..8151609 100644 --- a/src/app/vres/vres.page.ts +++ b/src/app/vres/vres.page.ts @@ -12,9 +12,9 @@ import { StoragehubService } from '../storagehub.service'; }) export class VresPage implements OnInit { - values: Item[] = []; + values: WSItem[] = []; - item: Item | undefined; + item: WSItem | undefined; folderid: string | undefined; @@ -27,12 +27,16 @@ export class VresPage implements OnInit { this.folderid = this.route.snapshot.paramMap.get('folderid') || undefined; if (!this.folderid) this.storagehub.getVres().subscribe( - (res) => this.values = res + (res) => { + const tmpItems$ : WSItem[] = [] + res.forEach(i => tmpItems$.push(new WSItem(i))); + this.values = tmpItems$; + } ); else this.storagehub.getItem(this.folderid).subscribe( (res) => { - this.item = res; + this.item = new WSItem(res); this.onSuccess(res); } ); @@ -42,21 +46,23 @@ export class VresPage implements OnInit { private onSuccess(item: Item) { this.storagehub.getChildren(item.id, false).subscribe( - (res) => this.values = res + (res) => { + const tmpItems$ : WSItem[] = [] + res.forEach(i => tmpItems$.push(new WSItem(i))); + this.values = tmpItems$; + } ) } - public getValues(): Item[] { + public getValues(): WSItem[] { return this.values; } - openFolder(item: Item) { - this.router.navigateByUrl(`tabs/vres/${item.id}`); + openFolder(item: WSItem) { + this.router.navigateByUrl(`tabs/vres/${item.item.id}`); } public getCurrentItem(): WSItem | undefined{ - if (this.item) - return new WSItem(this.item, this.storagehub); - return undefined; + return this.item; } } diff --git a/src/app/ws/ws.page.ts b/src/app/ws/ws.page.ts index 9683de8..382aaee 100644 --- a/src/app/ws/ws.page.ts +++ b/src/app/ws/ws.page.ts @@ -2,9 +2,9 @@ import { Component, OnInit } from '@angular/core'; import { Platform, AlertController, ToastController } from '@ionic/angular'; import { Router, ActivatedRoute } from '@angular/router'; import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx'; -import { Item } from '../model/item.model'; import { StoragehubService } from '../storagehub.service'; import { WSItem } from '../model/ws-item'; +import { Item } from '../model/item.model'; @Component({ selector: 'app-ws', @@ -14,11 +14,11 @@ import { WSItem } from '../model/ws-item'; }) export class WsPage implements OnInit { - values: Item[] = []; + values: WSItem[] = []; folderid: string | undefined; - item : Item | undefined ; + item : WSItem | undefined ; constructor(private storagehub: StoragehubService, private router: Router, @@ -33,7 +33,7 @@ export class WsPage implements OnInit { else this.storagehub.getItem(this.folderid).subscribe( (res) => { - this.item = res; + this.item = new WSItem(res); this.onSuccess(res); } ); @@ -42,21 +42,23 @@ export class WsPage implements OnInit { private onSuccess(item: Item) { this.storagehub.getChildren(item.id, false).subscribe( - (res) => this.values = res + (res) => { + const tmpItems$ : WSItem[] = [] + res.forEach(i => tmpItems$.push(new WSItem(i))); + this.values = tmpItems$; + } ) } - public getValues(): Item[] { - return this.values; + public getValues(): WSItem[] { + return this.values; } - openFolder(item: Item) { - this.router.navigateByUrl(`tabs/ws/${item.id}`); + openFolder(item: WSItem) { + this.router.navigateByUrl(`tabs/ws/${item.item.id}`); } public getCurrentItem() : WSItem | undefined{ - if (this.item) - return new WSItem(this.item, this.storagehub); - return undefined; + return this.item; } } diff --git a/src/proxy.conf.json b/src/proxy.conf.json new file mode 100644 index 0000000..cae6154 --- /dev/null +++ b/src/proxy.conf.json @@ -0,0 +1,9 @@ +{ + "/shub": { + "target": "https://workspace-repository.dev.d4science.org", + "secure": "true", + "changeOrigin": "true", + "logLevel": "debug", + "pathRewrite": {"^/shub" : ""} + } +} \ No newline at end of file