From 3bc2373984b671bce4e2acbee27e47b2c4f92d5f Mon Sep 17 00:00:00 2001 From: Franco Montenegro Date: Fri, 12 Aug 2022 18:16:54 -0300 Subject: [PATCH] Set file path and mime in file selector. --- electron/index.js | 2 ++ package.json | 1 + ui/component/common/file-selector.jsx | 11 ++++++++++- ui/component/publishFile/view.jsx | 2 +- yarn.lock | 10 ++++++++++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/electron/index.js b/electron/index.js index 8dea1e234..67fcb2420 100644 --- a/electron/index.js +++ b/electron/index.js @@ -20,6 +20,7 @@ import path from 'path'; import { diskSpaceLinux, diskSpaceWindows, diskSpaceMac } from '../ui/util/diskspace'; const { download } = require('electron-dl'); +const mime = require('mime'); const remote = require('@electron/remote/main'); const os = require('os'); const sudo = require('sudo-prompt'); @@ -318,6 +319,7 @@ ipcMain.handle('get-file-from-path', (event, path) => { const fileName = folders[folders.length - 1]; resolve({ name: fileName, + mime: mime.getType(fileName) || undefined, path: path, buffer: data, }); diff --git a/package.json b/package.json index b62603452..e0b489d73 100644 --- a/package.json +++ b/package.json @@ -54,6 +54,7 @@ "humanize-duration": "^3.27.0", "if-env": "^1.0.4", "match-sorter": "^6.3.0", + "mime": "^3.0.0", "node-html-parser": "^5.1.0", "parse-duration": "^1.0.0", "proxy-polyfill": "0.1.6", diff --git a/ui/component/common/file-selector.jsx b/ui/component/common/file-selector.jsx index a74b5cb27..ef15ec57f 100644 --- a/ui/component/common/file-selector.jsx +++ b/ui/component/common/file-selector.jsx @@ -83,7 +83,16 @@ class FileSelector extends React.PureComponent { if (!result) { return; } - const file = new File([result.buffer], result.name); + const file = new File([result.buffer], result.name, { + type: result.mime, + }); + // "path" is a read only property so we have to use this + // hack to overcome the limitation. + // $FlowFixMe + Object.defineProperty(file, 'path', { + value: result.path, + writable: false, + }); this.props.onFileChosen(file); }); }; diff --git a/ui/component/publishFile/view.jsx b/ui/component/publishFile/view.jsx index b53b7720c..7c9c2c15f 100644 --- a/ui/component/publishFile/view.jsx +++ b/ui/component/publishFile/view.jsx @@ -233,7 +233,7 @@ function PublishFile(props: Props) { isTextPost = contentType[1] === 'plain' || contentType[1] === 'markdown'; setCurrentFileType(contentType); } else if (file.name) { - // If user's machine is missign a valid content type registration + // If user's machine is missing a valid content type registration // for markdown content: text/markdown, file extension will be used instead const extension = file.name.split('.').pop(); isTextPost = MARKDOWN_FILE_EXTENSIONS.includes(extension); diff --git a/yarn.lock b/yarn.lock index 25071062b..4af269233 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11549,6 +11549,7 @@ __metadata: lodash-es: ^4.17.21 mammoth: ^1.4.16 match-sorter: ^6.3.0 + mime: ^3.0.0 moment: ^2.29.2 node-abi: ^2.5.1 node-fetch: ^2.6.7 @@ -12576,6 +12577,15 @@ __metadata: languageName: node linkType: hard +"mime@npm:^3.0.0": + version: 3.0.0 + resolution: "mime@npm:3.0.0" + bin: + mime: cli.js + checksum: f43f9b7bfa64534e6b05bd6062961681aeb406a5b53673b53b683f27fcc4e739989941836a355eef831f4478923651ecc739f4a5f6e20a76487b432bfd4db928 + languageName: node + linkType: hard + "mimic-fn@npm:^1.0.0": version: 1.2.0 resolution: "mimic-fn@npm:1.2.0"