Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

Organize este código: const fetch = require('node-fetch'); const FormData =…

  1. User

    Organize este código:

        const fetch = require('node-fetch');
        const FormData = require('form-data');
        const fs = require('fs');
        const axios = require('axios');
        async function fileUguu(fileBuffer) {
            const form = new FormData();
            form.append("files[]", fileBuffer, { filename: 'image.jpg' });
            try {
                const response = await axios({
                    url: "https://uguu.se/upload.php",
                    method: "POST",
                    headers: {
                        "User-Agent": "Mozilla/5.0",
                        ...form.getHeaders(),
                    },
                    data: form,
                });
                return response.data.files[0];
            } catch (error) {
                console.log('Error:', error.message);
                throw new Error('Falha ao enviar a imagem para o Uguu.');
            }
        }
        const owgi = await getFileBuffer(boij, "image");
        const imageUrl = await fileUguu(owgi);
        client.sendMessage(from, { text: `Aqui está o link da imagem: ${imageUrl.url}`})
    } else {
        reply('Marque uma imagem para gerar o link.');
    }
    

    } catch (e) { console.log(e); reply('Erro ao processar a imagem.'); } break;

    case 'videourl': case 'videopralink': try { if (isQuotedVideo) { boij = isQuotedVideo ? info.message.extendedTextMessage.contextInfo.quotedMessage.videoMessage : info.message.videoMessage; const fetch = require('node-fetch'); const FormData = require('form-data'); async function uploadVideoToTelegraph(fileBuffer) { const form = new FormData(); form.append("files[]", fileBuffer, { filename: 'image.jpg' }); try { const response = await axios({ url: "https://uguu.se/upload.php", method: "POST", headers: { "User-Agent": "Mozilla/5.0", ...form.getHeaders(),

  2. Assistant

    Sure! Here’s a more organized and structured version of your code. I've made sure to regroup imports, properly format the asynchronous functions, and reduce redundant code:

    const axios = require('axios');
    const FormData = require('form-data');
    const fs = require('fs');
    
    async function uploadFileToUguu(fileBuffer, fileName) {
        const form = new FormData();
        form.append("files[]", fileBuffer, { filename: fileName });
        
        try {
            const response = await axios({
                url: "https://uguu.se/upload.php",
                method: "POST",
                headers: {
                    "User-Agent": "Mozilla/5.0",
                    ...form.getHeaders(),
                },
                data: form,
            });
            return response.data.files[0];
        } catch (error) {
            console.log('Error uploading file:', error.message);
            throw new Error('Falha ao enviar a imagem para o Uguu.');
        }
    }
    
    async function handleImageUpload(isQuotedImage, info) {
        try {
            if (isQuotedImage) {
                const boij = info.message.extendedTextMessage.contextInfo.quotedMessage.imageMessage || info.message.imageMessage;
                const owgi = await getFileBuffer(boij, "image");
                const imageUrl = await uploadFileToUguu(owgi, 'image.jpg');
                client.sendMessage(from, { text: `Aqui está o link da imagem: ${imageUrl.url}` });
            } else {
                reply('Marque uma imagem para gerar o link.');
            }
        } catch (e) {
            console.log(e);
            reply('Erro ao processar a imagem.');
        }
    }
    
    async function handleVideoUpload(isQuotedVideo, info) {
        try {
            if (isQuotedVideo) {
                const boij = info.message.extendedTextMessage.contextInfo.quotedMessage.videoMessage || info.message.videoMessage;
                const videoBuffer = await getFileBuffer(boij, "video");
                const videoUrl = await uploadFileToUguu(videoBuffer, 'video.mp4'); // Assuming similar upload functionality for video
                client.sendMessage(from, { text: `Aqui está o link do vídeo: ${videoUrl.url}` });
            } else {
                reply('Marque um vídeo para gerar o link.');
            }
        } catch (e) {
            console.log(e);
            reply('Erro ao processar o vídeo.');
        }
    }
    
    // Usage
    switch (command) {
        case 'imageurl':
            await handleImageUpload(isQuotedImage, info);
            break;
    
        case 'videourl':
        case 'videopralink':
            await handleVideoUpload(isQuotedVideo, info);
            break;
    }
    

    Key Improvements:

    1. Function Modularity: Separated the logic for handling image uploads and video uploads into their own functions for clarity and reusability.
    2. Consistent Upload Function: Used the same uploadFileToUguu function for both image and video uploads. You can adjust the filename per type if needed.
    3. Error Handling: Centralized error handling, making it clearer and more straightforward to debug.
    4. Code Organization: Improved code structure and readability for easier maintenance and understanding.

    Feel free to modify the path of the file based on your requirements!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.