-
-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
- Loading branch information
1 parent
85c214e
commit 58a113d
Showing
7 changed files
with
169 additions
and
22 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
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,96 @@ | ||
import consola from 'consola' | ||
|
||
type EntryWithPath = { | ||
entry: FileSystemEntry, | ||
path: string | ||
} | ||
export type FileWithPath = { | ||
file: File, | ||
path: string | ||
} | ||
|
||
const isFile = (item: FileSystemEntry): item is FileSystemFileEntry => item.isFile | ||
|
||
const isDirectory = (item: FileSystemEntry): item is FileSystemDirectoryEntry => item.isDirectory | ||
|
||
const getFileAsync = async (fileEntry: FileSystemFileEntry) => { | ||
try { | ||
return new Promise<File>((resolve, reject) => fileEntry.file(resolve, reject)) | ||
} catch (e) { | ||
consola.error('[FileSystemFileEntry] file', e) | ||
} | ||
} | ||
|
||
const readEntriesAsync = async (directoryReader: FileSystemDirectoryReader) => { | ||
try { | ||
return new Promise<FileSystemEntry[]>((resolve, reject) => directoryReader.readEntries(resolve, reject)) | ||
} catch (e) { | ||
consola.error('[FileSystemDirectoryReader] readEntries', e) | ||
} | ||
} | ||
|
||
export const getFilesFromDataTransfer = async (dataTransfer: DataTransfer) => { | ||
if (dataTransfer.items.length) { | ||
const entries = [...dataTransfer.items] | ||
.map(x => x.webkitGetAsEntry()) | ||
.filter((x): x is FileSystemEntry => !!x) | ||
|
||
return await getFilesFromFileSystemEntries(entries) | ||
} else if (dataTransfer.files.length) { | ||
return convertFilesToFilesWithPath(dataTransfer.files) | ||
} | ||
} | ||
|
||
export const getFilesWithPathFromHTMLInputElement = async (input: HTMLInputElement) => { | ||
if (input.webkitEntries.length) { | ||
return await getFilesFromFileSystemEntries(input.webkitEntries) | ||
} else if (input.files?.length) { | ||
return convertFilesToFilesWithPath(input.files) | ||
} | ||
} | ||
|
||
export const convertFilesToFilesWithPath = (files: File[] | FileList) => { | ||
return [...files] | ||
.map(file => ({ | ||
file, | ||
path: file.webkitRelativePath.slice(0, -file.name.length) | ||
} as FileWithPath)) | ||
} | ||
|
||
export const getFilesFromFileSystemEntries = async (entries: readonly FileSystemEntry[]) => { | ||
const files: FileWithPath[] = [] | ||
const items = entries | ||
.map(entry => ({ | ||
entry, | ||
path: '' | ||
} as EntryWithPath)) | ||
|
||
let item = items.pop() | ||
while (item) { | ||
if (isFile(item.entry)) { | ||
const file = await getFileAsync(item.entry) | ||
|
||
if (file) { | ||
files.push({ | ||
file, | ||
path: item.path | ||
}) | ||
} | ||
} else if (isDirectory(item.entry)) { | ||
const subEntries = await readEntriesAsync(item.entry.createReader()) | ||
|
||
if (subEntries) { | ||
for (const entry of subEntries) { | ||
items.push({ | ||
entry, | ||
path: item.path + item.entry.name + '/' | ||
}) | ||
} | ||
} | ||
} | ||
|
||
item = items.pop() | ||
} | ||
|
||
return files | ||
} |