Skip to content

Commit

Permalink
fix: move document get down to lowest level
Browse files Browse the repository at this point in the history
  • Loading branch information
IanKrieger committed Sep 21, 2023
1 parent 2d550a7 commit c078396
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 68 deletions.
5 changes: 1 addition & 4 deletions src/user/reporting/ReportMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp";
import DownloadIcon from "@mui/icons-material/Download";
import { uInt8Array } from "util/uInt8Array";

interface ReportMenuProps {
hasVerifiedConversions: boolean;
Expand All @@ -32,8 +31,6 @@ export const ReportMenu = ({
const [isError, setIsError] = useState(false);
const set = (s: string) =>
document.getElementById("private-key")?.setAttribute("value", s);
const get = () =>
document.getElementById("private-key")?.getAttribute("value");
const { download, loading, error } = useDownloadCSV({
onComplete() {
setAnchorEl(null);
Expand Down Expand Up @@ -134,7 +131,7 @@ export const ReportMenu = ({
<Button
variant="contained"
onClick={() => {
download(campaignId, true, uInt8Array(get()));
download(campaignId, true);
}}
disabled={loading}
>
Expand Down
120 changes: 60 additions & 60 deletions src/user/reporting/csv.library.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState } from "react";
import { buildAdServerEndpoint } from "util/environment";
import Papa from "papaparse";
import tweetnacl from "tweetnacl";
import { uInt8Array } from "util/uInt8Array";

interface DownloadProps {
onComplete?: () => void;
onError?: () => void;
Expand All @@ -12,75 +12,74 @@ export function useDownloadCSV(props: DownloadProps = {}) {
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string>();

const download = useCallback(
(campaignId: string, isVac: boolean, privateKey?: Uint8Array) => {
setLoading(true);
setError(undefined);
const download = useCallback((campaignId: string, isVac: boolean) => {
setLoading(true);
setError(undefined);

const baseUrl = `/report/campaign/csv/${campaignId}`;
fetch(buildAdServerEndpoint(isVac ? `${baseUrl}/vac` : baseUrl), {
method: "GET",
mode: "cors",
credentials: "include",
headers: {
"Content-Type": "text/csv",
},
})
.then((res) => {
if (res.status !== 200) {
const err = "Unable to download CSV";
setError(err);
throw new Error(err);
}
const baseUrl = `/report/campaign/csv/${campaignId}`;
fetch(buildAdServerEndpoint(isVac ? `${baseUrl}/vac` : baseUrl), {
method: "GET",
mode: "cors",
credentials: "include",
headers: {
"Content-Type": "text/csv",
},
})
.then((res) => {
if (res.status !== 200) {
const err = "Unable to download CSV";
setError(err);
throw new Error(err);
}

return res.blob();
})
.then((blob) => {
const file = new Blob([blob], {
type: "text/csv",
endings: "transparent",
});
return res.blob();
})
.then((blob) => {
const file = new Blob([blob], {
type: "text/csv",
endings: "transparent",
});

if (isVac) {
return transformConversionEnvelope(file, privateKey);
}
if (isVac) {
return transformConversionEnvelope(file);
}

return Promise.resolve(file);
})
.then((file) => {
const fileURL = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = fileURL;
link.setAttribute("download", `${campaignId}.csv`);
document.body.appendChild(link);
link.click();
if (props.onComplete) props.onComplete();
})
.catch((e) => {
setError(e.message);
})
.finally(() => {
setLoading(false);
privateKey?.fill(0);
});
},
[],
);
return Promise.resolve(file);
})
.then((file) => {
const fileURL = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = fileURL;
link.setAttribute("download", `${campaignId}.csv`);
document.body.appendChild(link);
link.click();
if (props.onComplete) {
props.onComplete();
}
})
.catch((e) => {
setError(e.message);
})
.finally(() => {
setLoading(false);
});
}, []);

return { download, loading, error };
}

type Envelope = { ciphertext: string; epk: string; nonce: string };
async function transformConversionEnvelope(
blob: Blob,
privateKey?: Uint8Array,
): Promise<Blob> {
if (!privateKey) {
async function transformConversionEnvelope(blob: Blob): Promise<Blob> {
const ui8a = (s?: string | null) =>
s ? Uint8Array.from(atob(s), (c) => c.charCodeAt(0)) : new Uint8Array();
const privateKey = ui8a(
document.getElementById("private-key")?.getAttribute("value"),
);
if (privateKey.byteLength === 0) {
return Promise.resolve(blob);
}

const td = new TextDecoder();

return await new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = () => {
Expand All @@ -98,9 +97,9 @@ async function transformConversionEnvelope(
if (field.includes("Conversion")) {
const { ciphertext, nonce, epk }: Envelope = JSON.parse(value);
const res = tweetnacl.box.open(
uInt8Array(ciphertext),
uInt8Array(nonce),
uInt8Array(epk),
ui8a(ciphertext),
ui8a(nonce),
ui8a(epk),
privateKey,
);
return res
Expand All @@ -114,6 +113,7 @@ async function transformConversionEnvelope(
const newCSV = Papa.unparse(results.data, {
skipEmptyLines: "greedy",
});
privateKey.fill(0);
resolve(new Blob([newCSV]));
},
error(error: Error) {
Expand Down
4 changes: 0 additions & 4 deletions src/util/uInt8Array.ts

This file was deleted.

0 comments on commit c078396

Please sign in to comment.