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
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-04-01 16:42:41 +01:00
|
|
|
*/
|
|
|
|
|
|
2025-03-27 10:43:58 +00:00
|
|
|
import React, { type JSX } from "react";
|
2024-11-14 12:34:27 +00:00
|
|
|
import { FilesIcon } from "@vector-im/compound-design-tokens/assets/web/icons";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
|
|
|
|
import { _t } from "../../../languageHandler";
|
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
|
|
|
|
2025-12-01 16:00:09 +00:00
|
|
|
interface IState {
|
|
|
|
|
objectUrl?: string;
|
|
|
|
|
}
|
2019-04-01 16:42:41 +01:00
|
|
|
|
2025-12-01 16:00:09 +00:00
|
|
|
export default class UploadConfirmDialog extends React.Component<IProps, IState> {
|
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);
|
|
|
|
|
|
2025-12-01 16:00:09 +00:00
|
|
|
this.state = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public componentDidMount(): void {
|
|
|
|
|
if (this.props.file.type.startsWith("image/") || this.props.file.type.startsWith("video/")) {
|
|
|
|
|
this.setState({
|
|
|
|
|
// We do not filter the mimetype using getBlobSafeMimeType here as if the user is uploading the file
|
|
|
|
|
// themselves they should be trusting it enough to open/load it, and it will be rendered into a hidden
|
|
|
|
|
// canvas for thumbnail generation anyway
|
|
|
|
|
objectUrl: URL.createObjectURL(this.props.file),
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-04-01 16:42:41 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2025-12-01 16:00:09 +00:00
|
|
|
if (this.state.objectUrl) URL.revokeObjectURL(this.state.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}`;
|
2025-12-01 16:00:09 +00:00
|
|
|
const mimeType = this.props.file.type;
|
|
|
|
|
|
2023-03-14 11:09:35 +00:00
|
|
|
let preview: JSX.Element | undefined;
|
|
|
|
|
let placeholder: JSX.Element | undefined;
|
2025-12-01 16:00:09 +00:00
|
|
|
if (mimeType.startsWith("image/")) {
|
2022-03-24 11:59:20 +00:00
|
|
|
preview = (
|
2025-12-01 16:00:09 +00:00
|
|
|
<img
|
|
|
|
|
className="mx_UploadConfirmDialog_imagePreview"
|
|
|
|
|
src={this.state.objectUrl}
|
|
|
|
|
aria-labelledby={fileId}
|
|
|
|
|
/>
|
2022-03-24 11:59:20 +00:00
|
|
|
);
|
2025-12-01 16:00:09 +00:00
|
|
|
} else if (mimeType.startsWith("video/")) {
|
2022-03-24 11:59:20 +00:00
|
|
|
preview = (
|
|
|
|
|
<video
|
|
|
|
|
className="mx_UploadConfirmDialog_imagePreview"
|
2025-12-01 16:00:09 +00:00
|
|
|
src={this.state.objectUrl}
|
2022-03-24 11:59:20 +00:00
|
|
|
playsInline
|
|
|
|
|
controls={false}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2022-12-12 12:24:14 +01:00
|
|
|
} else {
|
2024-11-14 12:34:27 +00:00
|
|
|
placeholder = <FilesIcon className="mx_UploadConfirmDialog_fileIcon" height="18px" width="18px" />;
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|