Skip to content

Commit

Permalink
Prevent buffered downloads from exceeding maximum configured buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Edwards committed May 5, 2024
1 parent f557aa2 commit 163ff7a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
1 change: 0 additions & 1 deletion Crypter.Web/Npm/src/fileSaver/fileSaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export async function initializeAsync() : Promise<void> {

export function browserSupportsStreamingDownloads(): boolean {
let thisInstance: FileSaver = FileSaver.getInstance();
console.log(thisInstance.IsServiceWorkerAvailable);
return thisInstance.IsServiceWorkerAvailable;
}

Expand Down
13 changes: 10 additions & 3 deletions Crypter.Web/Shared/Transfer/DownloadFileTransfer.razor
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,17 @@
<div class="col-md-8">
<div class="card">
<div class="card-body text-center">
@if (DecryptionComplete)
@if (FileCannotBeDownloadedOnThisBrowser)
{
<div class="alert alert-warning text-center" role="alert">
This file is too large to download on this browser.
Try using a different browser.
</div>
}
else if (DecryptionComplete)
{
<div class="alert alert-success text-center" role="alert">
Decryption successful!
Decryption successful! Check your downloads folder.
</div>
}
else if (DecryptionInProgress)
Expand All @@ -85,7 +92,7 @@
else
{
<h5 class="card-title">Ready to Decrypt</h5>
<p class="card-text fst-italic">Click 'Decrypt' to decrypt the file</p>
<p class="card-text fst-italic">Click 'Decrypt' to decrypt and download the file</p>

@if (!DecryptionInProgress)
{
Expand Down
13 changes: 13 additions & 0 deletions Crypter.Web/Shared/Transfer/DownloadFileTransfer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using System;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Crypter.Common.Client.Enums;
using Crypter.Common.Client.Transfer.Handlers;
using Crypter.Common.Contracts.Features.Transfer;
using Crypter.Crypto.Common.StreamEncryption;
Expand All @@ -46,7 +47,10 @@ public partial class DownloadFileTransfer

[Inject]
private IFileSaverService FileSaverService { get; init; } = null!;

protected bool FileCannotBeDownloadedOnThisBrowser { get; set; } = false;

private long _maxBufferSizeMB = 0;
private string _fileName = string.Empty;
private string _contentType = string.Empty;
private long _fileSize = 0;
Expand All @@ -55,6 +59,10 @@ public partial class DownloadFileTransfer

protected override async Task OnInitializedAsync()
{
TransmissionType = FileSaverService.SupportsStreamingDownloads
? TransferTransmissionType.Stream
: TransferTransmissionType.Buffer;
_maxBufferSizeMB = (TransferSettings.MaximumBufferSizeMB + 1) * Convert.ToInt64(Math.Pow(10, 6));
await PrepareFilePreviewAsync();
FinishedLoading = true;
}
Expand All @@ -74,6 +82,11 @@ private async Task PrepareFilePreviewAsync()
SpecificRecipient = !string.IsNullOrEmpty(x.Recipient);
});

if (TransmissionType == TransferTransmissionType.Buffer && _fileSize > _maxBufferSizeMB)
{
FileCannotBeDownloadedOnThisBrowser = true;
}

ItemFound = previewResponse.IsRight;
}

Expand Down
5 changes: 5 additions & 0 deletions Crypter.Web/Shared/Transfer/DownloadTransferBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
*/

using System;
using Crypter.Common.Client.Enums;
using Crypter.Common.Client.Interfaces.Services;
using Crypter.Common.Client.Transfer;
using Crypter.Common.Client.Transfer.Models;
using Crypter.Common.Enums;
using EasyMonads;
using Microsoft.AspNetCore.Components;
Expand All @@ -44,11 +46,14 @@ public class DownloadTransferBase : ComponentBase
[Inject] protected TransferHandlerFactory TransferHandlerFactory { get; init; } = null!;

[Inject] protected ICryptoProvider CryptoProvider { get; init; } = null!;

[Inject] protected ClientTransferSettings TransferSettings { get; init; } = null!;

[Parameter] public required string TransferHashId { get; set; }

[Parameter] public TransferUserType UserType { get; set; }

protected TransferTransmissionType TransmissionType = TransferTransmissionType.Buffer;
protected bool FinishedLoading = false;
protected bool ItemFound = false;
protected bool DecryptionInProgress = false;
Expand Down

0 comments on commit 163ff7a

Please sign in to comment.