[Fusionforge-commits] r12728 - in trunk/src/plugins/projectimport: common etc

Olivier Berger olberger at fusionforge.org
Mon Mar 14 11:27:31 CET 2011


Author: olberger
Date: 2011-03-14 11:27:30 +0100 (Mon, 14 Mar 2011)
New Revision: 12728

Modified:
   trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php
   trunk/src/plugins/projectimport/common/UploadedFiles.class.php
   trunk/src/plugins/projectimport/etc/projectimport.ini
Log:
Add improved mime type handling for Zip OpenDocument Package containers


Modified: trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php
===================================================================
--- trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php	2011-03-13 12:38:03 UTC (rev 12727)
+++ trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php	2011-03-14 10:27:30 UTC (rev 12728)
@@ -22,6 +22,7 @@
  */
 
 forge_define_config_item('storage_base','projectimport-plugin','$core/data_path/plugins/projectimport/');
+forge_define_config_item('libmagic_db','projectimport-plugin','/usr/share/misc/magic.mgc');
 
 class ProjectImportPlugin extends Plugin {
 	function ProjectImportPlugin () {

Modified: trunk/src/plugins/projectimport/common/UploadedFiles.class.php
===================================================================
--- trunk/src/plugins/projectimport/common/UploadedFiles.class.php	2011-03-13 12:38:03 UTC (rev 12727)
+++ trunk/src/plugins/projectimport/common/UploadedFiles.class.php	2011-03-14 10:27:30 UTC (rev 12728)
@@ -42,6 +42,8 @@
 		
 	protected $html_generator;
 	
+	protected static $finfo;
+	
 	/**
 	 * Initializes the directory permissions
 	 */
@@ -56,6 +58,12 @@
 		}
 	}
 	
+	protected static function finfo() {
+		if (!isset(self::$finfo)) {
+			self::$finfo = new finfo(FILEINFO_MIME, forge_get_config('libmagic_db', 'projectimport-plugin'));
+		}
+		return self::$finfo;
+	}
 	/**
 	 * Constructor
 	 * @param HTML generator $HTML
@@ -64,6 +72,11 @@
 	public function AbstractFilesDirectory($HTML, $storage_base=False) {
 		
 		$this->html_generator = $HTML;
+		
+		if (!isset(self::$finfo)) {
+			self::$finfo = new finfo(FILEINFO_MIME, forge_get_config('libmagic_db', 'projectimport-plugin'));
+		}
+		
 		if(! $storage_base) {
 			$storage_base = tempnam("/tmp", "ff-projectimport");
 		}
@@ -115,20 +128,40 @@
 		return $html;
 	}
 	
+	public function getMimeType($filepath) {
+		$finfo = self::finfo();
+		if (!$finfo) {
+			$this->setError(_('Opening fileinfo database failed'));
+			return false;
+		}
+		$mimetype = $finfo->file($filepath);
+		$mimetype = strstr($mimetype, ';', true);
+			
+		// try to identify OpenDocument Package Zip container
+		if ($mimetype == 'application/octet-stream') {
+			$zip = new ZipArchive;
+			$res = $zip->open($filepath);
+			if ($res === TRUE) {
+				// if it has a mimetype file in the zip, then read its contents 
+				$contents = $zip->getFromName('mimetype');
+				if($contents) {
+					$mimetype = $contents;
+				}
+			}	
+		}
+		return $mimetype;
+	}
+	
 	/**
 	 * Returns an HTML box/table containing (single) file selection radio buttons
 	 * @param string $preselected filename
 	 * @return boolean|string
 	 */
 	public function displayFileSelectionForm($preselected = False) {
-		$html = '';
-		$finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // return mime type ala mimetype extension
+		$html = '';		
 
-		if (!$finfo) {
-			$this->setError(_('Opening fileinfo database failed'));
-			return false;
-		}
 		
+		
 		if (is_dir($this->dir_path)) {
 			$contents = scandir($this->dir_path);
 			if(count($contents) > 2) {
@@ -143,10 +176,19 @@
 					if ($file != '.' && $file != '..') {
 						$filepath = $this->dir_path . $file;
 						$filetype = filetype($filepath);
+						$mimetype = False;
+						if ($filetype == 'file') {
+							$mimetype = $this->getMimeType($filepath);
+						}
 						$html .= '<tr>';
-						$html .= '<td style="white-space: nowrap;"><tt>'. $file .'</tt></td>';
+						if ($mimetype == 'application/x-planetforge-forge-export') {
+							$html .= '<td style="white-space: nowrap;"><tt><b>'. $file .'</b></tt></td>';
+							$mimetype = '<b>PlanetForge forge export container ('.$mimetype.')</b>';
+						}
+						else {
+							$html .= '<td style="white-space: nowrap;"><tt>'. $file .'</tt></td>';
+						}
 						if ($filetype == 'file') {
-							$mimetype = $finfo->file($filepath);
 							$html .= '<td style="white-space: nowrap;">' . $mimetype . '</td>';
 							$sha_filename = sha1($file);
 							if ($file == $preselected) {

Modified: trunk/src/plugins/projectimport/etc/projectimport.ini
===================================================================
--- trunk/src/plugins/projectimport/etc/projectimport.ini	2011-03-13 12:38:03 UTC (rev 12727)
+++ trunk/src/plugins/projectimport/etc/projectimport.ini	2011-03-14 10:27:30 UTC (rev 12728)
@@ -7,4 +7,7 @@
 plugin_status = 'development'
 
 ; Uploaded files storage base
-storage_base = "$core/data_path/plugins/projectimport/"
\ No newline at end of file
+storage_base = "$core/data_path/plugins/projectimport/"
+
+; TODO: this may need to be shared in [core] ?
+libmagic_db = /usr/share/misc/magic.mgc
\ No newline at end of file




More information about the Fusionforge-commits mailing list