From 6af4e62d6505d332b381a45c0ae8a35404007bcc Mon Sep 17 00:00:00 2001 From: Megan Watson Date: Fri, 23 Nov 2007 10:24:47 +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 | 43 +++++++++++++++++++++++++++++++++---------- plugins/ktcore/folder/addDocument.php | 43 +++++++++++++++++++++++++------------------ 5 files changed, 89 insertions(+), 53 deletions(-) diff --git a/ktapi/KTAPIFolder.inc.php b/ktapi/KTAPIFolder.inc.php index 4984dd8..a0d8000 100644 --- a/ktapi/KTAPIFolder.inc.php +++ b/ktapi/KTAPIFolder.inc.php @@ -478,7 +478,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 3f234db..2d99a51 100644 --- a/ktwebdav/lib/KTWebDAVServer.inc.php +++ b/ktwebdav/lib/KTWebDAVServer.inc.php @@ -1371,9 +1371,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, ); @@ -1427,9 +1428,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 0a21c54..0a83968 100644 --- a/lib/documentmanagement/documentutil.inc.php +++ b/lib/documentmanagement/documentutil.inc.php @@ -234,7 +234,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); @@ -256,20 +256,20 @@ class KTDocumentUtil { 'documenttypeid' => $iDocumentTypeId, )); - 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'); @@ -694,7 +694,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(); } @@ -704,17 +704,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'))); @@ -725,7 +728,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 e72fa4b..7402f28 100644 --- a/lib/storage/ondiskhashedstoragemanager.inc.php +++ b/lib/storage/ondiskhashedstoragemanager.inc.php @@ -8,32 +8,32 @@ * KnowledgeTree Open Source Edition * Document Management Made Simple * Copyright (C) 2004 - 2007 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 The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place, * Blake Street, Observatory, 7925 South Africa. 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 - * copyright notice. + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. * Contributor( s): ______________________________________ */ @@ -77,6 +77,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 @@ -318,7 +341,7 @@ class KTOnDiskHashedStorageManager extends KTStorageManager { $sDocumentRoot = $oConfig->get('urls/documentRoot'); $iContentId = $oVersion->getContentVersionId(); $oContentVersion = KTDocumentContentVersion::get($iContentId); - + $sPath = $oContentVersion->getStoragePath(); $sFullPath = sprintf("%s/%s", $sDocumentRoot, $sPath); if (file_exists($sFullPath)) { diff --git a/plugins/ktcore/folder/addDocument.php b/plugins/ktcore/folder/addDocument.php index 0414656..83e6869 100644 --- a/plugins/ktcore/folder/addDocument.php +++ b/plugins/ktcore/folder/addDocument.php @@ -5,32 +5,32 @@ * KnowledgeTree Open Source Edition * Document Management Made Simple * Copyright (C) 2004 - 2007 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 The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place, * Blake Street, Observatory, 7925 South Africa. 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 - * copyright notice. + * must display the words "Powered by KnowledgeTree" and retain the original + * copyright notice. * Contributor( s): ______________________________________ * */ @@ -193,14 +193,20 @@ class KTFolderAddDocumentAction extends KTFolderAction { $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())); - } + //$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 @@ -294,7 +300,7 @@ class KTFolderAddDocumentAction extends KTFolderAction { $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'); @@ -309,7 +315,8 @@ class KTFolderAddDocumentAction extends KTFolderAction { $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, -- libgit2 0.21.4