(mp4Buffer: Buffer)
| 177 | return undefined; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | async function downloadMediaBuffer(client: any, target: any): Promise<Buffer | undefined> { |
| 182 | if (!client || !target) return undefined; |
| 183 | // For small media (thumbnails, static stickers, photos), try raw upload.GetFile first |
| 184 | // Fall back to downloadMedia if raw download fails or isn't applicable |
| 185 | try { |
| 186 | const media = target.media ?? target; |
| 187 | // Try to get document/photo info for raw download |
| 188 | let doc = media?.document ?? media?.photo; |
| 189 | if (doc && doc.id && doc.accessHash) { |
| 190 | // Small files: use raw download |
| 191 | const isLarge = doc.size && doc.size > 1024 * 1024; |
| 192 | if (!isLarge) { |
| 193 | const location = new Api.InputDocumentFileLocation({ |
| 194 | id: doc.id, |
| 195 | accessHash: doc.accessHash, |
| 196 | fileReference: doc.fileReference, |
| 197 | thumbSize: "w", |
| 198 | }); |
| 199 | const rawBuffer = await rawDownloadFile(client, location, doc.dcId); |
| 200 | if (rawBuffer) return rawBuffer; |
| 201 | } |
| 202 | } |
| 203 | } catch (_) {} |
| 204 | // Fallback to downloadMedia with timeout |
| 205 | try { |
| 206 | const downloaded = await withTimeout(client.downloadMedia(target, { outputFile: path.join(os.tmpdir(), `yvlu_media_${Date.now()}_${Math.random().toString(16).slice(2)}`) }), QUOTE_RPC_TIMEOUT_MS, "downloadMediaBuffer.downloadMedia"); |
| 207 | if (Buffer.isBuffer(downloaded)) return downloaded; |
| 208 | if (downloaded && typeof downloaded === "string" && fs.existsSync(downloaded)) return fs.readFileSync(downloaded); |
| 209 | } catch (err: any) { |
| 210 | console.warn("yvlu media download failed", err?.message || err); |
| 211 | } |
| 212 | return undefined; |
| 213 | } |
| 214 | |
| 215 | // Helper to check if file format needs conversion |
| 216 | function needsConversion(buffer: Buffer, mimeType: string | undefined): boolean { |
| 217 | if (!buffer || !mimeType) return false; |
| 218 | return mimeType === "application/x-tgsticker" || // TGS |
| 219 | mimeType === "video/mp4" || mimeType === "image/gif" || // GIF/MP4 |
no test coverage detected