<commit_before><?php

namespace YannickMahe\SelfHostedVideosBundle\Command;


use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Monolog\Logger;
use Monolog\Handler\NullHandler;

use FFMpeg\FFMpeg;
use FFMpeg\FFProbe;

use YannickMahe\SelfHostedVideosBundle\Entity\Video;


class AddFolderCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('shv:folder:add')
            ->setDescription('Add all the videos in a folder (non recursively) to the site')
            ->addArgument(
                'path',
                InputArgument::REQUIRED,
                'Which folder do you want to add?'
            )
            ->addOption(
               'remove',
               null,
               InputOption::VALUE_NONE,
               'If set, the task will remove the original files and the containing folder if it is empty'
            )
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $path = $input->getArgument('path');
        $path = realpath($path);

        if(!is_dir($path)){
            Throw new \Exception("No folder at at ".$path);
        }
        
        $output->writeln("Adding videos at ".$path); 

        $dir = opendir($path);
        while($file = readdir($dir)){
            $filepath = $path.$file;
            $ext = pathinfo($filepath, PATHINFO_EXTENSION);
            if(in_array($ext, array('mov','mpeg','avi','mkv','mp4','mpg'))){  //Todo: check if actually a video file
                $output->writeln("Adding video at ".$filepath);        
                try{
                    $video = new Video();
                    $em = $this->getContainer()->get('doctrine')->getEntityManager();
                    $video->setName(basename($filepath));
                    $em->persist($video);
                    $em->flush();
                    $video->moveFromDisc($filepath);

                    $logger = new Logger('MyLogger');
                    $logger->pushHandler(new NullHandler());
                    $ffmpeg = FFMpeg::load($logger);
                    $ffprobe = FFProbe::load($logger);
                    
                    $video->postProcess($ffmpeg,$ffprobe);
                    
                    $em->persist($video);
                    $em->flush();

                    if ($input->getOption('remove')) {
                        unlink($filepath);
                        $output->writeln("Deleted ".$filepath); 
                    }
                } catch (\Exception $e){
                    Throw $e;
                }
                
                $output->writeln("Video n° ".$video->getId()." has been added"); 

                if ($input->getOption('remove')) {
                    rmdir($path);
                    $output->writeln("Deleted ".$path); 
                }
            } else {
                $output->writeln($filepath." is not a video");
            }
        }
        /*

        */
    }
}<commit_msg>moved to basename, add dir separator<commit_after><?php

namespace YannickMahe\SelfHostedVideosBundle\Command;


use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

use Monolog\Logger;
use Monolog\Handler\NullHandler;

use FFMpeg\FFMpeg;
use FFMpeg\FFProbe;

use YannickMahe\SelfHostedVideosBundle\Entity\Video;


class AddFolderCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('shv:folder:add')
            ->setDescription('Add all the videos in a folder (non recursively) to the site')
            ->addArgument(
                'path',
                InputArgument::REQUIRED,
                'Which folder do you want to add?'
            )
            ->addOption(
               'remove',
               null,
               InputOption::VALUE_NONE,
               'If set, the task will remove the original files and the containing folder if it is empty'
            )
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $path = $input->getArgument('path');
        $path = realpath($path);

        if(!is_dir($path)){
            Throw new \Exception("No folder at at ".$path);
        }
        
        $output->writeln("Adding videos at ".$path); 

        $dir = opendir($path);
        while($file = readdir($dir)){
            $filepath = $path.DIRECTORY_SEPARATOR.$file;
            $ext = pathinfo($filepath, PATHINFO_EXTENSION);
            if(in_array($ext, array('mov','mpeg','avi','mkv','mp4','mpg'))){  //Todo: check if actually a video file
                $output->writeln("Adding video at ".$filepath);        
                try{
                    $video = new Video();
                    $em = $this->getContainer()->get('doctrine')->getEntityManager();
                    $video->setName(basename($filepath));
                    $em->persist($video);
                    $em->flush();
                    $video->moveFromDisc($filepath);

                    $logger = new Logger('MyLogger');
                    $logger->pushHandler(new NullHandler());
                    $ffmpeg = FFMpeg::load($logger);
                    $ffprobe = FFProbe::load($logger);
                    
                    $video->postProcess($ffmpeg,$ffprobe);
                    
                    $em->persist($video);
                    $em->flush();

                    if ($input->getOption('remove')) {
                        unlink($filepath);
                        $output->writeln("Deleted ".$filepath); 
                    }
                } catch (\Exception $e){
                    Throw $e;
                }
                
                $output->writeln("Video n° ".$video->getId()." has been added"); 

                if ($input->getOption('remove')) {
                    rmdir($path);
                    $output->writeln("Deleted ".$path); 
                }
            } else {
                $output->writeln($filepath." is not a video");
            }
        }
        /*

        */
    }
}
{'fraction_non_alphanumeric': 0.12650878533231474, 'fraction_numerical': 0.00030557677616501144, 'mean_word_length': 1.5620352250489238, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2143742', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1662, 'n_words': 341}