From b3298a29940573580b23f4b86256c678a8b904b3 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 24 May 2018 14:53:09 +0100 Subject: [PATCH] Upload files working --- src/files/FilesPage.js | 9 ++++- src/files/file-input/FileInput.js | 62 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 src/files/file-input/FileInput.js diff --git a/src/files/FilesPage.js b/src/files/FilesPage.js index c3c14976c..f6e51fb6d 100644 --- a/src/files/FilesPage.js +++ b/src/files/FilesPage.js @@ -1,10 +1,10 @@ import React from 'react' import PropTypes from 'prop-types' import { connect } from 'redux-bundler-react' -import Button from '../components/button/Button' import Breadcrumbs from './breadcrumbs/Breadcrumbs' import FilesList from './files-list/FilesList' import FilePreview from './file-preview/FilePreview' +import FileInput from './file-input/FileInput' const action = (name) => { return (...args) => { @@ -42,6 +42,10 @@ class FilesPage extends React.Component { } } + onFilesUpload = (files) => { + this.props.doFilesWrite(this.props.files.path, files) + } + render () { const {files} = this.props @@ -76,7 +80,7 @@ class FilesPage extends React.Component {
- +
{body}

Files

@@ -89,6 +93,7 @@ export default connect( 'doUpdateHash', 'doFilesDelete', 'doFilesRename', + 'doFilesWrite', 'selectFiles', 'selectGatewayUrl', FilesPage diff --git a/src/files/file-input/FileInput.js b/src/files/file-input/FileInput.js new file mode 100644 index 000000000..6bc1fbc30 --- /dev/null +++ b/src/files/file-input/FileInput.js @@ -0,0 +1,62 @@ +import React from 'react' +import PropTypes from 'prop-types' +import Button from '../../components/button/Button' + +export function readAsBuffer (file) { + return new Promise((resolve, reject) => { + const reader = new window.FileReader() + reader.onload = (event) => { + resolve({ + content: Buffer.from(reader.result), + name: file.name + }) + } + reader.onerror = (event) => { + reject(reader.error) + } + + reader.readAsArrayBuffer(file) + }) +} + +export default class FileInput extends React.Component { + static propTypes = { + upload: PropTypes.func.isRequired + } + + onChange = () => { + const raw = this.input.files + const { upload } = this.props + let promises = [] + + for (const file of raw) { + promises.push(readAsBuffer(file)) + console.log(file) + } + + Promise.all(promises) + .then(files => { + upload(files) + }) + + this.input.value = null + } + + onClick = () => { + this.input.click() + } + + render () { + return ( +
+ + { this.input = input }} + onChange={this.onChange} /> +
+ ) + } +}