Skip to content

Commit

Permalink
feat(explorer): Add support for uploading files
Browse files Browse the repository at this point in the history
  • Loading branch information
FranklinWaller committed Nov 4, 2019
1 parent 66f0266 commit e8bd7c1
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/js/apps/Explorer/Explorer.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
display: flex;
// justify-content: space-evenly;
flex-flow: wrap;
height: 100%;
outline: none;
}

.toolbarButton {
Expand Down
6 changes: 3 additions & 3 deletions src/js/apps/Explorer/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import WasmFs from "@wasmer/wasmfs";
import Folder from './components/Folder';
import File from './components/File';
import Dirent from 'memfs/lib/Dirent';
import { useDropzone } from 'react-dropzone'
import Dropzone from '../../components/molecules/Dropzone';
const styles = require('./Explorer.scss');

export default function Explorer() {
Expand Down Expand Up @@ -93,15 +93,15 @@ export default function Explorer() {
</Collapse>
</List>
}>
<div className={styles.files}>
<Dropzone currentPath={path} className={styles.files}>
{files.map((file) => {
if (file.isDirectory()) {
return <Folder key={file.name.toString()} folder={file} onClick={handleFileClick} />
} else if (file.isFile()) {
return <File key={file.name.toString()} file={file} onClick={handleFileClick} />
}
})}
</div>
</Dropzone>
</AppHeader>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/js/apps/Explorer/components/File/File.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
flex-direction: column;
align-items: center;
width: 25%;
height: 200px;
}

.iconWrapper {
Expand Down
1 change: 1 addition & 0 deletions src/js/apps/Explorer/components/Folder/Folder.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
flex-direction: column;
align-items: center;
width: 25%;
height: 200px;
}

.icon {
Expand Down
33 changes: 33 additions & 0 deletions src/js/components/molecules/Dropzone/Dropzone.tsx
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>
);
}
1 change: 1 addition & 0 deletions src/js/components/molecules/Dropzone/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Dropzone';
25 changes: 25 additions & 0 deletions src/js/services/FileReaderService.ts
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);
});
}

0 comments on commit e8bd7c1

Please sign in to comment.