diff --git a/lib/documentmanagement/documentutil.inc.php b/lib/documentmanagement/documentutil.inc.php index 764fb44..475e98c 100644 --- a/lib/documentmanagement/documentutil.inc.php +++ b/lib/documentmanagement/documentutil.inc.php @@ -76,16 +76,10 @@ class KTDocumentUtil { KTDocumentUtil::copyMetadata($oDocument, $iPreviousMetadataVersion); - $md5hash = md5_file($sFilename); - $content = $oDocument->_oDocumentContentVersion; - $content->setStorageHash($md5hash); - $content->update(); - - if (empty($aOptions)) $aOptions = array(); - $aOptions['md5hash'] = $md5hash; - - if (!$oStorage->upload($oDocument, $sFilename, $aOptions)) { - return PEAR::raiseError(_kt('An error occurred while storing the new file')); + $aOptions['temp_file'] = $sFilename; + $res = KTDocumentUtil::storeContents($oDocument, '', $aOptions); + if (PEAR::isError($res)) { + return $res; } $oDocument->setLastModifiedDate(getCurrentDateTime()); @@ -107,6 +101,7 @@ class KTDocumentUtil { $oDocument->setFileName($sFilename); $default->log->info('renamed document ' . $oDocument->getId() . ' to ' . $sFilename); + // detection of mime types needs to be refactored. this stuff is damn messy! // If the filename has changed then update the mime type $iMimeTypeId = KTMime::getMimeTypeID('', $sFilename); $oDocument->setMimeTypeId($iMimeTypeId); @@ -871,14 +866,6 @@ $sourceDocument->getName(), $sFilename = (isset($aOptions['temp_file'])) ? $aOptions['temp_file'] : ''; -// $oOutputFile = new KTFSFileLike($sFilename); -// $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile); -// if (($res === false)) { -// return PEAR::raiseError(_kt("Couldn't store contents, and no reason given.")); -// } else if (PEAR::isError($res)) { -// return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), $res->getMessage())); -// } - if(empty($sFilename)){ return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), _kt('The uploaded file does not exist.'))); } @@ -891,8 +878,9 @@ $sourceDocument->getName(), if (empty($aOptions)) $aOptions = array(); $aOptions['md5hash'] = $md5hash; + // detection of mime types needs to be refactored. this stuff is damn messy! $sType = KTMime::getMimeTypeFromFile($sFilename); - $iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName()); + $iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName(), $sFilename); $oDocument->setMimeTypeId($iMimeTypeId); $res = $oStorage->upload($oDocument, $sFilename, $aOptions); diff --git a/lib/mime.inc.php b/lib/mime.inc.php index c265f8f..941c06b 100644 --- a/lib/mime.inc.php +++ b/lib/mime.inc.php @@ -6,31 +6,31 @@ * Document Management Made Simple * Copyright (C) 2008 KnowledgeTree Inc. * Portions copyright The Jam Warehouse Software (Pty) Limited - * + * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License version 3 as published by the * Free Software Foundation. - * + * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * - * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, + * + * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco, * California 94120-7775, or email info@knowledgetree.com. - * + * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU General Public License version 3. - * + * * In accordance with Section 7(b) of the GNU General Public License version 3, * these Appropriate Legal Notices must retain the display of the "Powered by - * KnowledgeTree" logo and retain the original copyright notice. If the display of the + * KnowledgeTree" logo and retain the original copyright notice. If the display of the * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices - * must display the words "Powered by KnowledgeTree" and retain the original + * must display the words "Powered by KnowledgeTree" and retain the original * copyright notice. * Contributor( s): ______________________________________ * @@ -48,12 +48,10 @@ class KTMime { * @param string filename * @return int mime type primary key if found, else default mime type primary key (text/plain) */ - function getMimeTypeID($sMimeType, $sFileName) { + function getMimeTypeID($sMimeType, $sFileName, $sTempFile = null) { global $default; $sTable = KTUtil::getTableName('mimetypes'); - - // check by file extension $sExtension = KTMime::stripAllButExtension($sFileName); $res = DBUtil::getOneResultKey(array("SELECT id FROM " . $sTable . " WHERE LOWER(filetypes) = ?", array($sExtension)),'id'); @@ -65,7 +63,6 @@ class KTMime { return $res; } - // get the mime type id if (isset($sMimeType)) { $res = DBUtil::getResultArray(array("SELECT id FROM " . $sTable . " WHERE mimetypes = ?", array($sMimeType))); @@ -77,6 +74,22 @@ class KTMime { } } + if (!is_null($sTempFile)) + { + // The default is a binary file, so if mime magic can resolve better, lets try... + $sMimeType = KTMime::getMimeTypeFromFile($sTempFile); + if (!empty($sMimeType)) + { + $res = DBUtil::getResultArray(array("SELECT id FROM " . $sTable . " WHERE mimetypes = ?", array($sMimeType))); + if (PEAR::isError($res)) { + ; // pass ?! + } + if (count($res) != 0) { + return $res[0]['id']; + } + } + } + //otherwise return the default mime type return KTMime::getDefaultMimeTypeID(); } @@ -159,6 +172,11 @@ class KTMime { } if ($sType) { + $iSpacePos = strpos($sType, ' '); + if ($iSpacePos !== false) + { + $sType = substr($sType, 0, $iSpacePos); + } return preg_replace('/;.*$/', '', $sType); }