2019-04-01 16:42:41 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
|
Copyright 2019-2021 The Matrix.org Foundation C.I.C.
|
2019-06-21 09:03:28 +01:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2019-04-01 16:42:41 +01:00
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-04-01 16:42:41 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2022-07-06 13:29:26 +02:00
|
|
|
import { Icon as FileIcon } from "../../../../res/img/feather-customised/files.svg";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { _t } from "../../../languageHandler";
|
2021-05-06 14:39:44 +01:00
|
|
|
import { getBlobSafeMimeType } from "../../../utils/blobs";
|
2021-07-02 17:19:01 +02:00
|
|
|
import BaseDialog from "./BaseDialog";
|
|
|
|
|
import DialogButtons from "../elements/DialogButtons";
|
2023-04-11 19:18:41 +05:30
|
|
|
import { fileSize } from "../../../utils/FileUtils";
|
2021-05-06 14:11:34 +01:00
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
file: File;
|
2023-03-14 11:09:35 +00:00
|
|
|
currentIndex: number;
|
|
|
|
|
totalFiles: number;
|
2021-05-06 14:11:34 +01:00
|
|
|
onFinished: (uploadConfirmed: boolean, uploadAll?: boolean) => void;
|
|
|
|
|
}
|
2019-04-01 16:42:41 +01:00
|
|
|
|
2021-05-06 14:11:34 +01:00
|
|
|
export default class UploadConfirmDialog extends React.Component<IProps> {
|
2022-03-24 11:59:20 +00:00
|
|
|
private readonly objectUrl: string;
|
|
|
|
|
private readonly mimeType: string;
|
2019-04-01 16:42:41 +01:00
|
|
|
|
2023-02-28 10:31:48 +00:00
|
|
|
public static defaultProps: Partial<IProps> = {
|
2019-04-01 16:42:41 +01:00
|
|
|
totalFiles: 1,
|
2023-03-14 11:09:35 +00:00
|
|
|
currentIndex: 0,
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2019-04-01 16:42:41 +01:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(props: IProps) {
|
2019-04-01 16:42:41 +01:00
|
|
|
super(props);
|
|
|
|
|
|
2021-05-06 14:39:44 +01:00
|
|
|
// Create a fresh `Blob` for previewing (even though `File` already is
|
|
|
|
|
// one) so we can adjust the MIME type if needed.
|
|
|
|
|
this.mimeType = getBlobSafeMimeType(props.file.type);
|
|
|
|
|
const blob = new Blob([props.file], { type: this.mimeType });
|
|
|
|
|
this.objectUrl = URL.createObjectURL(blob);
|
2019-04-01 16:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2021-05-06 14:11:34 +01:00
|
|
|
if (this.objectUrl) URL.revokeObjectURL(this.objectUrl);
|
2019-04-01 16:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onCancelClick = (): void => {
|
2019-04-01 16:42:41 +01:00
|
|
|
this.props.onFinished(false);
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2019-04-01 16:42:41 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onUploadClick = (): void => {
|
2019-04-01 16:42:41 +01:00
|
|
|
this.props.onFinished(true);
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2019-04-01 16:42:41 +01:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onUploadAllClick = (): void => {
|
2019-06-16 11:43:13 +01:00
|
|
|
this.props.onFinished(true, true);
|
2021-06-29 13:11:58 +01:00
|
|
|
};
|
2019-06-16 11:43:13 +01:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2022-03-24 11:59:20 +00:00
|
|
|
let title: string;
|
2019-04-01 16:42:41 +01:00
|
|
|
if (this.props.totalFiles > 1 && this.props.currentIndex !== undefined) {
|
2023-10-03 19:17:26 +01:00
|
|
|
title = _t("upload_file|title_progress", {
|
2019-04-01 16:52:06 +01:00
|
|
|
current: this.props.currentIndex + 1,
|
2019-04-01 16:42:41 +01:00
|
|
|
total: this.props.totalFiles,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2023-10-03 19:17:26 +01:00
|
|
|
title = _t("upload_file|title");
|
2019-04-01 16:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-06 13:29:26 +02:00
|
|
|
const fileId = `mx-uploadconfirmdialog-${this.props.file.name}`;
|
2023-03-14 11:09:35 +00:00
|
|
|
let preview: JSX.Element | undefined;
|
|
|
|
|
let placeholder: JSX.Element | undefined;
|
2022-03-24 11:59:20 +00:00
|
|
|
if (this.mimeType.startsWith("image/")) {
|
|
|
|
|
preview = (
|
2022-07-06 13:29:26 +02:00
|
|
|
<img className="mx_UploadConfirmDialog_imagePreview" src={this.objectUrl} aria-labelledby={fileId} />
|
2022-03-24 11:59:20 +00:00
|
|
|
);
|
|
|
|
|
} else if (this.mimeType.startsWith("video/")) {
|
|
|
|
|
preview = (
|
|
|
|
|
<video
|
|
|
|
|
className="mx_UploadConfirmDialog_imagePreview"
|
|
|
|
|
src={this.objectUrl}
|
|
|
|
|
playsInline
|
|
|
|
|
controls={false}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2022-12-12 12:24:14 +01:00
|
|
|
} else {
|
2022-03-24 11:59:20 +00:00
|
|
|
placeholder = <FileIcon className="mx_UploadConfirmDialog_fileIcon" height={18} width={18} />;
|
2019-04-01 16:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-14 11:09:35 +00:00
|
|
|
let uploadAllButton: JSX.Element | undefined;
|
2019-06-16 11:43:13 +01:00
|
|
|
if (this.props.currentIndex + 1 < this.props.totalFiles) {
|
2023-10-03 19:17:26 +01:00
|
|
|
uploadAllButton = <button onClick={this.onUploadAllClick}>{_t("upload_file|upload_all_button")}</button>;
|
2019-06-16 11:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-01 16:42:41 +01:00
|
|
|
return (
|
2022-03-24 11:59:20 +00:00
|
|
|
<BaseDialog
|
|
|
|
|
className="mx_UploadConfirmDialog"
|
2019-04-03 16:27:45 +01:00
|
|
|
fixedWidth={false}
|
2021-05-06 14:11:34 +01:00
|
|
|
onFinished={this.onCancelClick}
|
2019-04-01 16:42:41 +01:00
|
|
|
title={title}
|
2022-03-24 11:59:20 +00:00
|
|
|
contentId="mx_Dialog_content"
|
2019-04-01 16:42:41 +01:00
|
|
|
>
|
2022-03-24 11:59:20 +00:00
|
|
|
<div id="mx_Dialog_content">
|
|
|
|
|
<div className="mx_UploadConfirmDialog_previewOuter">
|
|
|
|
|
<div className="mx_UploadConfirmDialog_previewInner">
|
|
|
|
|
{preview && <div>{preview}</div>}
|
2022-07-06 13:29:26 +02:00
|
|
|
<div id={fileId}>
|
2022-03-24 11:59:20 +00:00
|
|
|
{placeholder}
|
2023-04-11 19:18:41 +05:30
|
|
|
{this.props.file.name} ({fileSize(this.props.file.size)})
|
2022-03-24 11:59:20 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2019-04-01 16:42:41 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<DialogButtons
|
2023-08-23 11:57:22 +01:00
|
|
|
primaryButton={_t("action|upload")}
|
2019-04-01 16:42:41 +01:00
|
|
|
hasCancel={false}
|
2021-05-06 14:11:34 +01:00
|
|
|
onPrimaryButtonClick={this.onUploadClick}
|
2019-04-01 16:42:41 +01:00
|
|
|
focus={true}
|
2019-06-16 11:43:13 +01:00
|
|
|
>
|
2021-07-19 22:43:11 +01:00
|
|
|
{uploadAllButton}
|
2019-06-16 11:43:13 +01:00
|
|
|
</DialogButtons>
|
2019-04-01 16:42:41 +01:00
|
|
|
</BaseDialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|