Skip to content

Commit 71dae9d

Browse files
authored
feat(web): zkey downloads changed to use normal browser anchors (#127)
Removes custom progress bar and javascript function for downloading. Zkey downloads are changed to be wrapped in anchor tags "<a>" in order to use the browser's standard file download flow. This has the benefit of exposing the URLs to people can "Copy Link" and share these zkey URLs.
1 parent ce9d10d commit 71dae9d

File tree

1 file changed

+35
-76
lines changed

1 file changed

+35
-76
lines changed

web/src/pages/ProjectPage.tsx

+35-76
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ const ProjectPage: React.FC = () => {
6666
const { ceremonyName } = useParams<RouteParams>();
6767
const { user, projects, setRunTutorial, runTutorial } = useContext(StateContext);
6868
const { latestZkeys, finalBeacon, finalZkeys, hasUserContributed, projectData, isLoading, avatars, largestCircuitConstraints } = useProjectPageContext();
69-
//const [ downloadProgress, setDownloadProgress ] = useState(0)
70-
const [ {loaded, fileSize}, setDownloadSize] = useState({loaded: 0, fileSize: 0})
7169
// handle the callback from joyride
7270
const handleJoyrideCallback = (data: any) => {
7371
const { status } = data;
@@ -143,51 +141,6 @@ const ProjectPage: React.FC = () => {
143141
const { onCopy: copyBeaconValue, hasCopied: copiedBeaconValue } = useClipboard(beaconValue || "")
144142
const { onCopy: copyBeaconHash, hasCopied: copiedBeaconHash } = useClipboard(beaconHash || "")
145143

146-
let downloadProgress = fileSize > 0 ? Math.round(100*loaded/fileSize) : 0;
147-
148-
// Download a file from AWS S3 bucket.
149-
const downloadFileFromS3 = (index: number, name: string) => {
150-
if (finalZkeys) {
151-
fetch(finalZkeys[index].zkeyURL , {
152-
mode: 'cors',
153-
headers: {
154-
'Access-Control-Allow-Origin':'*'
155-
}})
156-
.then((response) => {
157-
const contentLength = response.headers.get('content-length');
158-
setDownloadSize(() => {return {loaded: 0, fileSize: parseInt(contentLength!, 10)}});
159-
160-
const res = new Response(new ReadableStream({
161-
async start(controller) {
162-
if (response.body) {
163-
const reader = response.body.getReader();
164-
for (;;) {
165-
const {done, value} = await reader.read();
166-
if (done) break;
167-
setDownloadSize(ds => {return {...ds, loaded: ds.loaded+value.byteLength}});
168-
controller.enqueue(value);
169-
}
170-
controller.close();
171-
} else {
172-
console.log(`no body`)
173-
}
174-
}
175-
}));
176-
177-
res.blob().then((blob) => {
178-
console.log()
179-
const fileURL = window.URL.createObjectURL(blob);
180-
181-
let alink = document.createElement("a");
182-
alink.href = fileURL;
183-
alink.download = name;
184-
alink.click();
185-
});
186-
});
187-
}
188-
189-
};
190-
191144
return (
192145
<>
193146
<HStack
@@ -602,20 +555,23 @@ const ProjectPage: React.FC = () => {
602555
{
603556
finalZkeys?.map((zkey, index) => {
604557
return (
605-
<Button
606-
margin={"20px"}
607-
key={index}
608-
leftIcon={<Box as={FaCloudDownloadAlt} w={3} h={3} />}
609-
fontSize={12}
610-
variant="outline"
611-
onClick={() => downloadFileFromS3(index, zkey.zkeyFilename)}
612-
fontWeight={"regular"}
613-
isDisabled={
614-
project?.ceremony.data.state !== CeremonyState.FINALIZED ? true : false
615-
}
616-
>
617-
Download {zkey.zkeyFilename}
618-
</Button>
558+
<a
559+
href={zkey.zkeyURL}
560+
key={index}
561+
>
562+
<Button
563+
margin={"20px"}
564+
leftIcon={<Box as={FaCloudDownloadAlt} w={3} h={3} />}
565+
fontSize={12}
566+
variant="outline"
567+
fontWeight={"regular"}
568+
isDisabled={
569+
project?.ceremony.data.state !== CeremonyState.FINALIZED ? true : false
570+
}
571+
>
572+
Download {zkey.zkeyFilename}
573+
</Button>
574+
</a>
619575
)
620576
})
621577
}
@@ -631,24 +587,27 @@ const ProjectPage: React.FC = () => {
631587
{
632588
latestZkeys?.map((zkey, index) => {
633589
return (
634-
<Button
635-
margin={"20px"}
636-
key={index}
637-
leftIcon={<Box as={FaCloudDownloadAlt} w={3} h={3} />}
638-
fontSize={12}
639-
variant="outline"
640-
onClick={() => downloadFileFromS3(index, zkey.zkeyFilename)}
641-
fontWeight={"regular"}
642-
isDisabled={
643-
project?.ceremony.data.state !== CeremonyState.FINALIZED ? true : false
644-
}
645-
>
646-
Download {zkey.zkeyFilename}
647-
</Button>
590+
<a
591+
href={zkey.zkeyURL}
592+
key={index}
593+
>
594+
<Button
595+
margin={"20px"}
596+
key={index}
597+
leftIcon={<Box as={FaCloudDownloadAlt} w={3} h={3} />}
598+
fontSize={12}
599+
variant="outline"
600+
fontWeight={"regular"}
601+
isDisabled={
602+
project?.ceremony.data.state !== CeremonyState.FINALIZED ? true : false
603+
}
604+
>
605+
Download {zkey.zkeyFilename}
606+
</Button>
607+
</a>
648608
)
649609
})
650610
}
651-
<Progress colorScheme="green" value={downloadProgress}></Progress>
652611
</>
653612
}
654613

0 commit comments

Comments
 (0)