Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit cdac3f9

Browse files
authored
Merge pull request #6404 from SimonBrandner/fix/file-label-behaviour
2 parents e4c6833 + e876073 commit cdac3f9

File tree

5 files changed

+66
-36
lines changed

5 files changed

+66
-36
lines changed

src/components/views/messages/MFileBody.tsx

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { replaceableComponent } from "../../../utils/replaceableComponent";
2323
import { mediaFromContent } from "../../../customisations/Media";
2424
import ErrorDialog from "../dialogs/ErrorDialog";
2525
import { TileShape } from "../rooms/EventTile";
26-
import { IContent } from "matrix-js-sdk/src";
26+
import { presentableTextForFile } from "../../../utils/FileUtils";
2727
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
2828
import { IBodyProps } from "./IBodyProps";
2929

@@ -93,35 +93,6 @@ export function computedStyle(element: HTMLElement) {
9393
return cssText;
9494
}
9595

96-
/**
97-
* Extracts a human readable label for the file attachment to use as
98-
* link text.
99-
*
100-
* @param {Object} content The "content" key of the matrix event.
101-
* @param {boolean} withSize Whether to include size information. Default true.
102-
* @return {string} the human readable link text for the attachment.
103-
*/
104-
export function presentableTextForFile(content: IContent, withSize = true): string {
105-
let linkText = _t("Attachment");
106-
if (content.body && content.body.length > 0) {
107-
// The content body should be the name of the file including a
108-
// file extension.
109-
linkText = content.body;
110-
}
111-
112-
if (content.info && content.info.size && withSize) {
113-
// If we know the size of the file then add it as human readable
114-
// string to the end of the link text so that the user knows how
115-
// big a file they are downloading.
116-
// The content.info also contains a MIME-type but we don't display
117-
// it since it is "ugly", users generally aren't aware what it
118-
// means and the type of the attachment can usually be inferrered
119-
// from the file extension.
120-
linkText += ' (' + filesize(content.info.size) + ')';
121-
}
122-
return linkText;
123-
}
124-
12596
interface IProps extends IBodyProps {
12697
/* whether or not to show the default placeholder for the file. Defaults to true. */
12798
showGenericPlaceholder: boolean;
@@ -173,7 +144,7 @@ export default class MFileBody extends React.Component<IProps, IState> {
173144
<div className="mx_MFileBody_info">
174145
<span className="mx_MFileBody_info_icon" />
175146
<span className="mx_MFileBody_info_filename">
176-
{ presentableTextForFile(content, false) }
147+
{ presentableTextForFile(content, _t("Attachment"), false) }
177148
</span>
178149
</div>
179150
);

src/components/views/messages/MImageBody.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export default class MImageBody extends React.Component<IBodyProps, IState> {
404404
}
405405

406406
// Overidden by MStickerBody
407-
protected getFileBody(): JSX.Element {
407+
protected getFileBody(): string | JSX.Element {
408408
// We only ever need the download bar if we're appearing outside of the timeline
409409
if (this.props.tileShape) {
410410
return <MFileBody {...this.props} showGenericPlaceholder={false} />;

src/components/views/messages/MImageReplyBody.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ limitations under the License.
1616

1717
import React from "react";
1818
import MImageBody from "./MImageBody";
19-
import { presentableTextForFile } from "./MFileBody";
19+
import { presentableTextForFile } from "../../../utils/FileUtils";
2020
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
2121
import SenderProfile from "./SenderProfile";
22+
import { EventType } from "matrix-js-sdk/src/@types/event";
23+
import { _t } from "../../../languageHandler";
2224

2325
const FORCED_IMAGE_HEIGHT = 44;
2426

@@ -32,8 +34,9 @@ export default class MImageReplyBody extends MImageBody {
3234
}
3335

3436
// Don't show "Download this_file.png ..."
35-
public getFileBody(): JSX.Element {
36-
return <>{ presentableTextForFile(this.props.mxEvent.getContent()) }</>;
37+
public getFileBody(): string {
38+
const sticker = this.props.mxEvent.getType() === EventType.Sticker;
39+
return presentableTextForFile(this.props.mxEvent.getContent(), sticker ? _t("Sticker") : _t("Image"), !sticker);
3740
}
3841

3942
render() {

src/i18n/strings/en_EN.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@
657657
"This homeserver has exceeded one of its resource limits.": "This homeserver has exceeded one of its resource limits.",
658658
"Please <a>contact your service administrator</a> to continue using the service.": "Please <a>contact your service administrator</a> to continue using the service.",
659659
"Unable to connect to Homeserver. Retrying...": "Unable to connect to Homeserver. Retrying...",
660+
"Attachment": "Attachment",
660661
"%(items)s and %(count)s others|other": "%(items)s and %(count)s others",
661662
"%(items)s and %(count)s others|one": "%(items)s and one other",
662663
"%(items)s and %(lastItem)s": "%(items)s and %(lastItem)s",
@@ -1878,13 +1879,14 @@
18781879
"Retry": "Retry",
18791880
"Reply": "Reply",
18801881
"Message Actions": "Message Actions",
1881-
"Attachment": "Attachment",
18821882
"Error decrypting attachment": "Error decrypting attachment",
18831883
"Decrypt %(text)s": "Decrypt %(text)s",
18841884
"Download %(text)s": "Download %(text)s",
18851885
"Invalid file%(extra)s": "Invalid file%(extra)s",
18861886
"Error decrypting image": "Error decrypting image",
18871887
"Show image": "Show image",
1888+
"Sticker": "Sticker",
1889+
"Image": "Image",
18881890
"Join the conference at the top of this room": "Join the conference at the top of this room",
18891891
"Join the conference from the room information card on the right": "Join the conference from the room information card on the right",
18901892
"Video conference ended by %(senderName)s": "Video conference ended by %(senderName)s",

src/utils/FileUtils.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2015 - 2021 The Matrix.org Foundation C.I.C.
3+
Copyright 2021 Šimon Brandner <[email protected]>
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
import filesize from 'filesize';
19+
import { IMediaEventContent } from '../customisations/models/IMediaEventContent';
20+
import { _t } from '../languageHandler';
21+
22+
/**
23+
* Extracts a human readable label for the file attachment to use as
24+
* link text.
25+
*
26+
* @param {IMediaEventContent} content The "content" key of the matrix event.
27+
* @param {string} fallbackText The fallback text
28+
* @param {boolean} withSize Whether to include size information. Default true.
29+
* @return {string} the human readable link text for the attachment.
30+
*/
31+
export function presentableTextForFile(
32+
content: IMediaEventContent,
33+
fallbackText = _t("Attachment"),
34+
withSize = true,
35+
): string {
36+
let text = fallbackText;
37+
if (content.body && content.body.length > 0) {
38+
// The content body should be the name of the file including a
39+
// file extension.
40+
text = content.body;
41+
}
42+
43+
if (content.info && content.info.size && withSize) {
44+
// If we know the size of the file then add it as human readable
45+
// string to the end of the link text so that the user knows how
46+
// big a file they are downloading.
47+
// The content.info also contains a MIME-type but we don't display
48+
// it since it is "ugly", users generally aren't aware what it
49+
// means and the type of the attachment can usually be inferrered
50+
// from the file extension.
51+
text += ' (' + filesize(content.info.size) + ')';
52+
}
53+
return text;
54+
}

0 commit comments

Comments
 (0)