fix file upload for browser

This commit is contained in:
Barry Gitarts 2019-02-01 05:50:56 -05:00
parent 9a3eff3e7c
commit a8315f5501
4 changed files with 14 additions and 5 deletions

View File

@ -18,6 +18,7 @@
"lodash": "^4.17.11", "lodash": "^4.17.11",
"memoize-one": "^4.0.3", "memoize-one": "^4.0.3",
"murmur-client": "^0.2.5", "murmur-client": "^0.2.5",
"pull-file-reader": "^1.0.2",
"react": "^16.7.0", "react": "^16.7.0",
"react-dom": "^16.7.0", "react-dom": "^16.7.0",
"react-dropzone": "^7.0.1", "react-dropzone": "^7.0.1",

View File

@ -9,7 +9,9 @@ import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button'; import Button from '@material-ui/core/Button';
import Dropzone from 'react-dropzone'; import Dropzone from 'react-dropzone';
import { Picker } from 'emoji-mart'; import { Picker } from 'emoji-mart';
import AddCircle from '@material-ui/icons/AddCircle' import AddCircle from '@material-ui/icons/AddCircle';
import 'emoji-mart/css/emoji-mart.css';
import ChatBox from './ChatBox'; import ChatBox from './ChatBox';
import ChatHeader from './ChatHeader'; import ChatHeader from './ChatHeader';
@ -92,7 +94,6 @@ class ChatRoom extends Component {
fileChangedHandler(event) { fileChangedHandler(event) {
const { ipfs, sendMessage } = this.props; const { ipfs, sendMessage } = this.props;
const file = event.target.files[0]; const file = event.target.files[0];
console.dir("handling file upload");
uploadFileAndSend(ipfs, file, sendMessage); uploadFileAndSend(ipfs, file, sendMessage);
} }

View File

@ -1,7 +1,7 @@
import fs from 'fs'; import fileReaderPullStream from 'pull-file-reader'
export const fileUpload = (node, filePath) => { export const fileUpload = (node, filePath) => {
const file = fs.readFileSync(filePath); const file = fileReaderPullStream(filePath)
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
node.files.add(file, (err, files) => { node.files.add(file, (err, files) => {
if (err) return reject(err) if (err) return reject(err)
@ -12,7 +12,7 @@ export const fileUpload = (node, filePath) => {
export const uploadFileAndSend = async (node, file, sendFn) => { export const uploadFileAndSend = async (node, file, sendFn) => {
const { name, path, type } = file; const { name, path, type } = file;
const files = await fileUpload(node, path); const files = await fileUpload(node, file);
const { hash } = files[0]; const { hash } = files[0];
const text = `/ipfs/${hash}`; const text = `/ipfs/${hash}`;
sendFn(text); sendFn(text);

View File

@ -13067,6 +13067,13 @@ pull-defer@^0.2.3, pull-defer@~0.2.2, pull-defer@~0.2.3:
resolved "https://registry.yarnpkg.com/pull-defer/-/pull-defer-0.2.3.tgz#4ee09c6d9e227bede9938db80391c3dac489d113" resolved "https://registry.yarnpkg.com/pull-defer/-/pull-defer-0.2.3.tgz#4ee09c6d9e227bede9938db80391c3dac489d113"
integrity sha512-/An3KE7mVjZCqNhZsr22k1Tx8MACnUnHZZNPSJ0S62td8JtYr/AiRG42Vz7Syu31SoTLUzVIe61jtT/pNdjVYA== integrity sha512-/An3KE7mVjZCqNhZsr22k1Tx8MACnUnHZZNPSJ0S62td8JtYr/AiRG42Vz7Syu31SoTLUzVIe61jtT/pNdjVYA==
pull-file-reader@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pull-file-reader/-/pull-file-reader-1.0.2.tgz#d9b0d3d2de56f1a1ebb2ce8d313e79567d92dc48"
integrity sha1-2bDT0t5W8aHrss6NMT55Vn2S3Eg=
dependencies:
typedarray-to-buffer "^3.1.2"
pull-file@^1.1.0: pull-file@^1.1.0:
version "1.1.0" version "1.1.0"
resolved "https://registry.yarnpkg.com/pull-file/-/pull-file-1.1.0.tgz#1dd987605d6357a0d23c1e4b826f7915a215129c" resolved "https://registry.yarnpkg.com/pull-file/-/pull-file-1.1.0.tgz#1dd987605d6357a0d23c1e4b826f7915a215129c"