Remove redundant duplicate mimetype field which doesn't conform to spec (#7045)
This commit is contained in:
parent
27e16362b6
commit
82807434b8
4 changed files with 95 additions and 30 deletions
38
src/@types/browser-encrypt-attachment.ts
Normal file
38
src/@types/browser-encrypt-attachment.ts
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare module "browser-encrypt-attachment" {
|
||||||
|
interface IInfo {
|
||||||
|
v: string;
|
||||||
|
key: {
|
||||||
|
alg: string;
|
||||||
|
key_ops: string[]; // eslint-disable-line camelcase
|
||||||
|
kty: string;
|
||||||
|
k: string;
|
||||||
|
ext: boolean;
|
||||||
|
};
|
||||||
|
iv: string;
|
||||||
|
hashes: {[alg: string]: string};
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IEncryptedAttachment {
|
||||||
|
data: ArrayBuffer;
|
||||||
|
info: IInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function encryptAttachment(plaintextBuffer: ArrayBuffer): Promise<IEncryptedAttachment>;
|
||||||
|
export function decryptAttachment(ciphertextBuffer: ArrayBuffer, info: IInfo): Promise<ArrayBuffer>;
|
||||||
|
}
|
26
src/@types/png-chunks-extract.ts
Normal file
26
src/@types/png-chunks-extract.ts
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare module "png-chunks-extract" {
|
||||||
|
interface IChunk {
|
||||||
|
name: string;
|
||||||
|
data: Uint8Array;
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractPngChunks(data: Uint8Array | Buffer): IChunk[];
|
||||||
|
|
||||||
|
export default extractPngChunks;
|
||||||
|
}
|
|
@ -18,6 +18,9 @@ limitations under the License.
|
||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { MatrixClient } from "matrix-js-sdk/src/client";
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
|
import { IEncryptedFile, IMediaEventInfo } from "./customisations/models/IMediaEventContent";
|
||||||
|
import { IUploadOpts } from "matrix-js-sdk/src/@types/requests";
|
||||||
|
import { MsgType } from "matrix-js-sdk/src/@types/event";
|
||||||
|
|
||||||
import dis from './dispatcher/dispatcher';
|
import dis from './dispatcher/dispatcher';
|
||||||
import * as sdk from './index';
|
import * as sdk from './index';
|
||||||
|
@ -307,7 +310,7 @@ function loadVideoElement(videoFile): Promise<HTMLVideoElement> {
|
||||||
function infoForVideoFile(matrixClient, roomId, videoFile) {
|
function infoForVideoFile(matrixClient, roomId, videoFile) {
|
||||||
const thumbnailType = "image/jpeg";
|
const thumbnailType = "image/jpeg";
|
||||||
|
|
||||||
let videoInfo;
|
let videoInfo: Partial<IMediaEventInfo>;
|
||||||
return loadVideoElement(videoFile).then((video) => {
|
return loadVideoElement(videoFile).then((video) => {
|
||||||
return createThumbnail(video, video.videoWidth, video.videoHeight, thumbnailType);
|
return createThumbnail(video, video.videoWidth, video.videoHeight, thumbnailType);
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
|
@ -356,49 +359,48 @@ export function uploadFile(
|
||||||
matrixClient: MatrixClient,
|
matrixClient: MatrixClient,
|
||||||
roomId: string,
|
roomId: string,
|
||||||
file: File | Blob,
|
file: File | Blob,
|
||||||
progressHandler?: any, // TODO: Types
|
progressHandler?: IUploadOpts["progressHandler"],
|
||||||
): IAbortablePromise<{url?: string, file?: any}> { // TODO: Types
|
): IAbortablePromise<{ url?: string, file?: IEncryptedFile }> {
|
||||||
let canceled = false;
|
let canceled = false;
|
||||||
if (matrixClient.isRoomEncrypted(roomId)) {
|
if (matrixClient.isRoomEncrypted(roomId)) {
|
||||||
// If the room is encrypted then encrypt the file before uploading it.
|
// If the room is encrypted then encrypt the file before uploading it.
|
||||||
// First read the file into memory.
|
// First read the file into memory.
|
||||||
let uploadPromise;
|
let uploadPromise: IAbortablePromise<string>;
|
||||||
let encryptInfo;
|
|
||||||
const prom = readFileAsArrayBuffer(file).then(function(data) {
|
const prom = readFileAsArrayBuffer(file).then(function(data) {
|
||||||
if (canceled) throw new UploadCanceledError();
|
if (canceled) throw new UploadCanceledError();
|
||||||
// Then encrypt the file.
|
// Then encrypt the file.
|
||||||
return encrypt.encryptAttachment(data);
|
return encrypt.encryptAttachment(data);
|
||||||
}).then(function(encryptResult) {
|
}).then(function(encryptResult) {
|
||||||
if (canceled) throw new UploadCanceledError();
|
if (canceled) throw new UploadCanceledError();
|
||||||
// Record the information needed to decrypt the attachment.
|
|
||||||
encryptInfo = encryptResult.info;
|
|
||||||
// Pass the encrypted data as a Blob to the uploader.
|
// Pass the encrypted data as a Blob to the uploader.
|
||||||
const blob = new Blob([encryptResult.data]);
|
const blob = new Blob([encryptResult.data]);
|
||||||
uploadPromise = matrixClient.uploadContent(blob, {
|
uploadPromise = matrixClient.uploadContent(blob, {
|
||||||
progressHandler: progressHandler,
|
progressHandler,
|
||||||
includeFilename: false,
|
includeFilename: false,
|
||||||
});
|
});
|
||||||
return uploadPromise;
|
|
||||||
}).then(function(url) {
|
return uploadPromise.then(url => {
|
||||||
if (canceled) throw new UploadCanceledError();
|
if (canceled) throw new UploadCanceledError();
|
||||||
// If the attachment is encrypted then bundle the URL along
|
|
||||||
// with the information needed to decrypt the attachment and
|
// If the attachment is encrypted then bundle the URL along
|
||||||
// add it under a file key.
|
// with the information needed to decrypt the attachment and
|
||||||
encryptInfo.url = url;
|
// add it under a file key.
|
||||||
if (file.type) {
|
return {
|
||||||
encryptInfo.mimetype = file.type;
|
file: {
|
||||||
}
|
...encryptResult.info,
|
||||||
return { "file": encryptInfo };
|
url,
|
||||||
}) as IAbortablePromise<{ file: any }>;
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}) as IAbortablePromise<{ file: IEncryptedFile }>;
|
||||||
prom.abort = () => {
|
prom.abort = () => {
|
||||||
canceled = true;
|
canceled = true;
|
||||||
if (uploadPromise) matrixClient.cancelUpload(uploadPromise);
|
if (uploadPromise) matrixClient.cancelUpload(uploadPromise);
|
||||||
};
|
};
|
||||||
return prom;
|
return prom;
|
||||||
} else {
|
} else {
|
||||||
const basePromise = matrixClient.uploadContent(file, {
|
const basePromise = matrixClient.uploadContent(file, { progressHandler });
|
||||||
progressHandler: progressHandler,
|
|
||||||
});
|
|
||||||
const promise1 = basePromise.then(function(url) {
|
const promise1 = basePromise.then(function(url) {
|
||||||
if (canceled) throw new UploadCanceledError();
|
if (canceled) throw new UploadCanceledError();
|
||||||
// If the attachment isn't encrypted then include the URL directly.
|
// If the attachment isn't encrypted then include the URL directly.
|
||||||
|
@ -554,29 +556,29 @@ export default class ContentMessages {
|
||||||
|
|
||||||
const prom = new Promise<void>((resolve) => {
|
const prom = new Promise<void>((resolve) => {
|
||||||
if (file.type.indexOf('image/') === 0) {
|
if (file.type.indexOf('image/') === 0) {
|
||||||
content.msgtype = 'm.image';
|
content.msgtype = MsgType.Image;
|
||||||
infoForImageFile(matrixClient, roomId, file).then((imageInfo) => {
|
infoForImageFile(matrixClient, roomId, file).then((imageInfo) => {
|
||||||
Object.assign(content.info, imageInfo);
|
Object.assign(content.info, imageInfo);
|
||||||
resolve();
|
resolve();
|
||||||
}, (e) => {
|
}, (e) => {
|
||||||
logger.error(e);
|
logger.error(e);
|
||||||
content.msgtype = 'm.file';
|
content.msgtype = MsgType.File;
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
} else if (file.type.indexOf('audio/') === 0) {
|
} else if (file.type.indexOf('audio/') === 0) {
|
||||||
content.msgtype = 'm.audio';
|
content.msgtype = MsgType.Audio;
|
||||||
resolve();
|
resolve();
|
||||||
} else if (file.type.indexOf('video/') === 0) {
|
} else if (file.type.indexOf('video/') === 0) {
|
||||||
content.msgtype = 'm.video';
|
content.msgtype = MsgType.Video;
|
||||||
infoForVideoFile(matrixClient, roomId, file).then((videoInfo) => {
|
infoForVideoFile(matrixClient, roomId, file).then((videoInfo) => {
|
||||||
Object.assign(content.info, videoInfo);
|
Object.assign(content.info, videoInfo);
|
||||||
resolve();
|
resolve();
|
||||||
}, (e) => {
|
}, (e) => {
|
||||||
content.msgtype = 'm.file';
|
content.msgtype = MsgType.File;
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
content.msgtype = 'm.file';
|
content.msgtype = MsgType.File;
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
}) as IAbortablePromise<void>;
|
}) as IAbortablePromise<void>;
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
export interface IEncryptedFile {
|
export interface IEncryptedFile {
|
||||||
url: string;
|
url: string;
|
||||||
mimetype?: string;
|
|
||||||
key: {
|
key: {
|
||||||
alg: string;
|
alg: string;
|
||||||
key_ops: string[]; // eslint-disable-line camelcase
|
key_ops: string[]; // eslint-disable-line camelcase
|
||||||
|
|
Loading…
Reference in a new issue