Skip to content

Commit 46c9209

Browse files
wali-srolandjitsu
authored andcommitted
fix: use File {path} prop if it exists
This fixes issues with Electron, which sets the `{path}` property on the File object.
1 parent 4c68e8a commit 46c9209

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/file-selector.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function isDragEvt(value: any): value is DragEvent {
2727
function getInputFiles(evt: Event) {
2828
const files = isInput(evt.target)
2929
? evt.target.files
30-
? fromList<File>(evt.target.files)
30+
? fromList<FileWithPath>(evt.target.files)
3131
: []
3232
: [];
3333
return files.map(file => toFileWithPath(file));
@@ -52,7 +52,7 @@ async function getDataTransferFiles(dt: DataTransfer, type: string) {
5252
return noIgnoredFiles(flatten<FileWithPath>(files));
5353
}
5454

55-
return noIgnoredFiles(fromList<File>(dt.files)
55+
return noIgnoredFiles(fromList<FileWithPath>(dt.files)
5656
.map(file => toFileWithPath(file)));
5757
}
5858

@@ -153,7 +153,7 @@ function fromDirEntry(entry: any) {
153153
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry
154154
async function fromFileEntry(entry: any) {
155155
return new Promise<FileWithPath>((resolve, reject) => {
156-
entry.file((file: File) => {
156+
entry.file((file: FileWithPath) => {
157157
const fwp = toFileWithPath(file, entry.fullPath);
158158
resolve(fwp);
159159
}, (err: any) => {

src/file.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ describe('toFile()', () => {
1616
expect(fileWithPath.type).toBe(type);
1717
});
1818

19+
it('does not overwrite {path} if it exists', () => {
20+
const fullPath = '/Users/test/Desktop/test/test.json';
21+
const path = '/test/test.json';
22+
const file = new File([], 'test.json');
23+
// @ts-ignore
24+
file.path = fullPath; // this is set only in the case of an electron app
25+
const fileWithPath = toFileWithPath(file, path);
26+
expect(fileWithPath.path).toBe(fullPath);
27+
});
28+
1929
it('sets the {path} if provided', () => {
2030
const path = '/test/test.json';
2131
const file = new File([], 'test.json');

src/file.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,25 @@ export const COMMON_MIME_TYPES = new Map([
1515
]);
1616

1717

18-
export function toFileWithPath(file: File, path?: string): FileWithPath {
18+
export function toFileWithPath(file: FileWithPath, path?: string): FileWithPath {
1919
const f = withMimeType(file);
20-
const {webkitRelativePath} = file as FileWithWebkitPath;
21-
Object.defineProperty(f, 'path', {
22-
value: typeof path === 'string'
23-
? path
24-
// If <input webkitdirectory> is set,
25-
// the File will have a {webkitRelativePath} property
26-
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
27-
: typeof webkitRelativePath === 'string' && webkitRelativePath.length > 0
28-
? webkitRelativePath
29-
: file.name,
30-
writable: false,
31-
configurable: false,
32-
enumerable: true
33-
});
20+
if (typeof f.path !== 'string') { // on electron, path is already set to the absolute path
21+
const {webkitRelativePath} = file as FileWithWebkitPath;
22+
Object.defineProperty(f, 'path', {
23+
value: typeof path === 'string'
24+
? path
25+
// If <input webkitdirectory> is set,
26+
// the File will have a {webkitRelativePath} property
27+
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/webkitdirectory
28+
: typeof webkitRelativePath === 'string' && webkitRelativePath.length > 0
29+
? webkitRelativePath
30+
: file.name,
31+
writable: false,
32+
configurable: false,
33+
enumerable: true
34+
});
35+
}
36+
3437
return f;
3538
}
3639

@@ -42,7 +45,7 @@ interface FileWithWebkitPath extends File {
4245
readonly webkitRelativePath?: string;
4346
}
4447

45-
function withMimeType(file: File) {
48+
function withMimeType(file: FileWithPath) {
4649
const {name} = file;
4750
const hasExtension = name && name.lastIndexOf('.') !== -1;
4851

0 commit comments

Comments
 (0)