Guess file type code

@param FileId $fileId
@return string
494708
public function guessFileType(FileId $fileId)
    {
        $extension = strtolower(\pathinfo($fileId->id(), PATHINFO_EXTENSION));

        foreach($this->fileTypeExtensions as $fileType => $extensions) {
            if(in_array($extension, $extensions)) {
                return $fileType;
            }
        }

        return 'file';
    }
615749
public function getFileLink($fileId, $target)
    {
        $file = Yii::$app->storage->getFile($fileId);
        if ($file) {
            $file->setTarget($target);
        }
        return $file;
    }
627703
public static function guessType($file)
    {
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));

        switch (true) {
            case $ext === 'json':
            case basename($file) === 'composer.lock':
                return self::JSON;
            case $ext === 'yml':
            case $ext === 'yaml':
                return self::YAML;
            case $ext === 'ini':
            case basename($file) === '.my.cnf':
                return self::INI;
        }

        return null;
    }
349048
public function get_page_type_idstring($id) {
        foreach ($this->types as $pagetype) {
            if ((int)$pagetype->typeid === (int)$id) {
                return $pagetype->idstring;
            }
        }
        return 'unknown';
    }
77357
def guess_filetype(filepath):
    """"""
    # If we have python-magic, use it to determine file type
    typ = None

    # Some specific checks for known file suffixes
    _fn = filepath.lower()
    _name, _ext = os.path.splitext(_fn)
    if _ext in known_types:
        typ = known_types[_ext]

    if typ is None and have_magic:
        try:
            # it seems there are conflicting versions of a 'magic'
            # module for python floating around...*sigh*
            if hasattr(magic, 'from_file'):
                typ = magic.from_file(filepath, mime=True)

            elif magic_tester is not None:
                descrip = magic_tester.file(filepath)
                if descrip.startswith("FITS image data"):
                    return ('image', 'fits')

        except Exception as e:
            pass

    if typ is None:
        # if no magic, or magic fails, fall back to mimetypes
        try:
            typ, enc = mimetypes.guess_type(filepath)

        except Exception as e:
            # fail
            pass

    if typ:
        typ, subtyp = typ.split('/')
        return (typ, subtyp)

    raise ValueError("Can't determine file type of '%s'" % (filepath))
957477
private function guessFromContent($filename)
    {
        $output = 'text';
        if (!is_file($filename)) {
            return $output;
        }
        if ($fileContents = file_get_contents($filename)) {
            if (1 === preg_match('/<?xml.*?>/', $fileContents)) {
                $output = 'xml';
            }
        }

        return $output;
    }
104155
public function get_file_by_id($fileid) {
        global $DB;

        $sql = "SELECT ".self::instance_sql_fields('f', 'r')."
                  FROM {files} f
             LEFT JOIN {files_reference} r
                       ON f.referencefileid = r.id
                 WHERE f.id = ?";
        if ($filerecord = $DB->get_record_sql($sql, array($fileid))) {
            return $this->get_file_instance($filerecord);
        } else {
            return false;
        }
    }
44152
public static function getMimeType($filePath)
    {
        $fInfo = finfo_open(FILEINFO_MIME_TYPE);
        $mime  = finfo_file($fInfo, $filePath);
        finfo_close($fInfo);

        return $mime;
    }
1047044
protected function buildCacheKey($fileId)
    {
        return [static::class, 'file', $fileId, Yii::$app->user->isGuest ? true : Yii::$app->user->id];
    }
1153918
private function guessContentType($content)
    {
        $fileInfo = new \finfo(FILEINFO_MIME_TYPE);

        if (is_resource($content)) {
            return $fileInfo->file(stream_get_meta_data($content)['uri']);
        }

        return $fileInfo->buffer($content);
    }
567130
public function getRaw($fileId = null)
    {
        // does nothing with the text
        $lamdaFunction = function($text){ 
            return [$text];
        };        
        return $this->getWords($fileId, new LambdaTokenizer($lamdaFunction));        
    }
925155
public function getElement($file_id)
    {
        $ret = ['success' => false, 'message' => '', 'result'  => []];
        
        /* @var LazaretFile $lazaretFile */
        $lazaretFile = $this->getLazaretFileRepository()->find($file_id);

        if (null === $lazaretFile) {
            $ret['message'] = $this->app->trans('File is not present in quarantine anymore, please refresh');

            return $this->app->json($ret);
        }

        $ret['result'] = [
            'filename' => $lazaretFile->getOriginalName(),
            'base_id'  => $lazaretFile->getBaseId(),
            'created'  => $lazaretFile->getCreated()->format(\DateTime::ATOM),
            'updated'  => $lazaretFile->getUpdated()->format(\DateTime::ATOM),
            'pathname' => $this->app['tmp.lazaret.path'].'/'.$lazaretFile->getFilename(),
            'sha256'   => $lazaretFile->getSha256(),
            'uuid'     => $lazaretFile->getUuid(),
        ];
        $ret['success'] = true;

        return $this->app->json($ret);
    }
664962
public function read($sessionId): string
    {
        $filename = $this->savePath . \DIRECTORY_SEPARATOR . $this->prefix . $sessionId;
        if (\file_exists($filename)) {
            return (string) \file_get_contents($filename);
        }

        return '';
    }
971751
protected function guessType()
    {
        $os = strtolower(PHP_OS);
        if (false !== strpos($os, 'cygwin')) {
            return self::OS_TYPE_CYGWIN;
        }
        if (false !== strpos($os, 'darwin')) {
            return self::OS_TYPE_DARWIN;
        }
        if (false !== strpos($os, 'bsd')) {
            return self::OS_TYPE_BSD;
        }
        if (0 === strpos($os, 'win')) {
            return self::OS_TYPE_WINDOWS;
        }

        return self::OS_TYPE_UNIX;
    }
119523
static function guessMimeType($file, $def = 'application/octet-stream')
	{
		// extract file extension (after . symbol)
		$ext = substr(strrchr(strtolower($file), '.'), 1);
	
		switch ( $ext )
		{
			case 'gif':
			case 'jpeg':
			case 'png':
				return "image/$ext";
			case 'jpg':
				return 'image/jpeg';
			case 'mp4':
			case 'mpeg':
			case 'avi':
				return "video/$ext";
			case 'mp3':
				return 'audio/mpeg3';
			case 'wav':
				return 'audio/wav';
			case 'pdf':
				return 'application/pdf';
			case 'htm':
			case 'html':
				return 'text/html';
			case 'txt':
				return 'text/plain';
			case 'eml':
				return 'message/rfc822';
			case 'doc':
				return 'application/msword';
			case 'docx':
				return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
			case 'xls':
				return 'application/vnd.ms-excel';
			case 'xlsx':
				return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
			case 'ppt':
			case 'pps':
				return 'application/vnd.ms-powerpoint';
			case 'pptx':
			case 'ppsx':
				return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
			case 'odt':
				return 'application/vnd.oasis.opendocument.text';
			case 'ods':
				return 'application/vnd.oasis.opendocument.spreadsheet';
			case 'odp':
				return 'application/vnd.oasis.opendocument.presentation';
			case 'zip':
				return 'application/zip';
			default :
				return $def;
		}
	}
663378
public function guess($data)
	{
		if (in_array(substr($data, 6, 4), ['JFIF', 'Exif'], true) || strpos($data, chr(255) . chr(216)) === 0) { // 0xFF 0xD8	// mpDF 5.7.2
			return 'jpeg';
		}

		if (in_array(substr($data, 0, 6), ['GIF87a', 'GIF89a'], true)) {
			return 'gif';
		}

		if (strpos($data, chr(137) . 'PNG' . chr(13) . chr(10) . chr(26) . chr(10)) === 0) {
			return 'png';
		}

		if (strpos($data, chr(215) . chr(205) . chr(198) . chr(154)) === 0) {
			return 'wmf';
		}

		if (preg_match('/<svg.*<\/svg>/is', $data)) {
			return 'svg';
		}

		if (strpos($data, 'BM') === 0) {
			return 'bmp';
		}

		return null;
	}
1308842
public static function getMimeType($filename)
	{
		$mime_types = static::getMimeTypes();
		
		$extension = pathinfo($filename, PATHINFO_EXTENSION);
    	$extension = strtolower($extension);
    	
		if (array_key_exists($extension, $mime_types)) {
            return $mime_types[$extension];
        } elseif (function_exists('finfo_open')) {
            $finfo = finfo_open(FILEINFO_MIME);
            $mimetype = finfo_file($finfo, $filename);
            finfo_close($finfo);
            return $mimetype;
        } 
        return 'application/octet-stream';
	}