feat(line): add outbound media support for image, video, and audio
pnpm install --frozen-lockfile pnpm build pnpm check pnpm vitest run extensions/line/src/channel.sendPayload.test.ts extensions/line/src/send.test.ts extensions/line/src/outbound-media.test.ts Co-authored-by: masatohoshino <246810661+masatohoshino@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>feature/PAZ-358-fe-credit-error-display
parent
f93ccc3443
commit
9449e54f4f
@ -0,0 +1,137 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { detectLineMediaKind, resolveLineOutboundMedia, validateLineMediaUrl } from "./outbound-media.js";
|
||||
|
||||
describe("validateLineMediaUrl", () => {
|
||||
it("accepts HTTPS URL", () => {
|
||||
expect(() => validateLineMediaUrl("https://example.com/image.jpg")).not.toThrow();
|
||||
});
|
||||
|
||||
it("accepts uppercase HTTPS scheme", () => {
|
||||
expect(() => validateLineMediaUrl("HTTPS://EXAMPLE.COM/img.jpg")).not.toThrow();
|
||||
});
|
||||
|
||||
it("rejects HTTP URL", () => {
|
||||
expect(() => validateLineMediaUrl("http://example.com/image.jpg")).toThrow(/must use HTTPS/i);
|
||||
});
|
||||
|
||||
it("rejects URL longer than 2000 chars", () => {
|
||||
const longUrl = `https://example.com/${"a".repeat(1981)}`;
|
||||
expect(longUrl.length).toBeGreaterThan(2000);
|
||||
expect(() => validateLineMediaUrl(longUrl)).toThrow(/2000 chars or less/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe("detectLineMediaKind", () => {
|
||||
it("maps image MIME to image", () => {
|
||||
expect(detectLineMediaKind("image/jpeg")).toBe("image");
|
||||
});
|
||||
|
||||
it("maps uppercase image MIME to image", () => {
|
||||
expect(detectLineMediaKind("IMAGE/JPEG")).toBe("image");
|
||||
});
|
||||
|
||||
it("maps video MIME to video", () => {
|
||||
expect(detectLineMediaKind("video/mp4")).toBe("video");
|
||||
});
|
||||
|
||||
it("maps audio MIME to audio", () => {
|
||||
expect(detectLineMediaKind("audio/mpeg")).toBe("audio");
|
||||
});
|
||||
|
||||
it("falls back unknown MIME to image", () => {
|
||||
expect(detectLineMediaKind("application/octet-stream")).toBe("image");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveLineOutboundMedia", () => {
|
||||
it("respects explicit media kind without remote MIME probing", async () => {
|
||||
await expect(
|
||||
resolveLineOutboundMedia("https://example.com/download?id=123", { mediaKind: "video" }),
|
||||
).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/download?id=123",
|
||||
mediaKind: "video",
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves explicit video kind when a preview URL is provided", async () => {
|
||||
await expect(
|
||||
resolveLineOutboundMedia("https://example.com/download?id=123", {
|
||||
mediaKind: "video",
|
||||
previewImageUrl: "https://example.com/preview.jpg",
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/download?id=123",
|
||||
mediaKind: "video",
|
||||
previewImageUrl: "https://example.com/preview.jpg",
|
||||
});
|
||||
});
|
||||
|
||||
it("infers audio kind from explicit duration metadata when mediaKind is omitted", async () => {
|
||||
await expect(
|
||||
resolveLineOutboundMedia("https://example.com/download?id=audio", {
|
||||
durationMs: 60000,
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/download?id=audio",
|
||||
mediaKind: "audio",
|
||||
durationMs: 60000,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not infer video from previewImageUrl alone", async () => {
|
||||
await expect(
|
||||
resolveLineOutboundMedia("https://example.com/image.jpg", {
|
||||
previewImageUrl: "https://example.com/preview.jpg",
|
||||
}),
|
||||
).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/image.jpg",
|
||||
mediaKind: "image",
|
||||
previewImageUrl: "https://example.com/preview.jpg",
|
||||
});
|
||||
});
|
||||
|
||||
it("infers media kinds from known HTTPS file extensions", async () => {
|
||||
await expect(resolveLineOutboundMedia("https://example.com/audio.mp3")).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/audio.mp3",
|
||||
mediaKind: "audio",
|
||||
});
|
||||
await expect(resolveLineOutboundMedia("https://example.com/video.mp4")).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/video.mp4",
|
||||
mediaKind: "video",
|
||||
});
|
||||
await expect(resolveLineOutboundMedia("https://example.com/image.jpg")).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/image.jpg",
|
||||
mediaKind: "image",
|
||||
});
|
||||
});
|
||||
|
||||
it("validates previewImageUrl when provided", async () => {
|
||||
await expect(
|
||||
resolveLineOutboundMedia("https://example.com/video.mp4", {
|
||||
mediaKind: "video",
|
||||
previewImageUrl: "http://example.com/preview.jpg",
|
||||
}),
|
||||
).rejects.toThrow(/must use HTTPS/i);
|
||||
});
|
||||
|
||||
it("falls back to image when no explicit LINE media options or known extension are present", async () => {
|
||||
await expect(
|
||||
resolveLineOutboundMedia("https://example.com/download?id=audio"),
|
||||
).resolves.toEqual({
|
||||
mediaUrl: "https://example.com/download?id=audio",
|
||||
mediaKind: "image",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects local paths because LINE outbound media requires public HTTPS URLs", async () => {
|
||||
await expect(resolveLineOutboundMedia("./assets/image.jpg")).rejects.toThrow(
|
||||
/requires a public https url/i,
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects non-HTTPS URL explicitly", async () => {
|
||||
await expect(resolveLineOutboundMedia("http://example.com/image.jpg")).rejects.toThrow(
|
||||
/must use HTTPS/i,
|
||||
);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,110 @@
|
||||
export type LineOutboundMediaKind = "image" | "video" | "audio";
|
||||
|
||||
export type LineOutboundMediaResolved = {
|
||||
mediaUrl: string;
|
||||
mediaKind: LineOutboundMediaKind;
|
||||
previewImageUrl?: string;
|
||||
durationMs?: number;
|
||||
trackingId?: string;
|
||||
};
|
||||
|
||||
type ResolveLineOutboundMediaOpts = {
|
||||
mediaKind?: LineOutboundMediaKind;
|
||||
previewImageUrl?: string;
|
||||
durationMs?: number;
|
||||
trackingId?: string;
|
||||
};
|
||||
|
||||
export function validateLineMediaUrl(url: string): void {
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
} catch {
|
||||
throw new Error(`LINE outbound media URL must be a valid URL: ${url}`);
|
||||
}
|
||||
if (parsed.protocol !== "https:") {
|
||||
throw new Error(`LINE outbound media URL must use HTTPS: ${url}`);
|
||||
}
|
||||
if (url.length > 2000) {
|
||||
throw new Error(`LINE outbound media URL must be 2000 chars or less (got ${url.length})`);
|
||||
}
|
||||
}
|
||||
|
||||
export function detectLineMediaKind(mimeType: string): LineOutboundMediaKind {
|
||||
const normalized = mimeType.toLowerCase();
|
||||
if (normalized.startsWith("image/")) {
|
||||
return "image";
|
||||
}
|
||||
if (normalized.startsWith("video/")) {
|
||||
return "video";
|
||||
}
|
||||
if (normalized.startsWith("audio/")) {
|
||||
return "audio";
|
||||
}
|
||||
return "image";
|
||||
}
|
||||
|
||||
function isHttpsUrl(url: string): boolean {
|
||||
try {
|
||||
return new URL(url).protocol === "https:";
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function detectLineMediaKindFromUrl(url: string): LineOutboundMediaKind | undefined {
|
||||
try {
|
||||
const pathname = new URL(url).pathname.toLowerCase();
|
||||
if (/\.(png|jpe?g|gif|webp|bmp|heic|heif|avif)$/i.test(pathname)) {
|
||||
return "image";
|
||||
}
|
||||
if (/\.(mp4|mov|m4v|webm)$/i.test(pathname)) {
|
||||
return "video";
|
||||
}
|
||||
if (/\.(mp3|m4a|aac|wav|ogg|oga)$/i.test(pathname)) {
|
||||
return "audio";
|
||||
}
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export async function resolveLineOutboundMedia(
|
||||
mediaUrl: string,
|
||||
opts: ResolveLineOutboundMediaOpts = {},
|
||||
): Promise<LineOutboundMediaResolved> {
|
||||
const trimmedUrl = mediaUrl.trim();
|
||||
if (isHttpsUrl(trimmedUrl)) {
|
||||
validateLineMediaUrl(trimmedUrl);
|
||||
const previewImageUrl = opts.previewImageUrl?.trim();
|
||||
if (previewImageUrl) {
|
||||
validateLineMediaUrl(previewImageUrl);
|
||||
}
|
||||
const mediaKind =
|
||||
opts.mediaKind ??
|
||||
(typeof opts.durationMs === "number" ? "audio" : undefined) ??
|
||||
(opts.trackingId?.trim() ? "video" : undefined) ??
|
||||
detectLineMediaKindFromUrl(trimmedUrl) ??
|
||||
"image";
|
||||
return {
|
||||
mediaUrl: trimmedUrl,
|
||||
mediaKind,
|
||||
...(previewImageUrl ? { previewImageUrl } : {}),
|
||||
...(typeof opts.durationMs === "number" ? { durationMs: opts.durationMs } : {}),
|
||||
...(opts.trackingId ? { trackingId: opts.trackingId } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = new URL(trimmedUrl);
|
||||
if (parsed.protocol !== "https:") {
|
||||
throw new Error(`LINE outbound media URL must use HTTPS: ${trimmedUrl}`);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof Error && e.message.startsWith("LINE outbound")) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
throw new Error("LINE outbound media currently requires a public HTTPS URL");
|
||||
}
|
||||
Loading…
Reference in New Issue