Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Edwards committed Apr 21, 2024
1 parent 8a243b5 commit 784e9d6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2023 Crypter File Transfer
* Copyright (C) 2024 Crypter File Transfer
*
* This file is part of the Crypter file transfer project.
*
Expand Down Expand Up @@ -34,6 +34,7 @@
using Crypter.Common.Contracts.Features.Transfer;
using Crypter.Common.Enums;
using Crypter.Crypto.Common;
using Crypter.Crypto.Common.StreamEncryption;
using EasyMonads;

namespace Crypter.Common.Client.Transfer.Handlers;
Expand Down Expand Up @@ -62,7 +63,7 @@ public async Task<Either<TransferPreviewError, FileTransferPreviewResponse>> Dow
return response;
}

public async Task<Either<DownloadTransferCiphertextError, byte[]>> DownloadCiphertextAsync()
public async Task<Either<DownloadTransferCiphertextError, DecryptionStream>> DownloadCiphertextAsync()
{
byte[] symmetricKey = SymmetricKey.Match(
() => throw new Exception("Missing symmetric key"),
Expand All @@ -82,9 +83,9 @@ public async Task<Either<DownloadTransferCiphertextError, byte[]>> DownloadCiphe
};
#pragma warning restore CS8524

return response.Match<Either<DownloadTransferCiphertextError, byte[]>>(
return response.Match<Either<DownloadTransferCiphertextError, DecryptionStream>>(
left => left,
right => Decrypt(symmetricKey, right.Stream, right.StreamSize),
right => new DecryptionStream(right.Stream, right.StreamSize, symmetricKey, CryptoProvider.StreamEncryptionFactory),
DownloadTransferCiphertextError.UnknownError);
}
}
26 changes: 16 additions & 10 deletions Crypter.Web/Shared/Transfer/DownloadFileTransfer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@
using System.Threading.Tasks;
using Crypter.Common.Client.Transfer.Handlers;
using Crypter.Common.Contracts.Features.Transfer;
using Crypter.Crypto.Common.StreamEncryption;
using Crypter.Web.Services;
using EasyMonads;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.JSInterop;

namespace Crypter.Web.Shared.Transfer;

[SupportedOSPlatform("browser")]
public partial class DownloadFileTransfer : IDisposable
{
[Inject]
private IJSRuntime JSRuntime { get; init; } = null!;

private string _fileName = string.Empty;
private string _contentType = string.Empty;
private long _fileSize = 0;
Expand Down Expand Up @@ -90,17 +96,17 @@ await recipientPrivateKey.IfSomeAsync(async privateKey =>
_downloadHandler.SetRecipientInfo(privateKey);

await SetProgressMessage(DecryptingLiteral);
Either<DownloadTransferCiphertextError, byte[]> decryptionResponse = await _downloadHandler.DownloadCiphertextAsync();

decryptionResponse.DoLeftOrNeither(
HandleDownloadError,
Either<DownloadTransferCiphertextError, DecryptionStream> decryptionResponse = await _downloadHandler.DownloadCiphertextAsync();

await decryptionResponse
.DoRightAsync(async decryptionStream =>
{
await JSRuntime.InvokeVoidAsync("sendStreamToServiceWorker", decryptionStream);
DecryptionComplete = true;
})
.DoLeftOrNeitherAsync(
HandleDownloadError,
() => HandleDownloadError());

decryptionResponse.DoRight(x =>
{
BrowserDownloadFileService.CopyBufferToJavaScript(_fileName, _contentType, x);
DecryptionComplete = true;
});
});

DecryptionInProgress = false;
Expand Down
76 changes: 49 additions & 27 deletions Crypter.Web/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link rel="stylesheet" href="css/main.css"/>

<!--
Copyright (C) 2023 Crypter File Transfer
Copyright (C) 2024 Crypter File Transfer
This file is part of the Crypter file transfer project.
Expand All @@ -57,32 +57,54 @@
-->
</head>

<body class="grt h-100">
<div id="app" class="d-flex flex-column h-100">
<div class="position-absolute w-100 text-center" style="top: 30vh;">
<h1 class="title">Crypter.dev</h1>
<h2>Private, encrypted file transfer and messaging</h2>
<div class="spinner-border mt-2" role="status">
<span class="visually-hidden">Loading...</span>
<body class="grt h-100">
<div id="app" class="d-flex flex-column h-100">
<div class="position-absolute w-100 text-center" style="top: 30vh;">
<h1 class="title">Crypter.dev</h1>
<h2>Private, encrypted file transfer and messaging</h2>
<div class="spinner-border mt-2" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<h2>Loading...</h2>
</div>
</div>
<h2>Loading...</h2>
</div>
</div>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="js/scripts.js"></script>
<script src="lib/bootstrap-5.2.3-dist/js/bootstrap.bundle.min.js"></script>
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<script>
Blazor.start({
environment: window.location.hostname == "localhost"
? "Development"
: "Production"
});
</script>
</body>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
<script src="js/scripts.js"></script>
<script src="lib/bootstrap-5.2.3-dist/js/bootstrap.bundle.min.js"></script>
<script src="_framework/blazor.webassembly.js" autostart="false"></script>
<script>
Blazor.start({
environment: window.location.hostname === "localhost"
? "Development"
: "Production"
});
</script>

<script>
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("/service-worker.js")
.then(function (registration) {
console.log("Service Worker registered with scope:", registration.scope);
})
.catch(function (error) {
console.error("Failed to register Service Worker:", error);
});
}
</script>

<script>
async function sendStreamToServiceWorker(fileName, stream) {
const registration = await navigator.serviceWorker.getRegistration();
if (registration) {
// Send the stream to the service worker
registration.active.postMessage({ fileName, stream }, [stream]);
}
}
</script>
</body>

</html>
30 changes: 30 additions & 0 deletions Crypter.Web/wwwroot/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
self.addEventListener('install', (event) => {
console.log('Service worker installed');
});

self.addEventListener('activate', (event) => {
console.log('Service worker activated');
});

self.addEventListener('fetch', function (event) {
let url = new URL(event.request.url);

// Define the URL that needs to be intercepted
if (url.pathname === '/blob/download') {
event.respondWith(
fetch(event.request)
.then(function (response) {
// Create a new Response object with the necessary headers to allow file download
let headers = new Headers(response.headers);
headers.append('Content-Disposition', 'attachment');
headers.append('Access-Control-Expose-Headers', 'Content-Disposition');

return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: headers
});
})
);
}
});

0 comments on commit 784e9d6

Please sign in to comment.