<commit_before><?php

namespace Nails\Common\Service;

use MimeType\MimeType;
use MimeTyper\Repository\MimeDbRepository;

/**
 * Class Mime
 *
 * @package Nails\Common\Service
 */
class Mime
{
    /**
     * The mime databse
     *
     * @var MimeDbRepository
     */
    protected $oDatabase;

    /**
     * The mime detector
     *
     * @var MimeType
     */
    protected $oDetector;

    // --------------------------------------------------------------------------

    /**
     * Mime constructor.
     *
     * @param MimeDbRepository $oDatabase The mime database to use
     * @param MimeType         $oDetector The mime detector to use
     */
    public function __construct(
        MimeDbRepository $oDatabase,
        MimeType $oDetector
    ) {
        $this->oDatabase = $oDatabase;
        $this->oDetector = $oDetector;
    }

    // --------------------------------------------------------------------------

    /**
     * Detect a file's mimetype
     *
     * @param string $sFile The path to the file to detect
     *
     * @return string
     * @throws \Exception
     */
    public function detectFromFile(string $sFile): string
    {
        return $this->oDetector->getType($sFile);
    }

    // --------------------------------------------------------------------------

    /**
     * Get extensions for a given mime type
     *
     * @param string $sMime The mime type to query
     *
     * @return string[]
     */
    public function getExtensionsForMime(string $sMime): array
    {
        return $this->oDatabase->findExtensions($sMime);
    }

    // --------------------------------------------------------------------------

    /**
     * Get mimes for a given extension
     *
     * @param string $sExtension The extension to query
     *
     * @return string[]
     */
    public function getMimesForExtension(string $sExtension): array<commit_msg>Mime: Using finfo before extension detector<commit_after><?php

namespace Nails\Common\Service;

use MimeType\MimeType;
use MimeTyper\Repository\MimeDbRepository;

/**
 * Class Mime
 *
 * @package Nails\Common\Service
 */
class Mime
{
    /**
     * The mime databse
     *
     * @var MimeDbRepository
     */
    protected $oDatabase;

    /**
     * The mime detector
     *
     * @var MimeType
     */
    protected $oDetector;

    // --------------------------------------------------------------------------

    /**
     * Mime constructor.
     *
     * @param MimeDbRepository $oDatabase The mime database to use
     * @param MimeType         $oDetector The mime detector to use
     */
    public function __construct(
        MimeDbRepository $oDatabase,
        MimeType $oDetector
    ) {
        $this->oDatabase = $oDatabase;
        $this->oDetector = $oDetector;
    }

    // --------------------------------------------------------------------------

    /**
     * Detect a file's mimetype, first using the system, followed by the detector
     *
     * @param string $sFile The path to the file to detect
     *
     * @return string
     * @throws \Exception
     */
    public function detectFromFile(string $sFile): string
    {
        if (!file_exists($sFile)) {
            return '';
        }

        $rHandle = finfo_open(FILEINFO_MIME_TYPE);
        $sMime   = finfo_file($rHandle, $sFile);

        if ($sMime === 'application/octet-stream' || empty($sMime)) {
            $sMime = $this->oDetector->getType($sFile);
        }

        return $sMime;
    }

    // --------------------------------------------------------------------------

    /**
     * Get extensions for a given mime type
     *
     * @param string $sMime The mime type to query
     *
     * @return string[]
     */
    public function getExtensionsForMime(string $sMime): array
    {
        return $this->oDatabase->findExtensions($sMime);
    }

    // --------------------------------------------------------------------------

    /**
     * Get mimes for a given extension
     *
     * @param string $sExtension The extension to query
     *
     * @return string[]
     */
    public function getMimesForExtension(string $sExtension): array
{'fraction_non_alphanumeric': 0.23537906137184114, 'fraction_numerical': 0.0, 'mean_word_length': 1.3179029559397657, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '2459985', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1107, 'n_words': 276}