From 25a71be143525770e6541f32e2a43ace49b51341 Mon Sep 17 00:00:00 2001 From: Franco Montenegro Date: Wed, 17 Aug 2022 12:33:59 -0300 Subject: [PATCH] Update get-file-from-path to work with folders; fix file-list component. --- electron/index.js | 37 +++++++++++++++++++-------- ui/component/common/file-list.jsx | 6 ++--- ui/component/common/file-selector.jsx | 1 - 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/electron/index.js b/electron/index.js index 67fcb2420..2c28a5a61 100644 --- a/electron/index.js +++ b/electron/index.js @@ -305,23 +305,40 @@ app.on('before-quit', () => { // Example: // const result = await ipcMain.invoke('get-file-from-path', 'path/to/file'); // const file = new File([result.buffer], result.name); +// NOTE: if path points to a folder, an empty +// file will be given. ipcMain.handle('get-file-from-path', (event, path) => { return new Promise((resolve, reject) => { - // Encoding null ensures data results in a Buffer. - fs.readFile(path, { encoding: null }, (err, data) => { - if (err) { - reject(err); + fs.stat(path, (error, stats) => { + if (error) { + reject(error); return; } // Separate folders considering "\" and "/" // as separators (cross platform) const folders = path.split(/[\\/]/); - const fileName = folders[folders.length - 1]; - resolve({ - name: fileName, - mime: mime.getType(fileName) || undefined, - path: path, - buffer: data, + const name = folders[folders.length - 1]; + if (stats.isDirectory()) { + resolve({ + name, + mime: undefined, + path, + buffer: new ArrayBuffer(0), + }); + return; + } + // Encoding null ensures data results in a Buffer. + fs.readFile(path, { encoding: null }, (err, data) => { + if (err) { + reject(err); + return; + } + resolve({ + name, + mime: mime.getType(name) || undefined, + path, + buffer: data, + }); }); }); }); diff --git a/ui/component/common/file-list.jsx b/ui/component/common/file-list.jsx index c664c4762..b4acb993c 100644 --- a/ui/component/common/file-list.jsx +++ b/ui/component/common/file-list.jsx @@ -31,11 +31,11 @@ function FileList(props: Props) { }; React.useEffect(() => { - if (radio.stops.length) { + if (radio.items.length) { if (!radio.currentId) { radio.first(); } else { - const first = radio.stops[0].ref.current; + const first = radio.items[0].ref.current; // First auto-selection if (first && first.id === radio.currentId && !radio.state) { const file = getFile(first.value); @@ -46,7 +46,7 @@ function FileList(props: Props) { if (radio.state) { // Find selected element - const stop = radio.stops.find((item) => item.id === radio.currentId); + const stop = radio.items.find((item) => item.id === radio.currentId); const element = stop && stop.ref.current; // Only update state if new item is selected if (element && element.value !== radio.state) { diff --git a/ui/component/common/file-selector.jsx b/ui/component/common/file-selector.jsx index 4c088ed9f..47d2e651e 100644 --- a/ui/component/common/file-selector.jsx +++ b/ui/component/common/file-selector.jsx @@ -77,7 +77,6 @@ class FileSelector extends React.PureComponent { if (path) { return ipcRenderer.invoke('get-file-from-path', path); } - return undefined; }) .then((result) => { if (!result) {