Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upload drag directory can not work #562

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/AjaxUploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AjaxUploader extends Component<UploadProps> {
}
};

onFileDrop = (e: React.DragEvent<HTMLDivElement>) => {
onFileDrop = async (e: React.DragEvent<HTMLDivElement>) => {
const { multiple } = this.props;

e.preventDefault();
Expand All @@ -76,11 +76,11 @@ class AjaxUploader extends Component<UploadProps> {
}

if (this.props.directory) {
traverseFileTree(
const files = await traverseFileTree(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个 test 吧~~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

尝试加了一个-。-

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

啥时候能发版啊。。。。。。

Array.prototype.slice.call(e.dataTransfer.items),
this.uploadFiles,
(_file: RcFile) => attrAccept(_file, this.props.accept),
);
this.uploadFiles(files);
} else {
let files = [...e.dataTransfer.files].filter((file: RcFile) =>
attrAccept(file, this.props.accept),
Expand Down
78 changes: 46 additions & 32 deletions src/traverseFileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,34 @@ interface InternalDataTransferItem extends DataTransferItem {
path: string;
}

const traverseFileTree = (files: InternalDataTransferItem[], callback, isAccepted) => {
// https://github.com/ant-design/ant-design/issues/50080
const traverseFileTree = async (files: InternalDataTransferItem[], isAccepted) => {
const flattenFileList = [];
const progressFileList = [];
files.forEach(file => progressFileList.push(file.webkitGetAsEntry() as any));
function loopFiles(item: InternalDataTransferItem) {
const dirReader = item.createReader();

function sequence() {
dirReader.readEntries((entries: InternalDataTransferItem[]) => {
const entryList = Array.prototype.slice.apply(entries);
async function readDirectory(directory: InternalDataTransferItem) {
const dirReader = directory.createReader();
const entries = [];

progressFileList.push(...entryList);
// Check if all the file has been viewed
const isFinished = !entryList.length;
if (!isFinished) {
sequence();
}
while (true) {
const results = await new Promise<InternalDataTransferItem[]>((resolve, reject) => {
dirReader.readEntries(resolve, reject);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

按照原本的逻辑,失败了就直接无视了。这里加了 reject 后,没有做错误处理,await 后会直接挂掉。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zombieJ 更新了下这块的处理,看看有没有问题

});
}

sequence();
}
// eslint-disable-next-line @typescript-eslint/naming-convention
const _traverseFileTree = (item: InternalDataTransferItem, path?: string) => {
if (!item) {
return;
if (!results.length) {
break;
}

for (const entry of results) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个换一下 for (let i ...),IE 11 不支持 of loop

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zombieJ 已更新

entries.push(entry);
}
}
// eslint-disable-next-line no-param-reassign
item.path = path || '';
if (item.isFile) {
return entries;
}

async function readFile(item: InternalDataTransferItem) {
return new Promise<RcFile & { webkitRelativePath?: string }>(reslove => {
item.file(file => {
if (isAccepted(file)) {
// https://github.com/ant-design/ant-design/issues/16426
Expand All @@ -57,23 +55,39 @@ const traverseFileTree = (files: InternalDataTransferItem[], callback, isAccepte
},
});
}
flattenFileList.push(file);
reslove(file);
} else {
reslove(null);
}
});
});
}

// eslint-disable-next-line @typescript-eslint/naming-convention
const _traverseFileTree = async (item: InternalDataTransferItem, path?: string) => {
if (!item) {
return;
}
// eslint-disable-next-line no-param-reassign
item.path = path || '';
if (item.isFile) {
const file = await readFile(item);
if (file) {
flattenFileList.push(file);
}
} else if (item.isDirectory) {
loopFiles(item);
const entries = await readDirectory(item);
progressFileList.push(...entries);
}
};

function walkFiles() {
let wipIndex = 0;
while (wipIndex < progressFileList.length) {
_traverseFileTree(progressFileList[wipIndex]);
wipIndex++;
}
callback(flattenFileList);
let wipIndex = 0;
while (wipIndex < progressFileList.length) {
await _traverseFileTree(progressFileList[wipIndex]);
wipIndex++;
}
walkFiles();

return flattenFileList;
};

export default traverseFileTree;
73 changes: 73 additions & 0 deletions tests/uploader.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,45 @@ const makeFileSystemEntry = item => {
return ret;
};

const makeFileSystemEntryAsync = item => {
const isDirectory = Array.isArray(item.children);
const ret = {
isDirectory,
isFile: !isDirectory,
file: handle => {
handle(new Item(item.name));
},
createReader: () => {
let first = true;
return {
async readEntries(handle) {
await sleep(100);

if (!first) {
return handle([]);
}

first = false;
return handle(item.children.map(makeFileSystemEntry));
},
};
},
};
return ret;
};

const makeDataTransferItem = item => {
return {
webkitGetAsEntry: () => makeFileSystemEntry(item),
};
};

const makeDataTransferItemAsync = item => {
return {
webkitGetAsEntry: () => makeFileSystemEntryAsync(item),
};
};

describe('uploader', () => {
let requests;
let xhr;
Expand Down Expand Up @@ -462,6 +495,46 @@ describe('uploader', () => {
}, 100);
});

it('dragging and dropping files to upload through asynchronous file reading is run normal', done => {
const input = uploader.container.querySelector('input')!;

const files = {
name: 'foo',
children: [
{
name: 'bar',
children: [
{
name: '1.png',
},
{
name: '2.png',
},
{
name: 'rc',
children: [
{
name: '5.webp',
},
{
name: '4.webp',
},
],
},
],
},
],
};
fireEvent.drop(input, { dataTransfer: { items: [makeDataTransferItemAsync(files)] } });
const mockStart = jest.fn();
handlers.onStart = mockStart;

setTimeout(() => {
expect(mockStart.mock.calls.length).toBe(2);
done();
}, 500);
});

it('unaccepted type files to upload will not trigger onStart when select directory', done => {
const input = uploader.container.querySelector('input')!;
const files = [
Expand Down
Loading