Example #1
0
 /**
  * detect mimetype based on both filename and content
  *
  * @param string $path
  * @return string
  */
 public function detect($path)
 {
     if (@is_dir($path)) {
         // directories are easy
         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));
         if ($info) {
             $mimeType = substr($info, 0, strpos($info, ';'));
             return empty($mimeType) ? 'application/octet-stream' : $mimeType;
         }
         finfo_close($finfo);
     }
     $isWrapped = strpos($path, '://') !== false and substr($path, 0, 7) === 'file://';
     if (!$isWrapped and $mimeType === 'application/octet-stream' && function_exists("mime_content_type")) {
         // use mime magic extension if available
         $mimeType = mime_content_type($path);
     }
     if (!$isWrapped and $mimeType === 'application/octet-stream' && \OC_Helper::canExecute("file")) {
         // it looks like we have a 'file' command,
         // lets see if it does have mime support
         $path = escapeshellarg($path);
         $fp = popen("file -b --mime-type {$path} 2>/dev/null", "r");
         $reply = fgets($fp);
         pclose($fp);
         //trim the newline
         $mimeType = trim($reply);
         if (empty($mimeType)) {
             $mimeType = 'application/octet-stream';
         }
     }
     return $mimeType;
 }
 /**
  * get the mimetype form a local file
  * @param string path
  * @return string
  * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
  */
 static function getMimeType($path)
 {
     $isWrapped = strpos($path, '://') !== false and substr($path, 0, 7) == 'file://';
     $mimeType = 'application/octet-stream';
     if ($mimeType == 'application/octet-stream') {
         self::$mimetypes = (include 'mimetypes.fixlist.php');
         $extension = strtolower(strrchr(basename($path), "."));
         $extension = substr($extension, 1);
         //remove leading .
         $mimeType = isset(self::$mimetypes[$extension]) ? self::$mimetypes[$extension] : 'application/octet-stream';
     }
     if (@is_dir($path)) {
         // directories are easy
         return "httpd/unix-directory";
     }
     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));
         if ($info) {
             $mimeType = substr($info, 0, strpos($info, ';'));
         }
         finfo_close($finfo);
     }
     if (!$isWrapped and $mimeType == 'application/octet-stream' && function_exists("mime_content_type")) {
         // use mime magic extension if available
         $mimeType = mime_content_type($path);
     }
     if (!$isWrapped and $mimeType == 'application/octet-stream' && OC_Helper::canExecute("file")) {
         // it looks like we have a 'file' command,
         // lets see it it does have mime support
         $path = escapeshellarg($path);
         $fp = popen("file -i -b {$path} 2>/dev/null", "r");
         $reply = fgets($fp);
         pclose($fp);
         //trim the character set from the end of the response
         $mimeType = substr($reply, 0, strrpos($reply, ' '));
     }
     if ($mimeType == 'application/octet-stream') {
         // Fallback solution: (try to guess the type by the file extension
         if (!self::$mimetypes || self::$mimetypes != (include 'mimetypes.list.php')) {
             self::$mimetypes = (include 'mimetypes.list.php');
         }
         $extension = strtolower(strrchr(basename($path), "."));
         $extension = substr($extension, 1);
         //remove leading .
         $mimeType = isset(self::$mimetypes[$extension]) ? self::$mimetypes[$extension] : 'application/octet-stream';
     }
     return $mimeType;
 }
Example #3
0
	/**
	 * get the mimetype form a local file
	 * @param string $path
	 * @return string
	 * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
	 */
	static function getMimeType($path) {
		$isWrapped=(strpos($path, '://')!==false) and (substr($path, 0, 7)=='file://');

		if (@is_dir($path)) {
			// directories are easy
			return "httpd/unix-directory";
		}

		if(strpos($path, '.')) {
			//try to guess the type by the file extension
			if(!self::$mimetypes || self::$mimetypes != include 'mimetypes.list.php') {
				self::$mimetypes=include 'mimetypes.list.php';
			}
			$extension=strtolower(strrchr(basename($path), "."));
			$extension=substr($extension, 1);//remove leading .
			$mimeType=(isset(self::$mimetypes[$extension]))?self::$mimetypes[$extension]:'application/octet-stream';
		}else{
			$mimeType='application/octet-stream';
		}

		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));
			if($info) {
				$mimeType=substr($info, 0, strpos($info, ';'));
			}
			finfo_close($finfo);
		}
		if (!$isWrapped and $mimeType=='application/octet-stream' && function_exists("mime_content_type")) {
			// use mime magic extension if available
			$mimeType = mime_content_type($path);
		}
		if (!$isWrapped and $mimeType=='application/octet-stream' && OC_Helper::canExecute("file")) {
			// it looks like we have a 'file' command,
			// lets see if it does have mime support
			$path=escapeshellarg($path);
			$fp = popen("file -i -b $path 2>/dev/null", "r");
			$reply = fgets($fp);
			pclose($fp);

			// we have smth like 'text/x-c++; charset=us-ascii\n'
			// and need to eliminate everything starting with semicolon including trailing LF
			$mimeType = preg_replace('/;.*/ms', '', trim($reply));

		}
		return $mimeType;
	}
 public function getMimeType($fspath)
 {
     if ($this->is_readable($fspath)) {
         if (@is_dir($this->datadir . $fspath)) {
             // directories are easy
             return "httpd/unix-directory";
         } elseif (function_exists('finfo_open') and function_exists('finfo_file') and $finfo = finfo_open(FILEINFO_MIME)) {
             $mimeType = strtolower(finfo_file($finfo, $this->datadir . $fspath));
             $mimeType = substr($mimeType, 0, strpos($mimeType, ';'));
             finfo_close($finfo);
             return $mimeType;
         } else {
             if (function_exists("mime_content_type")) {
                 // use mime magic extension if available
                 $mime_type = mime_content_type($this->datadir . $fspath);
             } else {
                 if (OC_Helper::canExecute("file")) {
                     // it looks like we have a 'file' command,
                     // lets see it it does have mime support
                     $fp = popen("file -i -b '{$this->datadir}{$fspath}' 2>/dev/null", "r");
                     $reply = fgets($fp);
                     pclose($fp);
                     //trim the character set from the end of the response
                     $mime_type = substr($reply, 0, strrpos($reply, ' '));
                 }
             }
         }
         if (empty($mime_type)) {
             // Fallback solution: (try to guess the type by the file extension
             if (!self::$mimetypes) {
                 self::$mimetypes = (include 'mimetypes.list.php');
             }
             $extention = strtolower(strrchr(basename($fspath), "."));
             $extention = substr($extention, 1);
             //remove leading .
             $mime_type = isset(self::$mimetypes[$extention]) ? self::$mimetypes[$extention] : 'application/octet-stream';
         }
         return $mime_type;
     }
 }
 /**
  * scan a file for music
  * @param string $path
  * @return boolean
  */
 public static function scanFile($path)
 {
     if (is_null(self::$useMp3Info)) {
         self::$useMp3Info = OC_Helper::canExecute("mp3info");
     }
     $file = OC_Filesystem::getLocalFile($path);
     if (substr($path, -3) == 'mp3' and self::$useMp3Info) {
         //use the command line tool id3info if possible
         $output = array();
         $size = filesize($file);
         exec('mp3info -p "%a\\n%l\\n%t\\n%n\\n%S" "' . $file . '"', $output);
         if (count($output) > 4) {
             $artist = $output[0];
             $album = $output[1];
             $title = $output[2];
             $track = $output[3];
             $length = $output[4];
         } else {
             return;
             //invalid mp3 file
         }
     } else {
         if (!self::isMusic($path)) {
             return;
         }
         if (!self::$getID3) {
             self::$getID3 = @new getID3();
         }
         $data = @self::$getID3->analyze($file);
         getid3_lib::CopyTagsToComments($data);
         if (!isset($data['comments'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading id3 tags in '{$file}'");
             }
             return;
         }
         if (!isset($data['comments']['artist'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading artist tag in '{$file}'");
             }
             $artist = 'unknown';
         } else {
             $artist = stripslashes($data['comments']['artist'][0]);
             $artist = utf8_encode($artist);
         }
         if (!isset($data['comments']['album'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading album tag in '{$file}'");
             }
             $album = 'unknown';
         } else {
             $album = stripslashes($data['comments']['album'][0]);
             $album = utf8_encode($album);
         }
         if (!isset($data['comments']['title'])) {
             if (defined("DEBUG") && DEBUG) {
                 error_log("error reading title tag in '{$file}'");
             }
             $title = 'unknown';
         } else {
             $title = stripslashes($data['comments']['title'][0]);
             $title = utf8_encode($title);
         }
         $size = $data['filesize'];
         $track = isset($data['comments']['track']) ? $data['comments']['track'][0] : 0;
         $length = isset($data['playtime_seconds']) ? round($data['playtime_seconds']) : 0;
     }
     if (!isset(self::$artists[$artist])) {
         $artistId = OC_MEDIA_COLLECTION::addArtist($artist);
         self::$artists[$artist] = $artistId;
     } else {
         $artistId = self::$artists[$artist];
     }
     if (!isset(self::$albums[$artist . '/' . $album])) {
         $albumId = OC_MEDIA_COLLECTION::addAlbum($album, $artistId);
         self::$albums[$artist . '/' . $album] = $albumId;
     } else {
         $albumId = self::$albums[$artist . '/' . $album];
     }
     $songId = OC_MEDIA_COLLECTION::addSong($title, $path, $artistId, $albumId, $length, $track, $size);
     return !($title == 'unkown' && $artist == 'unkown' && $album == 'unkown') ? $songId : 0;
 }