-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(explorer): Add support for uploading files
- Loading branch information
1 parent
66f0266
commit e8bd7c1
Showing
7 changed files
with
66 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
flex-direction: column; | ||
align-items: center; | ||
width: 25%; | ||
height: 200px; | ||
} | ||
|
||
.iconWrapper { | ||
|
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
flex-direction: column; | ||
align-items: center; | ||
width: 25%; | ||
height: 200px; | ||
} | ||
|
||
.icon { | ||
|
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,33 @@ | ||
import * as React from 'react'; | ||
import { useDropzone } from 'react-dropzone'; | ||
import WasmFs from "@wasmer/wasmfs"; | ||
import InstanceBag from '../../../InstanceBag'; | ||
import { readFileAsArrayBuffer } from '../../../services/FileReaderService'; | ||
|
||
interface Props { | ||
className?: string; | ||
currentPath: string; | ||
children: React.ReactNode; | ||
} | ||
|
||
export default function Dropzone(props: Props) { | ||
const onDrop = React.useCallback(async (acceptedFiles: File[]) => { | ||
const wasmFs = InstanceBag.get<WasmFs>('fs'); | ||
const promises = acceptedFiles.map(file => readFileAsArrayBuffer(file)); | ||
const processedFiles = await Promise.all(promises); | ||
|
||
processedFiles.forEach((file) => { | ||
const path = `${props.currentPath}/${file.name}`; | ||
wasmFs.fs.writeFileSync(path, new Uint8Array(file.bin)); | ||
}); | ||
}, [props.currentPath]); | ||
|
||
const { getInputProps, getRootProps, isDragActive } = useDropzone({ onDrop, noClick: true }); | ||
|
||
return ( | ||
<div className={props.className} {...getRootProps()}> | ||
<input {...getInputProps()} /> | ||
{props.children} | ||
</div> | ||
); | ||
} |
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 @@ | ||
export { default } from './Dropzone'; |
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,25 @@ | ||
interface ProcessedFile { | ||
name: string; | ||
size: number; | ||
bin: ArrayBuffer; | ||
} | ||
|
||
export function readFileAsArrayBuffer(file: File): Promise<ProcessedFile> { | ||
return new Promise((resolve, reject) => { | ||
const fileReader = new FileReader(); | ||
|
||
fileReader.onerror = (error) => { | ||
reject(error); | ||
} | ||
|
||
fileReader.onload = () => { | ||
resolve({ | ||
name: file.name, | ||
size: file.size, | ||
bin: <ArrayBuffer>fileReader.result, | ||
}); | ||
} | ||
|
||
fileReader.readAsArrayBuffer(file); | ||
}); | ||
} |