Skip to content

Commit 2cb482d

Browse files
VectorPeakchatgpt-codex-connector[bot]steipete
authored
fix(discord): split encoded video URLs from captions (#101815)
* fix(discord): split encoded video URLs from captions Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com> * refactor(discord): scope media filename decoding --------- Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 7daa090 commit 2cb482d

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from "vitest";
2+
import { isLikelyDiscordVideoMedia } from "./media-detection.js";
3+
4+
describe("isLikelyDiscordVideoMedia", () => {
5+
it.each([
6+
["plain local path", "/tmp/render.MP4?download=1", true],
7+
["encoded extension dot", "https://cdn.example/render%2Emp4?download=1", true],
8+
["encoded extension characters", "https://cdn.example/render%2Em%70%34", true],
9+
["malformed earlier segment", "https://cdn.example/bad%ZZ/render%2Emp4", true],
10+
["encoded suffix after extension", "https://cdn.example/render.mp4%2Fpreview", false],
11+
["double-encoded dot", "https://cdn.example/render%252Emp4", false],
12+
["non-video extension", "https://cdn.example/render%2Ejpg", false],
13+
])("classifies %s", (_name, mediaUrl, expected) => {
14+
expect(isLikelyDiscordVideoMedia(mediaUrl)).toBe(expected);
15+
});
16+
});

extensions/discord/src/media-detection.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ function normalizeMediaPathForExtension(mediaUrl: string): string {
1010
}
1111
try {
1212
const parsed = new URL(trimmed);
13-
return normalizeLowercaseStringOrEmpty(parsed.pathname);
13+
const fileName = parsed.pathname.slice(parsed.pathname.lastIndexOf("/") + 1);
14+
// Mirror media-loader filename decoding without reinterpreting escapes in
15+
// earlier URL path segments, which are irrelevant to the file extension.
16+
try {
17+
return normalizeLowercaseStringOrEmpty(decodeURIComponent(fileName));
18+
} catch {
19+
return normalizeLowercaseStringOrEmpty(fileName);
20+
}
1421
} catch {
1522
const withoutHash = trimmed.split("#", 1)[0] ?? trimmed;
1623
const withoutQuery = withoutHash.split("?", 1)[0] ?? withoutHash;

extensions/discord/src/outbound-adapter.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,14 @@ describe("discordOutbound", () => {
588588
it.each([
589589
{
590590
name: "implicit first-mode",
591+
mediaUrl: "/tmp/render.mp4",
591592
replyToIdSource: "implicit" as const,
592593
replyToMode: "first" as const,
593594
expectedReplies: [{ messageId: "reply-1", scope: "first" }, undefined],
594595
},
595596
{
596597
name: "implicit all-mode",
598+
mediaUrl: "/tmp/render.mp4",
597599
replyToIdSource: "implicit" as const,
598600
replyToMode: "all" as const,
599601
expectedReplies: [
@@ -603,6 +605,17 @@ describe("discordOutbound", () => {
603605
},
604606
{
605607
name: "explicit first-mode",
608+
mediaUrl: "/tmp/render.mp4",
609+
replyToIdSource: "explicit" as const,
610+
replyToMode: "first" as const,
611+
expectedReplies: [
612+
{ messageId: "reply-1", scope: "all" },
613+
{ messageId: "reply-1", scope: "all" },
614+
],
615+
},
616+
{
617+
name: "encoded URL extension",
618+
mediaUrl: "https://cdn.discordapp.com/attachments/1/render%2Emp4?ex=1",
606619
replyToIdSource: "explicit" as const,
607620
replyToMode: "first" as const,
608621
expectedReplies: [
@@ -615,7 +628,7 @@ describe("discordOutbound", () => {
615628
cfg: {},
616629
to: "channel:123456",
617630
text: "rendered clip",
618-
mediaUrl: "/tmp/render.mp4",
631+
mediaUrl: testCase.mediaUrl,
619632
accountId: "default",
620633
replyToId: "reply-1",
621634
replyToIdSource: testCase.replyToIdSource,
@@ -639,7 +652,7 @@ describe("discordOutbound", () => {
639652
expect(mediaCall[1]).toBe("");
640653
const mediaOptions = mockObjectArg(hoisted.sendMessageDiscordMock, "sendMessageDiscord", 1, 2);
641654
expect(mediaOptions.accountId).toBe("default");
642-
expect(mediaOptions.mediaUrl).toBe("/tmp/render.mp4");
655+
expect(mediaOptions.mediaUrl).toBe(testCase.mediaUrl);
643656
expect(mediaOptions.reply).toEqual(testCase.expectedReplies[1]);
644657
});
645658

0 commit comments

Comments
 (0)