lib/private/Files/Type/Detection.php
/**
	 * detect mimetype based on both filename and content
	 *
	 * @param string $path
	 * @return string
	 */
detect mimetype based on both filename and content
@param string $path
@return string
detect mimetype based on both filename and content @param string $path @return string
public function detect($path) {
		$this->loadMappings();
		if (@is_dir($path)) {
			return "httpd/unix-directory";
		}
		$mimeType = $this->detectPath($path);
		if ($mimeType === 'application/octet-stream' and function_exists('finfo_open')
			and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)
		) {
			$info = @strtolower(finfo_file($finfo, $path));
			finfo_close($finfo);
			if ($info) {
				$mimeType = strpos($info, ';') !== false ? substr($info, 0, strpos($info, ';')) : $info;
				return empty($mimeType) ? 'application/octet-stream' : $mimeType;
			}
		}
		$isWrapped = (strpos($path, '://') !== false) and (substr($path, 0, 7) === 'file://');
		if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) {
			$mimeType = mime_content_type($path);
		}
		if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {
			$path = escapeshellarg($path);
			$fp = popen("file -b --mime-type $path 2>/dev/null", "r");
			$reply = fgets($fp);
			pclose($fp);
			$mimeType = trim($reply);
			if (empty($mimeType)) {
				$mimeType = 'application/octet-stream';
			}
		}
		return $mimeType;
	}
public function detect ( $ path ) { $ this -> loadMappings ( ) ; if ( @ is_dir ( $ path ) ) { return " httpd/unix-directory " ; } $ mimeType = $ this -> detectPath ( $ path ) ; if ( $ mimeType === 'application/octet-stream' and function_exists ( 'finfo_open' ) and function_exists ( 'finfo_file' ) and $ finfo = finfo_open ( FILEINFO_MIME ) ) { $ info = @ strtolower ( finfo_file ( $ finfo , $ path ) ) ; finfo_close ( $ finfo ) ; if ( $ info ) { $ mimeType = strpos ( $ info , ';' ) !== false ? substr ( $ info , 0 , strpos ( $ info , ';' ) ) : $ info ; return empty ( $ mimeType ) ? 'application/octet-stream' : $ mimeType ; } } $ isWrapped = ( strpos ( $ path , '://' ) !== false ) and ( substr ( $ path , 0 , 7 ) === 'file://' ) ; if ( ! $ isWrapped and $ mimeType === 'application/octet-stream' && function_exists ( " mime_content_type " ) ) { $ mimeType = mime_content_type ( $ path ) ; } if ( ! $ isWrapped and $ mimeType === 'application/octet-stream' && \ OC_Helper :: canExecute ( " file " ) ) { $ path = escapeshellarg ( $ path ) ; $ fp = popen ( " file -b --mime-type  $ path  2>/dev/null " , " r " ) ; $ reply = fgets ( $ fp ) ; pclose ( $ fp ) ; $ mimeType = trim ( $ reply ) ; if ( empty ( $ mimeType ) ) { $ mimeType = 'application/octet-stream' ; } } return $ mimeType ; }
detect mimetype based on both filename and content
 @param string $path
 @return string
detect mimetype based on both filename and content @param string $path @return string
// directories are easy // use mime magic extension if available // it looks like we have a 'file' command, // lets see if it does have mime support //trim the newline