Allow parsing png files to fail if thumbnailing is successful (#10308)
parent
3e340d5906
commit
f74867e57b
|
@ -96,17 +96,22 @@ async function loadImageElement(imageFile: File): Promise<{
|
||||||
// Thus we could slice the file down to only sniff the first 0x1000
|
// Thus we could slice the file down to only sniff the first 0x1000
|
||||||
// bytes (but this makes extractPngChunks choke on the corrupt file)
|
// bytes (but this makes extractPngChunks choke on the corrupt file)
|
||||||
const headers = imageFile; //.slice(0, 0x1000);
|
const headers = imageFile; //.slice(0, 0x1000);
|
||||||
parsePromise = readFileAsArrayBuffer(headers).then((arrayBuffer) => {
|
parsePromise = readFileAsArrayBuffer(headers)
|
||||||
const buffer = new Uint8Array(arrayBuffer);
|
.then((arrayBuffer) => {
|
||||||
const chunks = extractPngChunks(buffer);
|
const buffer = new Uint8Array(arrayBuffer);
|
||||||
for (const chunk of chunks) {
|
const chunks = extractPngChunks(buffer);
|
||||||
if (chunk.name === "pHYs") {
|
for (const chunk of chunks) {
|
||||||
if (chunk.data.byteLength !== PHYS_HIDPI.length) return false;
|
if (chunk.name === "pHYs") {
|
||||||
return chunk.data.every((val, i) => val === PHYS_HIDPI[i]);
|
if (chunk.data.byteLength !== PHYS_HIDPI.length) return false;
|
||||||
|
return chunk.data.every((val, i) => val === PHYS_HIDPI[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
return false;
|
||||||
return false;
|
})
|
||||||
});
|
.catch((e) => {
|
||||||
|
console.error("Failed to parse PNG", e);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const [hidpi] = await Promise.all([parsePromise, imgPromise]);
|
const [hidpi] = await Promise.all([parsePromise, imgPromise]);
|
||||||
|
|
|
@ -115,10 +115,25 @@ describe("ContentMessages", () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should fall back to m.file for invalid image files", async () => {
|
it("should use m.image for PNG files which cannot be parsed but successfully thumbnail", async () => {
|
||||||
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
|
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
|
||||||
const file = new File([], "fileName", { type: "image/png" });
|
const file = new File([], "fileName", { type: "image/png" });
|
||||||
await contentMessages.sendContentToRoom(file, roomId, undefined, client, undefined);
|
await contentMessages.sendContentToRoom(file, roomId, undefined, client, undefined);
|
||||||
|
expect(client.sendMessage).toHaveBeenCalledWith(
|
||||||
|
roomId,
|
||||||
|
null,
|
||||||
|
expect.objectContaining({
|
||||||
|
url: "mxc://server/file",
|
||||||
|
msgtype: "m.image",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should fall back to m.file for invalid image files", async () => {
|
||||||
|
mocked(client.uploadContent).mockResolvedValue({ content_uri: "mxc://server/file" });
|
||||||
|
const file = new File([], "fileName", { type: "image/jpeg" });
|
||||||
|
mocked(BlurhashEncoder.instance.getBlurhash).mockRejectedValue("NOT_AN_IMAGE");
|
||||||
|
await contentMessages.sendContentToRoom(file, roomId, undefined, client, undefined);
|
||||||
expect(client.sendMessage).toHaveBeenCalledWith(
|
expect(client.sendMessage).toHaveBeenCalledWith(
|
||||||
roomId,
|
roomId,
|
||||||
null,
|
null,
|
||||||
|
|
Loading…
Reference in New Issue