fix: return full url of assets in admin and mgmt api (#3157)

* fix: return full url of assets in admin and mgmt api

* remove asset loading over asset service

* remove unused code

Co-authored-by: Max Peintner <[email protected]>
This commit is contained in:
Livio Amstutz
2022-02-04 15:02:18 +01:00
committed by GitHub
co-authored by Max Peintner
parent 31bdd3f431
commit ab62f2d79d
10 changed files with 588 additions and 626 deletions
@@ -1,6 +1,5 @@
import { HttpErrorResponse } from '@angular/common/http';
import { Component, EventEmitter, Injector, OnDestroy, Type } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { ActivatedRoute } from '@angular/router';
import { Subscription } from 'rxjs';
import { switchMap, take } from 'rxjs/operators';
@@ -66,8 +65,6 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
public previewData!: LabelPolicy.AsObject;
public data!: LabelPolicy.AsObject;
public images: { [key: string]: any; } = {};
public panelOpenState: boolean = false;
public isHoveringOverDarkLogo: boolean = false;
public isHoveringOverDarkIcon: boolean = false;
@@ -86,7 +83,6 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
public AssetType: any = AssetType;
public refreshPreview: EventEmitter<void> = new EventEmitter();
public loadingImages: boolean = false;
private org!: Org.AsObject;
public currentPolicy: GridPolicy = PRIVATELABEL_POLICY;
public InfoSectionType: any = InfoSectionType;
@@ -96,32 +92,35 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
private toast: ToastService,
private injector: Injector,
private assetService: AssetService,
private sanitizer: DomSanitizer,
private storage: StorageService,
private themeService: ThemeService,
) {
const org: Org.AsObject | null = (this.storage.getItem(StorageKey.organization, StorageLocation.session));
const org: Org.AsObject | null = this.storage.getItem(StorageKey.organization, StorageLocation.session);
if (org) {
this.org = org;
}
this.sub = this.route.data.pipe(switchMap(data => {
this.serviceType = data.serviceType;
this.sub = this.route.data
.pipe(
switchMap((data) => {
this.serviceType = data.serviceType;
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
this.service = this.injector.get(ManagementService as Type<ManagementService>);
break;
case PolicyComponentServiceType.ADMIN:
this.service = this.injector.get(AdminService as Type<AdminService>);
break;
}
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
this.service = this.injector.get(ManagementService as Type<ManagementService>);
break;
case PolicyComponentServiceType.ADMIN:
this.service = this.injector.get(AdminService as Type<AdminService>);
break;
}
return this.route.params;
})).subscribe(() => {
this.fetchData();
});
return this.route.params;
}),
)
.subscribe(() => {
this.fetchData();
});
}
public toggleHoverLogo(theme: Theme, isHovering: boolean): void {
@@ -140,7 +139,6 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
public onDropLogo(theme: Theme, filelist: FileList): Promise<any> | void {
const file = filelist.item(0);
if (file) {
if (file.size > MAX_ALLOWED_SIZE) {
this.toast.showInfo('POLICY.PRIVATELABELING.MAXSIZEEXCEEDED', true);
} else {
@@ -183,19 +181,19 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
}
public deleteFont(): Promise<any> {
const handler = (prom: Promise<any>) => prom.then(() => {
this.toast.showInfo('POLICY.TOAST.DELETESUCCESS', true);
setTimeout(() => {
this.loadingImages = true;
this.getPreviewData().then(data => {
if (data.policy) {
this.previewData = data.policy;
this.loadPreviewImages();
}
});
}, 1000);
}).catch(error => this.toast.showError(error));
const handler = (prom: Promise<any>) =>
prom
.then(() => {
this.toast.showInfo('POLICY.TOAST.DELETESUCCESS', true);
setTimeout(() => {
this.getPreviewData().then((data) => {
if (data.policy) {
this.previewData = data.policy;
}
});
}, 1000);
})
.catch((error) => this.toast.showError(error));
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
@@ -207,19 +205,18 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
public deleteAsset(type: AssetType, theme: Theme): any {
const previewHandler = (prom: Promise<any>) => {
return prom.then(() => {
this.toast.showInfo('POLICY.TOAST.DELETESUCCESS', true);
setTimeout(() => {
this.loadingImages = true;
this.getPreviewData().then(data => {
if (data.policy) {
this.previewData = data.policy;
this.loadPreviewImages();
}
});
}, 1000);
}).catch(error => this.toast.showError(error));
return prom
.then(() => {
this.toast.showInfo('POLICY.TOAST.DELETESUCCESS', true);
setTimeout(() => {
this.getPreviewData().then((data) => {
if (data.policy) {
this.previewData = data.policy;
}
});
}, 1000);
})
.catch((error) => this.toast.showError(error));
};
switch (this.serviceType) {
@@ -268,7 +265,6 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
public onDropIcon(theme: Theme, filelist: FileList): void {
const file = filelist.item(0);
if (file) {
if (file.size > MAX_ALLOWED_SIZE) {
this.toast.showInfo('POLICY.PRIVATELABELING.MAXSIZEEXCEEDED', true);
} else {
@@ -299,18 +295,20 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
}
private handleFontUploadPromise(task: Promise<any>): Promise<any> {
const enhTask = task.then(() => {
this.toast.showInfo('POLICY.TOAST.UPLOADSUCCESS', true);
setTimeout(() => {
this.getPreviewData().then(data => {
if (data.policy) {
this.previewData = data.policy;
}
});
}, 1000);
}).catch(error => this.toast.showError(error));
const enhTask = task
.then(() => {
this.toast.showInfo('POLICY.TOAST.UPLOADSUCCESS', true);
setTimeout(() => {
this.getPreviewData().then((data) => {
if (data.policy) {
this.previewData = data.policy;
}
});
}, 1000);
})
.catch((error) => this.toast.showError(error));
if (this.serviceType === PolicyComponentServiceType.MGMT && ((this.previewData as LabelPolicy.AsObject).isDefault)) {
if (this.serviceType === PolicyComponentServiceType.MGMT && (this.previewData as LabelPolicy.AsObject).isDefault) {
return this.savePolicy().then(() => enhTask);
} else {
return enhTask;
@@ -318,21 +316,20 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
}
private handleUploadPromise(task: Promise<any>): Promise<any> {
const enhTask = task.then(() => {
this.toast.showInfo('POLICY.TOAST.UPLOADSUCCESS', true);
setTimeout(() => {
this.loadingImages = true;
this.getPreviewData().then(data => {
const enhTask = task
.then(() => {
this.toast.showInfo('POLICY.TOAST.UPLOADSUCCESS', true);
setTimeout(() => {
this.getPreviewData().then((data) => {
if (data.policy) {
this.previewData = data.policy;
}
});
}, 1000);
})
.catch((error) => this.toast.showError(error));
if (data.policy) {
this.previewData = data.policy;
this.loadPreviewImages();
}
});
}, 1000);
}).catch(error => this.toast.showError(error));
if (this.serviceType === PolicyComponentServiceType.MGMT && ((this.previewData as LabelPolicy.AsObject).isDefault)) {
if (this.serviceType === PolicyComponentServiceType.MGMT && (this.previewData as LabelPolicy.AsObject).isDefault) {
return this.savePolicy().then(() => enhTask);
} else {
return enhTask;
@@ -342,133 +339,44 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
public fetchData(): void {
this.loading = true;
this.authService.canUseFeature(['label_policy.private_label']).pipe(take(1)).subscribe((canUse) => {
this.getPreviewData().then(data => {
if (data.policy) {
this.previewData = data.policy;
this.loading = false;
this.authService
.canUseFeature(['label_policy.private_label'])
.pipe(take(1))
.subscribe((canUse) => {
this.getPreviewData()
.then((data) => {
if (data.policy) {
this.previewData = data.policy;
this.loading = false;
}
})
.catch((error) => {
this.toast.showError(error);
});
if ((canUse === true && this.serviceType === PolicyComponentServiceType.MGMT) ||
this.serviceType === PolicyComponentServiceType.ADMIN) {
this.loadingImages = true;
this.loadPreviewImages();
}
}
}).catch(error => {
this.toast.showError(error);
this.getData()
.then((data) => {
if (data.policy) {
this.data = data.policy;
this.loading = false;
}
})
.catch((error) => {
this.toast.showError(error);
});
});
this.getData().then(data => {
if (data.policy) {
this.data = data.policy;
this.loading = false;
if ((canUse === true && this.serviceType === PolicyComponentServiceType.MGMT) ||
this.serviceType === PolicyComponentServiceType.ADMIN) {
this.loadImages();
}
}
}).catch(error => {
this.toast.showError(error);
});
});
}
private loadImages(): void {
const promises: Promise<any>[] = [];
if (this.serviceType === PolicyComponentServiceType.ADMIN) {
if (this.data.logoUrlDark) {
promises.push(this.loadAsset('darkLogo', AssetEndpoint.IAMDARKLOGO));
}
if (this.data.iconUrlDark) {
promises.push(this.loadAsset('darkIcon', AssetEndpoint.IAMDARKICON));
}
if (this.data.logoUrl) {
promises.push(this.loadAsset('logo', AssetEndpoint.IAMLOGO));
}
if (this.data.iconUrl) {
promises.push(this.loadAsset('icon', AssetEndpoint.IAMICON));
}
} else if (this.serviceType === PolicyComponentServiceType.MGMT) {
if (this.data.logoUrlDark) {
promises.push(this.loadAsset('darkLogo', AssetEndpoint.MGMTDARKLOGO));
}
if (this.data.iconUrlDark) {
promises.push(this.loadAsset('darkIcon', AssetEndpoint.MGMTDARKICON));
}
if (this.data.logoUrl) {
promises.push(this.loadAsset('logo', AssetEndpoint.MGMTLOGO));
}
if (this.data.iconUrl) {
promises.push(this.loadAsset('icon', AssetEndpoint.MGMTICON));
}
}
if (promises.length) {
Promise.all(promises).then(() => {
this.loadingImages = false;
this.refreshPreview.emit();
}).catch(error => {
this.loadingImages = false;
});
} else {
this.loadingImages = false;
}
}
private loadPreviewImages(): void {
const promises: Promise<any>[] = [];
if (this.serviceType === PolicyComponentServiceType.ADMIN) {
if (this.previewData.logoUrlDark) {
promises.push(this.loadAsset('previewDarkLogo', AssetEndpoint.IAMDARKLOGOPREVIEW));
}
if (this.previewData.iconUrlDark) {
promises.push(this.loadAsset('previewDarkIcon', AssetEndpoint.IAMDARKICONPREVIEW));
}
if (this.previewData.logoUrl) {
promises.push(this.loadAsset('previewLogo', AssetEndpoint.IAMLOGOPREVIEW));
}
if (this.previewData.iconUrl) {
promises.push(this.loadAsset('previewIcon', AssetEndpoint.IAMICONPREVIEW));
}
} else if (this.serviceType === PolicyComponentServiceType.MGMT) {
if (this.previewData.logoUrlDark) {
promises.push(this.loadAsset('previewDarkLogo', AssetEndpoint.MGMTDARKLOGOPREVIEW));
}
if (this.previewData.iconUrlDark) {
promises.push(this.loadAsset('previewDarkIcon', AssetEndpoint.MGMTDARKICONPREVIEW));
}
if (this.previewData.logoUrl) {
promises.push(this.loadAsset('previewLogo', AssetEndpoint.MGMTLOGOPREVIEW));
}
if (this.previewData.iconUrl) {
promises.push(this.loadAsset('previewIcon', AssetEndpoint.MGMTICONPREVIEW));
}
}
if (promises.length) {
Promise.all(promises).then(() => {
this.loadingImages = false;
this.refreshPreview.emit();
}).catch(error => {
this.loadingImages = false;
});
} else {
this.loadingImages = false;
}
}
public ngOnDestroy(): void {
this.sub.unsubscribe();
}
private async getPreviewData():
Promise<MgmtGetPreviewLabelPolicyResponse.AsObject |
AdminGetPreviewLabelPolicyResponse.AsObject |
MgmtGetLabelPolicyResponse.AsObject |
AdminGetLabelPolicyResponse.AsObject> {
private async getPreviewData(): Promise<
| MgmtGetPreviewLabelPolicyResponse.AsObject
| AdminGetPreviewLabelPolicyResponse.AsObject
| MgmtGetLabelPolicyResponse.AsObject
| AdminGetLabelPolicyResponse.AsObject
> {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
return (this.service as ManagementService).getPreviewLabelPolicy();
@@ -477,11 +385,12 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
}
}
private async getData():
Promise<MgmtGetPreviewLabelPolicyResponse.AsObject |
AdminGetPreviewLabelPolicyResponse.AsObject |
MgmtGetLabelPolicyResponse.AsObject |
AdminGetLabelPolicyResponse.AsObject> {
private async getData(): Promise<
| MgmtGetPreviewLabelPolicyResponse.AsObject
| AdminGetPreviewLabelPolicyResponse.AsObject
| MgmtGetLabelPolicyResponse.AsObject
| AdminGetLabelPolicyResponse.AsObject
> {
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
return (this.service as ManagementService).getLabelPolicy();
@@ -490,38 +399,28 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
}
}
private loadAsset(imagekey: string, url: string): Promise<any> {
return this.assetService.load(`${url}`, this.org.id).then(data => {
const objectURL = URL.createObjectURL(data);
this.images[imagekey] = this.sanitizer.bypassSecurityTrustUrl(objectURL);
this.refreshPreview.emit();
}).catch(error => {
this.toast.showError(error);
});
}
public removePolicy(): void {
if (this.service instanceof ManagementService) {
this.service.resetLabelPolicyToDefault().then(() => {
this.toast.showInfo('POLICY.TOAST.RESETSUCCESS', true);
setTimeout(() => {
this.fetchData();
}, 1000);
}).catch(error => {
this.toast.showError(error);
});
this.service
.resetLabelPolicyToDefault()
.then(() => {
this.toast.showInfo('POLICY.TOAST.RESETSUCCESS', true);
setTimeout(() => {
this.fetchData();
}, 1000);
})
.catch((error) => {
this.toast.showError(error);
});
}
}
public savePolicy(): Promise<any> {
const reloadPolicy = () => {
setTimeout(() => {
this.loadingImages = true;
this.getData().then(data => {
this.getData().then((data) => {
if (data.policy) {
this.data = data.policy;
this.loadImages();
}
});
}, 500);
@@ -532,34 +431,43 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
const req0 = new AddCustomLabelPolicyRequest();
this.overwriteValues(req0);
return (this.service as ManagementService).addCustomLabelPolicy(req0).then(() => {
this.toast.showInfo('POLICY.TOAST.SET', true);
return (this.service as ManagementService)
.addCustomLabelPolicy(req0)
.then(() => {
this.toast.showInfo('POLICY.TOAST.SET', true);
reloadPolicy();
}).catch((error: HttpErrorResponse) => {
this.toast.showError(error);
});
reloadPolicy();
})
.catch((error: HttpErrorResponse) => {
this.toast.showError(error);
});
} else {
const req1 = new UpdateCustomLabelPolicyRequest();
this.overwriteValues(req1);
return (this.service as ManagementService).updateCustomLabelPolicy(req1).then(() => {
this.toast.showInfo('POLICY.TOAST.SET', true);
return (this.service as ManagementService)
.updateCustomLabelPolicy(req1)
.then(() => {
this.toast.showInfo('POLICY.TOAST.SET', true);
reloadPolicy();
}).catch(error => {
this.toast.showError(error);
});
reloadPolicy();
})
.catch((error) => {
this.toast.showError(error);
});
}
case PolicyComponentServiceType.ADMIN:
const req = new UpdateLabelPolicyRequest();
this.overwriteValues(req);
return (this.service as AdminService).updateLabelPolicy(req).then(() => {
reloadPolicy();
this.toast.showInfo('POLICY.TOAST.SET', true);
}).catch(error => {
this.toast.showError(error);
});
return (this.service as AdminService)
.updateLabelPolicy(req)
.then(() => {
reloadPolicy();
this.toast.showInfo('POLICY.TOAST.SET', true);
})
.catch((error) => {
this.toast.showError(error);
});
}
}
@@ -592,39 +500,39 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
// dialog warning
switch (this.serviceType) {
case PolicyComponentServiceType.MGMT:
return (this.service as ManagementService).activateCustomLabelPolicy().then(() => {
this.toast.showInfo('POLICY.PRIVATELABELING.ACTIVATED', true);
setTimeout(() => {
this.loadingImages = true;
this.getData().then(data => {
if (data.policy) {
this.data = data.policy;
this.applyToConsole(data.policy);
this.loadImages();
}
});
}, 1000);
}).catch(error => {
this.toast.showError(error);
});
return (this.service as ManagementService)
.activateCustomLabelPolicy()
.then(() => {
this.toast.showInfo('POLICY.PRIVATELABELING.ACTIVATED', true);
setTimeout(() => {
this.getData().then((data) => {
if (data.policy) {
this.data = data.policy;
this.applyToConsole(data.policy);
}
});
}, 1000);
})
.catch((error) => {
this.toast.showError(error);
});
case PolicyComponentServiceType.ADMIN:
return (this.service as AdminService).activateLabelPolicy().then(() => {
this.toast.showInfo('POLICY.PRIVATELABELING.ACTIVATED', true);
setTimeout(() => {
this.loadingImages = true;
this.getData().then(data => {
if (data.policy) {
this.data = data.policy;
this.applyToConsole(data.policy);
this.loadImages();
}
});
}, 1000);
}).catch(error => {
this.toast.showError(error);
});
return (this.service as AdminService)
.activateLabelPolicy()
.then(() => {
this.toast.showInfo('POLICY.PRIVATELABELING.ACTIVATED', true);
setTimeout(() => {
this.getData().then((data) => {
if (data.policy) {
this.data = data.policy;
this.applyToConsole(data.policy);
}
});
}, 1000);
})
.catch((error) => {
this.toast.showError(error);
});
}
}
@@ -649,13 +557,16 @@ export class PrivateLabelingPolicyComponent implements OnDestroy {
}
public resetPolicy(): Promise<any> {
return (this.service as ManagementService).resetLabelPolicyToDefault().then(() => {
this.toast.showInfo('POLICY.PRIVATELABELING.RESET', true);
setTimeout(() => {
this.fetchData();
return (this.service as ManagementService)
.resetLabelPolicyToDefault()
.then(() => {
this.toast.showInfo('POLICY.PRIVATELABELING.RESET', true);
setTimeout(() => {
this.fetchData();
});
})
.catch((error) => {
this.toast.showError(error);
});
}).catch(error => {
this.toast.showError(error);
});
}
}