From e7480beeb95c99c610fc5e4f6afd56c143b8a0a6 Mon Sep 17 00:00:00 2001
From: Alex ORLUC <alex.orluc@maarch.org>
Date: Wed, 9 Oct 2019 14:24:41 +0200
Subject: [PATCH] FEAT #11954 TIME 0:30 add ext + size control upload file

---
 .../app/viewer/document-viewer.component.scss |  5 +++
 .../app/viewer/document-viewer.component.ts   | 44 +++++++++++++++++--
 2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/frontend/app/viewer/document-viewer.component.scss b/src/frontend/app/viewer/document-viewer.component.scss
index 45f6b9c2089..7d379f61610 100644
--- a/src/frontend/app/viewer/document-viewer.component.scss
+++ b/src/frontend/app/viewer/document-viewer.component.scss
@@ -86,3 +86,8 @@
     border-radius: 10px;
     background: #ffffff47;
 }
+
+.extensionsList {
+    height: 200px;
+    overflow: auto;
+}
\ No newline at end of file
diff --git a/src/frontend/app/viewer/document-viewer.component.ts b/src/frontend/app/viewer/document-viewer.component.ts
index aeb14dfec43..5e689fd6e93 100644
--- a/src/frontend/app/viewer/document-viewer.component.ts
+++ b/src/frontend/app/viewer/document-viewer.component.ts
@@ -8,6 +8,8 @@ import { tap, catchError, finalize, filter } from 'rxjs/operators';
 import { of } from 'rxjs';
 import { ConfirmComponent } from '../../plugins/modal/confirm.component';
 import { MatDialogRef, MatDialog } from '@angular/material';
+import { AlertComponent } from '../../plugins/modal/alert.component';
+import { SortPipe } from '../../plugins/sorting.pipe';
 
 
 @Component({
@@ -16,7 +18,7 @@ import { MatDialogRef, MatDialog } from '@angular/material';
     styleUrls: [
         'document-viewer.component.scss',
     ],
-    providers: [NotificationService, AppService]
+    providers: [NotificationService, AppService, SortPipe]
 })
 
 export class DocumentViewerComponent implements OnInit {
@@ -33,6 +35,9 @@ export class DocumentViewerComponent implements OnInit {
         src: null
     };
 
+    allowedExtensions: any[] = [];
+    maxFileSize: number = 0;
+
     dialogRef: MatDialogRef<any>;
 
     constructor(
@@ -41,12 +46,29 @@ export class DocumentViewerComponent implements OnInit {
         private headerService: HeaderService,
         public appService: AppService,
         private dialog: MatDialog,
+        private sortPipe: SortPipe
     ) {
         (<any>window).pdfWorkerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.min.js';
     }
 
     ngOnInit() {
-
+        this.http.get('../../rest/indexing/fileInformations').pipe(
+            tap((data: any) => {
+                this.allowedExtensions = data.informations.allowedFiles.map((ext: any) => {
+                    return {
+                        extension: '.' + ext.extension.toLowerCase(),
+                        mimeType: ext.mimeType,
+                    }
+                });
+                this.allowedExtensions = this.sortPipe.transform(this.allowedExtensions, 'extension');
+
+                this.maxFileSize = data.informations.maximumSize;
+            }),
+            catchError((err: any) => {
+                this.notify.handleErrors(err);
+                return of(false);
+            })
+        ).subscribe();
     }
 
     uploadTrigger(fileInput: any) {
@@ -58,7 +80,8 @@ export class DocumentViewerComponent implements OnInit {
         };
         this.noConvertedFound = false;
         this.loading = true;
-        if (fileInput.target.files && fileInput.target.files[0]) {
+
+        if (fileInput.target.files && fileInput.target.files[0] && this.checkFile(fileInput.target.files[0])) {
             var reader = new FileReader();
 
             this.file.name = fileInput.target.files[0].name;
@@ -76,6 +99,8 @@ export class DocumentViewerComponent implements OnInit {
                     this.loading = false;
                 }
             };
+        } else {
+            this.loading = false;
         }
     }
 
@@ -152,4 +177,17 @@ export class DocumentViewerComponent implements OnInit {
         this.uploadTrigger(fileInput);
     }
 
+    checkFile(file: any) {
+
+        if (this.allowedExtensions.map(ext => ext.mimeType).indexOf(file.type) === -1) {
+            this.dialog.open(AlertComponent, { autoFocus: false, disableClose: true, data: { title: 'Extension non autorisé !', msg: '<u>Extension autorisée</u> : <br/>' + this.allowedExtensions.map(ext => ext.extension).filter((elem: any, index: any, self: any) => index === self.indexOf(elem)).join(', ') } });
+            return false;
+        } else if (file.size > this.maxFileSize) {
+            this.dialog.open(AlertComponent, { autoFocus: false, disableClose: true, data: { title: 'Taille maximale dépassée ! ', msg: 'Taille maximale : ' + this.maxFileSize } });
+            return false;
+        } else {
+            return true;
+        }
+    }
+
 }
\ No newline at end of file
-- 
GitLab