Skip to content

Commit

Permalink
[native] Fix failing base64 decode for thumbhash
Browse files Browse the repository at this point in the history
Summary:
Fixes [[ https://linear.app/comm/issue/ENG-6031/video-encryption-failed | ENG-6031 ]]. It appears that `ThumbhashModule` sometimes returns base64 string containing newlines, which causes issues during encryption. More details in the linked issue description.

Even though still not sure if Android code is supposed to return base64 string with newlines, I decided to fix it by simply stripping them in JS. This is more future-proof than experimenting with native [[ https://developer.android.com/reference/android/util/Base64#encodeToString(byte%5B%5D,%20int) | flags ]] for base64 encoding.

Test Plan:
I wasn't able to repro Android `Base64.encodeToString()` returning newline so I appended it manually for testing:
- Confirmed that C++ `is_valid_base64()` returns `false` for this kind of thumbhash -> causes encryption failure
- Confirmed that my fix makes `is_valid_base64()` return `true` -> encryption succeeds -> cannot reporduce anymore with my manually appended `\n`.

Reviewers: atul, tomek

Reviewed By: atul, tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D10310
  • Loading branch information
barthap committed Dec 14, 2023
1 parent 9aaf6da commit d42495a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ThumbhashModule : Module() {

val rgba = bitmap.toRGBA()
val thumbHash = ThumbHash.rgbaToThumbHash(bitmap.width, bitmap.height, rgba)
return Base64.encodeToString(thumbHash, Base64.DEFAULT)
return Base64.encodeToString(thumbHash, Base64.NO_WRAP)
}

// endregion
Expand Down
8 changes: 7 additions & 1 deletion native/utils/thumbhash-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ const platformUtilsModule: {
+generateThumbHash: (photoURI: string) => Promise<string>,
} = requireNativeModule('Thumbhash');

const newLineRegex = /\n/g;
async function generateThumbHash(photoURI: string): Promise<string> {
invariant(
platformUtilsModule.generateThumbHash,
'generateThumbHash() unavailable. Check if Thumbhash expo-module is autolinked',
);
return await platformUtilsModule.generateThumbHash(photoURI);

const rawThumbHash = await platformUtilsModule.generateThumbHash(photoURI);

// Sometimes native base64 modules encode the string with newlines,
// which breaks the thumbhash format
return rawThumbHash.replace(newLineRegex, '');
}

export { generateThumbHash };

0 comments on commit d42495a

Please sign in to comment.