Add buttons to download specific files
parent
f64ff1b030
commit
8129cc0753
|
|
@ -2,12 +2,28 @@ import React, { Component, Fragment } from 'react';
|
||||||
import WebTorrent from 'webtorrent';
|
import WebTorrent from 'webtorrent';
|
||||||
import { saveAs } from 'file-saver/FileSaver';
|
import { saveAs } from 'file-saver/FileSaver';
|
||||||
|
|
||||||
|
const saveFile = (file) =>
|
||||||
|
{
|
||||||
|
file.getBlob((err, blob) =>
|
||||||
|
{
|
||||||
|
if (err)
|
||||||
|
{
|
||||||
|
console.error('WebTorrent error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('TRYING TO SAVE BLOB', blob)
|
||||||
|
saveAs(blob, file.name);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
class FileChatEntry extends Component
|
class FileChatEntry extends Component
|
||||||
{
|
{
|
||||||
state = {
|
state = {
|
||||||
active: false,
|
active: false,
|
||||||
numPeers: 0,
|
numPeers: 0,
|
||||||
progress: 0
|
progress: 0,
|
||||||
|
files: null
|
||||||
};
|
};
|
||||||
|
|
||||||
download = () =>
|
download = () =>
|
||||||
|
|
@ -31,17 +47,10 @@ class FileChatEntry extends Component
|
||||||
|
|
||||||
torrent.on('done', () => {
|
torrent.on('done', () => {
|
||||||
onProgress();
|
onProgress();
|
||||||
|
clearInterval(onProgress);
|
||||||
|
|
||||||
torrent.files.forEach((file) => {
|
this.setState({
|
||||||
file.getBlob((err, blob) => {
|
files: torrent.files
|
||||||
if (err)
|
|
||||||
{
|
|
||||||
console.error('webtorrent error!!!');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
saveAs(blob);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -62,6 +71,16 @@ class FileChatEntry extends Component
|
||||||
progress: {this.state.progress}
|
progress: {this.state.progress}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{this.state.files && (
|
||||||
|
<div>
|
||||||
|
{this.state.files.map((file, i) => (
|
||||||
|
<div key={i}>
|
||||||
|
<button onClick={() => saveFile(file)}>download {file.name}</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</Fragment>
|
</Fragment>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue