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

Commit ce0bd00

Browse files
NSV1991neerajv96
andauthored
fix file size display from kB to KB (#10561)
* fix file size display from kB to KB * add a wrapper function for filesize --------- Co-authored-by: Neeraj <[email protected]>
1 parent 8a15025 commit ce0bd00

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

src/components/structures/UploadBar.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616

1717
import React from "react";
1818
import { Room } from "matrix-js-sdk/src/models/room";
19-
import { filesize } from "filesize";
2019
import { IEventRelation } from "matrix-js-sdk/src/matrix";
2120
import { Optional } from "matrix-events-sdk";
2221

@@ -29,6 +28,7 @@ import AccessibleButton, { ButtonEvent } from "../views/elements/AccessibleButto
2928
import { RoomUpload } from "../../models/RoomUpload";
3029
import { ActionPayload } from "../../dispatcher/payloads";
3130
import { UploadPayload } from "../../dispatcher/payloads/UploadPayload";
31+
import { fileSize } from "../../utils/FileUtils";
3232

3333
interface IProps {
3434
room: Room;
@@ -114,7 +114,7 @@ export default class UploadBar extends React.PureComponent<IProps, IState> {
114114
count: this.state.countFiles - 1,
115115
});
116116

117-
const uploadSize = filesize(this.state.currentTotal!);
117+
const uploadSize = fileSize(this.state.currentTotal!);
118118
return (
119119
<div className="mx_UploadBar">
120120
<div className="mx_UploadBar_filename">

src/components/views/dialogs/UploadConfirmDialog.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ limitations under the License.
1616
*/
1717

1818
import React from "react";
19-
import { filesize } from "filesize";
2019

2120
import { Icon as FileIcon } from "../../../../res/img/feather-customised/files.svg";
2221
import { _t } from "../../../languageHandler";
2322
import { getBlobSafeMimeType } from "../../../utils/blobs";
2423
import BaseDialog from "./BaseDialog";
2524
import DialogButtons from "../elements/DialogButtons";
25+
import { fileSize } from "../../../utils/FileUtils";
2626

2727
interface IProps {
2828
file: File;
@@ -116,7 +116,7 @@ export default class UploadConfirmDialog extends React.Component<IProps> {
116116
{preview && <div>{preview}</div>}
117117
<div id={fileId}>
118118
{placeholder}
119-
{this.props.file.name} ({filesize(this.props.file.size)})
119+
{this.props.file.name} ({fileSize(this.props.file.size)})
120120
</div>
121121
</div>
122122
</div>

src/components/views/dialogs/UploadFailureDialog.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { filesize } from "filesize";
1817
import React from "react";
1918

2019
import { _t } from "../../../languageHandler";
2120
import ContentMessages from "../../../ContentMessages";
2221
import BaseDialog from "./BaseDialog";
2322
import DialogButtons from "../elements/DialogButtons";
23+
import { fileSize } from "../../../utils/FileUtils";
2424

2525
interface IProps {
2626
badFiles: File[];
@@ -52,8 +52,8 @@ export default class UploadFailureDialog extends React.Component<IProps> {
5252
"This file is <b>too large</b> to upload. " +
5353
"The file size limit is %(limit)s but this file is %(sizeOfThisFile)s.",
5454
{
55-
limit: filesize(this.props.contentMessages.getUploadLimit()),
56-
sizeOfThisFile: filesize(this.props.badFiles[0].size),
55+
limit: fileSize(this.props.contentMessages.getUploadLimit()),
56+
sizeOfThisFile: fileSize(this.props.badFiles[0].size),
5757
},
5858
{
5959
b: (sub) => <b>{sub}</b>,
@@ -71,7 +71,7 @@ export default class UploadFailureDialog extends React.Component<IProps> {
7171
message = _t(
7272
"These files are <b>too large</b> to upload. " + "The file size limit is %(limit)s.",
7373
{
74-
limit: filesize(this.props.contentMessages.getUploadLimit()),
74+
limit: fileSize(this.props.contentMessages.getUploadLimit()),
7575
},
7676
{
7777
b: (sub) => <b>{sub}</b>,
@@ -89,7 +89,7 @@ export default class UploadFailureDialog extends React.Component<IProps> {
8989
message = _t(
9090
"Some files are <b>too large</b> to be uploaded. " + "The file size limit is %(limit)s.",
9191
{
92-
limit: filesize(this.props.contentMessages.getUploadLimit()),
92+
limit: fileSize(this.props.contentMessages.getUploadLimit()),
9393
},
9494
{
9595
b: (sub) => <b>{sub}</b>,

src/components/views/messages/MFileBody.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ limitations under the License.
1515
*/
1616

1717
import React, { AllHTMLAttributes, createRef } from "react";
18-
import { filesize } from "filesize";
1918
import { logger } from "matrix-js-sdk/src/logger";
2019

2120
import { _t } from "../../../languageHandler";
2221
import Modal from "../../../Modal";
2322
import AccessibleButton from "../elements/AccessibleButton";
2423
import { mediaFromContent } from "../../../customisations/Media";
2524
import ErrorDialog from "../dialogs/ErrorDialog";
26-
import { presentableTextForFile } from "../../../utils/FileUtils";
25+
import { fileSize, presentableTextForFile } from "../../../utils/FileUtils";
2726
import { IMediaEventContent } from "../../../customisations/models/IMediaEventContent";
2827
import { IBodyProps } from "./IBodyProps";
2928
import { FileDownloader } from "../../../utils/FileDownloader";
@@ -198,7 +197,7 @@ export default class MFileBody extends React.Component<IProps, IState> {
198197
public render(): React.ReactNode {
199198
const isEncrypted = this.props.mediaEventHelper?.media.isEncrypted;
200199
const contentUrl = this.getContentUrl();
201-
const fileSize = this.content.info ? this.content.info.size : null;
200+
const contentFileSize = this.content.info ? this.content.info.size : null;
202201
const fileType = this.content.info ? this.content.info.mimetype : "application/octet-stream";
203202

204203
let placeholder: React.ReactNode = null;
@@ -310,7 +309,7 @@ export default class MFileBody extends React.Component<IProps, IState> {
310309
// we won't try and convert it. Likewise, if the file size is unknown then we'll assume
311310
// it is too big. There is the risk of the reported file size and the actual file size
312311
// being different, however the user shouldn't normally run into this problem.
313-
const fileTooBig = typeof fileSize === "number" ? fileSize > 524288000 : true;
312+
const fileTooBig = typeof contentFileSize === "number" ? contentFileSize > 524288000 : true;
314313

315314
if (["application/pdf"].includes(fileType) && !fileTooBig) {
316315
// We want to force a download on this type, so use an onClick handler.
@@ -351,7 +350,7 @@ export default class MFileBody extends React.Component<IProps, IState> {
351350
</a>
352351
{this.context.timelineRenderingType === TimelineRenderingType.File && (
353352
<div className="mx_MImageBody_size">
354-
{this.content.info?.size ? filesize(this.content.info.size) : ""}
353+
{this.content.info?.size ? fileSize(this.content.info.size) : ""}
355354
</div>
356355
)}
357356
</div>

src/utils/FileUtils.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,26 @@ export function presentableTextForFile(
6969
// it since it is "ugly", users generally aren't aware what it
7070
// means and the type of the attachment can usually be inferred
7171
// from the file extension.
72-
text += " (" + <string>filesize(content.info.size) + ")";
72+
text += " (" + <string>fileSize(content.info.size, { base: 2, standard: "jedec" }) + ")";
7373
}
7474
return text;
7575
}
76+
77+
/**
78+
* wrapper function to set default values for filesize function
79+
*
80+
* @param size size of file
81+
* @param options options to customize the response type or size type conversion e.g. 12kB, 12KB
82+
* @returns {string | number | any[] | {
83+
* value: any;
84+
* symbol: any;
85+
* exponent: number;
86+
* unit: string;}} formatted file size with unit e.g. 12kB, 12KB
87+
*/
88+
export function fileSize(
89+
size: Parameters<typeof filesize>[0],
90+
options?: Parameters<typeof filesize>[1],
91+
): ReturnType<typeof filesize> {
92+
const defaultOption: Parameters<typeof filesize>[1] = { base: 2, standard: "jedec", ...options };
93+
return filesize(size, defaultOption);
94+
}

0 commit comments

Comments
 (0)