File size: 5,091 Bytes
791adbe
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
{
  "id": "57b8aa94-5183-4ab4-9279-04a2abec01b7",
  "title": "catbox.js",
  "content": "this.zuckbotconfig = {\n  name: \"catbox\",\n  version: \"3.0.0\",\n  author: \"Nguyễn Thanh Tuấn\",\n  role: 0,\n  aliases: [\"upcat\"],\n  info: \"Upload ảnh, gif, mp3 lên Catbox (hỗ trợ stream, retry, worker, chunk)\",\n  Category: \"Tiện ích\",\n  usePrefix: false,\n  cd: 0,\n  shadowPrefix: false,\n  image: []\n};\n\nthis.onRun = async ({ api, event }) => {\n  const axios = require(\"axios\");\n  const FormData = require(\"form-data\");\n  const https = require(\"https\");\n  const fs = require(\"fs\");\n  const { Worker } = require(\"worker_threads\");\n\n  if (!event.messageReply?.attachments?.length) {\n    return api.sendMessage(\"Bạn chưa gửi ảnh, gif, mp3!\", event.threadID, event.messageID);\n  }\n\n  // 🔄 Dynamic Connection Pool\n  const agent = new https.Agent({ keepAlive: true });\n\n  // 🔁 Adaptive Retry\n  async function retryRequest(fn, maxAttempts = 5, baseDelay = 1000, maxDelay = 20000) {\n    for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n      try {\n        return await fn();\n      } catch (err) {\n        if (attempt === maxAttempts) throw err;\n        let delay = Math.min(baseDelay * 2 ** (attempt - 1), maxDelay);\n        delay += Math.random() * (delay / 2); // jitter\n        await new Promise(r => setTimeout(r, delay));\n      }\n    }\n  }\n\n  const links = [];\n\n  for (const attachment of event.messageReply.attachments) {\n    const validTypes = [\"photo\", \"video\", \"audio\", \"animated_image\"];\n    if (!validTypes.includes(attachment.type)) continue;\n\n    const ext = attachment.type === \"photo\" ? \"png\"\n              : attachment.type === \"animated_image\" ? \"gif\"\n              : attachment.type === \"video\" ? \"mp4\"\n              : \"mp3\";\n\n    try {\n      // 🗜 Stream Processing: tải file qua stream\n      const response = await axios.get(attachment.url, {\n        responseType: \"stream\",\n        httpsAgent: agent\n      });\n\n      // Nếu file lớn hơn 20MB → dùng Parallel Chunked Upload\n      let size = parseInt(attachment.size || 0);\n      if (size > 20 * 1024 * 1024) {\n        console.log(`⚡ Dùng Chunked Upload cho file lớn (${(size/1024/1024).toFixed(1)}MB)`);\n\n        const chunks = Math.ceil(size / (5 * 1024 * 1024)); // 5MB/chunk\n        const promises = [];\n\n        for (let i = 0; i < chunks; i++) {\n          promises.push(new Promise((resolve, reject) => {\n            const worker = new Worker(`\n              const { parentPort, workerData } = require(\"worker_threads\");\n              const axios = require(\"axios\");\n              const FormData = require(\"form-data\");\n              const fs = require(\"fs\");\n\n              (async () => {\n                const { url, start, end, index, ext } = workerData;\n                const res = await axios.get(url, { responseType: \"stream\", headers: { Range: \\`bytes=\\${start}-\\${end}\\` } });\n                const form = new FormData();\n                form.append(\"reqtype\", \"fileupload\");\n                form.append(\"userhash\", \"\");\n                form.append(\"fileToUpload\", res.data, \\`chunk_\\${index}.\\${ext}\\`);\n                const upload = await axios.post(\"https://catbox.moe/user/api.php\", form, { headers: form.getHeaders() });\n                parentPort.postMessage(upload.data);\n              })();\n            `, { eval: true, workerData: { url: attachment.url, start: i*5*1024*1024, end: Math.min((i+1)*5*1024*1024-1, size), index: i, ext } });\n            worker.on(\"message\", resolve);\n            worker.on(\"error\", reject);\n          }));\n        }\n\n        const results = await Promise.all(promises);\n        links.push(...results);\n\n      } else {\n        // File nhỏ → upload trực tiếp với retry\n        const form = new FormData();\n        form.append(\"reqtype\", \"fileupload\");\n        form.append(\"userhash\", \"\");\n        form.append(\"fileToUpload\", response.data, `upload.${ext}`);\n\n        const uploadResponse = await retryRequest(() =>\n          axios.post(\"https://catbox.moe/user/api.php\", form, {\n            headers: form.getHeaders(),\n            httpsAgent: agent,\n            timeout: 30000\n          })\n        );\n\n        const data = uploadResponse.data;\n        if (data.startsWith(\"Error:\")) {\n          api.sendMessage(\"❌ Lỗi khi upload: \" + data, event.threadID, event.messageID);\n        } else {\n          links.push(data);\n        }\n      }\n\n    } catch (err) {\n      api.sendMessage(`❌ Upload thất bại: ${err.message}`, event.threadID, event.messageID);\n    }\n  }\n\n  if (!links.length) {\n    return api.sendMessage(\"Không có file hợp lệ để upload!\", event.threadID, event.messageID);\n  }\n\n  return api.sendMessage(\"✅ Link Catbox:\\n\" + links.join(\"\\n\"), event.threadID, event.messageID);\n};",
  "language": "javascript",
  "createdAt": 1756745064726,
  "updatedAt": 1756745064726
}