Skip to content

Commit 308f892

Browse files
authored
Ensure consistent download file name on download from ImageView (#29913)
* Ensure consistent download file name on download from ImageView Signed-off-by: Michael Telatynski <[email protected]> * Ensure consistent download file name on download from ImageView Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 54a00ba commit 308f892

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/components/views/elements/ImageView.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
88
Please see LICENSE files in the repository root for full details.
99
*/
1010

11-
import React, { type JSX, createRef, type CSSProperties, useRef, useState } from "react";
11+
import React, { type JSX, createRef, type CSSProperties, useRef, useState, useMemo } from "react";
1212
import FocusLock from "react-focus-lock";
1313
import { type MatrixEvent, parseErrorResponse } from "matrix-js-sdk/src/matrix";
1414

@@ -33,6 +33,7 @@ import AccessibleButton from "./AccessibleButton";
3333
import Modal from "../../../Modal";
3434
import ErrorDialog from "../dialogs/ErrorDialog";
3535
import { FileDownloader } from "../../../utils/FileDownloader";
36+
import { MediaEventHelper } from "../../../utils/MediaEventHelper.ts";
3637

3738
// Max scale to keep gaps around the image
3839
const MAX_SCALE = 0.95;
@@ -549,7 +550,7 @@ export default class ImageView extends React.Component<IProps, IState> {
549550
title={_t("lightbox|rotate_right")}
550551
onClick={this.onRotateClockwiseClick}
551552
/>
552-
<DownloadButton url={this.props.src} fileName={this.props.name} />
553+
<DownloadButton url={this.props.src} fileName={this.props.name} mxEvent={this.props.mxEvent} />
553554
{contextMenuButton}
554555
<AccessibleButton
555556
className="mx_ImageView_button mx_ImageView_button_close"
@@ -582,10 +583,19 @@ export default class ImageView extends React.Component<IProps, IState> {
582583
}
583584
}
584585

585-
function DownloadButton({ url, fileName }: { url: string; fileName?: string }): JSX.Element {
586+
function DownloadButton({
587+
url,
588+
fileName,
589+
mxEvent,
590+
}: {
591+
url: string;
592+
fileName?: string;
593+
mxEvent?: MatrixEvent;
594+
}): JSX.Element {
586595
const downloader = useRef(new FileDownloader()).current;
587596
const [loading, setLoading] = useState(false);
588597
const blobRef = useRef<Blob>(undefined);
598+
const mediaEventHelper = useMemo(() => (mxEvent ? new MediaEventHelper(mxEvent) : undefined), [mxEvent]);
589599

590600
function showError(e: unknown): void {
591601
Modal.createDialog(ErrorDialog, {
@@ -625,7 +635,7 @@ function DownloadButton({ url, fileName }: { url: string; fileName?: string }):
625635
async function downloadBlob(blob: Blob): Promise<void> {
626636
await downloader.download({
627637
blob,
628-
name: fileName ?? _t("common|image"),
638+
name: mediaEventHelper?.fileName ?? fileName ?? _t("common|image"),
629639
});
630640
setLoading(false);
631641
}

0 commit comments

Comments
 (0)