<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: boxser778</title>
    <description>The latest articles on Forem by boxser778 (@boxser778).</description>
    <link>https://forem.com/boxser778</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F886571%2F64574404-55e5-45c6-95ec-b26ac4cdcae1.png</url>
      <title>Forem: boxser778</title>
      <link>https://forem.com/boxser778</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/boxser778"/>
    <language>en</language>
    <item>
      <title>Seeking Help with react</title>
      <dc:creator>boxser778</dc:creator>
      <pubDate>Sun, 03 Jul 2022 16:55:51 +0000</pubDate>
      <link>https://forem.com/boxser778/seeking-help-with-react-4lc1</link>
      <guid>https://forem.com/boxser778/seeking-help-with-react-4lc1</guid>
      <description>&lt;p&gt;hi , i got a little problem with my project .&lt;br&gt;
i try to create youtube clone just for trying .&lt;/p&gt;

&lt;p&gt;i ran into a problem when i upload video and the back create thumbnail its actuly not showen on the client side . i belive or thing that its related to Provider in react .&lt;br&gt;
would realy like to get some help becouse i searched everywere and could not find answer to my problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://giphy.com/gifs/fJA5VRltmMKl0OuIDZ"&gt;https://giphy.com/gifs/fJA5VRltmMKl0OuIDZ&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;this is the error i get on dev tool&lt;br&gt;
&lt;code&gt;&lt;br&gt;
:5000/uploads/thumbnails/thumbnail-1656867800686_1575467783672_Space%20-%2021542.png:1          GET http://localhost:5000/uploads/thumbnails/thumbnail-1656867800686_1575467783672_Space%20-%2021542.png net::ERR_CONNECTION_REFUSED&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;client side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';
import { Typography, Button, Form, message, Input } from 'antd';
import Dropzone from 'react-dropzone';
import Axios from 'axios';
import {getCurrentUser} from "../../services/userService"

const apiUrl = process.env.REACT_APP_API_URL;
const { TextArea } = Input;
const { Title } = Typography;

const PrivateOptions = [
    { value: 0, label: "Private" },
    { value: 1, label: "Public" }
]

const CategoryOptions = [
    { value: 0, label: "Film &amp;amp; Animation" },
    { value: 1, label: "Autos &amp;amp; Vehicles" },
    { value: 2, label: "Music" },
    { value: 3, label: "Pets &amp;amp; Animals" },
    { value: 4, label: "Sports" }
]

function VideoUploadPage(props) {

    const user = getCurrentUser();

    const [VideoTitle, setVideoTitle] = useState("")
    const [Description, setDescription] = useState("")
    const [Private, setPrivate] = useState(0)
    const [Category, setCategory] = useState("Film &amp;amp; Animation")
    const [FilePath, setFilePath] = useState("")
    const [Duration, setDuration] = useState("")
    const [ThumbnailPath, setThumbnailPath] = useState("")

    const onTitleChange = (e) =&amp;gt; {
        setVideoTitle(e.currentTarget.value)
    }

    const onDescriptionChange = (e) =&amp;gt; {
        setDescription(e.currentTarget.value)
    }

    const onPrivateChange = (e) =&amp;gt; {
        setPrivate(e.currentTarget.value)
    }

    const onCategoryChange = (e) =&amp;gt; {
        setCategory(e.currentTarget.value)
    }

    const onDropFuc = (files) =&amp;gt; {
        let formData = new FormData();
        const config = {
            header: {'content-type': 'multipart/form-data'}
        }

        formData.append("file", files[0])

        Axios.post(`${apiUrl}/videos/uploadfiles`, formData, config)
            .then(response =&amp;gt; {
                if(response.data.success){

                    let variable = {
                        url: response.data.url,
                        fileName: response.data.fileName
                    }

                    setFilePath(response.data.url)

                    Axios.post(`${apiUrl}/videos/thumbnail`, variable)
                        .then(response =&amp;gt; {
                            if(response.data.success){
                                setDuration(response.data.fileDuration)
                                setThumbnailPath(response.data.url)
                            } else {
                                alert("ERROR: Failed to display a thumbnail.")
                            }
                        })

                } else {
                    alert('ERROR: Failed to upload a video');
                }
            })
    }

    const onSubmit = (e) =&amp;gt; {
        e.preventDefault();

        const variables = {
            writer: user._id,
            title: VideoTitle,
            description: Description,
            privacy: Private,
            filePath: FilePath,
            category: Category,
            duration: Duration,
            thumbnail: ThumbnailPath
        }

        Axios.post(`${apiUrl}/videos/uploadvideo`, variables)
            .then(response =&amp;gt; {
                if(response.data.success){
                    message.success('Successfully uploaded the video!');
                    // setTimeout(() =&amp;gt; {
                    //     props.history.push('/') // After 2 seconds of video upload, move to the Landingpage.
                    // }, 2000);
                } else {
                    alert("ERROR: Failed to upload the video.")
                }
            })
        }

    return (
        &amp;lt;div style={{ maxWidth: '700px', margin: '2rem auto'}}&amp;gt;
            &amp;lt;div style={{ textAlign: 'center', marginBottom: '2rem' }}&amp;gt;
                &amp;lt;Title level={2}&amp;gt; Upload Video &amp;lt;/Title&amp;gt;
            &amp;lt;/div&amp;gt;

            &amp;lt;Form onSubmit={onSubmit}&amp;gt;
                &amp;lt;div style={{ display: 'flex', justifyContent: 'space-between' }}&amp;gt;
                    &amp;lt;Dropzone
                        onDrop = {onDropFuc}
                        multiple = {false}
                        maxsize = {100000000}
                    &amp;gt;
                        {({getRootProps, getInputProps}) =&amp;gt; (
                            &amp;lt;div style={{ width: '300px', height: '240px', border: '1px solid lightgray', 
                            display: 'flex', alignItems: 'center', justifyContent: 'center' }} {...getRootProps()}&amp;gt;
                                &amp;lt;input {...getInputProps()} /&amp;gt;
                            &amp;lt;/div&amp;gt;
                        )}
                    &amp;lt;/Dropzone&amp;gt;

                    {ThumbnailPath &amp;amp;&amp;amp;
                        &amp;lt;div&amp;gt;
                            &amp;lt;img src={`http://localhost:5000/${ThumbnailPath}`} alt="thumbnail" /&amp;gt;      
                        &amp;lt;/div&amp;gt;
                    }  
                &amp;lt;/div&amp;gt;

                &amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;

                &amp;lt;label&amp;gt;Title&amp;lt;/label&amp;gt;
                &amp;lt;Input
                    onChange = {onTitleChange}
                    value = {VideoTitle}
                /&amp;gt;

                &amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;

                &amp;lt;label&amp;gt;Description&amp;lt;/label&amp;gt;
                &amp;lt;TextArea
                    onChange = {onDescriptionChange}
                    value = {Description}
                /&amp;gt;

                &amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;

                &amp;lt;select onChange={onPrivateChange}&amp;gt; 
                    {PrivateOptions.map((item, index) =&amp;gt; (
                        &amp;lt;option key={index} value={item.value}&amp;gt;{item.label}&amp;lt;/option&amp;gt;
                    ))}
                &amp;lt;/select&amp;gt;

                &amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;

                &amp;lt;select onChange={onCategoryChange}&amp;gt; 
                    {CategoryOptions.map((item, index) =&amp;gt; (
                        &amp;lt;option key={index} value={item.value}&amp;gt;{item.label}&amp;lt;/option&amp;gt;
                    ))}
                &amp;lt;/select&amp;gt;

                &amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;

                &amp;lt;Button type='primary' size='large' onClick={onSubmit}&amp;gt;
                    Upload
                &amp;lt;/Button&amp;gt;
            &amp;lt;/Form&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}

export default VideoUploadPage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;server side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const router = express.Router();
const multer = require("multer");
var ffmpeg = require("fluent-ffmpeg");

const { Video } = require("../vi/videoModel");

let storage = multer.diskStorage({
    destination: (req, file, cb) =&amp;gt; {
        cb(null, 'uploads/');  
    },
    filename: (req, file, cb) =&amp;gt; {
        cb(null, `${Date.now()}_${file.originalname}`);
    },
    fileFilter: (req, file, cb) =&amp;gt; {
        const ext = path.extname(file.originalname);
        if(ext !== '.mp4') {
            return cb(res.status(400).end('ERROR: Only mp4 file is allowed.'), false);
        }
        cb(null, true)
    }
});

const upload = multer({storage: storage}).single("file");


//=================================
//             Video
//=================================


router.post('/uploadfiles', (req, res) =&amp;gt; {
    upload(req, res, err =&amp;gt; {
        if(err) {
            return res.json({ success: false, err })
        }
        return res.json({ success: true, url: res.req.file.path, fileName: res.req.file.filename })

    })
});

router.post('/getVideoDetail', (req, res) =&amp;gt; {

    Video.findOne({ "id" : req.body.videoId })
    .populate('writer')
    .exec((err, videoDetail) =&amp;gt; {
        if(err) return res.status(400).send(err);
        res.status(200).json({ success: true, videoDetail })
    })
});

router.post('/uploadvideo', (req, res) =&amp;gt; {
    const video = new Video(req.body)

    video.save((err, doc) =&amp;gt; {
        if(err) return res.json({ success: false, err })
        res.status(200).json({ success: true })
    })
})

router.get('/getVideos', (req, res) =&amp;gt; {
    Video.find()
        .populate('writer')
        .exec((err, videos) =&amp;gt; {
            if(err) return res.status(400).send(err);
            res.status(200).json({ success: true, videos })
        })
})

router.post("/thumbnail", (req, res) =&amp;gt; {

    let filePath ="";
    let fileDuration ="";

    ffmpeg.ffprobe(req.body.url, function(err, metadata){
        console.dir(metadata);
        console.log(metadata.format.duration);

        fileDuration = metadata.format.duration;
    });


    ffmpeg(req.body.url)
        .on('filenames', function (filenames) {
            console.log('Will generate ' + filenames.join(', '))
            console.log(filenames)

            filePath = "uploads/thumbnails/" + filenames[0];
        })
        .on('end', function () {
            console.log('Screenshots taken');
            return res.json({ success: true, url: filePath, fileDuration: fileDuration })
        })
        .on('error', function(err) {
            console.error(err);
            return res.json({ success: false, err });
        })
        .screenshots({
            count: 1,
            folder: 'uploads/thumbnails',
            size:'320x240',
            filename:'thumbnail-%b.png'
        });

});

module.exports = router;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;i would happily send the full project if needed just let me know thnx for helpers.&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
      <category>express</category>
    </item>
  </channel>
</rss>
