<commit_before><?php

namespace CanalTP\MediaManager\Media\Factory;

use CanalTP\MediaManager\Media\MediaType;
use CanalTP\MediaManager\Media\SoundMedia;
use CanalTP\MediaManager\Media\PictureMedia;
use CanalTP\MediaManager\Media\PdfMedia;
use CanalTP\MediaManager\Media\Factory\MediaFactoryInterface;
use CanalTP\MediaManager\Media\GenericMedia;

class MediaFactory implements MediaFactoryInterface
{
    private $mime = null;
    private $type = null;

    private function analyse($fileName)
    {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $this->mime = finfo_file($finfo, $fileName);
        finfo_close($finfo);
    }

    private function determine()
    {
        $this->type = null;

        switch ($this->mime) {
            case 'image/jpeg':
                $this->type = MediaType::PICTURE;
                break;
            case 'image/png':
                $this->type = MediaType::PICTURE;
                break;
            case 'audio/mpeg':
                $this->type = MediaType::SOUND;
                break;
            case 'application/pdf':
                $this->type = MediaType::PDF;
                break;
            default:
                $this->type = MediaType::UNKNOWN;
        }
    }

    private function product()
    {
        $media = null;

        switch ($this->type) {
            case MediaType::SOUND:
                $media = new SoundMedia();
                break;
            case MediaType::PICTURE:
                $media = new PictureMedia();
                break;
            case MediaType::PDF:
                $media = new PdfMedia();
                break;
            case MediaType::UNKNOWN:
                $media = new GenericMedia();
                break;
        }

        return ($media);
    }

    public function create($fileName)
    {
        $this->analyse($fileName);
        $this->determine();

        return ($this->product());
    }
}
<commit_msg>fix: avoid php mimetype detection when it seems it failed (use system instead)<commit_after><?php

namespace CanalTP\MediaManager\Media\Factory;

use CanalTP\MediaManager\Media\MediaType;
use CanalTP\MediaManager\Media\SoundMedia;
use CanalTP\MediaManager\Media\PictureMedia;
use CanalTP\MediaManager\Media\PdfMedia;
use CanalTP\MediaManager\Media\Factory\MediaFactoryInterface;
use CanalTP\MediaManager\Media\GenericMedia;

class MediaFactory implements MediaFactoryInterface
{
    private $mime = null;
    private $type = null;

    private function analyse($fileName)
    {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $this->mime = finfo_file($finfo, $fileName);
        finfo_close($finfo);

        // For an unknown reason, mimetype is not valid
        // when detected by PHP (sometimes)
        if ($this->mime == 'application/octet-stream') {
            try {
                $mime = shell_exec('mimetype -b '.$fileName);

                if ($mime) {
                    $this->mime = trim($mime);
                }
            } catch (\Exception $e) {
                // pass
            }
        }
    }

    private function determine()
    {
        $this->type = null;

        switch ($this->mime) {
            case 'image/jpeg':
                $this->type = MediaType::PICTURE;
                break;
            case 'image/png':
                $this->type = MediaType::PICTURE;
                break;
            case 'audio/mpeg':
                $this->type = MediaType::SOUND;
                break;
            case 'application/pdf':
                $this->type = MediaType::PDF;
                break;
            default:
                $this->type = MediaType::UNKNOWN;
        }
    }

    private function product()
    {
        $media = null;

        switch ($this->type) {
            case MediaType::SOUND:
                $media = new SoundMedia();
                break;
            case MediaType::PICTURE:
                $media = new PictureMedia();
                break;
            case MediaType::PDF:
                $media = new PdfMedia();
                break;
            case MediaType::UNKNOWN:
                $media = new GenericMedia();
                break;
        }

        return ($media);
    }

    public function create($fileName)
    {
        $this->analyse($fileName);
        $this->determine();

        return ($this->product());
    }
}

{'fraction_non_alphanumeric': 0.11243144424131626, 'fraction_numerical': 0.0, 'mean_word_length': 1.6256748650269945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '1766704', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1157, 'n_words': 249}