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 2490f24c..e6cb24ad 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 @@ -223,6 +223,65 @@ export class CommonApiService { }; } + public mimeCheckForImageAndVideoFiles(files: File[]) { + files.forEach(file => { + console.log(file); + const fileReader = new FileReader(); + fileReader.onloadend = () => { + const arr = new Uint8Array(fileReader.result as ArrayBuffer).subarray( + 0, + 4 + ); + let header = ''; + for (let i = 0; i < arr.length; i++) { + header += arr[i].toString(16); + } + console.log('File header: ' + header); + + // Check the file signature against known types + var type = 'unknown'; + // http://forensic-proof.com/archives/300?ckattempt=1 + + if (header.startsWith('89504e47')) { + // 89504E470D0A1A0A + type = 'image/png'; + } else if (header.startsWith('47494638')) { + // 474946383761 + // 474946383961 + type = 'image/gif'; + } else if (header.startsWith('ffd8ffe')) { + // ffd8ffe0 + // ffd8ffe1 + // ffd8ffe2 + // ffd8ffe8 + type = 'image/jpeg'; + } else if (header.startsWith('424d')) { + type = 'image/bmp'; + } else if (header.startsWith('4d4d') || header.startsWith('4949')) { + type = 'image/tiff'; + } else if (header.startsWith('4F676753')) { + // 4F67675300020000 + type = 'image/ogg'; + } else if (header.startsWith('38425053')) { + // 38 42 50 53 + type = 'image/psd'; + } else if (header.startsWith('25504446')) { + // 38 42 50 53 + type = 'image/ai'; + } else if (header.startsWith('52494646')) { + // avi + type = 'video/x-msvideo'; + } else if (header.startsWith('00000018')) { + // 0000001866747970 + type = 'video/x-msvideo'; + } + + console.log('type', type); + }; + fileReader.readAsArrayBuffer(file); + }); + } + public fileTalkShare( req: FileTalkShareRequest ): Observable { 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 f3820a0d..1159d8dc 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 @@ -1107,6 +1107,10 @@ export class MessagesComponent implements OnInit, OnDestroy, AfterViewInit { } } + // this.commonApiService.mimeCheckForImageAndVideoFiles( + // fileUploadItems.map(fui => fui.file) + // ); + const checkExt = this.commonApiService.acceptableExtensionForFileTalk( fileUploadItems.map(fui => FileUtil.getExtension(fui.file.name)) );