Skip to content

Commit d88445b

Browse files
fix(web): file-viewer-pdf-download-broken
1 parent fb063f6 commit d88445b

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

web/src/components/FileViewer/index.tsx

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const StyledDocViewer = styled(DocViewer)`
2828
* @returns renders the file
2929
*/
3030
const FileViewer: React.FC<{ url: string }> = ({ url }) => {
31-
const docs = [{ uri: url }];
31+
const docs = [{ uri: url, fileName: fileNameIfIpfsUrl(url) }];
3232
return (
3333
<Wrapper className="file-viewer-wrapper">
3434
<StyledDocViewer
@@ -50,4 +50,25 @@ const FileViewer: React.FC<{ url: string }> = ({ url }) => {
5050
);
5151
};
5252

53+
const fileNameIfIpfsUrl = (url: string) => {
54+
if (!url || typeof url !== "string") {
55+
return "document";
56+
}
57+
58+
const ipfsPattern = /(?:ipfs:\/\/|https?:\/\/(?:[A-Za-z0-9.-]+)\/ipfs\/)([A-Za-z0-9]+[A-Za-z0-9\-_]*)\/?(.*)/;
59+
60+
const match = url.match(ipfsPattern);
61+
62+
if (match) {
63+
const ipfsHash = match[1];
64+
const path = match[2] || "";
65+
66+
const sanitizedPath = path.replace(/\//g, "_");
67+
68+
return `ipfs-${ipfsHash}${sanitizedPath ? `_${sanitizedPath}` : ""}`;
69+
} else {
70+
return "document";
71+
}
72+
};
73+
5374
export default FileViewer;

0 commit comments

Comments
 (0)