From add9dda75960fe0b091df42bddf8b9f60e41d49f Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 8 Oct 2019 21:36:33 +0200 Subject: [PATCH] Desktop, Cli: Give correct mime type to more file types --- ElectronClient/app/package-lock.json | 5 ----- ElectronClient/app/package.json | 1 - ReactNativeClient/lib/mime-utils.js | 10 ++++++++++ ReactNativeClient/lib/shim-init-node.js | 3 +-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ElectronClient/app/package-lock.json b/ElectronClient/app/package-lock.json index 4d09d82f5d..c410ec26d1 100644 --- a/ElectronClient/app/package-lock.json +++ b/ElectronClient/app/package-lock.json @@ -4568,11 +4568,6 @@ } } }, - "mime": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/mime/-/mime-2.3.1.tgz", - "integrity": "sha512-OEUllcVoydBHGN1z84yfQDimn58pZNNNXgZlHXSboxMlFvgI6MXSWpWKpFRra7H1HxpVhHTkrghfRW49k6yjeg==" - }, "mime-db": { "version": "1.42.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.42.0.tgz", diff --git a/ElectronClient/app/package.json b/ElectronClient/app/package.json index eaee4ed9b9..ca38f66010 100644 --- a/ElectronClient/app/package.json +++ b/ElectronClient/app/package.json @@ -125,7 +125,6 @@ "markdown-it-sup": "^1.0.0", "markdown-it-toc-done-right": "^4.0.2", "md5": "^2.2.1", - "mime": "^2.3.1", "moment": "^2.22.2", "multiparty": "^4.2.1", "mustache": "^3.0.1", diff --git a/ReactNativeClient/lib/mime-utils.js b/ReactNativeClient/lib/mime-utils.js index a14a65ed39..2fe62c335b 100644 --- a/ReactNativeClient/lib/mime-utils.js +++ b/ReactNativeClient/lib/mime-utils.js @@ -768,6 +768,9 @@ const mimeTypes = [ { t: 'x-conference/x-cooltalk', e: ['ice'] }, ]; +// Note: if the list above is ever updated, make sure Markdown doesn't appear twice +mimeTypes.push({ t: 'text/markdown', e: ['md', 'markdown'] }); + const mime = { fromFileExtension(ext) { ext = ext.toLowerCase(); @@ -780,6 +783,13 @@ const mime = { return null; }, + fromFilename(name) { + if (!name) return null; + const splitted = name.trim().split('.'); + if (splitted.length <= 1) return null; + return mime.fromFileExtension(splitted[splitted.length - 1]); + }, + toFileExtension(mimeType) { mimeType = mimeType.toLowerCase(); for (let i = 0; i < mimeTypes.length; i++) { diff --git a/ReactNativeClient/lib/shim-init-node.js b/ReactNativeClient/lib/shim-init-node.js index 87f040ad61..b15709076f 100644 --- a/ReactNativeClient/lib/shim-init-node.js +++ b/ReactNativeClient/lib/shim-init-node.js @@ -122,7 +122,6 @@ function shimInit() { const { uuid } = require('lib/uuid.js'); const { basename, fileExtension, safeFileExtension } = require('lib/path-utils.js'); - const mime = require('mime/lite'); if (!(await fs.pathExists(filePath))) throw new Error(_('Cannot access %s', filePath)); @@ -132,7 +131,7 @@ function shimInit() { let resource = Resource.new(); resource.id = resourceId; - resource.mime = mime.getType(filePath); + resource.mime = mimeUtils.fromFilename(filePath); resource.title = basename(filePath); let fileExt = safeFileExtension(fileExtension(filePath));