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

Commit 85e82c4

Browse files
committed
make code more readable
1 parent 443364f commit 85e82c4

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

src/@types/browser-encrypt-attachment.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ declare module "browser-encrypt-attachment" {
3232
info: IEncryptedAttachmentInfo;
3333
}
3434

35-
function encryptAttachment(plaintextBuffer: ArrayBuffer): Promise<IEncryptedAttachment>;
36-
function decryptAttachment(ciphertextBuffer: ArrayBuffer, info: IEncryptedAttachmentInfo): Promise<ArrayBuffer>;
37-
38-
export { encryptAttachment, decryptAttachment };
35+
export function encryptAttachment(plaintextBuffer: ArrayBuffer): Promise<IEncryptedAttachment>;
36+
export function decryptAttachment(
37+
ciphertextBuffer: ArrayBuffer,
38+
info: IEncryptedAttachmentInfo,
39+
): Promise<ArrayBuffer>;
3940
}

src/ContentMessages.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { MatrixClient } from "matrix-js-sdk/src/client";
2222
import { IImageInfo } from "matrix-js-sdk/src/@types/partials";
2323
import { IUploadOpts } from "matrix-js-sdk/src/@types/requests";
2424
import { IEncryptedFile } from "matrix-js-sdk/src/@types/event";
25-
import encrypt from "browser-encrypt-attachment";
25+
import * as encrypt from "browser-encrypt-attachment";
2626
import extractPngChunks from "png-chunks-extract";
2727

2828
import dis from './dispatcher/dispatcher';
@@ -318,6 +318,15 @@ function readFileAsArrayBuffer(file: File | Blob): Promise<ArrayBuffer> {
318318
});
319319
}
320320

321+
interface IChunk {
322+
begin: number;
323+
end: number;
324+
}
325+
326+
const JPEG_SOI_MARKER = 0xFFD8;
327+
const JPEG_APP1_MARKER = 0xFFE1;
328+
const JPEG_SOS_MARKER = 0xFFDA;
329+
321330
/**
322331
* Strip EXIF metadata from a JPEG
323332
* Taken from http://jsfiddle.net/mowglisanu/frhwm2xe/3/
@@ -329,35 +338,38 @@ function stripJpegMetadata(data: ArrayBuffer): ArrayBuffer {
329338
const dv = new DataView(data);
330339
let offset = 0;
331340
let recess = 0;
332-
const pieces = [];
341+
const pieces: IChunk[] = [];
333342
let i = 0;
343+
let blockSize: number;
334344

335-
const newPieces = [];
345+
const newPieces: ArrayBuffer[] = [];
336346

337347
// FIXME: check this isn't stripping off any EXIF color profile data
338348
// as that will break the colorimetry of the image. We're stripping
339349
// this for privacy purposes rather than filesize.
340-
if (dv.getUint16(offset) == 0xffd8) {
350+
if (dv.getUint16(offset) === JPEG_SOI_MARKER) {
341351
offset += 2;
342352
let app1 = dv.getUint16(offset);
343353
offset += 2;
344354
while (offset < dv.byteLength) {
345-
if (app1 == 0xffe1) {
346-
pieces[i] = { recess: recess, offset: offset - 2 };
347-
recess = offset + dv.getUint16(offset);
348-
i++;
349-
} else if (app1 == 0xffda) {
355+
blockSize = dv.getUint16(offset);
356+
if (app1 === JPEG_APP1_MARKER) {
357+
// if the marker we are in is an APP1 marker then mark it for extraction
358+
pieces[i++] = { begin: recess, end: offset - 2 };
359+
recess = offset + blockSize;
360+
} else if (app1 === JPEG_SOS_MARKER) {
350361
break;
351362
}
352-
offset += dv.getUint16(offset);
363+
offset += blockSize; // jump to the next marker
353364
app1 = dv.getUint16(offset);
354-
offset += 2;
365+
offset += 2; // enter the next marker block
355366
}
356367

357368
if (pieces.length > 0) {
358-
pieces.forEach(function(v) {
359-
newPieces.push(data.slice(v.recess, v.offset));
369+
pieces.forEach(piece => {
370+
newPieces.push(data.slice(piece.begin, piece.end));
360371
});
372+
361373
newPieces.push(data.slice(recess));
362374
}
363375
}

src/utils/DecryptFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
// Pull in the encryption lib so that we can decrypt attachments.
18-
import encrypt from 'browser-encrypt-attachment';
18+
import * as encrypt from 'browser-encrypt-attachment';
1919
import { IEncryptedFile } from 'matrix-js-sdk/src/@types/event';
2020

2121
import { mediaFromContent } from "../customisations/Media";

0 commit comments

Comments
 (0)