Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcottle committed Dec 17, 2024
1 parent bd1c3d4 commit 7bb5620
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/pages/NodeFilesPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
</div>

<!-- progress bar -->
<div v-if="fileTransfer.status === 'accepted' || fileTransfer.status === 'sending' || fileTransfer.status === 'receiving'" class="mt-2">
<div v-if="fileTransfer.status === 'sending' || fileTransfer.status === 'receiving'" class="mt-2">
<div class="overflow-hidden rounded-full bg-gray-200">
<div class="h-2 rounded-full bg-blue-500" :style="{ width: `${fileTransfer.progress}%` }"></div>
</div>
Expand Down
11 changes: 8 additions & 3 deletions src/js/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ class Connection {
filename: fileTransferOffer.fileName,
filesize: fileTransferOffer.fileSize,
progress: 0,
chunks: {},
data: new Uint8Array(0),
};

GlobalState.fileTransfers.push(fileTransfer);
Expand Down Expand Up @@ -634,12 +634,17 @@ class Connection {
return;
}

// do nothing if file transfer completed, cancelled or rejected
if(fileTransfer.status === FileTransferrer.STATUS_COMPLETED || fileTransfer.status === FileTransferrer.STATUS_CANCELLED || fileTransfer.status === FileTransferrer.STATUS_REJECTED){
console.log(`[FileTransfer] ${fileTransfer.id} cancelled, but already in completed state`);
return;
}

console.log(`[FileTransfer] ${fileTransfer.id} requested FileChunk[offset=${requestFileChunk.offset}, length=${requestFileChunk.length}]`);

// update file transfer progress
const filePointer = requestFileChunk.offset + requestFileChunk.length;
fileTransfer.status = FileTransferrer.STATUS_SENDING;
fileTransfer.progress = Math.min(100, Math.ceil(filePointer / fileTransfer.filesize * 100));
fileTransfer.progress = Math.min(100, Math.ceil(requestFileChunk.offset + requestFileChunk.length / fileTransfer.filesize * 100));

// send file part
await FileTransferrer.sendFileChunk(fileTransfer, requestFileChunk.offset, requestFileChunk.length);
Expand Down
9 changes: 3 additions & 6 deletions src/js/FileTransferrer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class FileTransferrer {
static DIRECTION_OUTGOING = "outgoing";

static STATUS_OFFERING = "offering";
static STATUS_ACCEPTED = "accepted";
static STATUS_REJECTED = "rejected";
static STATUS_CANCELLED = "cancelled";
static STATUS_COMPLETED = "completed";
Expand Down Expand Up @@ -69,7 +68,7 @@ class FileTransferrer {
static async acceptFileTransfer(fileTransfer) {

// create buffer for file data
fileTransfer.status = this.STATUS_ACCEPTED;
fileTransfer.status = this.STATUS_RECEIVING;
fileTransfer.data = new Uint8Array(0);

// loop until all bytes received
Expand Down Expand Up @@ -106,9 +105,7 @@ class FileTransferrer {
}

// update file transfer progress
const filePointer = fileChunk.offset + fileChunk.length;
fileTransfer.status = FileTransferrer.STATUS_RECEIVING;
fileTransfer.progress = Math.min(100, Math.ceil(filePointer / fileTransfer.filesize * 100));
fileTransfer.progress = Math.min(100, Math.ceil(fileChunk.offset + fileChunk.length / fileTransfer.filesize * 100));

} catch(e) {
this.log("failed to get file chunk", e);
Expand All @@ -129,7 +126,7 @@ class FileTransferrer {
try {
this.log(`rejectFileTransfer attempt ${attempt}`);
await FileTransferAPI.rejectFileTransfer(fileTransfer.from, fileTransfer.id);
fileTransfer.status = this.STATUS_ACCEPTED;
fileTransfer.status = this.STATUS_REJECTED;
return;
} catch(e) {
console.log(e);
Expand Down

0 comments on commit 7bb5620

Please sign in to comment.