2015-07-08 15:34:26 +02:00
|
|
|
/*
|
2016-01-07 05:06:39 +01:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-04-01 17:42:41 +02:00
|
|
|
Copyright 2019 New Vector Ltd
|
2020-05-24 16:47:52 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2015-07-08 15:34:26 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
2021-10-28 10:40:38 +02:00
|
|
|
import { IUploadOpts } from "matrix-js-sdk/src/@types/requests";
|
2022-03-11 10:04:22 +01:00
|
|
|
import { MsgType } from "matrix-js-sdk/src/@types/event";
|
2022-04-05 19:29:27 +02:00
|
|
|
import encrypt from "matrix-encrypt-attachment";
|
2021-12-09 10:10:23 +01:00
|
|
|
import extractPngChunks from "png-chunks-extract";
|
|
|
|
import { IAbortablePromise, IImageInfo } from "matrix-js-sdk/src/@types/partials";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-03-21 13:03:59 +01:00
|
|
|
import { IEventRelation, ISendEventResponse, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2022-03-11 10:04:22 +01:00
|
|
|
import { THREAD_RELATION_TYPE } from "matrix-js-sdk/src/models/thread";
|
2021-05-21 15:52:27 +02:00
|
|
|
|
2021-12-09 10:10:23 +01:00
|
|
|
import { IEncryptedFile, IMediaEventInfo } from "./customisations/models/IMediaEventContent";
|
2020-05-14 04:41:41 +02:00
|
|
|
import dis from './dispatcher/dispatcher';
|
2017-05-25 12:39:08 +02:00
|
|
|
import { _t } from './languageHandler';
|
2019-04-01 17:42:41 +02:00
|
|
|
import Modal from './Modal';
|
2020-06-01 18:05:53 +02:00
|
|
|
import Spinner from "./components/views/elements/Spinner";
|
2020-06-03 03:07:46 +02:00
|
|
|
import { Action } from "./dispatcher/actions";
|
2021-03-05 21:20:50 +01:00
|
|
|
import {
|
|
|
|
UploadCanceledPayload,
|
|
|
|
UploadErrorPayload,
|
|
|
|
UploadFinishedPayload,
|
|
|
|
UploadProgressPayload,
|
|
|
|
UploadStartedPayload,
|
|
|
|
} from "./dispatcher/payloads/UploadPayload";
|
2021-06-29 14:11:58 +02:00
|
|
|
import { IUpload } from "./models/IUpload";
|
2021-09-08 19:26:54 +02:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
|
|
|
import { decorateStartSendingTime, sendRoundTripMetric } from "./sendTimePerformanceMetrics";
|
2022-01-26 10:04:19 +01:00
|
|
|
import { TimelineRenderingType } from "./contexts/RoomContext";
|
2022-03-23 06:26:30 +01:00
|
|
|
import { RoomViewStore } from "./stores/RoomViewStore";
|
2022-03-21 13:03:59 +01:00
|
|
|
import { addReplyToMessageContent } from "./utils/Reply";
|
2022-03-03 00:33:40 +01:00
|
|
|
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
|
|
|
|
import UploadFailureDialog from "./components/views/dialogs/UploadFailureDialog";
|
|
|
|
import UploadConfirmDialog from "./components/views/dialogs/UploadConfirmDialog";
|
2022-03-24 22:13:11 +01:00
|
|
|
import { createThumbnail } from "./utils/image-media";
|
2022-03-31 19:40:35 +02:00
|
|
|
import { attachRelation } from "./components/views/rooms/SendMessageComposer";
|
2016-11-15 12:22:39 +01:00
|
|
|
|
2019-01-14 18:10:20 +01:00
|
|
|
// scraped out of a macOS hidpi (5660ppm) screenshot png
|
|
|
|
// 5669 px (x-axis) , 5669 px (y-axis) , per metre
|
|
|
|
const PHYS_HIDPI = [0x00, 0x00, 0x16, 0x25, 0x00, 0x00, 0x16, 0x25, 0x01];
|
2016-11-15 12:22:39 +01:00
|
|
|
|
2019-04-08 20:07:17 +02:00
|
|
|
export class UploadCanceledError extends Error {}
|
2016-11-15 12:22:39 +01:00
|
|
|
|
2020-05-24 16:47:52 +02:00
|
|
|
interface IMediaConfig {
|
|
|
|
"m.upload.size"?: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IContent {
|
|
|
|
body: string;
|
|
|
|
msgtype: string;
|
|
|
|
info: {
|
|
|
|
size: number;
|
|
|
|
mimetype?: string;
|
|
|
|
};
|
|
|
|
file?: string;
|
|
|
|
url?: string;
|
|
|
|
}
|
|
|
|
|
2016-11-15 12:22:39 +01:00
|
|
|
/**
|
|
|
|
* Load a file into a newly created image element.
|
|
|
|
*
|
2019-04-01 17:42:41 +02:00
|
|
|
* @param {File} imageFile The file to load in an image element.
|
2016-11-15 12:22:39 +01:00
|
|
|
* @return {Promise} A promise that resolves with the html image element.
|
|
|
|
*/
|
2020-05-24 16:47:52 +02:00
|
|
|
async function loadImageElement(imageFile: File) {
|
2015-07-08 15:34:26 +02:00
|
|
|
// Load the file into an html element
|
2016-11-15 12:22:39 +01:00
|
|
|
const img = document.createElement("img");
|
2017-10-19 18:16:52 +02:00
|
|
|
const objectUrl = URL.createObjectURL(imageFile);
|
2019-04-09 12:32:44 +02:00
|
|
|
const imgPromise = new Promise((resolve, reject) => {
|
|
|
|
img.onload = function() {
|
|
|
|
URL.revokeObjectURL(objectUrl);
|
|
|
|
resolve(img);
|
|
|
|
};
|
|
|
|
img.onerror = function(e) {
|
|
|
|
reject(e);
|
|
|
|
};
|
|
|
|
});
|
2017-10-19 18:16:52 +02:00
|
|
|
img.src = objectUrl;
|
2015-07-08 15:34:26 +02:00
|
|
|
|
2019-01-14 18:10:20 +01:00
|
|
|
// check for hi-dpi PNGs and fudge display resolution as needed.
|
|
|
|
// this is mainly needed for macOS screencaps
|
2019-04-09 12:32:44 +02:00
|
|
|
let parsePromise;
|
2019-01-14 18:10:20 +01:00
|
|
|
if (imageFile.type === "image/png") {
|
|
|
|
// in practice macOS happens to order the chunks so they fall in
|
|
|
|
// the first 0x1000 bytes (thanks to a massive ICC header).
|
|
|
|
// Thus we could slice the file down to only sniff the first 0x1000
|
2019-04-09 12:18:06 +02:00
|
|
|
// bytes (but this makes extractPngChunks choke on the corrupt file)
|
2019-01-14 18:10:20 +01:00
|
|
|
const headers = imageFile; //.slice(0, 0x1000);
|
2019-04-09 12:32:44 +02:00
|
|
|
parsePromise = readFileAsArrayBuffer(headers).then(arrayBuffer => {
|
2019-01-14 18:10:20 +01:00
|
|
|
const buffer = new Uint8Array(arrayBuffer);
|
2019-04-09 12:18:06 +02:00
|
|
|
const chunks = extractPngChunks(buffer);
|
2019-01-14 18:10:20 +01:00
|
|
|
for (const chunk of chunks) {
|
|
|
|
if (chunk.name === 'pHYs') {
|
|
|
|
if (chunk.data.byteLength !== PHYS_HIDPI.length) return;
|
2020-05-24 16:47:52 +02:00
|
|
|
return chunk.data.every((val, i) => val === PHYS_HIDPI[i]);
|
2019-01-14 18:10:20 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-09 12:32:44 +02:00
|
|
|
return false;
|
2019-01-14 18:10:20 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:32:44 +02:00
|
|
|
const [hidpi] = await Promise.all([parsePromise, imgPromise]);
|
|
|
|
const width = hidpi ? (img.width >> 1) : img.width;
|
|
|
|
const height = hidpi ? (img.height >> 1) : img.height;
|
2021-06-29 14:11:58 +02:00
|
|
|
return { width, height, img };
|
2015-07-08 15:34:26 +02:00
|
|
|
}
|
|
|
|
|
2021-08-05 14:04:20 +02:00
|
|
|
// Minimum size for image files before we generate a thumbnail for them.
|
|
|
|
const IMAGE_SIZE_THRESHOLD_THUMBNAIL = 1 << 15; // 32KB
|
|
|
|
// Minimum size improvement for image thumbnails, if both are not met then don't bother uploading thumbnail.
|
|
|
|
const IMAGE_THUMBNAIL_MIN_REDUCTION_SIZE = 1 << 16; // 1MB
|
|
|
|
const IMAGE_THUMBNAIL_MIN_REDUCTION_PERCENT = 0.1; // 10%
|
|
|
|
// We don't apply these thresholds to video thumbnails as a poster image is always useful
|
|
|
|
// and videos tend to be much larger.
|
|
|
|
|
2016-11-15 12:22:39 +01:00
|
|
|
/**
|
|
|
|
* Read the metadata for an image file and create and upload a thumbnail of the image.
|
|
|
|
*
|
|
|
|
* @param {MatrixClient} matrixClient A matrixClient to upload the thumbnail with.
|
|
|
|
* @param {String} roomId The ID of the room the image will be uploaded in.
|
2019-04-01 17:42:41 +02:00
|
|
|
* @param {File} imageFile The image to read and thumbnail.
|
2016-11-15 12:22:39 +01:00
|
|
|
* @return {Promise} A promise that resolves with the attachment info.
|
|
|
|
*/
|
2021-08-05 14:04:20 +02:00
|
|
|
async function infoForImageFile(matrixClient: MatrixClient, roomId: string, imageFile: File) {
|
2017-10-11 18:56:17 +02:00
|
|
|
let thumbnailType = "image/png";
|
2020-05-24 16:47:52 +02:00
|
|
|
if (imageFile.type === "image/jpeg") {
|
2016-11-15 12:22:39 +01:00
|
|
|
thumbnailType = "image/jpeg";
|
|
|
|
}
|
|
|
|
|
2021-08-05 14:04:20 +02:00
|
|
|
const imageElement = await loadImageElement(imageFile);
|
|
|
|
|
|
|
|
const result = await createThumbnail(imageElement.img, imageElement.width, imageElement.height, thumbnailType);
|
|
|
|
const imageInfo = result.info;
|
|
|
|
|
2022-03-29 08:03:41 +02:00
|
|
|
// For lesser supported image types, always include the thumbnail even if it is larger
|
|
|
|
if (!["image/avif", "image/webp"].includes(imageFile.type)) {
|
|
|
|
// we do all sizing checks here because we still rely on thumbnail generation for making a blurhash from.
|
|
|
|
const sizeDifference = imageFile.size - imageInfo.thumbnail_info.size;
|
|
|
|
if (
|
|
|
|
// image is small enough already
|
|
|
|
imageFile.size <= IMAGE_SIZE_THRESHOLD_THUMBNAIL ||
|
|
|
|
// thumbnail is not sufficiently smaller than original
|
|
|
|
(sizeDifference <= IMAGE_THUMBNAIL_MIN_REDUCTION_SIZE &&
|
|
|
|
sizeDifference <= (imageFile.size * IMAGE_THUMBNAIL_MIN_REDUCTION_PERCENT))
|
|
|
|
) {
|
|
|
|
delete imageInfo["thumbnail_info"];
|
|
|
|
return imageInfo;
|
|
|
|
}
|
2021-08-05 14:04:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const uploadResult = await uploadFile(matrixClient, roomId, result.thumbnail);
|
|
|
|
|
|
|
|
imageInfo["thumbnail_url"] = uploadResult.url;
|
|
|
|
imageInfo["thumbnail_file"] = uploadResult.file;
|
|
|
|
return imageInfo;
|
2016-11-15 12:22:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-21 22:04:36 +02:00
|
|
|
* Load a file into a newly created video element and pull some strings
|
|
|
|
* in an attempt to guarantee the first frame will be showing.
|
2016-11-15 12:22:39 +01:00
|
|
|
*
|
2019-04-01 17:42:41 +02:00
|
|
|
* @param {File} videoFile The file to load in an video element.
|
2016-11-15 12:22:39 +01:00
|
|
|
* @return {Promise} A promise that resolves with the video image element.
|
|
|
|
*/
|
2020-05-24 16:47:52 +02:00
|
|
|
function loadVideoElement(videoFile): Promise<HTMLVideoElement> {
|
2019-11-12 12:40:38 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Load the file into an html element
|
|
|
|
const video = document.createElement("video");
|
2021-05-21 22:04:36 +02:00
|
|
|
video.preload = "metadata";
|
|
|
|
video.playsInline = true;
|
|
|
|
video.muted = true;
|
2019-11-12 12:40:38 +01:00
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
2020-05-24 16:47:52 +02:00
|
|
|
reader.onload = function(ev) {
|
2019-11-12 12:40:38 +01:00
|
|
|
// Wait until we have enough data to thumbnail the first frame.
|
2021-05-21 22:04:36 +02:00
|
|
|
video.onloadeddata = async function() {
|
2019-11-12 12:40:38 +01:00
|
|
|
resolve(video);
|
2021-05-21 22:04:36 +02:00
|
|
|
video.pause();
|
2019-11-12 12:40:38 +01:00
|
|
|
};
|
|
|
|
video.onerror = function(e) {
|
|
|
|
reject(e);
|
|
|
|
};
|
2021-05-21 22:04:36 +02:00
|
|
|
|
2022-03-22 13:23:25 +01:00
|
|
|
let dataUrl = ev.target.result as string;
|
|
|
|
// Chrome chokes on quicktime but likes mp4, and `file.type` is
|
|
|
|
// read only, so do this horrible hack to unbreak quicktime
|
|
|
|
if (dataUrl.startsWith("data:video/quicktime;")) {
|
|
|
|
dataUrl = dataUrl.replace("data:video/quicktime;", "data:video/mp4;");
|
|
|
|
}
|
|
|
|
|
|
|
|
video.src = dataUrl;
|
2021-05-21 22:04:36 +02:00
|
|
|
video.load();
|
|
|
|
video.play();
|
2016-07-19 17:05:15 +02:00
|
|
|
};
|
2019-11-12 12:40:38 +01:00
|
|
|
reader.onerror = function(e) {
|
|
|
|
reject(e);
|
2016-07-19 17:05:15 +02:00
|
|
|
};
|
2019-11-12 12:40:38 +01:00
|
|
|
reader.readAsDataURL(videoFile);
|
|
|
|
});
|
2016-07-19 17:05:15 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 12:22:39 +01:00
|
|
|
/**
|
|
|
|
* Read the metadata for a video file and create and upload a thumbnail of the video.
|
|
|
|
*
|
|
|
|
* @param {MatrixClient} matrixClient A matrixClient to upload the thumbnail with.
|
|
|
|
* @param {String} roomId The ID of the room the video will be uploaded to.
|
2019-04-01 17:42:41 +02:00
|
|
|
* @param {File} videoFile The video to read and thumbnail.
|
2016-11-15 12:22:39 +01:00
|
|
|
* @return {Promise} A promise that resolves with the attachment info.
|
|
|
|
*/
|
|
|
|
function infoForVideoFile(matrixClient, roomId, videoFile) {
|
|
|
|
const thumbnailType = "image/jpeg";
|
|
|
|
|
2021-10-28 10:40:38 +02:00
|
|
|
let videoInfo: Partial<IMediaEventInfo>;
|
2021-06-02 06:21:04 +02:00
|
|
|
return loadVideoElement(videoFile).then((video) => {
|
2016-11-15 12:22:39 +01:00
|
|
|
return createThumbnail(video, video.videoWidth, video.videoHeight, thumbnailType);
|
2021-06-02 06:21:04 +02:00
|
|
|
}).then((result) => {
|
2016-11-15 12:22:39 +01:00
|
|
|
videoInfo = result.info;
|
|
|
|
return uploadFile(matrixClient, roomId, result.thumbnail);
|
2021-06-02 06:21:04 +02:00
|
|
|
}).then((result) => {
|
2016-11-15 12:22:39 +01:00
|
|
|
videoInfo.thumbnail_url = result.url;
|
|
|
|
videoInfo.thumbnail_file = result.file;
|
|
|
|
return videoInfo;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-08 12:42:20 +01:00
|
|
|
/**
|
|
|
|
* Read the file as an ArrayBuffer.
|
2019-04-01 17:42:41 +02:00
|
|
|
* @param {File} file The file to read
|
2016-11-08 12:42:20 +01:00
|
|
|
* @return {Promise} A promise that resolves with an ArrayBuffer when the file
|
|
|
|
* is read.
|
|
|
|
*/
|
2020-05-24 16:47:52 +02:00
|
|
|
function readFileAsArrayBuffer(file: File | Blob): Promise<ArrayBuffer> {
|
2019-11-12 12:40:38 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = function(e) {
|
2020-05-24 16:47:52 +02:00
|
|
|
resolve(e.target.result as ArrayBuffer);
|
2019-11-12 12:40:38 +01:00
|
|
|
};
|
|
|
|
reader.onerror = function(e) {
|
|
|
|
reject(e);
|
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
2016-11-08 12:42:20 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 12:22:39 +01:00
|
|
|
/**
|
|
|
|
* Upload the file to the content repository.
|
|
|
|
* If the room is encrypted then encrypt the file before uploading.
|
|
|
|
*
|
|
|
|
* @param {MatrixClient} matrixClient The matrix client to upload the file with.
|
|
|
|
* @param {String} roomId The ID of the room being uploaded to.
|
|
|
|
* @param {File} file The file to upload.
|
2017-07-14 18:01:03 +02:00
|
|
|
* @param {Function?} progressHandler optional callback to be called when a chunk of
|
|
|
|
* data is uploaded.
|
2016-11-15 12:22:39 +01:00
|
|
|
* @return {Promise} A promise that resolves with an object.
|
|
|
|
* If the file is unencrypted then the object will have a "url" key.
|
|
|
|
* If the file is encrypted then the object will have a "file" key.
|
|
|
|
*/
|
2021-06-25 21:54:05 +02:00
|
|
|
export function uploadFile(
|
2021-06-02 06:21:04 +02:00
|
|
|
matrixClient: MatrixClient,
|
|
|
|
roomId: string,
|
|
|
|
file: File | Blob,
|
2021-10-28 10:40:38 +02:00
|
|
|
progressHandler?: IUploadOpts["progressHandler"],
|
|
|
|
): IAbortablePromise<{ url?: string, file?: IEncryptedFile }> {
|
2020-05-24 16:13:53 +02:00
|
|
|
let canceled = false;
|
2016-11-15 12:22:39 +01:00
|
|
|
if (matrixClient.isRoomEncrypted(roomId)) {
|
|
|
|
// If the room is encrypted then encrypt the file before uploading it.
|
|
|
|
// First read the file into memory.
|
2021-10-28 10:40:38 +02:00
|
|
|
let uploadPromise: IAbortablePromise<string>;
|
2019-04-08 18:53:39 +02:00
|
|
|
const prom = readFileAsArrayBuffer(file).then(function(data) {
|
2019-04-08 20:07:17 +02:00
|
|
|
if (canceled) throw new UploadCanceledError();
|
2016-11-15 12:22:39 +01:00
|
|
|
// Then encrypt the file.
|
|
|
|
return encrypt.encryptAttachment(data);
|
|
|
|
}).then(function(encryptResult) {
|
2019-04-08 20:07:17 +02:00
|
|
|
if (canceled) throw new UploadCanceledError();
|
2021-10-28 10:40:38 +02:00
|
|
|
|
2016-11-15 12:22:39 +01:00
|
|
|
// Pass the encrypted data as a Blob to the uploader.
|
|
|
|
const blob = new Blob([encryptResult.data]);
|
2019-04-08 18:53:39 +02:00
|
|
|
uploadPromise = matrixClient.uploadContent(blob, {
|
2021-10-28 10:40:38 +02:00
|
|
|
progressHandler,
|
2018-06-18 14:48:23 +02:00
|
|
|
includeFilename: false,
|
2016-11-15 12:22:39 +01:00
|
|
|
});
|
2021-10-28 10:40:38 +02:00
|
|
|
|
|
|
|
return uploadPromise.then(url => {
|
|
|
|
if (canceled) throw new UploadCanceledError();
|
|
|
|
|
|
|
|
// If the attachment is encrypted then bundle the URL along
|
|
|
|
// with the information needed to decrypt the attachment and
|
|
|
|
// add it under a file key.
|
|
|
|
return {
|
|
|
|
file: {
|
|
|
|
...encryptResult.info,
|
|
|
|
url,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}) as IAbortablePromise<{ file: IEncryptedFile }>;
|
2021-07-10 16:43:46 +02:00
|
|
|
prom.abort = () => {
|
2019-04-08 20:07:17 +02:00
|
|
|
canceled = true;
|
2021-05-20 14:23:17 +02:00
|
|
|
if (uploadPromise) matrixClient.cancelUpload(uploadPromise);
|
2019-04-08 18:53:39 +02:00
|
|
|
};
|
|
|
|
return prom;
|
2016-11-15 12:22:39 +01:00
|
|
|
} else {
|
2021-10-28 10:40:38 +02:00
|
|
|
const basePromise = matrixClient.uploadContent(file, { progressHandler });
|
2021-05-21 15:52:27 +02:00
|
|
|
const promise1 = basePromise.then(function(url) {
|
2020-05-24 16:13:53 +02:00
|
|
|
if (canceled) throw new UploadCanceledError();
|
2016-11-15 12:22:39 +01:00
|
|
|
// If the attachment isn't encrypted then include the URL directly.
|
2021-05-21 15:52:27 +02:00
|
|
|
return { url };
|
2021-07-10 16:43:46 +02:00
|
|
|
}) as IAbortablePromise<{ url: string }>;
|
|
|
|
promise1.abort = () => {
|
2020-05-24 16:13:53 +02:00
|
|
|
canceled = true;
|
2021-05-20 14:23:17 +02:00
|
|
|
matrixClient.cancelUpload(basePromise);
|
2020-05-24 16:13:53 +02:00
|
|
|
};
|
2016-12-16 18:11:28 +01:00
|
|
|
return promise1;
|
2016-11-15 12:22:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 17:42:41 +02:00
|
|
|
export default class ContentMessages {
|
2020-05-24 16:47:52 +02:00
|
|
|
private inprogress: IUpload[] = [];
|
|
|
|
private mediaConfig: IMediaConfig = null;
|
2019-04-01 17:42:41 +02:00
|
|
|
|
2022-01-26 10:04:19 +01:00
|
|
|
public sendStickerContentToRoom(
|
2021-12-03 09:22:13 +01:00
|
|
|
url: string,
|
|
|
|
roomId: string,
|
|
|
|
threadId: string | null,
|
|
|
|
info: IImageInfo,
|
|
|
|
text: string,
|
|
|
|
matrixClient: MatrixClient,
|
2022-01-26 10:04:19 +01:00
|
|
|
): Promise<ISendEventResponse> {
|
2021-12-03 09:22:13 +01:00
|
|
|
const prom = matrixClient.sendStickerMessage(roomId, threadId, url, info, text).catch((e) => {
|
2021-10-15 16:31:29 +02:00
|
|
|
logger.warn(`Failed to send content with URL ${url} to room ${roomId}`, e);
|
2018-03-29 17:24:03 +02:00
|
|
|
throw e;
|
2018-03-29 17:23:20 +02:00
|
|
|
});
|
2020-10-29 16:53:14 +01:00
|
|
|
return prom;
|
2018-01-04 10:53:26 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:04:19 +01:00
|
|
|
public getUploadLimit(): number | null {
|
2020-05-24 16:47:52 +02:00
|
|
|
if (this.mediaConfig !== null && this.mediaConfig["m.upload.size"] !== undefined) {
|
|
|
|
return this.mediaConfig["m.upload.size"];
|
2019-04-02 11:50:17 +02:00
|
|
|
} else {
|
|
|
|
return null;
|
2019-04-01 17:42:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:04:19 +01:00
|
|
|
public async sendContentListToRoom(
|
2021-11-03 09:43:24 +01:00
|
|
|
files: File[],
|
|
|
|
roomId: string,
|
2022-02-22 12:14:56 +01:00
|
|
|
relation: IEventRelation | undefined,
|
2021-11-03 09:43:24 +01:00
|
|
|
matrixClient: MatrixClient,
|
2022-01-26 10:04:19 +01:00
|
|
|
context = TimelineRenderingType.Room,
|
|
|
|
): Promise<void> {
|
2019-04-01 17:42:41 +02:00
|
|
|
if (matrixClient.isGuest()) {
|
2021-06-29 14:11:58 +02:00
|
|
|
dis.dispatch({ action: 'require_registration' });
|
2019-04-01 17:42:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-23 06:26:30 +01:00
|
|
|
const replyToEvent = RoomViewStore.instance.getQuotingEvent();
|
2020-06-01 16:00:55 +02:00
|
|
|
if (!this.mediaConfig) { // hot-path optimization to not flash a spinner if we don't need to
|
2020-06-01 18:05:53 +02:00
|
|
|
const modal = Modal.createDialog(Spinner, null, 'mx_Dialog_spinner');
|
2021-05-20 14:23:17 +02:00
|
|
|
await this.ensureMediaConfigFetched(matrixClient);
|
2020-06-01 16:00:55 +02:00
|
|
|
modal.close();
|
|
|
|
}
|
2019-04-01 17:42:41 +02:00
|
|
|
|
|
|
|
const tooBigFiles = [];
|
|
|
|
const okFiles = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < files.length; ++i) {
|
2020-05-24 17:00:57 +02:00
|
|
|
if (this.isFileSizeAcceptable(files[i])) {
|
2019-04-01 17:42:41 +02:00
|
|
|
okFiles.push(files[i]);
|
|
|
|
} else {
|
|
|
|
tooBigFiles.push(files[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tooBigFiles.length > 0) {
|
2021-06-29 14:11:58 +02:00
|
|
|
const { finished } = Modal.createTrackedDialog<[boolean]>('Upload Failure', '', UploadFailureDialog, {
|
2020-05-24 16:47:52 +02:00
|
|
|
badFiles: tooBigFiles,
|
|
|
|
totalFiles: files.length,
|
|
|
|
contentMessages: this,
|
2019-04-01 17:42:41 +02:00
|
|
|
});
|
2020-07-13 01:19:15 +02:00
|
|
|
const [shouldContinue] = await finished;
|
2019-04-01 17:42:41 +02:00
|
|
|
if (!shouldContinue) return;
|
|
|
|
}
|
|
|
|
|
2019-06-16 12:43:13 +02:00
|
|
|
let uploadAll = false;
|
2019-12-27 14:59:57 +01:00
|
|
|
// Promise to complete before sending next file into room, used for synchronisation of file-sending
|
|
|
|
// to match the order the files were specified in
|
2021-06-02 06:21:04 +02:00
|
|
|
let promBefore: Promise<any> = Promise.resolve();
|
2019-04-01 17:42:41 +02:00
|
|
|
for (let i = 0; i < okFiles.length; ++i) {
|
|
|
|
const file = okFiles[i];
|
2019-06-16 12:43:13 +02:00
|
|
|
if (!uploadAll) {
|
2021-06-29 14:11:58 +02:00
|
|
|
const { finished } = Modal.createTrackedDialog<[boolean, boolean]>('Upload Files confirmation',
|
2020-08-29 02:11:08 +02:00
|
|
|
'', UploadConfirmDialog, {
|
|
|
|
file,
|
|
|
|
currentIndex: i,
|
|
|
|
totalFiles: okFiles.length,
|
|
|
|
},
|
|
|
|
);
|
2020-07-13 01:19:15 +02:00
|
|
|
const [shouldContinue, shouldUploadAll] = await finished;
|
2019-06-16 12:43:13 +02:00
|
|
|
if (!shouldContinue) break;
|
2020-05-24 16:47:52 +02:00
|
|
|
if (shouldUploadAll) {
|
|
|
|
uploadAll = true;
|
|
|
|
}
|
2019-06-16 12:43:13 +02:00
|
|
|
}
|
2021-11-24 09:40:15 +01:00
|
|
|
|
2022-03-21 13:03:59 +01:00
|
|
|
promBefore = this.sendContentToRoom(file, roomId, relation, matrixClient, replyToEvent, promBefore);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (replyToEvent) {
|
|
|
|
// Clear event being replied to
|
|
|
|
dis.dispatch({
|
|
|
|
action: "reply_to_event",
|
|
|
|
event: null,
|
|
|
|
context,
|
|
|
|
});
|
2020-05-24 17:00:57 +02:00
|
|
|
}
|
2022-01-26 10:04:19 +01:00
|
|
|
|
|
|
|
// Focus the correct composer
|
|
|
|
dis.dispatch({
|
|
|
|
action: Action.FocusSendMessageComposer,
|
|
|
|
context,
|
|
|
|
});
|
2020-05-24 17:00:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:04:19 +01:00
|
|
|
public getCurrentUploads(relation?: IEventRelation): IUpload[] {
|
2021-11-03 09:43:24 +01:00
|
|
|
return this.inprogress.filter(upload => {
|
|
|
|
const noRelation = !relation && !upload.relation;
|
|
|
|
const matchingRelation = relation && upload.relation
|
|
|
|
&& relation.rel_type === upload.relation.rel_type
|
|
|
|
&& relation.event_id === upload.relation.event_id;
|
|
|
|
|
|
|
|
return (noRelation || matchingRelation) && !upload.canceled;
|
|
|
|
});
|
2020-05-24 17:00:57 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:04:19 +01:00
|
|
|
public cancelUpload(promise: Promise<any>, matrixClient: MatrixClient): void {
|
2020-05-24 17:00:57 +02:00
|
|
|
let upload: IUpload;
|
|
|
|
for (let i = 0; i < this.inprogress.length; ++i) {
|
|
|
|
if (this.inprogress[i].promise === promise) {
|
|
|
|
upload = this.inprogress[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (upload) {
|
|
|
|
upload.canceled = true;
|
2021-05-20 14:23:17 +02:00
|
|
|
matrixClient.cancelUpload(upload.promise);
|
2021-06-29 14:11:58 +02:00
|
|
|
dis.dispatch<UploadCanceledPayload>({ action: Action.UploadCanceled, upload });
|
2019-04-01 17:42:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 09:43:24 +01:00
|
|
|
private sendContentToRoom(
|
|
|
|
file: File,
|
|
|
|
roomId: string,
|
2022-02-22 12:14:56 +01:00
|
|
|
relation: IEventRelation | undefined,
|
2021-11-03 09:43:24 +01:00
|
|
|
matrixClient: MatrixClient,
|
2022-03-21 13:03:59 +01:00
|
|
|
replyToEvent: MatrixEvent | undefined,
|
2021-11-03 09:43:24 +01:00
|
|
|
promBefore: Promise<any>,
|
|
|
|
) {
|
2020-05-24 16:47:52 +02:00
|
|
|
const content: IContent = {
|
2017-01-08 02:20:59 +01:00
|
|
|
body: file.name || 'Attachment',
|
2015-12-02 19:16:16 +01:00
|
|
|
info: {
|
|
|
|
size: file.size,
|
2017-10-11 18:56:17 +02:00
|
|
|
},
|
2020-05-24 16:47:52 +02:00
|
|
|
msgtype: "", // set later
|
2015-12-02 19:16:16 +01:00
|
|
|
};
|
|
|
|
|
2022-03-31 19:40:35 +02:00
|
|
|
attachRelation(content, relation);
|
2022-03-21 13:03:59 +01:00
|
|
|
if (replyToEvent) {
|
|
|
|
addReplyToMessageContent(content, replyToEvent, {
|
|
|
|
includeLegacyFallback: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:26:54 +02:00
|
|
|
if (SettingsStore.getValue("Performance.addSendMessageTimingMetadata")) {
|
|
|
|
decorateStartSendingTime(content);
|
|
|
|
}
|
|
|
|
|
2015-12-02 19:16:16 +01:00
|
|
|
// if we have a mime type for the file, add it to the message metadata
|
|
|
|
if (file.type) {
|
|
|
|
content.info.mimetype = file.type;
|
|
|
|
}
|
|
|
|
|
2021-01-20 14:40:46 +01:00
|
|
|
const prom = new Promise<void>((resolve) => {
|
2020-05-24 16:47:52 +02:00
|
|
|
if (file.type.indexOf('image/') === 0) {
|
2021-10-28 10:40:38 +02:00
|
|
|
content.msgtype = MsgType.Image;
|
2020-05-24 16:47:52 +02:00
|
|
|
infoForImageFile(matrixClient, roomId, file).then((imageInfo) => {
|
2020-10-13 18:36:40 +02:00
|
|
|
Object.assign(content.info, imageInfo);
|
2019-11-12 12:40:38 +01:00
|
|
|
resolve();
|
2020-05-24 16:47:52 +02:00
|
|
|
}, (e) => {
|
2021-10-15 16:30:53 +02:00
|
|
|
logger.error(e);
|
2021-10-28 10:40:38 +02:00
|
|
|
content.msgtype = MsgType.File;
|
2019-11-12 12:40:38 +01:00
|
|
|
resolve();
|
|
|
|
});
|
2020-05-24 16:47:52 +02:00
|
|
|
} else if (file.type.indexOf('audio/') === 0) {
|
2021-10-28 10:40:38 +02:00
|
|
|
content.msgtype = MsgType.Audio;
|
2019-11-12 12:40:38 +01:00
|
|
|
resolve();
|
2020-05-24 16:47:52 +02:00
|
|
|
} else if (file.type.indexOf('video/') === 0) {
|
2021-10-28 10:40:38 +02:00
|
|
|
content.msgtype = MsgType.Video;
|
2020-05-24 16:47:52 +02:00
|
|
|
infoForVideoFile(matrixClient, roomId, file).then((videoInfo) => {
|
2020-10-13 18:36:40 +02:00
|
|
|
Object.assign(content.info, videoInfo);
|
2019-11-12 12:40:38 +01:00
|
|
|
resolve();
|
2020-05-24 16:47:52 +02:00
|
|
|
}, (e) => {
|
2021-10-28 10:40:38 +02:00
|
|
|
content.msgtype = MsgType.File;
|
2019-11-12 12:40:38 +01:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
} else {
|
2021-10-28 10:40:38 +02:00
|
|
|
content.msgtype = MsgType.File;
|
2019-11-12 12:40:38 +01:00
|
|
|
resolve();
|
|
|
|
}
|
2021-07-10 16:43:46 +02:00
|
|
|
}) as IAbortablePromise<void>;
|
2015-12-02 19:16:16 +01:00
|
|
|
|
2020-05-24 16:47:52 +02:00
|
|
|
// create temporary abort handler for before the actual upload gets passed off to js-sdk
|
2021-07-10 16:43:46 +02:00
|
|
|
prom.abort = () => {
|
2020-05-24 16:47:52 +02:00
|
|
|
upload.canceled = true;
|
2020-05-24 16:13:53 +02:00
|
|
|
};
|
|
|
|
|
2020-05-24 16:47:52 +02:00
|
|
|
const upload: IUpload = {
|
2017-01-08 02:20:59 +01:00
|
|
|
fileName: file.name || 'Attachment',
|
2021-11-03 09:43:24 +01:00
|
|
|
roomId,
|
|
|
|
relation,
|
2020-05-24 16:47:52 +02:00
|
|
|
total: file.size,
|
2016-11-15 12:22:39 +01:00
|
|
|
loaded: 0,
|
2020-05-24 16:13:53 +02:00
|
|
|
promise: prom,
|
2015-12-02 19:16:16 +01:00
|
|
|
};
|
|
|
|
this.inprogress.push(upload);
|
2021-06-29 14:11:58 +02:00
|
|
|
dis.dispatch<UploadStartedPayload>({ action: Action.UploadStarted, upload });
|
2015-12-02 19:16:16 +01:00
|
|
|
|
2017-07-14 18:01:03 +02:00
|
|
|
function onProgress(ev) {
|
|
|
|
upload.total = ev.total;
|
|
|
|
upload.loaded = ev.loaded;
|
2021-06-29 14:11:58 +02:00
|
|
|
dis.dispatch<UploadProgressPayload>({ action: Action.UploadProgress, upload });
|
2017-07-14 18:01:03 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 16:47:52 +02:00
|
|
|
let error;
|
2019-11-12 12:40:38 +01:00
|
|
|
return prom.then(function() {
|
2020-05-24 16:13:53 +02:00
|
|
|
if (upload.canceled) throw new UploadCanceledError();
|
2016-12-16 18:11:28 +01:00
|
|
|
// XXX: upload.promise must be the promise that
|
|
|
|
// is returned by uploadFile as it has an abort()
|
|
|
|
// method hacked onto it.
|
2021-07-10 16:43:46 +02:00
|
|
|
upload.promise = uploadFile(matrixClient, roomId, file, onProgress);
|
2016-12-16 18:11:28 +01:00
|
|
|
return upload.promise.then(function(result) {
|
2016-11-15 12:22:39 +01:00
|
|
|
content.file = result.file;
|
|
|
|
content.url = result.url;
|
|
|
|
});
|
2020-05-24 16:47:52 +02:00
|
|
|
}).then(() => {
|
2019-12-27 14:59:57 +01:00
|
|
|
// Await previous message being sent into the room
|
|
|
|
return promBefore;
|
|
|
|
}).then(function() {
|
2020-05-24 16:13:53 +02:00
|
|
|
if (upload.canceled) throw new UploadCanceledError();
|
2022-03-11 10:04:22 +01:00
|
|
|
const threadId = relation?.rel_type === THREAD_RELATION_TYPE.name
|
2021-11-24 09:40:15 +01:00
|
|
|
? relation.event_id
|
|
|
|
: null;
|
|
|
|
const prom = matrixClient.sendMessage(roomId, threadId, content);
|
2021-09-08 19:26:54 +02:00
|
|
|
if (SettingsStore.getValue("Performance.addSendMessageTimingMetadata")) {
|
|
|
|
prom.then(resp => {
|
|
|
|
sendRoundTripMetric(matrixClient, roomId, resp.event_id);
|
|
|
|
});
|
|
|
|
}
|
2020-10-29 16:53:14 +01:00
|
|
|
return prom;
|
2015-12-02 19:16:16 +01:00
|
|
|
}, function(err) {
|
2016-02-15 20:29:56 +01:00
|
|
|
error = err;
|
2015-12-03 11:52:06 +01:00
|
|
|
if (!upload.canceled) {
|
2021-06-29 14:11:58 +02:00
|
|
|
let desc = _t("The file '%(fileName)s' failed to upload.", { fileName: upload.fileName });
|
2020-05-24 16:47:52 +02:00
|
|
|
if (err.http_status === 413) {
|
2019-04-01 17:42:41 +02:00
|
|
|
desc = _t(
|
|
|
|
"The file '%(fileName)s' exceeds this homeserver's size limit for uploads",
|
2021-06-29 14:11:58 +02:00
|
|
|
{ fileName: upload.fileName },
|
2019-04-01 17:42:41 +02:00
|
|
|
);
|
2015-12-03 11:52:06 +01:00
|
|
|
}
|
2017-08-10 16:17:52 +02:00
|
|
|
Modal.createTrackedDialog('Upload failed', '', ErrorDialog, {
|
2017-05-23 16:16:31 +02:00
|
|
|
title: _t('Upload Failed'),
|
|
|
|
description: desc,
|
2015-12-03 11:52:06 +01:00
|
|
|
});
|
|
|
|
}
|
2016-11-15 12:22:39 +01:00
|
|
|
}).finally(() => {
|
2017-10-11 18:56:17 +02:00
|
|
|
for (let i = 0; i < this.inprogress.length; ++i) {
|
2020-05-24 16:47:52 +02:00
|
|
|
if (this.inprogress[i].promise === upload.promise) {
|
|
|
|
this.inprogress.splice(i, 1);
|
2015-12-02 19:16:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 20:29:56 +01:00
|
|
|
if (error) {
|
2019-04-01 17:42:41 +02:00
|
|
|
// 413: File was too big or upset the server in some way:
|
|
|
|
// clear the media size limit so we fetch it again next time
|
|
|
|
// we try to upload
|
|
|
|
if (error && error.http_status === 413) {
|
2020-05-24 16:47:52 +02:00
|
|
|
this.mediaConfig = null;
|
2019-04-01 17:42:41 +02:00
|
|
|
}
|
2021-06-29 14:11:58 +02:00
|
|
|
dis.dispatch<UploadErrorPayload>({ action: Action.UploadFailed, upload, error });
|
2017-10-11 18:56:17 +02:00
|
|
|
} else {
|
2021-06-29 14:11:58 +02:00
|
|
|
dis.dispatch<UploadFinishedPayload>({ action: Action.UploadFinished, upload });
|
|
|
|
dis.dispatch({ action: 'message_sent' });
|
2016-02-15 20:29:56 +01:00
|
|
|
}
|
2015-07-08 15:34:26 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:00:57 +02:00
|
|
|
private isFileSizeAcceptable(file: File) {
|
|
|
|
if (this.mediaConfig !== null &&
|
|
|
|
this.mediaConfig["m.upload.size"] !== undefined &&
|
|
|
|
file.size > this.mediaConfig["m.upload.size"]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-12-02 19:16:16 +01:00
|
|
|
}
|
|
|
|
|
2021-05-20 14:23:17 +02:00
|
|
|
private ensureMediaConfigFetched(matrixClient: MatrixClient) {
|
2020-05-24 17:00:57 +02:00
|
|
|
if (this.mediaConfig !== null) return;
|
|
|
|
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log("[Media Config] Fetching");
|
2021-05-20 14:23:17 +02:00
|
|
|
return matrixClient.getMediaConfig().then((config) => {
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log("[Media Config] Fetched config:", config);
|
2020-05-24 17:00:57 +02:00
|
|
|
return config;
|
|
|
|
}).catch(() => {
|
|
|
|
// Media repo can't or won't report limits, so provide an empty object (no limits).
|
2021-09-21 17:48:09 +02:00
|
|
|
logger.log("[Media Config] Could not fetch config, so not limiting uploads.");
|
2020-05-24 17:00:57 +02:00
|
|
|
return {};
|
|
|
|
}).then((config) => {
|
|
|
|
this.mediaConfig = config;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static sharedInstance() {
|
2020-07-20 21:43:49 +02:00
|
|
|
if (window.mxContentMessages === undefined) {
|
|
|
|
window.mxContentMessages = new ContentMessages();
|
2015-12-02 19:16:16 +01:00
|
|
|
}
|
2020-07-20 21:43:49 +02:00
|
|
|
return window.mxContentMessages;
|
2015-12-02 19:16:16 +01:00
|
|
|
}
|
|
|
|
}
|