-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add basic folder upload * wire up folder uploads for modal * Rename Modal * fix types * remove types * lingui extract * fix lint * add source attribution * lingui extract * incorporate feedback * lingui extract * fix lint * Fix casing * Rename component for consistency * lingui extract * clean up types Co-authored-by: GitHub Actions <actions@github.com> Co-authored-by: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> Co-authored-by: Ryan Noble <ryanjnoble@gmail.com>
- Loading branch information
1 parent
669e3a2
commit 0e454aa
Showing
10 changed files
with
134 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
packages/files-ui/src/Utils/getFilesFromDataTransferItems.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//Shamelessly borrowed from https://github.com/anatol-grabowski/datatransfer-files-promise with added Types | ||
|
||
type FileWithPath = File & {filepath: string} | ||
|
||
const getFilesFromDataTransferItems = async (dataTransferItems: DataTransferItemList): Promise<Array<FileWithPath>> => { | ||
const readFile = (entry: FileEntry, path = ""): Promise<FileWithPath> => { | ||
return new Promise((resolve, reject) => { | ||
entry.file((file: File) => { | ||
Object.defineProperty(file, "filepath", { | ||
value: path | ||
}) | ||
resolve(file as FileWithPath) | ||
}, (err: Error) => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
|
||
const dirReadEntries = (dirReader: DirectoryReader, path: string): Promise<FileWithPath[]> => { | ||
return new Promise((resolve, reject) => { | ||
dirReader.readEntries(async (entries: FileSystemEntry[]) => { | ||
let files = [] as Array<FileWithPath> | ||
for (const entry of entries) { | ||
const itemFiles = await getFilesFromEntry(entry, path) as Array<FileWithPath> | ||
files = files.concat(itemFiles) | ||
} | ||
resolve(files) | ||
}, (err: Error) => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
|
||
const readDir = async (entry: DirectoryEntry, path: string) => { | ||
const dirReader = entry.createReader() | ||
const newPath = path + entry.name + "/" | ||
let files = [] as Array<FileWithPath> | ||
let newFiles | ||
do { | ||
newFiles = await dirReadEntries(dirReader, newPath) | ||
files = files.concat(newFiles) | ||
} while (newFiles.length > 0) | ||
return files | ||
} | ||
|
||
const getFilesFromEntry = async (entry: FileSystemEntry, path = "") => { | ||
if (entry.isFile) { | ||
const file = await readFile(entry as FileEntry, path) | ||
return [file] | ||
} | ||
if (entry.isDirectory) { | ||
const files = await readDir(entry as DirectoryEntry, path) | ||
return files | ||
} | ||
} | ||
|
||
let files = [] as Array<FileWithPath> | ||
const entries = [] | ||
|
||
// Pull out all entries before reading them | ||
for (let i = 0, ii = dataTransferItems.length; i < ii; i++) { | ||
entries.push(dataTransferItems[i].webkitGetAsEntry()) | ||
} | ||
|
||
// Recursively read through all entries | ||
for (const entry of entries) { | ||
if (entry) { | ||
const newFiles = await getFilesFromEntry(entry) | ||
if (newFiles) { | ||
files = files.concat(newFiles) | ||
} | ||
} | ||
} | ||
|
||
return files | ||
} | ||
|
||
export default getFilesFromDataTransferItems |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters