refactor(telegram): dedupe media download

This commit is contained in:
Peter Steinberger
2026-02-15 16:21:42 +00:00
parent cc0bfa0f39
commit 6b65a055e6

View File

@@ -309,6 +309,16 @@ export async function resolveMedia(
stickerMetadata?: StickerMetadata;
} | null> {
const msg = ctx.message;
const downloadAndSaveTelegramFile = async (filePath: string, fetchImpl: typeof fetch) => {
const url = `https://api.telegram.org/file/bot${token}/${filePath}`;
const fetched = await fetchRemoteMedia({
url,
fetchImpl,
filePathHint: filePath,
});
const originalName = fetched.fileName ?? filePath;
return saveMediaBuffer(fetched.buffer, fetched.contentType, "inbound", maxBytes, originalName);
};
// Handle stickers separately - only static stickers (WEBP) are supported
if (msg.sticker) {
@@ -333,20 +343,7 @@ export async function resolveMedia(
logVerbose("telegram: fetch not available for sticker download");
return null;
}
const url = `https://api.telegram.org/file/bot${token}/${file.file_path}`;
const fetched = await fetchRemoteMedia({
url,
fetchImpl,
filePathHint: file.file_path,
});
const originalName = fetched.fileName ?? file.file_path;
const saved = await saveMediaBuffer(
fetched.buffer,
fetched.contentType,
"inbound",
maxBytes,
originalName,
);
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl);
// Check sticker cache for existing description
const cached = sticker.file_unique_id ? getCachedSticker(sticker.file_unique_id) : null;
@@ -431,20 +428,7 @@ export async function resolveMedia(
if (!fetchImpl) {
throw new Error("fetch is not available; set channels.telegram.proxy in config");
}
const url = `https://api.telegram.org/file/bot${token}/${file.file_path}`;
const fetched = await fetchRemoteMedia({
url,
fetchImpl,
filePathHint: file.file_path,
});
const originalName = fetched.fileName ?? file.file_path;
const saved = await saveMediaBuffer(
fetched.buffer,
fetched.contentType,
"inbound",
maxBytes,
originalName,
);
const saved = await downloadAndSaveTelegramFile(file.file_path, fetchImpl);
let placeholder = "<media:document>";
if (msg.photo) {
placeholder = "<media:image>";