From 262da92f9081c6d91fa18316877ae0bbb4975db8 Mon Sep 17 00:00:00 2001 From: megan_w Date: Fri, 23 Nov 2007 10:22:50 +0000 Subject: [PATCH] KTS-2721 "Refactor adding a document to use the storage manager" Fixed. Removed all references to KTFSFileLike. Added a function to the storagemanager for uploading a temp file. --- ktapi/KTAPIFolder.inc.php | 3 ++- ktwebdav/lib/KTWebDAVServer.inc.php | 10 ++++++---- lib/documentmanagement/documentutil.inc.php | 43 +++++++++++++++++++++++-------------------- lib/storage/ondiskhashedstoragemanager.inc.php | 25 ++++++++++++++++++++++++- plugins/ktcore/folder/addDocument.php | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------- 5 files changed, 119 insertions(+), 84 deletions(-) diff --git a/ktapi/KTAPIFolder.inc.php b/ktapi/KTAPIFolder.inc.php index 2085307..a3d8c86 100644 --- a/ktapi/KTAPIFolder.inc.php +++ b/ktapi/KTAPIFolder.inc.php @@ -450,7 +450,8 @@ class KTAPI_Folder extends KTAPI_FolderItem $options = array( - 'contents' => new KTFSFileLike($tempfilename), + //'contents' => new KTFSFileLike($tempfilename), + 'temp_file' => $tempfilename, 'novalidate' => true, 'documenttype' => DocumentType::get($documenttypeid), 'description' => $title, diff --git a/ktwebdav/lib/KTWebDAVServer.inc.php b/ktwebdav/lib/KTWebDAVServer.inc.php index 312c64a..b8e8852 100644 --- a/ktwebdav/lib/KTWebDAVServer.inc.php +++ b/ktwebdav/lib/KTWebDAVServer.inc.php @@ -1259,9 +1259,10 @@ class KTWebDAVServer extends HTTP_WebDAV_Server ); $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true); - include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); + //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); $aOptions = array( - 'contents' => new KTFSFileLike($sTempFilename), + //'contents' => new KTFSFileLike($sTempFilename), + 'temp_file' => $sTempFilename, 'metadata' => array(), 'novalidate' => true, ); @@ -1309,9 +1310,10 @@ class KTWebDAVServer extends HTTP_WebDAV_Server ); $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true); - include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); + //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); $aOptions = array( - 'contents' => new KTFSFileLike($sTempFilename), + //'contents' => new KTFSFileLike($sTempFilename), + 'temp_file' => $sTempFilename, 'metadata' => array(), 'novalidate' => true, ); diff --git a/lib/documentmanagement/documentutil.inc.php b/lib/documentmanagement/documentutil.inc.php index 68e0919..efb8660 100644 --- a/lib/documentmanagement/documentutil.inc.php +++ b/lib/documentmanagement/documentutil.inc.php @@ -183,7 +183,7 @@ class KTDocumentUtil { function &_add($oFolder, $sFilename, $oUser, $aOptions) { global $default; - $oContents = KTUtil::arrayGet($aOptions, 'contents'); + //$oContents = KTUtil::arrayGet($aOptions, 'contents'); $aMetadata = KTUtil::arrayGet($aOptions, 'metadata', null, false); $oDocumentType = KTUtil::arrayGet($aOptions, 'documenttype'); $sDescription = KTUtil::arrayGet($aOptions, 'description', $sFilename); @@ -208,20 +208,20 @@ class KTDocumentUtil { return $oDocument; } - if (is_null($oContents)) { - $res = KTDocumentUtil::setIncomplete($oDocument, 'contents'); - if (PEAR::isError($res)) { - $oDocument->delete(); - return $res; - } - } else { +// if (is_null($oContents)) { +// $res = KTDocumentUtil::setIncomplete($oDocument, 'contents'); +// if (PEAR::isError($res)) { +// $oDocument->delete(); +// return $res; +// } +// } else { // $oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Storing contents'))); - $res = KTDocumentUtil::storeContents($oDocument, $oContents, $aOptions); + $res = KTDocumentUtil::storeContents($oDocument, '', $aOptions); if (PEAR::isError($res)) { $oDocument->delete(); return $res; } - } +// } if (is_null($aMetadata)) { $res = KTDocumentUtil::setIncomplete($oDocument, 'metadata'); @@ -547,7 +547,7 @@ class KTDocumentUtil { /** * Stores contents (filelike) from source into the document storage */ - function storeContents(&$oDocument, $oContents, $aOptions = null) { + function storeContents(&$oDocument, $oContents = null, $aOptions = null) { if (is_null($aOptions)) { $aOptions = array(); } @@ -557,17 +557,20 @@ class KTDocumentUtil { $oKTConfig =& KTConfig::getSingleton(); $sBasedir = $oKTConfig->get('urls/tmpDirectory'); - $sFilename = tempnam($sBasedir, 'kt_storecontents'); - $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())); - } + $sFilename = (isset($aOptions['temp_file'])) ? $aOptions['temp_file'] : tempnam($sBasedir, 'kt_storecontents'); + +// $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())); +// } + $sType = KTMime::getMimeTypeFromFile($sFilename); $iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName()); $oDocument->setMimeTypeId($iMimeTypeId); + $res = $oStorage->upload($oDocument, $sFilename); if ($res === false) { return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), _kt('No reason given'))); @@ -578,7 +581,7 @@ class KTDocumentUtil { KTDocumentUtil::setComplete($oDocument, 'contents'); if ($aOptions['cleanup_initial_file']) { - @unlink($oContents->sFilename); + @unlink($sFilename); } return true; diff --git a/lib/storage/ondiskhashedstoragemanager.inc.php b/lib/storage/ondiskhashedstoragemanager.inc.php index 3c6015c..426fbff 100644 --- a/lib/storage/ondiskhashedstoragemanager.inc.php +++ b/lib/storage/ondiskhashedstoragemanager.inc.php @@ -71,6 +71,29 @@ class KTOnDiskHashedStorageManager extends KTStorageManager { } } + /** + * Upload a temporary file + * + * @param unknown_type $sUploadedFile + * @param unknown_type $sTmpFilePath + * @return unknown + */ + function uploadTmpFile($sUploadedFile, $sTmpFilePath) { + + //copy the file accross + if (OS_WINDOWS) { + $sTmpFilePath = str_replace('\\','/',$sTmpFilePath); + } + if ($this->writeToFile($sUploadedFile, $sTmpFilePath)) { + if (file_exists($sTmpFilePath)) { + return true; + } else { + return false; + } + } + return false; + } + function writeToFile($sTmpFilePath, $sDocumentFileSystemPath) { // Make it easy to write compressed/encrypted storage @@ -162,7 +185,7 @@ class KTOnDiskHashedStorageManager extends KTStorageManager { } // HTTP/1.0 //header("Pragma: no-cache"); // Don't send this header! It breaks IE. - + $oFile = new KTFSFileLike($sPath); KTFileLikeUtil::send_contents($oFile); } else { diff --git a/plugins/ktcore/folder/addDocument.php b/plugins/ktcore/folder/addDocument.php index e1bffcf..e714980 100644 --- a/plugins/ktcore/folder/addDocument.php +++ b/plugins/ktcore/folder/addDocument.php @@ -6,7 +6,7 @@ * License Version 1.1.2 ("License"); You may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.knowledgetree.com/KPL - * + * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. * See the License for the specific language governing rights and @@ -17,9 +17,9 @@ * (ii) the KnowledgeTree copyright notice * in the same form as they appear in the distribution. See the License for * requirements. - * + * * The Original Code is: KnowledgeTree Open Source - * + * * The Initial Developer of the Original Code is The Jam Warehouse Software * (Pty) Ltd, trading as KnowledgeTree. * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright @@ -52,7 +52,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { if (empty($res)) { return $res; } - + $postExpected = KTUtil::arrayGet($_REQUEST, "postExpected"); $postReceived = KTUtil::arrayGet($_REQUEST, "postReceived"); if (!empty($postExpected)) { @@ -67,7 +67,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { function form_initialdata() { $oForm = new KTForm; - + $oForm->setOptions(array( 'label' => _kt("Add a document"), 'action' => 'processInitialData', @@ -79,13 +79,13 @@ class KTFolderAddDocumentAction extends KTFolderAction { 'submit_label' => _kt("Add"), 'file_upload' => true, )); - + $aTypes; foreach (DocumentType::getListForUserAndFolder($this->oUser, $this->oFolder) as $oDocumentType) { if(!$oDocumentType->getDisabled()) { $aTypes[] = $oDocumentType; } - } + } $oForm->setWidgets(array( array('ktcore.widgets.file',array( 'label' => _kt('File'), @@ -109,9 +109,9 @@ class KTFolderAddDocumentAction extends KTFolderAction { 'id_method' => 'getId', 'label_method' => 'getName', 'simple_select' => false, - )), + )), )); - + $oForm->setValidators(array( array('ktcore.validators.file', array( 'test' => 'file', @@ -126,65 +126,70 @@ class KTFolderAddDocumentAction extends KTFolderAction { 'output' => 'document_type', 'class' => 'DocumentType', 'ids' => true, - )), + )), )); - + return $oForm; } - + function getFieldsetsForType($iTypeId) { $typeid = KTUtil::getId($iTypeId); $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); $aSpecificFieldsetIds = KTFieldset::getForDocumentType($typeid, array('ids' => false)); - + $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); - return $fieldsets; + return $fieldsets; } function do_main() { $this->oPage->setBreadcrumbDetails(_kt("Add a document")); $oForm = $this->form_initialdata(); return $oForm->renderPage(_kt('Add a document to: ') . $this->oFolder->getName()); - } - + } + function do_processInitialData() { $oForm = $this->form_initialdata(); $res = $oForm->validate(); if (!empty($res['errors'])) { return $oForm->handleError(); - } + } $data = $res['results']; $key = KTUtil::randomString(32); - - // joy joy, we need to store the file first, or PHP will (helpfully) + + // joy joy, we need to store the file first, or PHP will (helpfully) // clean it up for us $oKTConfig =& KTConfig::getSingleton(); - $sBasedir = $oKTConfig->get("urls/tmpDirectory"); - + $sBasedir = $oKTConfig->get("urls/tmpDirectory"); + $sFilename = tempnam($sBasedir, 'kt_storecontents'); - $oContents = new KTFSFileLike($data['file']['tmp_name']); - $oOutputFile = new KTFSFileLike($sFilename); - $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile); - $data['file']['tmp_name'] = $sFilename; - if (PEAR::isError($res)) { - $oForm->handleError(sprintf(_kt("Failed to store file: %s"), $res->getMessage())); - } - $_SESSION['_add_data'] = array($key => $data); - +// $oContents = new KTFSFileLike($data['file']['tmp_name']); +// $oOutputFile = new KTFSFileLike($sFilename); +// $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile); +// +// if (PEAR::isError($res)) { +// $oForm->handleError(sprintf(_kt("Failed to store file: %s"), $res->getMessage())); +// } + + $oStorage =& KTStorageManagerUtil::getSingleton(); + $oStorage->uploadTmpFile($data['file']['tmp_name'], $sFilename); + + $data['file']['tmp_name'] = $sFilename; + $_SESSION['_add_data'] = array($key => $data); + // if we don't need metadata $fieldsets = $this->getFieldsetsForType($data['document_type']); if (empty($fieldsets)) { - return $this->successRedirectTo('finalise', _kt("File uploaded successfully. Processing."), sprintf("fFileKey=%s", $key)); + return $this->successRedirectTo('finalise', _kt("File uploaded successfully. Processing."), sprintf("fFileKey=%s", $key)); } - + // if we need metadata - + $this->successRedirectTo('metadata', _kt("File uploaded successfully. Please fill in the metadata below."), sprintf("fFileKey=%s", $key)); } - + function form_metadata($sess_key) { $oForm = new KTForm; $oForm->setOptions(array( @@ -197,49 +202,49 @@ class KTFolderAddDocumentAction extends KTFolderAction { 'context' => &$this, 'extraargs' => $this->meldPersistQuery("","",true), )); - + $oFReg =& KTFieldsetRegistry::getSingleton(); - + $doctypeid = $_SESSION['_add_data'][$sess_key]['document_type']; - + $widgets = array(); $validators = array(); $fieldsets = $this->getFieldsetsForType($doctypeid); - + foreach ($fieldsets as $oFieldset) { $widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); - $validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); + $validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument)); } - + $oForm->setWidgets($widgets); - $oForm->setValidators($validators); - + $oForm->setValidators($validators); + return $oForm; } - + function do_metadata() { $this->persistParams(array('fFileKey')); - + $oForm = $this->form_metadata($_REQUEST['fFileKey']); return $oForm->render(); } - + function do_finalise() { - $this->persistParams(array('fFileKey')); + $this->persistParams(array('fFileKey')); $sess_key = $_REQUEST['fFileKey']; $oForm = $this->form_metadata($sess_key); $res = $oForm->validate(); if (!empty($res['errors'])) { - return $oForm->handleError(); + return $oForm->handleError(); } $data = $res['results']; - + $extra_d = $_SESSION['_add_data'][$sess_key]; $doctypeid = $extra_d['document_type']; $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false)); $aSpecificFieldsetIds = KTFieldset::getForDocumentType($doctypeid, array('ids' => false)); $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds); - + $MDPack = array(); foreach ($fieldsets as $oFieldset) { @@ -247,9 +252,9 @@ class KTFolderAddDocumentAction extends KTFolderAction { $values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId()); foreach ($fields as $oField) { - $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); + $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); // ALT.METADATA.LAYER.DIE.DIE.DIE - if (!is_null($val)) { + if (!is_null($val)) { $MDPack[] = array( $oField, $val @@ -258,14 +263,14 @@ class KTFolderAddDocumentAction extends KTFolderAction { } } - // older code + // older code $mpo =& new JavascriptObserver($this); $oUploadChannel =& KTUploadChannel::getSingleton(); $oUploadChannel->addObserver($mpo); - + require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php'); - require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); + //require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php'); require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc'); require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php'); require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php'); @@ -274,13 +279,14 @@ class KTFolderAddDocumentAction extends KTFolderAction { 'redirect_to' => array('main', sprintf('fFolderId=%d', $this->oFolder->getId())), 'max_str_len' => 200, ); - + $aFile = $this->oValidator->validateFile($extra_d['file'], $aErrorOptions); $sTitle = sanitizeForSQL($extra_d['document_name']); - + $iFolderId = $this->oFolder->getId(); $aOptions = array( - 'contents' => new KTFSFileLike($aFile['tmp_name']), + //'contents' => new KTFSFileLike($aFile['tmp_name']), + 'temp_file' => $aFile['tmp_name'], 'documenttype' => DocumentType::get($extra_d['document_type']), 'metadata' => $MDPack, 'description' => $sTitle, @@ -302,7 +308,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { exit(0); } - + } ?> -- libgit2 0.21.4