public static function guess(string $path, string $cmd = null): ?string
    {
        if (! \is_file($path)) {
            throw new FileNotFoundException($path);
        }

        if (! \is_readable($path)) {
            throw new AccessDeniedException($path);
        }

        if ($cmd === null) {
            $cmd = 'file -b --mime %s';

            if (\stripos(\PHP_OS, 'win') !== 0) {
                $cmd .= ' 2>/dev/null';
            }
        }

        \ob_start();

        // need to use --mime instead of -i.
        \passthru(\sprintf($cmd, \escapeshellarg($path)), $return);

        if ($return > 0) {
            \ob_end_clean();

            return null;
        }

        $type = \trim((string) \ob_get_clean());

        if (\preg_match('#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i', $type, $match) === false) {
            // it's not a type, but an error message
            return null;
        }

        return $match[1];
    }