Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1874f3b

Browse files
authored
Merge pull request #6269 from matrix-org/travis/encrypt-voice
Encrypt the voice message file if needed
2 parents 18b4708 + c16228b commit 1874f3b

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/ContentMessages.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ function readFileAsArrayBuffer(file: File | Blob): Promise<ArrayBuffer> {
307307
* If the file is unencrypted then the object will have a "url" key.
308308
* If the file is encrypted then the object will have a "file" key.
309309
*/
310-
function uploadFile(
310+
export function uploadFile(
311311
matrixClient: MatrixClient,
312312
roomId: string,
313313
file: File | Blob,

src/components/views/rooms/VoiceRecordComposerTile.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
6565
}
6666

6767
await this.state.recorder.stop();
68-
const mxc = await this.state.recorder.upload();
68+
const upload = await this.state.recorder.upload(this.props.room.roomId);
6969
MatrixClientPeg.get().sendMessage(this.props.room.roomId, {
7070
"body": "Voice message",
7171
//"msgtype": "org.matrix.msc2516.voice",
7272
"msgtype": MsgType.Audio,
73-
"url": mxc,
73+
"url": upload.mxc,
74+
"file": upload.encrypted,
7475
"info": {
7576
duration: Math.round(this.state.recorder.durationSeconds * 1000),
7677
mimetype: this.state.recorder.contentType,
@@ -81,7 +82,8 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
8182
// https://github.com/matrix-org/matrix-doc/pull/3245
8283
"org.matrix.msc1767.text": "Voice message",
8384
"org.matrix.msc1767.file": {
84-
url: mxc,
85+
url: upload.mxc,
86+
file: upload.encrypted,
8587
name: "Voice message.ogg",
8688
mimetype: this.state.recorder.contentType,
8789
size: this.state.recorder.contentLength,

src/voice/VoiceRecording.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import {PayloadEvent, WORKLET_NAME} from "./consts";
2727
import {UPDATE_EVENT} from "../stores/AsyncStore";
2828
import {Playback} from "./Playback";
2929
import {createAudioContext} from "./compat";
30+
import { IEncryptedFile } from "matrix-js-sdk/src/@types/event";
31+
import { uploadFile } from "../ContentMessages";
3032

3133
const CHANNELS = 1; // stereo isn't important
3234
export const SAMPLE_RATE = 48000; // 48khz is what WebRTC uses. 12khz is where we lose quality.
@@ -49,6 +51,11 @@ export enum RecordingState {
4951
Uploaded = "uploaded",
5052
}
5153

54+
export interface IUpload {
55+
mxc?: string; // for unencrypted uploads
56+
encrypted?: IEncryptedFile;
57+
}
58+
5259
export class VoiceRecording extends EventEmitter implements IDestroyable {
5360
private recorder: Recorder;
5461
private recorderContext: AudioContext;
@@ -58,7 +65,7 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
5865
private recorderWorklet: AudioWorkletNode;
5966
private recorderProcessor: ScriptProcessorNode;
6067
private buffer = new Uint8Array(0); // use this.audioBuffer to access
61-
private mxc: string;
68+
private lastUpload: IUpload;
6269
private recording = false;
6370
private observable: SimpleObservable<IRecordingUpdate>;
6471
private amplitudes: number[] = []; // at each second mark, generated
@@ -214,13 +221,6 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
214221
return this.buffer.length > 0;
215222
}
216223

217-
public get mxcUri(): string {
218-
if (!this.mxc) {
219-
throw new Error("Recording has not been uploaded yet");
220-
}
221-
return this.mxc;
222-
}
223-
224224
private onAudioProcess = (ev: AudioProcessingEvent) => {
225225
this.processAudioUpdate(ev.playbackTime);
226226

@@ -290,7 +290,7 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
290290
};
291291

292292
public async start(): Promise<void> {
293-
if (this.mxc || this.hasRecording) {
293+
if (this.lastUpload || this.hasRecording) {
294294
throw new Error("Recording already prepared");
295295
}
296296
if (this.recording) {
@@ -362,20 +362,19 @@ export class VoiceRecording extends EventEmitter implements IDestroyable {
362362
this.observable.close();
363363
}
364364

365-
public async upload(): Promise<string> {
365+
public async upload(inRoomId: string): Promise<IUpload> {
366366
if (!this.hasRecording) {
367367
throw new Error("No recording available to upload");
368368
}
369369

370-
if (this.mxc) return this.mxc;
370+
if (this.lastUpload) return this.lastUpload;
371371

372372
this.emit(RecordingState.Uploading);
373-
this.mxc = await this.client.uploadContent(new Blob([this.audioBuffer], {
373+
const { url: mxc, file: encrypted } = await uploadFile(this.client, inRoomId, new Blob([this.audioBuffer], {
374374
type: this.contentType,
375-
}), {
376-
onlyContentUri: false, // to stop the warnings in the console
377-
}).then(r => r['content_uri']);
375+
}));
376+
this.lastUpload = { mxc, encrypted };
378377
this.emit(RecordingState.Uploaded);
379-
return this.mxc;
378+
return this.lastUpload;
380379
}
381380
}

0 commit comments

Comments
 (0)