From a33f30f2edfe5ae1085285a2395fe4284f8ce231 Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio@bekerle.com> Date: Fri, 5 Jun 2020 07:56:36 +0200 Subject: [PATCH] Integrate mime type checking for files in a ZIP file (VPU/Apps/Signature#11) --- packages/file-handling/src/fileupload.js | 30 ++++++++++++++---------- packages/file-handling/vendor/common | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/file-handling/src/fileupload.js b/packages/file-handling/src/fileupload.js index 8ccce8cf..158575f4 100644 --- a/packages/file-handling/src/fileupload.js +++ b/packages/file-handling/src/fileupload.js @@ -1,6 +1,5 @@ import {i18n} from './i18n'; import {css, html} from 'lit-element'; -import {ifDefined} from 'lit-html/directives/if-defined'; import {ScopedElementsMixin} from '@open-wc/scoped-elements'; // import JSONLD from 'vpu-common/jsonld'; import VPULitElement from 'vpu-common/vpu-lit-element'; @@ -246,22 +245,29 @@ export class FileUpload extends ScopedElementsMixin(VPULitElement) { if (zipEntry.dir) { return; } - // TODO: find way to check mime type, see https://github.com/Stuk/jszip/issues/626 - // if (!this.checkFileType(zipEntry)) { - // return; - // } await zipEntry.async("blob") - .then((blob) => { - filesToHandle.push(new File([blob], zipEntry.name)); - }, (e) => { - // handle the error - console.error("Decompressing of file in " + file.name + " failed:" + e.message); - }); + .then(async (blob) => { + // get mime type of Blob, see https://github.com/Stuk/jszip/issues/626 + const mimeType = await commonUtils.getMimeTypeOfFile(blob); + + // create new file with name and mime type + const zipEntryFile = new File([blob], zipEntry.name, { type: mimeType }); + + // check mime type + if (!this.checkFileType(zipEntryFile)) { + return; + } + + filesToHandle.push(zipEntryFile); + }, (e) => { + // handle the error + console.error("Decompressing of file in " + file.name + " failed: " + e.message); + }); }); }, function (e) { // handle the error - console.error("Loading of " + file.name + " failed:" + e.message); + console.error("Loading of " + file.name + " failed: " + e.message); }); return filesToHandle; diff --git a/packages/file-handling/vendor/common b/packages/file-handling/vendor/common index 821137e2..338c8012 160000 --- a/packages/file-handling/vendor/common +++ b/packages/file-handling/vendor/common @@ -1 +1 @@ -Subproject commit 821137e246fdcee9ccb04e4cfe420e7abec3186e +Subproject commit 338c8012f85e7b54a1e32f0f7af603e864573463 -- GitLab