From 8363126a7b16cc1afcf49917b51fed548a02a349 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Fri, 3 Jul 2020 10:36:32 +0200
Subject: [PATCH] FEAT #13671 TIME 0:35 fix check image size and resolution

---
 .../customization/customization.component.ts  | 63 +++++++++++++------
 1 file changed, 45 insertions(+), 18 deletions(-)

diff --git a/src/frontend/app/installer/customization/customization.component.ts b/src/frontend/app/installer/customization/customization.component.ts
index 7a7d2482dd4..ce1b7a8ba0e 100644
--- a/src/frontend/app/installer/customization/customization.component.ts
+++ b/src/frontend/app/installer/customization/customization.component.ts
@@ -65,7 +65,7 @@ export class CustomizationComponent implements OnInit {
             tap(() => {
                 this.checkCustomExist();
             }),
-            ).subscribe();
+        ).subscribe();
     }
 
     initStep() {
@@ -137,7 +137,7 @@ export class CustomizationComponent implements OnInit {
             language_url: `../node_modules/tinymce-i18n/langs/${LANG.langISO.replace('-', '_')}.js`,
             menubar: false,
             statusbar: false,
-            readonly : readonly,
+            readonly: readonly,
             plugins: [
                 'autolink'
             ],
@@ -155,20 +155,20 @@ export class CustomizationComponent implements OnInit {
     getInfoToInstall(): StepAction[] {
         return [
             {
-                idStep : 'createCustom',
+                idStep: 'createCustom',
                 body: {
                     customId: this.stepFormGroup.controls['customId'].value,
                     applicationName: this.stepFormGroup.controls['appName'].value,
                 },
                 description: this.lang.stepInstanceActionDesc,
-                route : {
-                    method : 'POST',
-                    url : '../rest/installer/custom'
+                route: {
+                    method: 'POST',
+                    url: '../rest/installer/custom'
                 },
                 installPriority: 1
             },
             {
-                idStep : 'customization',
+                idStep: 'customization',
                 body: {
                     loginMessage: tinymce.get('loginMessage').getContent(),
                     homeMessage: tinymce.get('homeMessage').getContent(),
@@ -176,9 +176,9 @@ export class CustomizationComponent implements OnInit {
                     logo: this.stepFormGroup.controls['uploadedLogo'].value,
                 },
                 description: this.lang.stepCustomizationActionDesc,
-                route : {
-                    method : 'POST',
-                    url : '../rest/installer/customization'
+                route: {
+                    method: 'POST',
+                    url: '../rest/installer/customization'
                 },
                 installPriority: 3
             }
@@ -187,8 +187,8 @@ export class CustomizationComponent implements OnInit {
 
     uploadTrigger(fileInput: any, mode: string) {
         if (fileInput.target.files && fileInput.target.files[0]) {
-            const allowedExtension = mode !== 'logo' ? ['image/jpg', 'image/jpeg'] : ['image/svg+xml'];
-            if (allowedExtension.indexOf(fileInput.target.files[0].type) !== -1) {
+            const res = this.canUploadFile(fileInput.target.files[0], mode);
+            if (res === true) {
                 const reader = new FileReader();
 
                 reader.readAsDataURL(fileInput.target.files[0]);
@@ -196,17 +196,44 @@ export class CustomizationComponent implements OnInit {
                     if (mode === 'logo') {
                         this.stepFormGroup.controls['uploadedLogo'].setValue(value.target.result);
                     } else {
-                        this.backgroundList.push({
-                            filename: value.target.result,
-                            url: value.target.result,
-                        });
-                        this.stepFormGroup.controls['bodyLoginBackground'].setValue(value.target.result);
+                        const img = new Image();
+                        img.onload = (imgDim: any) => {
+                            if (imgDim.target.width < 1920 || imgDim.target.height < 1080) {
+                                this.notify.error(this.scanPipe.transform(this.lang.badImageResolution, ['1920x1080']));
+                            } else {
+                                this.backgroundList.push({
+                                    filename: value.target.result,
+                                    url: value.target.result,
+                                });
+                                this.stepFormGroup.controls['bodyLoginBackground'].setValue(value.target.result);
+                            }
+                        };
+                        img.src = value.target.result;
                     }
                 };
             } else {
-                this.notify.error(this.scanPipe.transform(this.lang.onlyExtensionsAllowed, [allowedExtension.join(', ')]));
+                this.notify.error(res);
+            }
+        }
+    }
+
+    canUploadFile(file: any, mode: string) {
+        const allowedExtension = mode !== 'logo' ? ['image/jpg', 'image/jpeg'] : ['image/svg+xml'];
+
+        if (mode === 'logo') {
+            if (file.size > 5000000) {
+                return this.scanPipe.transform(this.lang.maxFileSizeExceeded, ['5mo']);
+            } else if (allowedExtension.indexOf(file.type) === -1) {
+                return this.scanPipe.transform(this.lang.onlyExtensionsAllowed, [allowedExtension.join(', ')]);
+            }
+        } else {
+            if (file.size > 10000000) {
+                return this.scanPipe.transform(this.lang.maxFileSizeExceeded, ['10mo']);
+            } else if (allowedExtension.indexOf(file.type) === -1) {
+                return this.scanPipe.transform(this.lang.onlyExtensionsAllowed, [allowedExtension.join(', ')]);
             }
         }
+        return true;
     }
 
     logoURL() {
-- 
GitLab