refactor: simplify attachment handling in forwardMessage function by removing buffer downloads and using URLs directly for media uploads

This commit is contained in:
devilreef 2026-01-08 22:52:54 +06:00
parent 2643756443
commit 53177e554f

View file

@ -1,17 +1,8 @@
import type { ForwardMessageOptions } from '../types.js' import type { ForwardMessageOptions } from '../types.js'
import { Buffer } from 'node:buffer'
import { MediaUpload } from 'wrappergram' import { MediaUpload } from 'wrappergram'
import { config } from '../config.js' import { config } from '../config.js'
import { telegram } from './client.js' import { telegram } from './client.js'
const MAX_FILE_SIZE = 50 * 1024 * 1024 // 50MB Telegram limit
interface DownloadedAttachment {
buffer: Buffer
name: string
contentType: string | null
}
export async function forwardMessage(opts: ForwardMessageOptions): Promise<void> { export async function forwardMessage(opts: ForwardMessageOptions): Promise<void> {
const { topicId, author, role, channel, content, attachments, messageLink } = opts const { topicId, author, role, channel, content, attachments, messageLink } = opts
@ -32,54 +23,32 @@ export async function forwardMessage(opts: ForwardMessageOptions): Promise<void>
if (attachments.length === 0) if (attachments.length === 0)
return return
// Download all attachments first
const downloaded: DownloadedAttachment[] = []
for (const att of attachments) {
try {
const response = await fetch(att.url)
const buffer = Buffer.from(await response.arrayBuffer())
if (buffer.length > MAX_FILE_SIZE) {
console.warn(`Skipping attachment ${att.name}: exceeds 50MB limit`)
continue
}
downloaded.push({ buffer, name: att.name, contentType: att.contentType })
}
catch (err) {
console.error(`Failed to download attachment ${att.name}:`, err)
}
}
if (downloaded.length === 0)
return
// Separate media (photos/videos) from documents // Separate media (photos/videos) from documents
const media: DownloadedAttachment[] = [] const media = attachments.filter((att) => {
const documents: DownloadedAttachment[] = []
for (const att of downloaded) {
const isImage = att.contentType?.startsWith('image/') const isImage = att.contentType?.startsWith('image/')
const isVideo = att.contentType?.startsWith('video/') const isVideo = att.contentType?.startsWith('video/')
return isImage || isVideo
if (isImage || isVideo) { })
media.push(att) const documents = attachments.filter((att) => {
} const isImage = att.contentType?.startsWith('image/')
else { const isVideo = att.contentType?.startsWith('video/')
documents.push(att) return !isImage && !isVideo
} })
}
// Send media as group or single // Send media as group or single
if (media.length > 1) { if (media.length > 1) {
try { try {
const mediaItems = await Promise.all(media.map(async (att) => {
const file = await MediaUpload.url(att.url)
return {
type: att.contentType?.startsWith('video/') ? 'video' as const : 'photo' as const,
media: file,
}
}))
await telegram.api.sendMediaGroup({ await telegram.api.sendMediaGroup({
chat_id: config.telegram.chatId, chat_id: config.telegram.chatId,
message_thread_id: topicId, message_thread_id: topicId,
media: media.map(att => ({ media: mediaItems,
type: att.contentType?.startsWith('video/') ? 'video' : 'photo',
media: MediaUpload.buffer(att.buffer, att.name),
})),
}) })
} }
catch (err) { catch (err) {
@ -92,14 +61,14 @@ export async function forwardMessage(opts: ForwardMessageOptions): Promise<void>
if (att.contentType?.startsWith('video/')) { if (att.contentType?.startsWith('video/')) {
await telegram.api.sendVideo({ await telegram.api.sendVideo({
chat_id: config.telegram.chatId, chat_id: config.telegram.chatId,
video: MediaUpload.buffer(att.buffer, att.name), video: await MediaUpload.url(att.url),
message_thread_id: topicId, message_thread_id: topicId,
}) })
} }
else { else {
await telegram.api.sendPhoto({ await telegram.api.sendPhoto({
chat_id: config.telegram.chatId, chat_id: config.telegram.chatId,
photo: MediaUpload.buffer(att.buffer, att.name), photo: await MediaUpload.url(att.url),
message_thread_id: topicId, message_thread_id: topicId,
}) })
} }
@ -114,7 +83,7 @@ export async function forwardMessage(opts: ForwardMessageOptions): Promise<void>
try { try {
await telegram.api.sendDocument({ await telegram.api.sendDocument({
chat_id: config.telegram.chatId, chat_id: config.telegram.chatId,
document: MediaUpload.buffer(att.buffer, att.name), document: await MediaUpload.url(att.url),
message_thread_id: topicId, message_thread_id: topicId,
}) })
} }