nb/hide-http
Alex Kern 11 years ago
parent c4b63ec0e3
commit d8febe77be

@ -1,38 +1,46 @@
import ChunkedBlob from './ChunkedBlob'; import ChunkedBlob from './ChunkedBlob'
export default class DownloadFile { export default class DownloadFile {
constructor(name, size, type) { constructor(name, size, type) {
this.name = name; this.name = name
this.size = size; this.size = size
this.type = type; this.type = type
this.packets = new ChunkedBlob(); this.packets = new ChunkedBlob()
} }
addPacket(b) { addPacket(b) {
this.packets.add(b); this.packets.add(b)
} }
clearPackets() { clearPackets() {
this.packets = new ChunkedBlob(); this.packets = new ChunkedBlob()
} }
isComplete() { isComplete() {
return this.packets.size === this.size; return this.getProgress() === 1
} }
getProgress() { getProgress() {
return this.packets.size / this.size; return this.packets.size / this.size
} }
download() { download() {
let blob = this.packets.toBlob(); let blob = this.packets.toBlob()
let url = URL.createObjectURL(blob); let url = URL.createObjectURL(blob)
let a = document.createElement('a'); let a = document.createElement('a')
a.download = this.name; a.download = this.name
a.href = url; a.href = url
a.click(); a.click()
URL.revokeObjectURL(url); URL.revokeObjectURL(url)
}
toJSON() {
return {
name: this.name,
size: this.size,
type: this.type
}
} }
} }

@ -1,25 +1,33 @@
const packetSize = 16 * 1024; const packetSize = 16 * 1024
export default class UploadFile { export default class UploadFile {
constructor(file) { constructor(file) {
this.name = file.name; this.name = file.name
this.size = file.size; this.size = file.size
this.type = file.type; this.type = file.type
this.blob = file; this.blob = file
} }
countPackets() { countPackets() {
return Math.ceil(this.size / packetSize); return Math.ceil(this.size / packetSize)
} }
getPacket(i) { getPacket(i) {
if (i < 0 || i >= this.countPackets()) if (i < 0 || i >= this.countPackets())
throw new Error('Packet out of bounds'); throw new Error('Packet out of bounds')
let start = i * packetSize; let start = i * packetSize
let end = Math.min(start + packetSize, this.size); let end = Math.min(start + packetSize, this.size)
return this.blob.slice(start, end); return this.blob.slice(start, end)
}
toJSON() {
return {
name: this.name,
size: this.size,
type: this.type
}
} }
} }

@ -1,2 +1,2 @@
var Alt = require('alt'); import Alt from 'alt'
export default new Alt(); export default new Alt()

@ -15,9 +15,12 @@ export default alt.createStore(class DownloadStore {
this.progress = 0; this.progress = 0;
this.on('bootstrap', () => { this.on('bootstrap', () => {
if (this.file && !(this.file instanceof DownloadFile)) if (this.file && !(this.file instanceof DownloadFile)) {
this.file = new DownloadFile(this.file.name, this.file.size, this.file.type); this.file = new DownloadFile(this.file.name,
}); this.file.size,
this.file.type);
}
})
} }
onRequestDownload() { onRequestDownload() {
@ -40,6 +43,7 @@ export default alt.createStore(class DownloadStore {
conn.on('data', (data) => { conn.on('data', (data) => {
if (this.status !== 'downloading') return; if (this.status !== 'downloading') return;
console.log(data.byteLength);
this.file.addPacket(data); this.file.addPacket(data);
i++; i++;

@ -53,6 +53,7 @@ export default alt.createStore(class UploadStore {
for (let j = 0; i < totalPackets && j < chunkSize; i++, j++) { for (let j = 0; i < totalPackets && j < chunkSize; i++, j++) {
let packet = this.file.getPacket(i); let packet = this.file.getPacket(i);
console.log(packet.size);
conn.send(packet); conn.send(packet);
} }

@ -50,7 +50,7 @@ routes.use(function (req, res) {
ReactRouter.run(clientRoutes, req.url, function (Handler) { ReactRouter.run(clientRoutes, req.url, function (Handler) {
var html = React.renderToString(<Handler data={alt.takeSnapshot()} />); var html = React.renderToString(<Handler data={alt.takeSnapshot()} />);
alt.recycle(); alt.flush();
res.write('<!DOCTYPE html>'); res.write('<!DOCTYPE html>');
res.end(html); res.end(html);
}); });

File diff suppressed because one or more lines are too long

@ -51,9 +51,9 @@
} }
.arrow.arrow-animated .arrow-border { .arrow.arrow-animated .arrow-border {
-webkit-animation: rotate 2s infinite linear; -webkit-animation: rotate 5s infinite linear;
-moz-animation: rotate 2s infinite linear; -moz-animation: rotate 5s infinite linear;
animation: rotate 2s infinite linear; animation: rotate 5s infinite linear;
} }
.arrow .arrow-image { .arrow .arrow-image {

Loading…
Cancel
Save