public static function getMime($stream, $mime)
    {
        if (is_string($mime)) {
            return $mime;
        }
        if (!$mime || !$stream->isSeekable() || !$stream->isReadable()) {
            return;
        }
        $finfo = finfo_open(FILEINFO_MIME_TYPE);

        $old = $stream->tell();
        $stream->rewind();
        $end = $stream->end();

        $size = min($end - 0, 4);
        if ($size === 0) {
            return;
        }

        $stream->seek($size, SEEK_SET);
        $signature = $stream->read($size, false);

        $size = min($end - 0, 1024);
        $stream->rewind();
        $signature = $stream->read($size, false) . $signature;
        $stream->seek($old, SEEK_SET);

        return finfo_buffer($finfo, $signature);
    }