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

Commit

Permalink
Merge branch 'develop' into yaya-usman/sharedialog_console_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Mar 28, 2022
2 parents cdd3008 + a7a0c55 commit 61edfae
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 53 deletions.
1 change: 1 addition & 0 deletions res/css/views/beta/_BetaCard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ limitations under the License.
color: #FFFFFF;
display: inline-block;
vertical-align: text-bottom;
word-break: keep-all; // avoid multiple lines on CJK language

&.mx_AccessibleButton {
cursor: pointer;
Expand Down
1 change: 1 addition & 0 deletions res/css/views/messages/_DisambiguatedProfile.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ limitations under the License.
font-size: 1.1rem;
margin-left: 5px;
opacity: 0.5; // Match mx_TextualEvent
color: $primary-content;
}
}
4 changes: 2 additions & 2 deletions src/components/structures/RoomDirectory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -727,9 +727,9 @@ export default class RoomDirectory extends React.Component<IProps, IState> {
</div>;
}
const explanation =
_t("If you can't find the room you're looking for, ask for an invite or <a>Create a new room</a>.", null,
_t("If you can't find the room you're looking for, ask for an invite or <a>create a new room</a>.", null,
{ a: sub => (
<AccessibleButton kind="secondary" onClick={this.onCreateRoomClick}>
<AccessibleButton kind="link_inline" onClick={this.onCreateRoomClick}>
{ sub }
</AccessibleButton>
) },
Expand Down
4 changes: 2 additions & 2 deletions src/components/structures/TimelinePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class TimelinePanel extends React.Component<IProps, IState> {
// updates from pagination will happen when the paginate completes.
if (toStartOfTimeline || !data || !data.liveEvent) return;

if (!this.messagePanel.current) return;
if (!this.messagePanel.current?.getScrollState()) return;

if (!this.messagePanel.current.getScrollState().stuckAtBottom) {
// we won't load this event now, because we don't want to push any
Expand All @@ -570,7 +570,7 @@ class TimelinePanel extends React.Component<IProps, IState> {
}

// tell the timeline window to try to advance itself, but not to make
// an http request to do so.
// a http request to do so.
//
// we deliberately avoid going via the ScrollPanel for this call - the
// ScrollPanel might already have an active pagination promise, which
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3007,7 +3007,7 @@
"Try different words or check for typos. Some results may not be visible as they're private and you need an invite to join them.": "Try different words or check for typos. Some results may not be visible as they're private and you need an invite to join them.",
"Find a room…": "Find a room…",
"Find a room… (e.g. %(exampleRoom)s)": "Find a room… (e.g. %(exampleRoom)s)",
"If you can't find the room you're looking for, ask for an invite or <a>Create a new room</a>.": "If you can't find the room you're looking for, ask for an invite or <a>Create a new room</a>.",
"If you can't find the room you're looking for, ask for an invite or <a>create a new room</a>.": "If you can't find the room you're looking for, ask for an invite or <a>create a new room</a>.",
"Filter": "Filter",
"Filter rooms and people": "Filter rooms and people",
"Clear filter": "Clear filter",
Expand Down
119 changes: 81 additions & 38 deletions src/utils/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,105 @@
* limitations under the License.
*/

import { arrayHasDiff } from "./arrays";

export function mayBeAnimated(mimeType: string): boolean {
return ["image/gif", "image/webp"].includes(mimeType);
return ["image/gif", "image/webp", "image/png", "image/apng"].includes(mimeType);
}

function arrayBufferRead(arr: ArrayBuffer, start: number, len: number): Uint8Array {
return new Uint8Array(arr.slice(start, start + len));
}

function arrayBufferReadInt(arr: ArrayBuffer, start: number): number {
const dv = new DataView(arr, start, 4);
return dv.getUint32(0);
}

function arrayBufferReadStr(arr: ArrayBuffer, start: number, len: number): string {
return String.fromCharCode.apply(null, arrayBufferRead(arr, start, len));
}

export async function blobIsAnimated(mimeType: string, blob: Blob): Promise<boolean> {
if (mimeType === "image/webp") {
// Only extended file format WEBP images support animation, so grab the expected data range and verify header.
// Based on https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
const arr = await blob.slice(0, 17).arrayBuffer();
if (
arrayBufferReadStr(arr, 0, 4) === "RIFF" &&
arrayBufferReadStr(arr, 8, 4) === "WEBP" &&
arrayBufferReadStr(arr, 12, 4) === "VP8X"
) {
const [flags] = arrayBufferRead(arr, 16, 1);
// Flags: R R I L E X _A_ R (reversed)
const animationFlagMask = 1 << 1;
return (flags & animationFlagMask) != 0;
}
} else if (mimeType === "image/gif") {
// Based on https://gist.github.com/zakirt/faa4a58cec5a7505b10e3686a226f285
// More info at http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
const dv = new DataView(await blob.arrayBuffer(), 10);

const globalColorTable = dv.getUint8(0);
let globalColorTableSize = 0;
// check first bit, if 0, then we don't have a Global Color Table
if (globalColorTable & 0x80) {
// grab the last 3 bits, to calculate the global color table size -> RGB * 2^(N+1)
// N is the value in the last 3 bits.
globalColorTableSize = 3 * Math.pow(2, (globalColorTable & 0x7) + 1);
switch (mimeType) {
case "image/webp": {
// Only extended file format WEBP images support animation, so grab the expected data range and verify header.
// Based on https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
const arr = await blob.slice(0, 17).arrayBuffer();
if (
arrayBufferReadStr(arr, 0, 4) === "RIFF" &&
arrayBufferReadStr(arr, 8, 4) === "WEBP" &&
arrayBufferReadStr(arr, 12, 4) === "VP8X"
) {
const [flags] = arrayBufferRead(arr, 16, 1);
// Flags: R R I L E X _A_ R (reversed)
const animationFlagMask = 1 << 1;
return (flags & animationFlagMask) != 0;
}
break;
}

// move on to the Graphics Control Extension
const offset = 3 + globalColorTableSize;
case "image/gif": {
// Based on https://gist.github.com/zakirt/faa4a58cec5a7505b10e3686a226f285
// More info at http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
const dv = new DataView(await blob.arrayBuffer(), 10);

const globalColorTable = dv.getUint8(0);
let globalColorTableSize = 0;
// check first bit, if 0, then we don't have a Global Color Table
if (globalColorTable & 0x80) {
// grab the last 3 bits, to calculate the global color table size -> RGB * 2^(N+1)
// N is the value in the last 3 bits.
globalColorTableSize = 3 * Math.pow(2, (globalColorTable & 0x7) + 1);
}

const extensionIntroducer = dv.getUint8(offset);
const graphicsControlLabel = dv.getUint8(offset + 1);
let delayTime = 0;
// move on to the Graphics Control Extension
const offset = 3 + globalColorTableSize;

// Graphics Control Extension section is where GIF animation data is stored
// First 2 bytes must be 0x21 and 0xF9
if ((extensionIntroducer & 0x21) && (graphicsControlLabel & 0xF9)) {
// skip to the 2 bytes with the delay time
delayTime = dv.getUint16(offset + 4);
const extensionIntroducer = dv.getUint8(offset);
const graphicsControlLabel = dv.getUint8(offset + 1);
let delayTime = 0;

// Graphics Control Extension section is where GIF animation data is stored
// First 2 bytes must be 0x21 and 0xF9
if ((extensionIntroducer & 0x21) && (graphicsControlLabel & 0xF9)) {
// skip to the 2 bytes with the delay time
delayTime = dv.getUint16(offset + 4);
}

return !!delayTime;
}

return !!delayTime;
case "image/png":
case "image/apng": {
// Based on https://stackoverflow.com/a/68618296
const arr = await blob.arrayBuffer();
if (arrayHasDiff([
0x89,
0x50, 0x4E, 0x47,
0x0D, 0x0A,
0x1A,
0x0A,
], Array.from(arrayBufferRead(arr, 0, 8)))) {
return false;
}

for (let i = 8; i < blob.size;) {
const length = arrayBufferReadInt(arr, i);
i += 4;
const type = arrayBufferReadStr(arr, i, 4);
i += 4;

switch (type) {
case "acTL":
return true;
case "IDAT":
return false;
}
i += length + 4;
}
break;
}
}

return false;
Expand Down
1 change: 1 addition & 0 deletions src/utils/blobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const ALLOWED_BLOB_MIMETYPES = [
'image/jpeg',
'image/gif',
'image/png',
'image/apng',
'image/webp',

'video/mp4',
Expand Down
34 changes: 24 additions & 10 deletions test/Image-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,47 @@ describe("Image", () => {
expect(mayBeAnimated("image/webp")).toBeTruthy();
});
it("image/png", async () => {
expect(mayBeAnimated("image/png")).toBeFalsy();
expect(mayBeAnimated("image/png")).toBeTruthy();
});
it("image/apng", async () => {
expect(mayBeAnimated("image/apng")).toBeTruthy();
});
it("image/jpeg", async () => {
expect(mayBeAnimated("image/jpeg")).toBeFalsy();
});
});

describe("blobIsAnimated", () => {
const animatedGif = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.gif"))]);
const animatedWebp = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.webp"))]);
const staticGif = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.gif"))]);
const staticWebp = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.webp"))]);

it("Animated GIF", async () => {
expect(await blobIsAnimated("image/gif", animatedGif)).toBeTruthy();
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.gif"))]);
expect(await blobIsAnimated("image/gif", img)).toBeTruthy();
});

it("Static GIF", async () => {
expect(await blobIsAnimated("image/gif", staticGif)).toBeFalsy();
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.gif"))]);
expect(await blobIsAnimated("image/gif", img)).toBeFalsy();
});

it("Animated WEBP", async () => {
expect(await blobIsAnimated("image/webp", animatedWebp)).toBeTruthy();
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.webp"))]);
expect(await blobIsAnimated("image/webp", img)).toBeTruthy();
});

it("Static WEBP", async () => {
expect(await blobIsAnimated("image/webp", staticWebp)).toBeFalsy();
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.webp"))]);
expect(await blobIsAnimated("image/webp", img)).toBeFalsy();
});

it("Animated PNG", async () => {
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "animated-logo.apng"))]);
expect(await blobIsAnimated("image/png", img)).toBeTruthy();
expect(await blobIsAnimated("image/apng", img)).toBeTruthy();
});

it("Static PNG", async () => {
const img = new Blob([fs.readFileSync(path.resolve(__dirname, "images", "static-logo.png"))]);
expect(await blobIsAnimated("image/png", img)).toBeFalsy();
expect(await blobIsAnimated("image/apng", img)).toBeFalsy();
});
});
});
Binary file added test/images/animated-logo.apng
Binary file not shown.
Binary file added test/images/static-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 61edfae

Please sign in to comment.