Skip to content

Commit

Permalink
Add download option for non image media
Browse files Browse the repository at this point in the history
  • Loading branch information
beastafk committed Oct 3, 2024
1 parent a15dad4 commit f1c4f59
Showing 1 changed file with 42 additions and 27 deletions.
69 changes: 42 additions & 27 deletions src/components/media.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { get } from "lodash";
import { useState } from "react";

import Typography from "@mui/material/Typography";
import BlockIcon from "@mui/icons-material/Block";
import IconCancel from "@mui/icons-material/Cancel";
import ClearIcon from "@mui/icons-material/Clear";
import DeleteSweepIcon from "@mui/icons-material/DeleteSweep";
import FileOpenIcon from "@mui/icons-material/FileOpen";
import LockIcon from "@mui/icons-material/Lock";
import LockOpenIcon from "@mui/icons-material/LockOpen";
import IconButton from '@mui/material/IconButton';
import CloseIcon from '@mui/icons-material/Close';
import ZoomInIcon from '@mui/icons-material/ZoomIn';
import { Box, Dialog, DialogContent, DialogContentText, DialogTitle, Tooltip, Link } from "@mui/material";
import DownloadIcon from '@mui/icons-material/Download';
import { Grid2 as Grid, Box, Dialog, DialogContent, DialogContentText, DialogTitle, Tooltip, Link } from "@mui/material";
import { alpha, useTheme } from "@mui/material/styles";
import {
BooleanInput,
Expand Down Expand Up @@ -314,17 +311,9 @@ export const QuarantineMediaButton = (props: ButtonProps) => {
);
};

export const ViewMediaButton = ({ mxcURL, label, mimetype }) => {
if (!mimetype.startsWith("image/")) {
return (
<>
<Box style={{ whiteSpace: "pre" }}>
{label}
</Box>
</>
);
}
export const ViewMediaButton = ({ mxcURL, label, uploadName, mimetype }) => {
const translate = useTranslate();
const isImage = mimetype && mimetype.startsWith("image/");

const openFileInNewTab = (blobURL: string) => {
const anchorElement = document.createElement("a");
Expand All @@ -336,27 +325,50 @@ export const ViewMediaButton = ({ mxcURL, label, mimetype }) => {
setTimeout(() => URL.revokeObjectURL(blobURL), 10);
};

const previewFile = async () => {
const downloadFile = async (blobURL: string) => {
console.log("downloadFile", blobURL, uploadName);
const anchorElement = document.createElement("a");
anchorElement.href = blobURL;
anchorElement.download = uploadName;
document.body.appendChild(anchorElement);
anchorElement.click();
document.body.removeChild(anchorElement);
setTimeout(() => URL.revokeObjectURL(blobURL), 10);;
};

const handleFile = async (preview: boolean) => {
const response = await fetchAuthenticatedMedia(mxcURL, "original");
const blob = await response.blob();
const blobURL = URL.createObjectURL(blob);
openFileInNewTab(blobURL);
if (preview) {
openFileInNewTab(blobURL);
} else {
downloadFile(blobURL);
}
};

return (
<>
<Box style={{ whiteSpace: "pre" }}>
<Box display="flex" alignItems="center">
<Tooltip title={translate("resources.users_media.action.open")}>
<span>
<Button
onClick={() => previewFile()}
style={{ minWidth: 0, paddingLeft: 0, paddingRight: 0 }}
>
<FileOpenIcon />
</Button>
{isImage && (
<Button
onClick={() => handleFile(true)}
style={{ minWidth: 0, padding: 0, marginRight: 8 }}
>
<FileOpenIcon />
</Button>
)}
</span>
</Tooltip>
{label}
<Button
onClick={() => handleFile(false)}
style={{ minWidth: 0, padding: 0, marginRight: 8 }}
>
<DownloadIcon />
</Button>
<span>{label}</span>
</Box>
</>
);
Expand All @@ -374,9 +386,10 @@ export const MediaIDField = ({ source }) => {
return null;
}

const uploadName = decodeURIComponent(get(record, "upload_name")?.toString());
const mxcURL = `mxc://${homeserver}/${mediaID}`;

return <ViewMediaButton mxcURL={mxcURL} label={mediaID} mimetype={record.media_type}/>;
return <ViewMediaButton mxcURL={mxcURL} label={mediaID} uploadName={uploadName} mimetype={record.media_type}/>;
};

export const ReportMediaContent = ({ source }) => {
Expand All @@ -390,5 +403,7 @@ export const ReportMediaContent = ({ source }) => {
return null;
}

return <ViewMediaButton mxcURL={mxcURL} label={mxcURL} mimetype={record.media_type}/>;
const uploadName = decodeURIComponent(get(record, "event_json.content.body")?.toString());

return <ViewMediaButton mxcURL={mxcURL} label={mxcURL} uploadName={uploadName} mimetype={record.media_type}/>;
};

0 comments on commit f1c4f59

Please sign in to comment.