Skip to content

Commit

Permalink
fix: try-catch on render (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtsukino authored Jun 11, 2024
1 parent bc32d1b commit b5bd2bb
Showing 1 changed file with 39 additions and 52 deletions.
91 changes: 39 additions & 52 deletions server/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,73 +67,55 @@ app.get('/gateway/ipfs/:cid', async (req, res) => {
});

app.get('/ipfs/:cid', async (req, res) => {
let file: string,
jsonProof: Proof,
proof: {
time: number;
sent: string;
recv: string;
notaryUrl: string;
// If there is no file from CID or JSON cannot be parsed, redirect to root
try {
const storeConfig: AppRootState = {
notaryKey: { key: '' },
proofUpload: {
proofs: [],
selectedProof: null,
},
proofs: { ipfs: {} },
};

const storeConfig: AppRootState = {
notaryKey: { key: '' },
proofUpload: {
proofs: [],
selectedProof: null,
},
proofs: { ipfs: {} },
};
const file = await getCID(req.params.cid);
const jsonProof: Proof = JSON.parse(file);

// If there is no file from CID or JSON cannot be parsed, redirect to root
try {
file = await getCID(req.params.cid);
jsonProof = JSON.parse(file);
} catch (e) {
res.redirect('/');
return;
}
storeConfig.proofs.ipfs[req.params.cid] = {
raw: jsonProof,
};

storeConfig.proofs.ipfs[req.params.cid] = {
raw: jsonProof,
};

/**
* Verify the proof if notary url exist
* redirect to root if verification fails
*/
if (jsonProof.notaryUrl) {
try {
proof = await verify(
/**
* Verify the proof if notary url exist
* redirect to root if verification fails
*/
if (jsonProof.notaryUrl) {
const proof = await verify(
file,
await fetchPublicKeyFromNotary(jsonProof.notaryUrl),
);
proof.notaryUrl = jsonProof.notaryUrl;
storeConfig.proofs.ipfs[req.params.cid].proof = proof;
} catch (e) {
res.redirect('/');
return;
}
}

const store = configureAppStore(storeConfig);
const html = renderToString(
<Provider store={store}>
<StaticRouter location={req.url}>
<App />
</StaticRouter>
</Provider>,
);
const store = configureAppStore(storeConfig);
const html = renderToString(
<Provider store={store}>
<StaticRouter location={req.url}>
<App />
</StaticRouter>
</Provider>,
);

const preloadedState = store.getState();
const preloadedState = store.getState();

const img = await htmlToImage({
html: html,
});
const img = await htmlToImage({
html: html,
});

const imgUrl = 'data:image/png;base64,' + img.toString('base64');
const imgUrl = 'data:image/png;base64,' + img.toString('base64');

res.send(`
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
Expand All @@ -152,6 +134,11 @@ app.get('/ipfs/:cid', async (req, res) => {
</body>
</html>
`);
} catch (e) {
console.error(e);
res.redirect('/');
return;
}
});

app.get('*', (req, res) => {
Expand Down

0 comments on commit b5bd2bb

Please sign in to comment.