Skip to content

Commit cceb494

Browse files
committed
Issue 53148: Uncaught TypeError from webkitGetAsEntry()
1 parent ed6e69c commit cceb494

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

packages/components/src/internal/components/files/FileAttachmentContainer.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ import { ALL_FILES_LIMIT_KEY } from './models';
3131

3232
const isDirectoryEntry = (entry: FileSystemEntry): entry is FileSystemDirectoryEntry => entry.isDirectory;
3333
const isFileEntry = (entry: FileSystemEntry): entry is FileSystemFileEntry => entry.isFile;
34+
export const getTransferItemDirectoryEntry = (
35+
transferItems: DataTransferItemList,
36+
index: number
37+
): FileSystemDirectoryEntry | undefined => {
38+
const entry = transferItems?.[index]?.webkitGetAsEntry();
39+
if (entry && isDirectoryEntry(entry)) return entry;
40+
return undefined;
41+
};
3442

3543
interface Props {
3644
acceptedFormats?: string; // comma separated list of allowed extensions i.e. '.png, .jpg, .jpeg'
@@ -173,7 +181,7 @@ export class FileAttachmentContainer extends React.PureComponent<Props, State> {
173181
const invalidNames = new Set<string>();
174182

175183
Array.from(fileList).forEach((file, index) => {
176-
if (transferItems && transferItems[index].webkitGetAsEntry().isDirectory) {
184+
if (getTransferItemDirectoryEntry(transferItems, index)) {
177185
if (!allowDirectories) {
178186
invalidDirectories.push(file.name);
179187
invalidNames.add(file.name);
@@ -312,7 +320,7 @@ export class FileAttachmentContainer extends React.PureComponent<Props, State> {
312320
newFiles[file.name] = file;
313321
haveValidFiles = true;
314322

315-
if (transferItems && transferItems[index].webkitGetAsEntry().isDirectory) {
323+
if (getTransferItemDirectoryEntry(transferItems, index)) {
316324
hasDirectory = true;
317325
}
318326
}
@@ -325,8 +333,8 @@ export class FileAttachmentContainer extends React.PureComponent<Props, State> {
325333
this.dirCbCount = 0;
326334
this.fileCbCount = 0;
327335
Array.from(fileList).forEach((file, index) => {
328-
const entry = transferItems[index].webkitGetAsEntry();
329-
if (isDirectoryEntry(entry)) {
336+
const entry = getTransferItemDirectoryEntry(transferItems, index);
337+
if (entry) {
330338
delete files[file.name];
331339
this.getFilesFromDirectory(files, entry, this, () => {
332340
this._handleFiles(files);

packages/components/src/internal/components/forms/input/FileInput.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { FILELINK_RANGE_URI } from '../../domainproperties/constants';
2929

3030
import { fileMatchesAcceptedFormat } from '../../files/actions';
3131

32+
import { getTransferItemDirectoryEntry } from '../../files/FileAttachmentContainer';
33+
3234
import { DisableableInput, DisableableInputProps, DisableableInputState } from './DisableableInput';
3335

3436
export interface FileInputProps extends DisableableInputProps {
@@ -42,11 +44,11 @@ export interface FileInputProps extends DisableableInputProps {
4244
labelClassName?: string;
4345
maxFileSize?: number;
4446
name?: string;
47+
onChange?: (fileMap: Record<string, File>) => void;
4548
queryColumn?: QueryColumn;
4649
renderFieldLabel?: (queryColumn: QueryColumn, label?: string, description?: string) => ReactNode;
4750
showLabel?: boolean;
4851
toggleDisabledTooltip?: string;
49-
onChange?: (fileMap: Record<string, File>) => void;
5052
}
5153

5254
type FileInputImplProps = FileInputProps & FormsyInjectedProps<any>;
@@ -107,7 +109,7 @@ class FileInputImpl extends DisableableInput<FileInputImplProps, State> {
107109
return;
108110
}
109111

110-
if (transferItems && transferItems[0].webkitGetAsEntry().isDirectory) {
112+
if (getTransferItemDirectoryEntry(transferItems, 0)) {
111113
this.setState({ error: 'Folders are not supported, only one file allowed' });
112114
return;
113115
}
@@ -122,7 +124,9 @@ class FileInputImpl extends DisableableInput<FileInputImplProps, State> {
122124
}
123125

124126
if (maxFileSize && file.size > maxFileSize) {
125-
this.setState({ error: `File size must not exceed ${Math.round(maxFileSize / 1024).toLocaleString()} KB.` });
127+
this.setState({
128+
error: `File size must not exceed ${Math.round(maxFileSize / 1024).toLocaleString()} KB.`,
129+
});
126130
return;
127131
}
128132
if (emptyFileNotAllowed && file.size === 0) {

0 commit comments

Comments
 (0)