Add error status and check if too many bytes are received

This commit is contained in:
schlagmichdoch 2024-02-15 01:33:06 +01:00
parent a98499ea5a
commit 42bd71a3dc
5 changed files with 37 additions and 21 deletions

View file

@ -590,7 +590,7 @@ class Peer {
}
_abortTransfer() {
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: null});
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: 'error'});
this._reset();
}
@ -847,23 +847,24 @@ class Peer {
}
_onChunkReceived(chunk) {
if(this._state !== Peer.STATE_RECEIVE_PROCEEDING || !this._digester || !(chunk.byteLength || chunk.size)) {
if (this._state !== Peer.STATE_RECEIVE_PROCEEDING || !this._digester || !chunk.byteLength) {
this._sendState();
return;
}
this._digester.unchunk(chunk);
let progress = (this._totalBytesReceived + this._digester._bytesReceived) / this._acceptedRequest.totalSize;
if (isNaN(progress)) progress = 1
if (progress > 1) {
try {
this._digester.unchunk(chunk);
}
catch (e) {
this._abortTransfer();
Logger.error("Too many bytes received. Abort!");
Logger.error(e);
return;
}
let progress = this._digester
? (this._totalBytesReceived + this._digester._bytesReceived) / this._acceptedRequest.totalSize
: 1;
Events.fire('set-progress', {peerId: this._peerId, progress: progress, status: 'receive'});
// occasionally notify sender about our progress
@ -1834,8 +1835,12 @@ class FileDigester {
unchunk(chunk) {
this._buffer.push(chunk);
this._bytesReceived += chunk.byteLength || chunk.size;
this._bytesReceivedSinceLastTime += chunk.byteLength || chunk.size;
this._bytesReceived += chunk.byteLength;
this._bytesReceivedSinceLastTime += chunk.byteLength;
if (this._bytesReceived > this._size) {
throw new Error("Too many bytes received. Abort!");
}
// If more than half of maxBytesWithoutConfirmation received -> send confirmation
if (2 * this._bytesReceivedSinceLastTime > this._maxBytesWithoutConfirmation) {
@ -1843,6 +1848,7 @@ class FileDigester {
this._bytesReceivedSinceLastTime = 0;
}
// File not completely received -> Wait for next chunk.
if (this._bytesReceived < this._size) return;
// We are done receiving. Preferably use a file worker to process the file to prevent exceeding of available RAM

View file

@ -729,7 +729,8 @@ class PeerUI {
this._progressQueue.unshift({progress: progress, status: status});
this.setProgress(0.5, status);
return;
} else if (progressSpillsOverFull) {
}
else if (progressSpillsOverFull) {
this._progressQueue.unshift({progress: progress, status: status});
this.setProgress(1, status);
return;
@ -755,7 +756,8 @@ class PeerUI {
this.$progress.classList.remove('animate');
this.$progress.classList.remove('over50');
this.$progress.classList.add('animate');
} else if (this._currentProgress === 0.5) {
}
else if (this._currentProgress === 0.5) {
this.$progress.classList.remove('animate');
this.$progress.classList.add('over50');
this.$progress.classList.add('animate');
@ -763,7 +765,8 @@ class PeerUI {
if (this._currentProgress < progress) {
this.$progress.classList.add('animate');
} else {
}
else {
this.$progress.classList.remove('animate');
}
@ -800,14 +803,15 @@ class PeerUI {
"process": Localization.getTranslation("peer-ui.processing"),
"wait": Localization.getTranslation("peer-ui.waiting"),
"transfer-complete": Localization.getTranslation("peer-ui.transfer-complete"),
"receive-complete": Localization.getTranslation("peer-ui.receive-complete")
"receive-complete": Localization.getTranslation("peer-ui.receive-complete"),
"error": Localization.getTranslation("peer-ui.error")
}[status];
this.$el.setAttribute('status', status);
this.$el.querySelector('.status').innerText = statusName;
this._currentStatus = status;
if (status.indexOf("-complete") || status === "receive-complete") {
if (["transfer-complete", "receive-complete", "error"].includes(status)) {
this.statusTimeout = setTimeout(() => {
this.setProgress(0, null);
}, 10000);