import { Octokit } from '@octokit/rest'; import AdmZip from 'adm-zip'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const octokit = new Octokit({ auth: "ghp_123", userAgent: 'WhatsApp-Bot v1.0' }); const MAX_FILE_SIZE = 100 * 1024 * 1024; // 100MB async function uploadToGitHub(repoName, filePath, fileContent, isBase64 = false) { try { const content = isBase64 ? fileContent : Buffer.from(fileContent).toString('base64'); let sha = null; try { const { data } = await octokit.repos.getContent({ owner: (await octokit.users.getAuthenticated()).data.login, repo: repoName, path: filePath }); sha = data.sha; } catch (error) { if (error.status !== 404) throw error; } await octokit.repos.createOrUpdateFileContents({ owner: (await octokit.users.getAuthenticated()).data.login, repo: repoName, path: filePath, message: `Upload ${filePath}`, content: content, sha: sha }); return true; } catch (error) { console.error('GitHub upload error:', error); throw new Error(`Failed to upload to GitHub: ${error.message}`); } } function GetType(Data) { return new Promise((resolve, reject) => { let Result, Status; if (Buffer.isBuffer(Data)) { Result = Buffer.from(Data).toString("base64"); Status = 0; } else { Status = 1; } resolve({ status: Status, result: Result, }); }); } const handler = async (m, { conn, text, usedPrefix, command }) => { if (!m.quoted) { throw `❌ Mohon reply file ZIP yang akan diupload!\n\nContoh: ${usedPrefix + command} nama-repo`; } if (!text) { throw `❌ Format salah!\nGunakan: ${usedPrefix + command} `; } let tempFilePath; try { const repoName = text.trim(); let media; try { media = await conn.downloadMediaMessage(m.quoted, 'buffer', {}, { reuploadRequest: conn.updateMediaMessage }); } catch (downloadError) { console.error('Download error:', downloadError); throw "❌ Gagal mendownload file. Pastikan Anda mereply file ZIP yang valid."; } if (!media || media.length === 0) { throw "❌ File kosong atau tidak valid!"; } const tempDir = './tmp/'; if (!fs.existsSync(tempDir)) { fs.mkdirSync(tempDir, { recursive: true }); } let fileName = `upload_${Date.now()}.zip`; const quotedMsg = m.quoted.message; const docMsg = quotedMsg?.documentMessage || quotedMsg?.extendedTextMessage?.contextInfo?.quotedMessage?.documentMessage; if (docMsg?.fileName) { fileName = docMsg.fileName; } tempFilePath = path.join(tempDir, fileName); fs.writeFileSync(tempFilePath, media); const zip = new AdmZip(tempFilePath); const zipEntries = zip.getEntries(); let extractedCount = 0; let uploadedFolders = new Set(); let rootFolder = null; // Cari root folder for (const entry of zipEntries) { if (entry.isDirectory) { const pathParts = entry.entryName.split('/').filter(p => p); if (pathParts.length === 1) { rootFolder = pathParts[0]; break; } } } // Upload file-file dari ZIP for (const entry of zipEntries) { if (entry.isDirectory) continue; const fullPath = entry.entryName; let targetPath = fullPath; // Hilangkan root folder dari path jika ada if (rootFolder && fullPath.startsWith(rootFolder + '/')) { targetPath = fullPath.substring(rootFolder.length + 1); } // Buat folder structure const pathParts = targetPath.split('/').filter(p => p); if (pathParts.length > 1) { let currentPath = ''; for (let i = 0; i < pathParts.length - 1; i++) { currentPath += (currentPath ? '/' : '') + pathParts[i]; if (!uploadedFolders.has(currentPath)) { await uploadToGitHub(repoName, currentPath + '/.gitkeep', Buffer.from(''), false); uploadedFolders.add(currentPath); } } } // Upload file await uploadToGitHub(repoName, targetPath, entry.getData()); extractedCount++; } // Upload file ZIP asli juga await uploadToGitHub(repoName, fileName, media); const userInfo = await octokit.users.getAuthenticated(); const githubUrl = `https://github.com/${userInfo.data.login}/${repoName}`; m.reply(`✅ Berhasil mengupload file ke GitHub!\n\n📦 File diupload: ${extractedCount}\n📁 Folder dibuat: ${uploadedFolders.size}\n🔗 Repository: ${githubUrl}`); } catch (e) { console.error('Error:', e); throw `❌ Gagal memproses: ${e.message}`; } finally { // Cleanup temporary file if (tempFilePath && fs.existsSync(tempFilePath)) { fs.unlinkSync(tempFilePath); } } } handler.help = ['upfile ']; handler.tags = ['tools']; handler.command = ['upfile', 'uploadfile']; export default handler;