fix(telegram): preserve original filename from Telegram document/audio/video uploads

The downloadAndSaveTelegramFile inner function only used the server-side
file path (e.g. "documents/file_42.pdf") or the Content-Disposition
header (which Telegram doesn't send) to derive the saved filename.
The original filename provided by Telegram via msg.document.file_name,
msg.audio.file_name, msg.video.file_name, and msg.animation.file_name
was never passed through, causing all inbound files to lose their
user-provided names.

Now downloadAndSaveTelegramFile accepts an optional telegramFileName
parameter that takes priority over the fetched/server-side name.
The resolveMedia call site extracts the original name from the message
and passes it through.

Closes #31768

Made-with: Cursor
This commit is contained in:
Kay-051
2026-03-02 22:36:34 +08:00
committed by Peter Steinberger
parent e45d26b9ed
commit da05395c2a
2 changed files with 180 additions and 6 deletions

View File

@@ -53,7 +53,11 @@ export async function resolveMedia(
stickerMetadata?: StickerMetadata;
} | null> {
const msg = ctx.message;
const downloadAndSaveTelegramFile = async (filePath: string, fetchImpl: typeof fetch) => {
const downloadAndSaveTelegramFile = async (
filePath: string,
fetchImpl: typeof fetch,
telegramFileName?: string,
) => {
const url = `https://api.telegram.org/file/bot${token}/${filePath}`;
const fetched = await fetchRemoteMedia({
url,
@@ -62,7 +66,7 @@ export async function resolveMedia(
maxBytes,
ssrfPolicy: TELEGRAM_MEDIA_SSRF_POLICY,
});
const originalName = fetched.fileName ?? filePath;
const originalName = telegramFileName ?? fetched.fileName ?? filePath;
return saveMediaBuffer(fetched.buffer, fetched.contentType, "inbound", maxBytes, originalName);
};
@@ -184,7 +188,12 @@ export async function resolveMedia(
if (!fetchImpl) {
throw new Error("fetch is not available; set channels.telegram.proxy in config");
}
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl);
const telegramFileName =
msg.document?.file_name ??
msg.audio?.file_name ??
msg.video?.file_name ??
msg.animation?.file_name;
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl, telegramFileName);
const placeholder = resolveTelegramMediaPlaceholder(msg) ?? "<media:document>";
return { path: saved.path, contentType: saved.contentType, placeholder };
}