From 7ffe9d45611346404e2b82a17f3da2dae07b8e1b Mon Sep 17 00:00:00 2001 From: richard-loafle <44828666+richard-loafle@users.noreply.github.com> Date: Fri, 14 Feb 2020 09:30:31 +0900 Subject: [PATCH 1/2] check mime is added --- package-lock.json | 85 ++++++++++++++----- package.json | 2 + .../src/lib/services/common-api.service.ts | 31 ++++++- .../components/messages.component.ts | 28 +++++- .../src/lib/utils/mime.util.ts | 14 +++ 5 files changed, 134 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index d1bd62ae..20dfa8d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "ucap-webmessenger", - "version": "0.0.19", + "version": "0.0.20", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -3281,6 +3281,11 @@ "defer-to-connect": "^1.0.1" } }, + "@tokenizer/token": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.1.1.tgz", + "integrity": "sha512-XO6INPbZCxdprl+9qa/AAbFFOMzzwqYxpjPgLICrMD6C2FCw6qfJOPcBk6JqqPLSaZ/Qx87qn4rpPmPMwaAK6w==" + }, "@tweenjs/tween.js": { "version": "17.4.0", "resolved": "https://registry.npmjs.org/@tweenjs/tween.js/-/tween.js-17.4.0.tgz", @@ -3334,8 +3339,7 @@ "@types/debug": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz", - "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==", - "dev": true + "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==" }, "@types/detect-browser": { "version": "4.0.0", @@ -3388,6 +3392,15 @@ "integrity": "sha512-g1QUuhYVVAamfCifK7oB7G3aIl4BbOyzDOqVyUfEr4tfBKrXfeH+M+Tg7HKCXSrbzxYdhyCP7z9WbKo0R2hBCw==", "dev": true }, + "@types/file-type": { + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/@types/file-type/-/file-type-10.9.1.tgz", + "integrity": "sha512-oq0fy8Jqj19HofanFsZ56o5anMDUQtFO9B3wfLqM9o42RyCe1WT+wRbSvRbL2l8ARZXNaJturHk0b442+0yi+g==", + "dev": true, + "requires": { + "file-type": "*" + } + }, "@types/filesize": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/@types/filesize/-/filesize-4.2.0.tgz", @@ -4584,8 +4597,7 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "base": { "version": "0.11.2", @@ -4870,7 +4882,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5647,8 +5658,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "concat-stream": { "version": "1.6.2", @@ -7811,6 +7821,17 @@ "integrity": "sha512-Wz3c3XQ5xroCxd1G8b7yL0Ehkf0TC9oYC6buPFkNnU9EnaPlifeAFCyCh+iewXTyFRcg0a6j3J7FmJsIhlhBdw==", "dev": true }, + "file-type": { + "version": "14.1.2", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-14.1.2.tgz", + "integrity": "sha512-9NI4+QzDlEPB6OETc/FcJt8i8vNT396VweRwEwLcE07MnorWkZDopNuc+MeoYA7ArbzoK044JbaDExWys8deTQ==", + "requires": { + "readable-web-to-node-stream": "^2.0.0", + "strtok3": "^6.0.0", + "token-types": "^2.0.0", + "typedarray-to-buffer": "^3.1.5" + } + }, "file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", @@ -8099,8 +8120,7 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "fsevents": { "version": "2.0.7", @@ -8155,7 +8175,6 @@ "version": "7.1.4", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", - "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -8713,8 +8732,7 @@ "ieee754": { "version": "1.1.13", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", - "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==", - "dev": true + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" }, "iferr": { "version": "0.1.5", @@ -8815,7 +8833,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -8824,8 +8841,7 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "ini": { "version": "1.3.5", @@ -11158,7 +11174,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -11891,7 +11906,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "requires": { "wrappy": "1" } @@ -12356,6 +12370,11 @@ "sha.js": "^2.4.8" } }, + "peek-readable": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/peek-readable/-/peek-readable-3.1.0.tgz", + "integrity": "sha512-KGuODSTV6hcgdZvDrIDBUkN0utcAVj1LL7FfGbM0viKTtCHmtZcuEJ+lGqsp0fTFkGqesdtemV2yUSMeyy3ddA==" + }, "pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", @@ -13164,6 +13183,11 @@ "util-deprecate": "~1.0.1" } }, + "readable-web-to-node-stream": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/readable-web-to-node-stream/-/readable-web-to-node-stream-2.0.0.tgz", + "integrity": "sha512-+oZJurc4hXpaaqsN68GoZGQAQIA3qr09Or4fqEsargABnbe5Aau8hFn6ISVleT3cpY/0n/8drn7huyyEvTbghA==" + }, "readdir-scoped-modules": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/readdir-scoped-modules/-/readdir-scoped-modules-1.1.0.tgz", @@ -14859,6 +14883,17 @@ "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", "dev": true }, + "strtok3": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-6.0.0.tgz", + "integrity": "sha512-ZXlmE22LZnIBvEU3n/kZGdh770fYFie65u5+2hLK9s74DoFtpkQIdBZVeYEzlolpGa+52G5IkzjUWn+iXynOEQ==", + "requires": { + "@tokenizer/token": "^0.1.1", + "@types/debug": "^4.1.5", + "debug": "^4.1.1", + "peek-readable": "^3.1.0" + } + }, "style-loader": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/style-loader/-/style-loader-1.0.0.tgz", @@ -15148,7 +15183,6 @@ "version": "0.1.0", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.1.0.tgz", "integrity": "sha512-J7Z2K08jbGcdA1kkQpJSqLF6T0tdQqpR2pnSUXsIchbPdTI9v3e85cLW0d6WDhwuAleOV71j2xWs8qMPfK7nKw==", - "dev": true, "requires": { "rimraf": "^2.6.3" }, @@ -15157,7 +15191,6 @@ "version": "2.7.1", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", - "dev": true, "requires": { "glob": "^7.1.3" } @@ -15235,6 +15268,15 @@ "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==", "dev": true }, + "token-types": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/token-types/-/token-types-2.0.0.tgz", + "integrity": "sha512-WWvu8sGK8/ZmGusekZJJ5NM6rRVTTDO7/bahz4NGiSDb/XsmdYBn6a1N/bymUHuWYTWeuLUg98wUzvE4jPdCZw==", + "requires": { + "@tokenizer/token": "^0.1.0", + "ieee754": "^1.1.13" + } + }, "tough-cookie": { "version": "2.4.3", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-2.4.3.tgz", @@ -17720,8 +17762,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "write-file-atomic": { "version": "2.4.3", diff --git a/package.json b/package.json index 0df2281f..127d7cd1 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "electron-store": "^5.1.0", "electron-updater": "^4.2.0", "electron-window-state": "^5.0.3", + "file-type": "^14.1.2", "fs-extra": "^8.1.0", "rxjs": "^6.5.4", "semver": "^6.3.0", @@ -88,6 +89,7 @@ "@types/electron-debug": "^2.1.0", "@types/electron-devtools-installer": "^2.2.0", "@types/extract-text-webpack-plugin": "^3.0.4", + "@types/file-type": "^10.9.1", "@types/file-saver": "^2.0.1", "@types/filesize": "^4.1.0", "@types/fs-extra": "^8.0.1", diff --git a/projects/ucap-webmessenger-api-common/src/lib/services/common-api.service.ts b/projects/ucap-webmessenger-api-common/src/lib/services/common-api.service.ts index e6cb24ad..1c70a908 100644 --- a/projects/ucap-webmessenger-api-common/src/lib/services/common-api.service.ts +++ b/projects/ucap-webmessenger-api-common/src/lib/services/common-api.service.ts @@ -72,7 +72,7 @@ import { import { _MODULE_CONFIG } from '../config/token'; import { ModuleConfig } from '../config/module-config'; import { Urls } from '../config/urls'; -import { UrlConfig } from '@ucap-webmessenger/core'; +import { UrlConfig, MimeUtil } from '@ucap-webmessenger/core'; @Injectable({ providedIn: 'root' @@ -223,6 +223,35 @@ export class CommonApiService { }; } + public acceptableMimeForFileTalk( + files: File[] + ): Promise<{ accept: boolean; rejected: string[] }> { + return new Promise<{ accept: boolean; rejected: string[] }>( + async (resolve, reject) => { + let accept = true; + const rejected: string[] = []; + for (const file of files) { + const info = await MimeUtil.getMimeFromBlob(file); + + if ( + !file || + -1 === + this.moduleConfig.acceptableFileExtensions.indexOf( + info.ext.toLocaleLowerCase() + ) + ) { + rejected.push(info.ext); + accept = false; + } + } + resolve({ + accept, + rejected + }); + } + ); + } + public mimeCheckForImageAndVideoFiles(files: File[]) { files.forEach(file => { console.log(file); diff --git a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts index 1159d8dc..7796e74d 100644 --- a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts +++ b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts @@ -1111,14 +1111,36 @@ export class MessagesComponent implements OnInit, OnDestroy, AfterViewInit { // fileUploadItems.map(fui => fui.file) // ); - const checkExt = this.commonApiService.acceptableExtensionForFileTalk( - fileUploadItems.map(fui => FileUtil.getExtension(fui.file.name)) + const checkExt = await this.commonApiService.acceptableMimeForFileTalk( + fileUploadItems.map(fui => fui.file) ); + + // const checkExt = this.commonApiService.acceptableExtensionForFileTalk( + // fileUploadItems.map(fui => FileUtil.getExtension(fui.file.name)) + // ); if (!checkExt.accept) { if (!!this.fileUploadQueue) { this.fileUploadQueue.onUploadComplete(); } + // this.snackBarService.openFromComponent< + // AlertSnackbarComponent, + // AlertSnackbarData + // >(AlertSnackbarComponent, { + // duration: 1000, + // verticalPosition: 'bottom', + // horizontalPosition: 'center', + // data: { + // html: this.translateService.instant( + // 'common.file.errors.notSupporedType', + // { + // supporedType: + // checkExt.reject.length > 0 ? checkExt.reject.join(',') : '' + // } + // ) + // } + // }); + this.snackBarService.openFromComponent< AlertSnackbarComponent, AlertSnackbarData @@ -1131,7 +1153,7 @@ export class MessagesComponent implements OnInit, OnDestroy, AfterViewInit { 'common.file.errors.notSupporedType', { supporedType: - checkExt.reject.length > 0 ? checkExt.reject.join(',') : '' + checkExt.rejected.length > 0 ? checkExt.rejected.join(',') : '' } ) } diff --git a/projects/ucap-webmessenger-core/src/lib/utils/mime.util.ts b/projects/ucap-webmessenger-core/src/lib/utils/mime.util.ts index 039f8306..4b4a4077 100644 --- a/projects/ucap-webmessenger-core/src/lib/utils/mime.util.ts +++ b/projects/ucap-webmessenger-core/src/lib/utils/mime.util.ts @@ -1,3 +1,5 @@ +import * as FileType from 'file-type/browser'; + const MimeMap: Map = new Map([ ['123', 'application/vnd.lotus-1-2-3'], ['3dml', 'text/vnd.in3d.3dml'], @@ -871,4 +873,16 @@ export class MimeUtil { static getMimeFromExtension(ext: string): string | undefined { return MimeMap.get(ext); } + + static getMimeFromBlob(blob: Blob) { + return FileType.fromBlob(blob); + } + + static getMimeFromBuffer(buffer: Buffer) { + return FileType.fromBuffer(buffer); + } + + static getMimeFromStream(stream: ReadableStream) { + return FileType.fromStream(stream); + } } From da22b6d28264653bd9804f0738b96572c9580ecc Mon Sep 17 00:00:00 2001 From: richard-loafle <44828666+richard-loafle@users.noreply.github.com> Date: Fri, 14 Feb 2020 09:35:01 +0900 Subject: [PATCH 2/2] message is added --- .../src/app/layouts/messenger/components/messages.component.ts | 2 +- projects/ucap-webmessenger-app/src/assets/i18n/en.json | 1 + projects/ucap-webmessenger-app/src/assets/i18n/ko.json | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts index 7796e74d..4b06ac3f 100644 --- a/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts +++ b/projects/ucap-webmessenger-app/src/app/layouts/messenger/components/messages.component.ts @@ -1150,7 +1150,7 @@ export class MessagesComponent implements OnInit, OnDestroy, AfterViewInit { horizontalPosition: 'center', data: { html: this.translateService.instant( - 'common.file.errors.notSupporedType', + 'common.file.errors.notAcceptableMime', { supporedType: checkExt.rejected.length > 0 ? checkExt.rejected.join(',') : '' diff --git a/projects/ucap-webmessenger-app/src/assets/i18n/en.json b/projects/ucap-webmessenger-app/src/assets/i18n/en.json index 1eb09bdf..7f03a5bd 100644 --- a/projects/ucap-webmessenger-app/src/assets/i18n/en.json +++ b/projects/ucap-webmessenger-app/src/assets/i18n/en.json @@ -441,6 +441,7 @@ "noPreview": "This file does not support preview.", "cantPlay": "This file does not support playing.", "notSupporedType": "File format is not supported.
({{supporedType}})", + "notAcceptableMime": "File type is invalid.
({{supporedType}})", "oversize": "You cannot upload files larger than {{size}} megabytes." } }, diff --git a/projects/ucap-webmessenger-app/src/assets/i18n/ko.json b/projects/ucap-webmessenger-app/src/assets/i18n/ko.json index cff93453..7f9392b1 100644 --- a/projects/ucap-webmessenger-app/src/assets/i18n/ko.json +++ b/projects/ucap-webmessenger-app/src/assets/i18n/ko.json @@ -441,6 +441,7 @@ "noPreview": "미리보기를 지원하지 않는 파일입니다.", "cantPlay": "재생을 지원하지 않는 파일입니다.", "notSupporedType": "지원하지 않는 파일형식입니다.
({{supporedType}})", + "notAcceptableMime": "유효하지 않은 파일 타입입니다.
({{supporedType}})", "oversize": "{{maxSize}}MB 이상 파일을 업로드 할 수 없습니다." } },