From 40587cd2983bdbbbf1dfb8030d8674ad899c2fd5 Mon Sep 17 00:00:00 2001 From: unocelli Date: Sun, 15 Dec 2024 20:25:06 +0100 Subject: [PATCH] feat: add format verification for open project and import view #1538 --- client/src/app/_services/project.service.ts | 50 ++++++++++++++++++--- client/src/app/editor/editor.component.ts | 2 +- client/src/assets/i18n/en.json | 1 + 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/client/src/app/_services/project.service.ts b/client/src/app/_services/project.service.ts index 4a47c3bb..9b1b9452 100644 --- a/client/src/app/_services/project.service.ts +++ b/client/src/app/_services/project.service.ts @@ -835,10 +835,10 @@ export class ProjectService { } private notifyError(msgCode: string) { - this.translateService.get(msgCode).subscribe((txt: string) => { msgCode = txt; }); + const msg = this.translateService.instant(msgCode); if (msgCode) { - console.error(`FUXA Error: ${msgCode}`); - this.toastr.error(msgCode, '', { + console.error(`FUXA Error: ${msg}`); + this.toastr.error(msg, '', { timeOut: 3000, closeButton: true, disableTimeOut: true @@ -875,11 +875,47 @@ export class ProjectService { * @param prj project data to save */ setProject(prj: ProjectData, skipNotification = false) { - this.projectData = prj; - if (this.appService.isClientApp) { - this.projectData = ResourceStorageService.defileProject(prj); + if (this.verifyProject(prj)) { + this.projectData = prj; + if (this.appService.isClientApp) { + this.projectData = ResourceStorageService.defileProject(prj); + } + this.save(skipNotification); + } + } + + verifyProject(prj: ProjectData): boolean { + let result = true; + if (Utils.isNullOrUndefined(prj.version)) { + result = false; + } else if (Utils.isNullOrUndefined(prj.hmi)) { + result = false; + } else if (Utils.isNullOrUndefined(prj.devices)) { + result = false; + } + if (!result) { + this.notifyError('msg.project-format-error'); } - this.save(skipNotification); + return result; + } + + verifyView(view: View): boolean { + let result = true; + if (Utils.isNullOrUndefined(view.svgcontent)) { + result = false; + } else if (Utils.isNullOrUndefined(view.id)) { + result = false; + } else if (Utils.isNullOrUndefined(view.profile)) { + result = false; + } else if (Utils.isNullOrUndefined(view.type)) { + result = false; + } else if (Utils.isNullOrUndefined(view.items)) { + result = false; + } + if (!result) { + this.notifyError('msg.view-format-error'); + } + return result; } setNewProject() { diff --git a/client/src/app/editor/editor.component.ts b/client/src/app/editor/editor.component.ts index 56ac57c6..53799776 100644 --- a/client/src/app/editor/editor.component.ts +++ b/client/src/app/editor/editor.component.ts @@ -1134,7 +1134,7 @@ export class EditorComponent implements OnInit, AfterViewInit, OnDestroy { let reader = new FileReader(); reader.onload = (data) => { let view = JSON.parse(reader.result.toString()); - if (view) { + if (this.projectService.verifyView(view)) { let idx = 1; let startname = view.name; let existView = null; diff --git a/client/src/assets/i18n/en.json b/client/src/assets/i18n/en.json index b3e1430e..f82be5bf 100644 --- a/client/src/assets/i18n/en.json +++ b/client/src/assets/i18n/en.json @@ -1578,6 +1578,7 @@ "msg.project-save-success": "Project save successful!", "msg.project-save-error": "Project save failed!", "msg.project-format-error": "Project wrong format!", + "msg.view-format-error": "View wrong format!", "msg.project-save-unauthorized": "Project save failed! Unauthorized!", "msg.project-save-ask": "Do you want to leave the project?", "msg.login-username-required": "Enter a username",