| 22 | // Prefer link (gif/image). Send as sticker if applicable; fallback to image |
| 23 | // helper to convert media buffer to sticker webp |
| 24 | async function convertMediaToSticker(mediaBuffer, isAnimated) { |
| 25 | const tmpDir = path.join(process.cwd(), 'tmp'); |
| 26 | if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir, { recursive: true }); |
| 27 | |
| 28 | const inputExt = isAnimated ? 'gif' : 'jpg'; |
| 29 | const input = path.join(tmpDir, `animu_${Date.now()}.${inputExt}`); |
| 30 | const output = path.join(tmpDir, `animu_${Date.now()}.webp`); |
| 31 | fs.writeFileSync(input, mediaBuffer); |
| 32 | |
| 33 | const ffmpegCmd = isAnimated |
| 34 | ? `ffmpeg -y -i "${input}" -vf "scale=512:512:force_original_aspect_ratio=decrease,pad=512:512:(ow-iw)/2:(oh-ih)/2:color=#00000000,fps=15" -c:v libwebp -preset default -loop 0 -vsync 0 -pix_fmt yuva420p -quality 60 -compression_level 6 "${output}"` |
| 35 | : `ffmpeg -y -i "${input}" -vf "scale=512:512:force_original_aspect_ratio=decrease,pad=512:512:(ow-iw)/2:(oh-ih)/2:color=#00000000" -c:v libwebp -preset default -loop 0 -vsync 0 -pix_fmt yuva420p -quality 75 -compression_level 6 "${output}"`; |
| 36 | |
| 37 | await new Promise((resolve, reject) => { |
| 38 | exec(ffmpegCmd, (err) => (err ? reject(err) : resolve())); |
| 39 | }); |
| 40 | |
| 41 | let webpBuffer = fs.readFileSync(output); |
| 42 | |
| 43 | // Add sticker metadata |
| 44 | const img = new webp.Image(); |
| 45 | await img.load(webpBuffer); |
| 46 | |
| 47 | const json = { |
| 48 | 'sticker-pack-id': crypto.randomBytes(32).toString('hex'), |
| 49 | 'sticker-pack-name': 'Anime Stickers', |
| 50 | 'emojis': ['🎌'] |
| 51 | }; |
| 52 | const exifAttr = Buffer.from([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00]); |
| 53 | const jsonBuffer = Buffer.from(JSON.stringify(json), 'utf8'); |
| 54 | const exif = Buffer.concat([exifAttr, jsonBuffer]); |
| 55 | exif.writeUIntLE(jsonBuffer.length, 14, 4); |
| 56 | img.exif = exif; |
| 57 | |
| 58 | const finalBuffer = await img.save(null); |
| 59 | |
| 60 | try { fs.unlinkSync(input); } catch {} |
| 61 | try { fs.unlinkSync(output); } catch {} |
| 62 | return finalBuffer; |
| 63 | } |
| 64 | |
| 65 | if (data.link) { |
| 66 | const link = data.link; |