@@ -22,7 +22,7 @@ import { MatrixClient } from "matrix-js-sdk/src/client";
22
22
import { IImageInfo } from "matrix-js-sdk/src/@types/partials" ;
23
23
import { IUploadOpts } from "matrix-js-sdk/src/@types/requests" ;
24
24
import { IEncryptedFile } from "matrix-js-sdk/src/@types/event" ;
25
- import encrypt from "browser-encrypt-attachment" ;
25
+ import * as encrypt from "browser-encrypt-attachment" ;
26
26
import extractPngChunks from "png-chunks-extract" ;
27
27
28
28
import dis from './dispatcher/dispatcher' ;
@@ -318,6 +318,15 @@ function readFileAsArrayBuffer(file: File | Blob): Promise<ArrayBuffer> {
318
318
} ) ;
319
319
}
320
320
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
+
321
330
/**
322
331
* Strip EXIF metadata from a JPEG
323
332
* Taken from http://jsfiddle.net/mowglisanu/frhwm2xe/3/
@@ -329,35 +338,38 @@ function stripJpegMetadata(data: ArrayBuffer): ArrayBuffer {
329
338
const dv = new DataView ( data ) ;
330
339
let offset = 0 ;
331
340
let recess = 0 ;
332
- const pieces = [ ] ;
341
+ const pieces : IChunk [ ] = [ ] ;
333
342
let i = 0 ;
343
+ let blockSize : number ;
334
344
335
- const newPieces = [ ] ;
345
+ const newPieces : ArrayBuffer [ ] = [ ] ;
336
346
337
347
// FIXME: check this isn't stripping off any EXIF color profile data
338
348
// as that will break the colorimetry of the image. We're stripping
339
349
// this for privacy purposes rather than filesize.
340
- if ( dv . getUint16 ( offset ) == 0xffd8 ) {
350
+ if ( dv . getUint16 ( offset ) === JPEG_SOI_MARKER ) {
341
351
offset += 2 ;
342
352
let app1 = dv . getUint16 ( offset ) ;
343
353
offset += 2 ;
344
354
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 ) {
350
361
break ;
351
362
}
352
- offset += dv . getUint16 ( offset ) ;
363
+ offset += blockSize ; // jump to the next marker
353
364
app1 = dv . getUint16 ( offset ) ;
354
- offset += 2 ;
365
+ offset += 2 ; // enter the next marker block
355
366
}
356
367
357
368
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 ) ) ;
360
371
} ) ;
372
+
361
373
newPieces . push ( data . slice ( recess ) ) ;
362
374
}
363
375
}
0 commit comments