Update get-file-from-path to work with folders; fix file-list component.

This commit is contained in:
Franco Montenegro 2022-08-17 12:33:59 -03:00
parent fa5c1a1cf3
commit 25a71be143
3 changed files with 30 additions and 14 deletions

View file

@ -305,23 +305,40 @@ app.on('before-quit', () => {
// Example: // Example:
// const result = await ipcMain.invoke('get-file-from-path', 'path/to/file'); // const result = await ipcMain.invoke('get-file-from-path', 'path/to/file');
// const file = new File([result.buffer], result.name); // 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) => { ipcMain.handle('get-file-from-path', (event, path) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Encoding null ensures data results in a Buffer. fs.stat(path, (error, stats) => {
fs.readFile(path, { encoding: null }, (err, data) => { if (error) {
if (err) { reject(error);
reject(err);
return; return;
} }
// Separate folders considering "\" and "/" // Separate folders considering "\" and "/"
// as separators (cross platform) // as separators (cross platform)
const folders = path.split(/[\\/]/); const folders = path.split(/[\\/]/);
const fileName = folders[folders.length - 1]; const name = folders[folders.length - 1];
resolve({ if (stats.isDirectory()) {
name: fileName, resolve({
mime: mime.getType(fileName) || undefined, name,
path: path, mime: undefined,
buffer: data, 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,
});
}); });
}); });
}); });

View file

@ -31,11 +31,11 @@ function FileList(props: Props) {
}; };
React.useEffect(() => { React.useEffect(() => {
if (radio.stops.length) { if (radio.items.length) {
if (!radio.currentId) { if (!radio.currentId) {
radio.first(); radio.first();
} else { } else {
const first = radio.stops[0].ref.current; const first = radio.items[0].ref.current;
// First auto-selection // First auto-selection
if (first && first.id === radio.currentId && !radio.state) { if (first && first.id === radio.currentId && !radio.state) {
const file = getFile(first.value); const file = getFile(first.value);
@ -46,7 +46,7 @@ function FileList(props: Props) {
if (radio.state) { if (radio.state) {
// Find selected element // 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; const element = stop && stop.ref.current;
// Only update state if new item is selected // Only update state if new item is selected
if (element && element.value !== radio.state) { if (element && element.value !== radio.state) {

View file

@ -77,7 +77,6 @@ class FileSelector extends React.PureComponent<Props> {
if (path) { if (path) {
return ipcRenderer.invoke('get-file-from-path', path); return ipcRenderer.invoke('get-file-from-path', path);
} }
return undefined;
}) })
.then((result) => { .then((result) => {
if (!result) { if (!result) {