-
-
Notifications
You must be signed in to change notification settings - Fork 320
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 按照原本的逻辑,失败了就直接无视了。这里加了 reject 后,没有做错误处理,await 后会直接挂掉。 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个换一下 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个 test 吧~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
尝试加了一个-。-
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
啥时候能发版啊。。。。。。