Skip to content

Commit

Permalink
Update App.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
dmihalcik-virtru committed May 10, 2024
1 parent c6027f5 commit 8b947da
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions web-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clsx } from 'clsx';
import { useState, useEffect, type ChangeEvent } from 'react';
import { useState, useEffect, type ChangeEvent, useRef } from 'react';
import { showSaveFilePicker } from 'native-file-system-adapter';
import './App.css';
import { type Chunker, type DecryptSource, NanoTDFClient, TDF3Client } from '@opentdf/client';
Expand Down Expand Up @@ -75,7 +75,7 @@ async function getNewFileHandle(
}

type Containers = 'html' | 'tdf' | 'nano';
type CurrentDataController = AbortController | undefined;
type CurrentDataControllers = Record<string, AbortController>;
type FileInputSource = {
type: 'file';
file: File;
Expand Down Expand Up @@ -230,7 +230,7 @@ function App() {
const [encryptContainerType, setEncryptContainerType] = useState<Containers>('tdf');
const [inputSources, setInputSources] = useState<InputSource[]>([]);
const [sinkType, setSinkType] = useState<SinkType>('file');
const [streamController, setStreamController] = useState<CurrentDataController>();
const streamControllers = useRef<CurrentDataControllers>({});

const handleContainerFormatRadioChange =
(handler: typeof setDecryptContainerType) => (e: ChangeEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -449,7 +449,7 @@ function App() {
) {
let source: ReadableStream<Uint8Array>, size: number;
const sc = new AbortController();
setStreamController(sc);
streamControllers.current[inputFileName] = sc;
switch (inputSource.type) {
case 'file':
size = inputSource.file.size;
Expand Down Expand Up @@ -514,16 +514,14 @@ function App() {
await cipherText.stream.pipeTo(drain(), { signal: sc.signal });
break;
}
} catch (e) {
setDownloadState(`Encrypt Failed: ${e}`);
console.error('Encrypt Failed', e);
} finally {
delete streamControllers.current[inputFileName];
}
setStreamController(undefined);
}

async function encryptTdf(inputSource: InputSource, inputFileName: string, client: TDF3Client) {
const sc = new AbortController();
setStreamController(sc);
streamControllers.current[inputFileName] = sc;
let source: ReadableStream<Uint8Array>, size: number;
switch (inputSource.type) {
case 'file':
Expand Down Expand Up @@ -582,11 +580,9 @@ function App() {
await cipherText.stream.pipeTo(drain(), { signal: sc.signal });
break;
}
} catch (e) {
setDownloadState(`Encrypt Failed: ${e}`);
console.error('Encrypt Failed', e);
} finally {
delete streamControllers.current[inputFileName];
}
setStreamController(undefined);
}
};
async function decryptNano(
Expand All @@ -610,7 +606,6 @@ function App() {
const writable = await f.createWritable();
try {
await writable.write(plainText);
setDownloadState('Decrypt Complete');
} finally {
await writable.close();
}
Expand All @@ -624,7 +619,7 @@ function App() {
}

let promises: (() => Promise<void>)[];
const memory = [];
const memory: MemoryInputSource[] = [];
const handleDecrypt = async () => {
if (!inputSources.length) {
console.log('PLEASE SELECT FILE');
Expand Down Expand Up @@ -667,7 +662,13 @@ function App() {
break;
}
}
await Promise.all(promises.map(limit));
try {
await Promise.all(promises.map(limit));
setDownloadState('Decrypt Complete');
} catch (e) {
console.error('Decrypt Failed', e);
setDownloadState(`Decrypt Failed: ${e}`);
}

if (memory.length) {
setInputSources(memory);
Expand All @@ -679,7 +680,6 @@ function App() {
if (sinkType === 'fsapi') {
f = await getNewFileHandle(decryptedFileExtension(fileNameFor(inputSource)), dfn);
}
try {
const sc = new AbortController();
setStreamController(sc);
let source: DecryptSource;
Expand Down Expand Up @@ -726,11 +726,6 @@ function App() {
await plainText.stream.pipeTo(drain(), { signal: sc.signal });
break;
}
} catch (e) {
console.error('Decrypt Failed', e);
setDownloadState(`Decrypt Failed: ${e}`);
}
setStreamController(undefined);
}
};

Expand Down

0 comments on commit 8b947da

Please sign in to comment.