Skip to content

Commit

Permalink
foo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Edwards committed May 15, 2024
1 parent 79ab7bc commit 7ff7386
Showing 1 changed file with 34 additions and 39 deletions.
73 changes: 34 additions & 39 deletions Crypter.Web/wwwroot/chromiumFetchTest/script.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
window.addEventListener('load',
function() {
document.getElementById("transmit").onclick = async() => {
document.getElementById("transmit").onclick = () => {
let files = document.getElementById('largeFile');
await uploadAsync(files.files[0]);
};
let stream = openStream(files.files[0]);
fetch('https://dev.crypter.dev/api/file/transfer/sink', {
method: 'POST',
headers: {'Content-Type': `multipart/form-data;boundary="${boundaryText}"`},
body: stream,
duplex: 'half'
})
.then(response => console.log(response))
.catch(error => console.log(error));
}
}, false);

function wait(milliseconds) {
Expand All @@ -12,9 +20,11 @@ function wait(milliseconds) {

const boundaryText = '4c4b7e34-dc34-4971-913c-6b6ceb09ee33';

async function uploadAsync(file) {
const readableStream = new ReadableStream({
async start(controller) {
function openStream(file) {
const reader = file.stream().getReader();

return new ReadableStream({
start(controller) {
controller.enqueue(`--${boundaryText}`);
controller.enqueue('\r\n');
controller.enqueue('Content-Type: application/json; charset=utf-8');
Expand All @@ -28,42 +38,27 @@ async function uploadAsync(file) {
controller.enqueue('Content-Disposition: form-data; name=Ciphertext; filename=Ciphertext; filename*=utf-8\'\'Ciphertext');
controller.enqueue('\r\n\r\n');

const stream = file.stream();

for await (const chunk of stream.values({ preventCancel: true })) {
console.log('enqueue chunk');
controller.enqueue(chunk);
}

/*
async function pushAsync() {
console.log('chunk');
await reader.read()
.then(async ({done, value}) => {
if (done) {
return;
}
controller.enqueue(value);
await pushAsync();
function push() {
// "done" is a Boolean and value a "Uint8Array"
reader.read().then(({ done, value }) => {
// If there is no more data to read
if (done) {
controller.enqueue('\r\n');
controller.enqueue(`--${boundaryText}--`);
controller.enqueue('\r\n\r\n');
console.log("done", done);
controller.close();
return;
}
// Get the data and send it to the browser via the controller
controller.enqueue(value);
// Check chunks by logging to the console
console.log(done, value);
push();
});
}
await pushAsync();
*/

controller.enqueue('\r\n');
controller.enqueue(`--${boundaryText}--`);
controller.enqueue('\r\n\r\n');

controller.close();
push();
}
});

return await fetch('https://dev.crypter.dev/api/file/transfer/sink', {
method: 'POST',
headers: {'Content-Type': `multipart/form-data;boundary="${boundaryText}"`},
body: readableStream,
duplex: 'half'
})
.then(response => console.log(response))
.catch(error => console.log(error));
}

0 comments on commit 7ff7386

Please sign in to comment.