2015-07-08 21:34:26 +08:00
|
|
|
/*
|
2016-01-07 12:06:39 +08:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-04-01 23:42:41 +08:00
|
|
|
Copyright 2019 New Vector Ltd
|
2015-07-08 21:34:26 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2019-04-01 23:42:41 +08:00
|
|
|
import extend from './extend';
|
|
|
|
import dis from './dispatcher';
|
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
|
|
|
import sdk from './index';
|
2017-05-25 18:39:08 +08:00
|
|
|
import { _t } from './languageHandler';
|
2019-04-01 23:42:41 +08:00
|
|
|
import Modal from './Modal';
|
|
|
|
import RoomViewStore from './stores/RoomViewStore';
|
|
|
|
import encrypt from "browser-encrypt-attachment";
|
2019-04-09 18:18:06 +08:00
|
|
|
import extractPngChunks from "png-chunks-extract";
|
2016-11-12 20:20:36 +08:00
|
|
|
|
2016-11-15 19:22:39 +08:00
|
|
|
// Polyfill for Canvas.toBlob API using Canvas.toDataURL
|
2019-04-01 23:42:41 +08:00
|
|
|
import "blueimp-canvas-to-blob";
|
2016-11-15 19:22:39 +08:00
|
|
|
|
|
|
|
const MAX_WIDTH = 800;
|
|
|
|
const MAX_HEIGHT = 600;
|
|
|
|
|
2019-01-15 01:10:20 +08: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 19:22:39 +08:00
|
|
|
|
2019-04-09 02:07:17 +08:00
|
|
|
export class UploadCanceledError extends Error {}
|
2016-11-15 19:22:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a thumbnail for a image DOM element.
|
|
|
|
* The image will be smaller than MAX_WIDTH and MAX_HEIGHT.
|
|
|
|
* The thumbnail will have the same aspect ratio as the original.
|
|
|
|
* Draws the element into a canvas using CanvasRenderingContext2D.drawImage
|
|
|
|
* Then calls Canvas.toBlob to get a blob object for the image data.
|
|
|
|
*
|
|
|
|
* Since it needs to calculate the dimensions of the source image and the
|
|
|
|
* thumbnailed image it returns an info object filled out with information
|
|
|
|
* about the original image and the thumbnail.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} element The element to thumbnail.
|
|
|
|
* @param {integer} inputWidth The width of the image in the input element.
|
|
|
|
* @param {integer} inputHeight the width of the image in the input element.
|
|
|
|
* @param {String} mimeType The mimeType to save the blob as.
|
|
|
|
* @return {Promise} A promise that resolves with an object with an info key
|
|
|
|
* and a thumbnail key.
|
|
|
|
*/
|
|
|
|
function createThumbnail(element, inputWidth, inputHeight, mimeType) {
|
2019-11-12 19:40:38 +08:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
let targetWidth = inputWidth;
|
|
|
|
let targetHeight = inputHeight;
|
|
|
|
if (targetHeight > MAX_HEIGHT) {
|
|
|
|
targetWidth = Math.floor(targetWidth * (MAX_HEIGHT / targetHeight));
|
|
|
|
targetHeight = MAX_HEIGHT;
|
|
|
|
}
|
|
|
|
if (targetWidth > MAX_WIDTH) {
|
|
|
|
targetHeight = Math.floor(targetHeight * (MAX_WIDTH / targetWidth));
|
|
|
|
targetWidth = MAX_WIDTH;
|
|
|
|
}
|
2019-11-12 19:56:53 +08:00
|
|
|
|
2019-11-12 19:40:38 +08:00
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = targetWidth;
|
|
|
|
canvas.height = targetHeight;
|
|
|
|
canvas.getContext("2d").drawImage(element, 0, 0, targetWidth, targetHeight);
|
|
|
|
canvas.toBlob(function(thumbnail) {
|
|
|
|
resolve({
|
|
|
|
info: {
|
|
|
|
thumbnail_info: {
|
|
|
|
w: targetWidth,
|
|
|
|
h: targetHeight,
|
|
|
|
mimetype: thumbnail.type,
|
|
|
|
size: thumbnail.size,
|
|
|
|
},
|
|
|
|
w: inputWidth,
|
|
|
|
h: inputHeight,
|
2016-11-15 19:22:39 +08:00
|
|
|
},
|
2019-11-12 19:40:38 +08:00
|
|
|
thumbnail: thumbnail,
|
|
|
|
});
|
|
|
|
}, mimeType);
|
|
|
|
});
|
2016-11-15 19:22:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a file into a newly created image element.
|
|
|
|
*
|
2019-04-01 23:42:41 +08:00
|
|
|
* @param {File} imageFile The file to load in an image element.
|
2016-11-15 19:22:39 +08:00
|
|
|
* @return {Promise} A promise that resolves with the html image element.
|
|
|
|
*/
|
2019-04-09 18:32:44 +08:00
|
|
|
async function loadImageElement(imageFile) {
|
2015-07-08 21:34:26 +08:00
|
|
|
// Load the file into an html element
|
2016-11-15 19:22:39 +08:00
|
|
|
const img = document.createElement("img");
|
2017-10-20 00:16:52 +08:00
|
|
|
const objectUrl = URL.createObjectURL(imageFile);
|
2019-04-09 18:32:44 +08:00
|
|
|
const imgPromise = new Promise((resolve, reject) => {
|
|
|
|
img.onload = function() {
|
|
|
|
URL.revokeObjectURL(objectUrl);
|
|
|
|
resolve(img);
|
|
|
|
};
|
|
|
|
img.onerror = function(e) {
|
|
|
|
reject(e);
|
|
|
|
};
|
|
|
|
});
|
2017-10-20 00:16:52 +08:00
|
|
|
img.src = objectUrl;
|
2015-07-08 21:34:26 +08:00
|
|
|
|
2019-01-15 01:10:20 +08:00
|
|
|
// check for hi-dpi PNGs and fudge display resolution as needed.
|
|
|
|
// this is mainly needed for macOS screencaps
|
2019-04-09 18:32:44 +08:00
|
|
|
let parsePromise;
|
2019-01-15 01:10:20 +08: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 18:18:06 +08:00
|
|
|
// bytes (but this makes extractPngChunks choke on the corrupt file)
|
2019-01-15 01:10:20 +08:00
|
|
|
const headers = imageFile; //.slice(0, 0x1000);
|
2019-04-09 18:32:44 +08:00
|
|
|
parsePromise = readFileAsArrayBuffer(headers).then(arrayBuffer => {
|
2019-01-15 01:10:20 +08:00
|
|
|
const buffer = new Uint8Array(arrayBuffer);
|
2019-04-09 18:18:06 +08:00
|
|
|
const chunks = extractPngChunks(buffer);
|
2019-01-15 01:10:20 +08:00
|
|
|
for (const chunk of chunks) {
|
|
|
|
if (chunk.name === 'pHYs') {
|
|
|
|
if (chunk.data.byteLength !== PHYS_HIDPI.length) return;
|
2019-04-09 18:32:44 +08:00
|
|
|
const hidpi = chunk.data.every((val, i) => val === PHYS_HIDPI[i]);
|
|
|
|
return hidpi;
|
2019-01-15 01:10:20 +08:00
|
|
|
}
|
|
|
|
}
|
2019-04-09 18:32:44 +08:00
|
|
|
return false;
|
2019-01-15 01:10:20 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:32:44 +08:00
|
|
|
const [hidpi] = await Promise.all([parsePromise, imgPromise]);
|
|
|
|
const width = hidpi ? (img.width >> 1) : img.width;
|
|
|
|
const height = hidpi ? (img.height >> 1) : img.height;
|
|
|
|
return {width, height, img};
|
2015-07-08 21:34:26 +08:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:22:39 +08: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 23:42:41 +08:00
|
|
|
* @param {File} imageFile The image to read and thumbnail.
|
2016-11-15 19:22:39 +08:00
|
|
|
* @return {Promise} A promise that resolves with the attachment info.
|
|
|
|
*/
|
|
|
|
function infoForImageFile(matrixClient, roomId, imageFile) {
|
2017-10-12 00:56:17 +08:00
|
|
|
let thumbnailType = "image/png";
|
2016-11-15 19:22:39 +08:00
|
|
|
if (imageFile.type == "image/jpeg") {
|
|
|
|
thumbnailType = "image/jpeg";
|
|
|
|
}
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
let imageInfo;
|
2019-01-15 01:10:20 +08:00
|
|
|
return loadImageElement(imageFile).then(function(r) {
|
|
|
|
return createThumbnail(r.img, r.width, r.height, thumbnailType);
|
2016-11-15 19:22:39 +08:00
|
|
|
}).then(function(result) {
|
|
|
|
imageInfo = result.info;
|
|
|
|
return uploadFile(matrixClient, roomId, result.thumbnail);
|
|
|
|
}).then(function(result) {
|
|
|
|
imageInfo.thumbnail_url = result.url;
|
|
|
|
imageInfo.thumbnail_file = result.file;
|
|
|
|
return imageInfo;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a file into a newly created video element.
|
|
|
|
*
|
2019-04-01 23:42:41 +08:00
|
|
|
* @param {File} videoFile The file to load in an video element.
|
2016-11-15 19:22:39 +08:00
|
|
|
* @return {Promise} A promise that resolves with the video image element.
|
|
|
|
*/
|
|
|
|
function loadVideoElement(videoFile) {
|
2019-11-12 19:40:38 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Load the file into an html element
|
|
|
|
const video = document.createElement("video");
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = function(e) {
|
|
|
|
video.src = e.target.result;
|
|
|
|
|
|
|
|
// Once ready, returns its size
|
|
|
|
// Wait until we have enough data to thumbnail the first frame.
|
|
|
|
video.onloadeddata = function() {
|
|
|
|
resolve(video);
|
|
|
|
};
|
|
|
|
video.onerror = function(e) {
|
|
|
|
reject(e);
|
|
|
|
};
|
2016-07-19 23:05:15 +08:00
|
|
|
};
|
2019-11-12 19:40:38 +08:00
|
|
|
reader.onerror = function(e) {
|
|
|
|
reject(e);
|
2016-07-19 23:05:15 +08:00
|
|
|
};
|
2019-11-12 19:40:38 +08:00
|
|
|
reader.readAsDataURL(videoFile);
|
|
|
|
});
|
2016-07-19 23:05:15 +08:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:22:39 +08: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 23:42:41 +08:00
|
|
|
* @param {File} videoFile The video to read and thumbnail.
|
2016-11-15 19:22:39 +08:00
|
|
|
* @return {Promise} A promise that resolves with the attachment info.
|
|
|
|
*/
|
|
|
|
function infoForVideoFile(matrixClient, roomId, videoFile) {
|
|
|
|
const thumbnailType = "image/jpeg";
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
let videoInfo;
|
2016-11-15 19:22:39 +08:00
|
|
|
return loadVideoElement(videoFile).then(function(video) {
|
|
|
|
return createThumbnail(video, video.videoWidth, video.videoHeight, thumbnailType);
|
|
|
|
}).then(function(result) {
|
|
|
|
videoInfo = result.info;
|
|
|
|
return uploadFile(matrixClient, roomId, result.thumbnail);
|
|
|
|
}).then(function(result) {
|
|
|
|
videoInfo.thumbnail_url = result.url;
|
|
|
|
videoInfo.thumbnail_file = result.file;
|
|
|
|
return videoInfo;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-08 19:42:20 +08:00
|
|
|
/**
|
|
|
|
* Read the file as an ArrayBuffer.
|
2019-04-01 23:42:41 +08:00
|
|
|
* @param {File} file The file to read
|
2016-11-08 19:42:20 +08:00
|
|
|
* @return {Promise} A promise that resolves with an ArrayBuffer when the file
|
|
|
|
* is read.
|
|
|
|
*/
|
|
|
|
function readFileAsArrayBuffer(file) {
|
2019-11-12 19:40:38 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = function(e) {
|
|
|
|
resolve(e.target.result);
|
|
|
|
};
|
|
|
|
reader.onerror = function(e) {
|
|
|
|
reject(e);
|
|
|
|
};
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
});
|
2016-11-08 19:42:20 +08:00
|
|
|
}
|
|
|
|
|
2016-11-15 19:22:39 +08: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-15 00:01:03 +08:00
|
|
|
* @param {Function?} progressHandler optional callback to be called when a chunk of
|
|
|
|
* data is uploaded.
|
2016-11-15 19:22:39 +08: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.
|
|
|
|
*/
|
2017-07-15 00:01:03 +08:00
|
|
|
function uploadFile(matrixClient, roomId, file, progressHandler) {
|
2016-11-15 19:22:39 +08:00
|
|
|
if (matrixClient.isRoomEncrypted(roomId)) {
|
|
|
|
// If the room is encrypted then encrypt the file before uploading it.
|
|
|
|
// First read the file into memory.
|
2019-04-09 02:07:17 +08:00
|
|
|
let canceled = false;
|
2019-04-09 00:53:39 +08:00
|
|
|
let uploadPromise;
|
|
|
|
let encryptInfo;
|
|
|
|
const prom = readFileAsArrayBuffer(file).then(function(data) {
|
2019-04-09 02:07:17 +08:00
|
|
|
if (canceled) throw new UploadCanceledError();
|
2016-11-15 19:22:39 +08:00
|
|
|
// Then encrypt the file.
|
|
|
|
return encrypt.encryptAttachment(data);
|
|
|
|
}).then(function(encryptResult) {
|
2019-04-09 02:07:17 +08:00
|
|
|
if (canceled) throw new UploadCanceledError();
|
2016-11-15 19:22:39 +08:00
|
|
|
// Record the information needed to decrypt the attachment.
|
2019-04-09 00:53:39 +08:00
|
|
|
encryptInfo = encryptResult.info;
|
2016-11-15 19:22:39 +08:00
|
|
|
// Pass the encrypted data as a Blob to the uploader.
|
|
|
|
const blob = new Blob([encryptResult.data]);
|
2019-04-09 00:53:39 +08:00
|
|
|
uploadPromise = matrixClient.uploadContent(blob, {
|
2017-07-15 00:01:03 +08:00
|
|
|
progressHandler: progressHandler,
|
2018-06-18 20:48:23 +08:00
|
|
|
includeFilename: false,
|
2016-11-15 19:22:39 +08:00
|
|
|
});
|
2019-04-09 00:53:39 +08:00
|
|
|
|
|
|
|
return uploadPromise;
|
|
|
|
}).then(function(url) {
|
|
|
|
// 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.
|
|
|
|
encryptInfo.url = url;
|
|
|
|
if (file.type) {
|
|
|
|
encryptInfo.mimetype = file.type;
|
|
|
|
}
|
|
|
|
return {"file": encryptInfo};
|
2016-11-15 19:22:39 +08:00
|
|
|
});
|
2019-04-09 00:53:39 +08:00
|
|
|
prom.abort = () => {
|
2019-04-09 02:07:17 +08:00
|
|
|
canceled = true;
|
2019-04-09 00:53:39 +08:00
|
|
|
if (uploadPromise) MatrixClientPeg.get().cancelUpload(uploadPromise);
|
|
|
|
};
|
|
|
|
return prom;
|
2016-11-15 19:22:39 +08:00
|
|
|
} else {
|
2017-07-15 00:01:03 +08:00
|
|
|
const basePromise = matrixClient.uploadContent(file, {
|
|
|
|
progressHandler: progressHandler,
|
|
|
|
});
|
2016-12-17 01:11:28 +08:00
|
|
|
const promise1 = basePromise.then(function(url) {
|
2016-11-15 19:22:39 +08:00
|
|
|
// If the attachment isn't encrypted then include the URL directly.
|
|
|
|
return {"url": url};
|
|
|
|
});
|
2016-12-17 01:11:28 +08:00
|
|
|
// XXX: copy over the abort method to the new promise
|
|
|
|
promise1.abort = basePromise.abort;
|
|
|
|
return promise1;
|
2016-11-15 19:22:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 23:42:41 +08:00
|
|
|
export default class ContentMessages {
|
2015-12-03 02:16:16 +08:00
|
|
|
constructor() {
|
|
|
|
this.inprogress = [];
|
|
|
|
this.nextId = 0;
|
2019-04-01 23:42:41 +08:00
|
|
|
this._mediaConfig = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sharedInstance() {
|
|
|
|
if (global.mx_ContentMessages === undefined) {
|
|
|
|
global.mx_ContentMessages = new ContentMessages();
|
|
|
|
}
|
|
|
|
return global.mx_ContentMessages;
|
|
|
|
}
|
|
|
|
|
|
|
|
_isFileSizeAcceptable(file) {
|
|
|
|
if (this._mediaConfig !== null &&
|
|
|
|
this._mediaConfig["m.upload.size"] !== undefined &&
|
|
|
|
file.size > this._mediaConfig["m.upload.size"]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ensureMediaConfigFetched() {
|
|
|
|
if (this._mediaConfig !== null) return;
|
|
|
|
|
|
|
|
console.log("[Media Config] Fetching");
|
|
|
|
return MatrixClientPeg.get().getMediaConfig().then((config) => {
|
|
|
|
console.log("[Media Config] Fetched config:", config);
|
|
|
|
return config;
|
|
|
|
}).catch(() => {
|
|
|
|
// Media repo can't or won't report limits, so provide an empty object (no limits).
|
|
|
|
console.log("[Media Config] Could not fetch config, so not limiting uploads.");
|
|
|
|
return {};
|
|
|
|
}).then((config) => {
|
|
|
|
this._mediaConfig = config;
|
|
|
|
});
|
2015-10-03 01:37:15 +08:00
|
|
|
}
|
|
|
|
|
2018-01-05 05:57:24 +08:00
|
|
|
sendStickerContentToRoom(url, roomId, info, text, matrixClient) {
|
2018-03-29 23:23:20 +08:00
|
|
|
return MatrixClientPeg.get().sendStickerMessage(roomId, url, info, text).catch((e) => {
|
|
|
|
console.warn(`Failed to send content with URL ${url} to room ${roomId}`, e);
|
2018-03-29 23:24:03 +08:00
|
|
|
throw e;
|
2018-03-29 23:23:20 +08:00
|
|
|
});
|
2018-01-04 17:53:26 +08:00
|
|
|
}
|
|
|
|
|
2019-04-01 23:42:41 +08:00
|
|
|
getUploadLimit() {
|
|
|
|
if (this._mediaConfig !== null && this._mediaConfig["m.upload.size"] !== undefined) {
|
|
|
|
return this._mediaConfig["m.upload.size"];
|
2019-04-02 17:50:17 +08:00
|
|
|
} else {
|
|
|
|
return null;
|
2019-04-01 23:42:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendContentListToRoom(files, roomId, matrixClient) {
|
|
|
|
if (matrixClient.isGuest()) {
|
|
|
|
dis.dispatch({action: 'require_registration'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isQuoting = Boolean(RoomViewStore.getQuotingEvent());
|
|
|
|
if (isQuoting) {
|
|
|
|
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
|
|
|
const shouldUpload = await new Promise((resolve) => {
|
|
|
|
Modal.createTrackedDialog('Upload Reply Warning', '', QuestionDialog, {
|
|
|
|
title: _t('Replying With Files'),
|
|
|
|
description: (
|
|
|
|
<div>{_t(
|
2019-04-02 17:50:17 +08:00
|
|
|
'At this time it is not possible to reply with a file. ' +
|
|
|
|
'Would you like to upload this file without replying?',
|
2019-04-01 23:42:41 +08:00
|
|
|
)}</div>
|
|
|
|
),
|
|
|
|
hasCancelButton: true,
|
|
|
|
button: _t("Continue"),
|
|
|
|
onFinished: (shouldUpload) => {
|
|
|
|
resolve(shouldUpload);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
if (!shouldUpload) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this._ensureMediaConfigFetched();
|
|
|
|
|
|
|
|
const tooBigFiles = [];
|
|
|
|
const okFiles = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < files.length; ++i) {
|
|
|
|
if (this._isFileSizeAcceptable(files[i])) {
|
|
|
|
okFiles.push(files[i]);
|
|
|
|
} else {
|
|
|
|
tooBigFiles.push(files[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tooBigFiles.length > 0) {
|
|
|
|
const UploadFailureDialog = sdk.getComponent("dialogs.UploadFailureDialog");
|
|
|
|
const uploadFailureDialogPromise = new Promise((resolve) => {
|
|
|
|
Modal.createTrackedDialog('Upload Failure', '', UploadFailureDialog, {
|
|
|
|
badFiles: tooBigFiles,
|
|
|
|
totalFiles: files.length,
|
|
|
|
contentMessages: this,
|
|
|
|
onFinished: (shouldContinue) => {
|
|
|
|
resolve(shouldContinue);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const shouldContinue = await uploadFailureDialogPromise;
|
|
|
|
if (!shouldContinue) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const UploadConfirmDialog = sdk.getComponent("dialogs.UploadConfirmDialog");
|
2019-06-16 18:43:13 +08:00
|
|
|
let uploadAll = false;
|
2019-12-27 21:59:57 +08: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
|
|
|
|
let promBefore = Promise.resolve();
|
2019-04-01 23:42:41 +08:00
|
|
|
for (let i = 0; i < okFiles.length; ++i) {
|
|
|
|
const file = okFiles[i];
|
2019-06-16 18:43:13 +08:00
|
|
|
if (!uploadAll) {
|
|
|
|
const shouldContinue = await new Promise((resolve) => {
|
|
|
|
Modal.createTrackedDialog('Upload Files confirmation', '', UploadConfirmDialog, {
|
|
|
|
file,
|
|
|
|
currentIndex: i,
|
|
|
|
totalFiles: okFiles.length,
|
|
|
|
onFinished: (shouldContinue, shouldUploadAll) => {
|
|
|
|
if (shouldUploadAll) {
|
|
|
|
uploadAll = true;
|
|
|
|
}
|
|
|
|
resolve(shouldContinue);
|
|
|
|
},
|
|
|
|
});
|
2019-04-01 23:42:41 +08:00
|
|
|
});
|
2019-06-16 18:43:13 +08:00
|
|
|
if (!shouldContinue) break;
|
|
|
|
}
|
2019-12-27 21:59:57 +08:00
|
|
|
promBefore = this._sendContentToRoom(file, roomId, matrixClient, promBefore);
|
2019-04-01 23:42:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 21:59:57 +08:00
|
|
|
_sendContentToRoom(file, roomId, matrixClient, promBefore) {
|
2016-11-15 19:22:39 +08:00
|
|
|
const content = {
|
2017-01-08 09:20:59 +08:00
|
|
|
body: file.name || 'Attachment',
|
2015-12-03 02:16:16 +08:00
|
|
|
info: {
|
|
|
|
size: file.size,
|
2017-10-12 00:56:17 +08:00
|
|
|
},
|
2015-12-03 02:16:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// if we have a mime type for the file, add it to the message metadata
|
|
|
|
if (file.type) {
|
|
|
|
content.info.mimetype = file.type;
|
|
|
|
}
|
|
|
|
|
2019-11-12 19:40:38 +08:00
|
|
|
const prom = new Promise((resolve) => {
|
|
|
|
if (file.type.indexOf('image/') == 0) {
|
|
|
|
content.msgtype = 'm.image';
|
|
|
|
infoForImageFile(matrixClient, roomId, file).then((imageInfo)=>{
|
|
|
|
extend(content.info, imageInfo);
|
|
|
|
resolve();
|
|
|
|
}, (error)=>{
|
|
|
|
console.error(error);
|
|
|
|
content.msgtype = 'm.file';
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
} else if (file.type.indexOf('audio/') == 0) {
|
|
|
|
content.msgtype = 'm.audio';
|
|
|
|
resolve();
|
|
|
|
} else if (file.type.indexOf('video/') == 0) {
|
|
|
|
content.msgtype = 'm.video';
|
|
|
|
infoForVideoFile(matrixClient, roomId, file).then((videoInfo)=>{
|
|
|
|
extend(content.info, videoInfo);
|
|
|
|
resolve();
|
|
|
|
}, (error)=>{
|
|
|
|
content.msgtype = 'm.file';
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
} else {
|
2019-11-12 19:56:53 +08:00
|
|
|
content.msgtype = 'm.file';
|
2019-11-12 19:40:38 +08:00
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2015-12-03 02:16:16 +08:00
|
|
|
|
2016-11-15 19:22:39 +08:00
|
|
|
const upload = {
|
2017-01-08 09:20:59 +08:00
|
|
|
fileName: file.name || 'Attachment',
|
2015-12-03 02:16:16 +08:00
|
|
|
roomId: roomId,
|
|
|
|
total: 0,
|
2016-11-15 19:22:39 +08:00
|
|
|
loaded: 0,
|
2015-12-03 02:16:16 +08:00
|
|
|
};
|
|
|
|
this.inprogress.push(upload);
|
|
|
|
dis.dispatch({action: 'upload_started'});
|
|
|
|
|
2019-07-23 15:13:51 +08:00
|
|
|
// Focus the composer view
|
|
|
|
dis.dispatch({action: 'focus_composer'});
|
|
|
|
|
2017-10-12 00:56:17 +08:00
|
|
|
let error;
|
2017-07-15 00:01:03 +08:00
|
|
|
|
|
|
|
function onProgress(ev) {
|
|
|
|
upload.total = ev.total;
|
|
|
|
upload.loaded = ev.loaded;
|
|
|
|
dis.dispatch({action: 'upload_progress', upload: upload});
|
|
|
|
}
|
|
|
|
|
2019-11-12 19:40:38 +08:00
|
|
|
return prom.then(function() {
|
2016-12-17 01:11:28 +08:00
|
|
|
// XXX: upload.promise must be the promise that
|
|
|
|
// is returned by uploadFile as it has an abort()
|
|
|
|
// method hacked onto it.
|
2016-11-15 19:22:39 +08:00
|
|
|
upload.promise = uploadFile(
|
2017-07-15 00:01:03 +08:00
|
|
|
matrixClient, roomId, file, onProgress,
|
2016-12-17 01:11:28 +08:00
|
|
|
);
|
|
|
|
return upload.promise.then(function(result) {
|
2016-11-15 19:22:39 +08:00
|
|
|
content.file = result.file;
|
|
|
|
content.url = result.url;
|
|
|
|
});
|
2019-12-27 21:59:57 +08:00
|
|
|
}).then((url) => {
|
|
|
|
// Await previous message being sent into the room
|
|
|
|
return promBefore;
|
|
|
|
}).then(function() {
|
2015-12-03 02:16:16 +08:00
|
|
|
return matrixClient.sendMessage(roomId, content);
|
|
|
|
}, function(err) {
|
2016-02-16 03:29:56 +08:00
|
|
|
error = err;
|
2015-12-03 18:52:06 +08:00
|
|
|
if (!upload.canceled) {
|
2019-04-01 23:42:41 +08:00
|
|
|
let desc = _t("The file '%(fileName)s' failed to upload.", {fileName: upload.fileName});
|
2015-12-03 18:52:06 +08:00
|
|
|
if (err.http_status == 413) {
|
2019-04-01 23:42:41 +08:00
|
|
|
desc = _t(
|
|
|
|
"The file '%(fileName)s' exceeds this homeserver's size limit for uploads",
|
|
|
|
{fileName: upload.fileName},
|
|
|
|
);
|
2015-12-03 18:52:06 +08:00
|
|
|
}
|
2017-10-12 00:56:17 +08:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2017-08-10 22:17:52 +08:00
|
|
|
Modal.createTrackedDialog('Upload failed', '', ErrorDialog, {
|
2017-05-23 22:16:31 +08:00
|
|
|
title: _t('Upload Failed'),
|
|
|
|
description: desc,
|
2015-12-03 18:52:06 +08:00
|
|
|
});
|
|
|
|
}
|
2016-11-15 19:22:39 +08:00
|
|
|
}).finally(() => {
|
|
|
|
const inprogressKeys = Object.keys(this.inprogress);
|
2017-10-12 00:56:17 +08:00
|
|
|
for (let i = 0; i < this.inprogress.length; ++i) {
|
|
|
|
const k = inprogressKeys[i];
|
2016-11-15 19:22:39 +08:00
|
|
|
if (this.inprogress[k].promise === upload.promise) {
|
|
|
|
this.inprogress.splice(k, 1);
|
2015-12-03 02:16:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 03:29:56 +08:00
|
|
|
if (error) {
|
2019-04-01 23:42:41 +08: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) {
|
|
|
|
this._mediaConfig = null;
|
|
|
|
}
|
2018-06-21 18:29:06 +08:00
|
|
|
dis.dispatch({action: 'upload_failed', upload, error});
|
2017-10-12 00:56:17 +08:00
|
|
|
} else {
|
2018-06-21 18:29:06 +08:00
|
|
|
dis.dispatch({action: 'upload_finished', upload});
|
2019-04-01 23:42:41 +08:00
|
|
|
dis.dispatch({action: 'message_sent'});
|
2016-02-16 03:29:56 +08:00
|
|
|
}
|
2015-07-08 21:34:26 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-12-03 02:16:16 +08:00
|
|
|
getCurrentUploads() {
|
2019-04-09 00:53:39 +08:00
|
|
|
return this.inprogress.filter(u => !u.canceled);
|
2015-12-03 02:16:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cancelUpload(promise) {
|
2016-11-15 19:22:39 +08:00
|
|
|
const inprogressKeys = Object.keys(this.inprogress);
|
2017-10-12 00:56:17 +08:00
|
|
|
let upload;
|
|
|
|
for (let i = 0; i < this.inprogress.length; ++i) {
|
|
|
|
const k = inprogressKeys[i];
|
2015-12-03 02:16:16 +08:00
|
|
|
if (this.inprogress[k].promise === promise) {
|
|
|
|
upload = this.inprogress[k];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (upload) {
|
|
|
|
upload.canceled = true;
|
|
|
|
MatrixClientPeg.get().cancelUpload(upload.promise);
|
2019-04-09 00:53:39 +08:00
|
|
|
dis.dispatch({action: 'upload_canceled', upload});
|
2015-12-03 02:16:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|