Skip to content

Commit 2215f8e

Browse files
committed
Fix+workaround for broken file path picker in desktop v1.24 & v1.23
1 parent 832bd20 commit 2215f8e

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/services/desktop-api.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ interface DesktopApi {
4747

4848
openContextMenu?: (options: NativeContextMenuDefinition) => Promise<string | undefined>;
4949
restartApp?: () => Promise<void>;
50+
51+
/**
52+
* Given a file object, returns its path. If the path isn't available (e.g. file
53+
* object constructed, not selected) then this returns null. If file isn't a
54+
* File at all, it throws.
55+
*/
56+
getPathForFile?: (file: File) => string | null;
5057
}
5158

5259
interface NativeContextMenuDefinition {

src/util/ui.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import * as React from 'react';
22
import { useHotkeys as rawUseHotkeys } from "react-hotkeys-hook";
33
import { action, observable } from 'mobx';
44

5-
import { desktopVersion } from '../services/service-versions';
65
import { getDeferred, delay } from './promise';
76
import { logError } from '../errors';
87

8+
import { desktopVersion } from '../services/service-versions';
9+
import { DesktopApi } from '../services/desktop-api';
10+
911
export function isReactElement(node: any): node is React.ReactElement {
1012
return node && !!node.$$typeof;
1113
}
@@ -128,9 +130,22 @@ export function uploadFile(
128130
const file = fileInput.files[0];
129131

130132
if (type === 'path') {
131-
// file.path is an Electron-only extra property:
132-
// https://github.com/electron/electron/blob/master/docs/api/file-object.md
133-
result.resolve((file as unknown as { path: string }).path);
133+
const fileWithPath = (file as unknown as { path?: string });
134+
if (DesktopApi.getPathForFile) {
135+
// In the latest desktop app, we can request the file path via the desktop API:
136+
return result.resolve(DesktopApi.getPathForFile(file));
137+
} else if (fileWithPath.path) {
138+
// On older Electron, we can access the path on the File object directly:
139+
return result.resolve(fileWithPath.path);
140+
} else {
141+
// In the middle ground, we released a desktop version that includes an Electron update
142+
// with disables file.path, with no fallback. Anybody using this needs to update.
143+
window.alert(
144+
"File path access is not available due to a temporary issue in this release.\n\n" +
145+
"Please update to the latest HTTP Toolkit from httptoolkit.com"
146+
);
147+
return result.resolve(null);
148+
}
134149
} else {
135150
const fileReader = new FileReader();
136151

0 commit comments

Comments
 (0)