Commit 262da92f9081c6d91fa18316877ae0bbb4975db8

Authored by megan_w
1 parent 2208336f

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.

Committed by: Megan Watson
Reviewed by: Conrad Vermeulen



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/STABLE/branches/3.4.5-Release-Branch@7730 c91229c3-7414-0410-bfa2-8a42b809f60b
ktapi/KTAPIFolder.inc.php
... ... @@ -450,7 +450,8 @@ class KTAPI_Folder extends KTAPI_FolderItem
450 450  
451 451  
452 452 $options = array(
453   - 'contents' => new KTFSFileLike($tempfilename),
  453 + //'contents' => new KTFSFileLike($tempfilename),
  454 + 'temp_file' => $tempfilename,
454 455 'novalidate' => true,
455 456 'documenttype' => DocumentType::get($documenttypeid),
456 457 'description' => $title,
... ...
ktwebdav/lib/KTWebDAVServer.inc.php
... ... @@ -1259,9 +1259,10 @@ class KTWebDAVServer extends HTTP_WebDAV_Server
1259 1259 );
1260 1260 $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true);
1261 1261  
1262   - include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
  1262 + //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
1263 1263 $aOptions = array(
1264   - 'contents' => new KTFSFileLike($sTempFilename),
  1264 + //'contents' => new KTFSFileLike($sTempFilename),
  1265 + 'temp_file' => $sTempFilename,
1265 1266 'metadata' => array(),
1266 1267 'novalidate' => true,
1267 1268 );
... ... @@ -1309,9 +1310,10 @@ class KTWebDAVServer extends HTTP_WebDAV_Server
1309 1310 );
1310 1311 $this->ktwebdavLog("aFileArray is " . print_r($aFileArray, true), 'info', true);
1311 1312  
1312   - include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
  1313 + //include_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
1313 1314 $aOptions = array(
1314   - 'contents' => new KTFSFileLike($sTempFilename),
  1315 + //'contents' => new KTFSFileLike($sTempFilename),
  1316 + 'temp_file' => $sTempFilename,
1315 1317 'metadata' => array(),
1316 1318 'novalidate' => true,
1317 1319 );
... ...
lib/documentmanagement/documentutil.inc.php
... ... @@ -183,7 +183,7 @@ class KTDocumentUtil {
183 183 function &_add($oFolder, $sFilename, $oUser, $aOptions) {
184 184 global $default;
185 185  
186   - $oContents = KTUtil::arrayGet($aOptions, 'contents');
  186 + //$oContents = KTUtil::arrayGet($aOptions, 'contents');
187 187 $aMetadata = KTUtil::arrayGet($aOptions, 'metadata', null, false);
188 188 $oDocumentType = KTUtil::arrayGet($aOptions, 'documenttype');
189 189 $sDescription = KTUtil::arrayGet($aOptions, 'description', $sFilename);
... ... @@ -208,20 +208,20 @@ class KTDocumentUtil {
208 208 return $oDocument;
209 209 }
210 210  
211   - if (is_null($oContents)) {
212   - $res = KTDocumentUtil::setIncomplete($oDocument, 'contents');
213   - if (PEAR::isError($res)) {
214   - $oDocument->delete();
215   - return $res;
216   - }
217   - } else {
  211 +// if (is_null($oContents)) {
  212 +// $res = KTDocumentUtil::setIncomplete($oDocument, 'contents');
  213 +// if (PEAR::isError($res)) {
  214 +// $oDocument->delete();
  215 +// return $res;
  216 +// }
  217 +// } else {
218 218 // $oUploadChannel->sendMessage(new KTUploadGenericMessage(_kt('Storing contents')));
219   - $res = KTDocumentUtil::storeContents($oDocument, $oContents, $aOptions);
  219 + $res = KTDocumentUtil::storeContents($oDocument, '', $aOptions);
220 220 if (PEAR::isError($res)) {
221 221 $oDocument->delete();
222 222 return $res;
223 223 }
224   - }
  224 +// }
225 225  
226 226 if (is_null($aMetadata)) {
227 227 $res = KTDocumentUtil::setIncomplete($oDocument, 'metadata');
... ... @@ -547,7 +547,7 @@ class KTDocumentUtil {
547 547 /**
548 548 * Stores contents (filelike) from source into the document storage
549 549 */
550   - function storeContents(&$oDocument, $oContents, $aOptions = null) {
  550 + function storeContents(&$oDocument, $oContents = null, $aOptions = null) {
551 551 if (is_null($aOptions)) {
552 552 $aOptions = array();
553 553 }
... ... @@ -557,17 +557,20 @@ class KTDocumentUtil {
557 557 $oKTConfig =& KTConfig::getSingleton();
558 558 $sBasedir = $oKTConfig->get('urls/tmpDirectory');
559 559  
560   - $sFilename = tempnam($sBasedir, 'kt_storecontents');
561   - $oOutputFile = new KTFSFileLike($sFilename);
562   - $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile);
563   - if (($res === false)) {
564   - return PEAR::raiseError(_kt("Couldn't store contents, and no reason given."));
565   - } else if (PEAR::isError($res)) {
566   - return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), $res->getMessage()));
567   - }
  560 + $sFilename = (isset($aOptions['temp_file'])) ? $aOptions['temp_file'] : tempnam($sBasedir, 'kt_storecontents');
  561 +
  562 +// $oOutputFile = new KTFSFileLike($sFilename);
  563 +// $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile);
  564 +// if (($res === false)) {
  565 +// return PEAR::raiseError(_kt("Couldn't store contents, and no reason given."));
  566 +// } else if (PEAR::isError($res)) {
  567 +// return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), $res->getMessage()));
  568 +// }
  569 +
568 570 $sType = KTMime::getMimeTypeFromFile($sFilename);
569 571 $iMimeTypeId = KTMime::getMimeTypeID($sType, $oDocument->getFileName());
570 572 $oDocument->setMimeTypeId($iMimeTypeId);
  573 +
571 574 $res = $oStorage->upload($oDocument, $sFilename);
572 575 if ($res === false) {
573 576 return PEAR::raiseError(sprintf(_kt("Couldn't store contents: %s"), _kt('No reason given')));
... ... @@ -578,7 +581,7 @@ class KTDocumentUtil {
578 581 KTDocumentUtil::setComplete($oDocument, 'contents');
579 582  
580 583 if ($aOptions['cleanup_initial_file']) {
581   - @unlink($oContents->sFilename);
  584 + @unlink($sFilename);
582 585 }
583 586  
584 587 return true;
... ...
lib/storage/ondiskhashedstoragemanager.inc.php
... ... @@ -71,6 +71,29 @@ class KTOnDiskHashedStorageManager extends KTStorageManager {
71 71 }
72 72 }
73 73  
  74 + /**
  75 + * Upload a temporary file
  76 + *
  77 + * @param unknown_type $sUploadedFile
  78 + * @param unknown_type $sTmpFilePath
  79 + * @return unknown
  80 + */
  81 + function uploadTmpFile($sUploadedFile, $sTmpFilePath) {
  82 +
  83 + //copy the file accross
  84 + if (OS_WINDOWS) {
  85 + $sTmpFilePath = str_replace('\\','/',$sTmpFilePath);
  86 + }
  87 + if ($this->writeToFile($sUploadedFile, $sTmpFilePath)) {
  88 + if (file_exists($sTmpFilePath)) {
  89 + return true;
  90 + } else {
  91 + return false;
  92 + }
  93 + }
  94 + return false;
  95 + }
  96 +
74 97 function writeToFile($sTmpFilePath, $sDocumentFileSystemPath) {
75 98 // Make it easy to write compressed/encrypted storage
76 99  
... ... @@ -162,7 +185,7 @@ class KTOnDiskHashedStorageManager extends KTStorageManager {
162 185 }
163 186 // HTTP/1.0
164 187 //header("Pragma: no-cache"); // Don't send this header! It breaks IE.
165   -
  188 +
166 189 $oFile = new KTFSFileLike($sPath);
167 190 KTFileLikeUtil::send_contents($oFile);
168 191 } else {
... ...
plugins/ktcore/folder/addDocument.php
... ... @@ -6,7 +6,7 @@
6 6 * License Version 1.1.2 ("License"); You may not use this file except in
7 7 * compliance with the License. You may obtain a copy of the License at
8 8 * http://www.knowledgetree.com/KPL
9   - *
  9 + *
10 10 * Software distributed under the License is distributed on an "AS IS"
11 11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
12 12 * See the License for the specific language governing rights and
... ... @@ -17,9 +17,9 @@
17 17 * (ii) the KnowledgeTree copyright notice
18 18 * in the same form as they appear in the distribution. See the License for
19 19 * requirements.
20   - *
  20 + *
21 21 * The Original Code is: KnowledgeTree Open Source
22   - *
  22 + *
23 23 * The Initial Developer of the Original Code is The Jam Warehouse Software
24 24 * (Pty) Ltd, trading as KnowledgeTree.
25 25 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -52,7 +52,7 @@ class KTFolderAddDocumentAction extends KTFolderAction {
52 52 if (empty($res)) {
53 53 return $res;
54 54 }
55   -
  55 +
56 56 $postExpected = KTUtil::arrayGet($_REQUEST, "postExpected");
57 57 $postReceived = KTUtil::arrayGet($_REQUEST, "postReceived");
58 58 if (!empty($postExpected)) {
... ... @@ -67,7 +67,7 @@ class KTFolderAddDocumentAction extends KTFolderAction {
67 67  
68 68 function form_initialdata() {
69 69 $oForm = new KTForm;
70   -
  70 +
71 71 $oForm->setOptions(array(
72 72 'label' => _kt("Add a document"),
73 73 'action' => 'processInitialData',
... ... @@ -79,13 +79,13 @@ class KTFolderAddDocumentAction extends KTFolderAction {
79 79 'submit_label' => _kt("Add"),
80 80 'file_upload' => true,
81 81 ));
82   -
  82 +
83 83 $aTypes;
84 84 foreach (DocumentType::getListForUserAndFolder($this->oUser, $this->oFolder) as $oDocumentType) {
85 85 if(!$oDocumentType->getDisabled()) {
86 86 $aTypes[] = $oDocumentType;
87 87 }
88   - }
  88 + }
89 89 $oForm->setWidgets(array(
90 90 array('ktcore.widgets.file',array(
91 91 'label' => _kt('File'),
... ... @@ -109,9 +109,9 @@ class KTFolderAddDocumentAction extends KTFolderAction {
109 109 'id_method' => 'getId',
110 110 'label_method' => 'getName',
111 111 'simple_select' => false,
112   - )),
  112 + )),
113 113 ));
114   -
  114 +
115 115 $oForm->setValidators(array(
116 116 array('ktcore.validators.file', array(
117 117 'test' => 'file',
... ... @@ -126,65 +126,70 @@ class KTFolderAddDocumentAction extends KTFolderAction {
126 126 'output' => 'document_type',
127 127 'class' => 'DocumentType',
128 128 'ids' => true,
129   - )),
  129 + )),
130 130 ));
131   -
  131 +
132 132 return $oForm;
133 133 }
134   -
  134 +
135 135 function getFieldsetsForType($iTypeId) {
136 136 $typeid = KTUtil::getId($iTypeId);
137 137 $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false));
138 138 $aSpecificFieldsetIds = KTFieldset::getForDocumentType($typeid, array('ids' => false));
139   -
  139 +
140 140 $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds);
141   - return $fieldsets;
  141 + return $fieldsets;
142 142 }
143 143  
144 144 function do_main() {
145 145 $this->oPage->setBreadcrumbDetails(_kt("Add a document"));
146 146 $oForm = $this->form_initialdata();
147 147 return $oForm->renderPage(_kt('Add a document to: ') . $this->oFolder->getName());
148   - }
149   -
  148 + }
  149 +
150 150 function do_processInitialData() {
151 151 $oForm = $this->form_initialdata();
152 152 $res = $oForm->validate();
153 153 if (!empty($res['errors'])) {
154 154 return $oForm->handleError();
155   - }
  155 + }
156 156 $data = $res['results'];
157 157 $key = KTUtil::randomString(32);
158 158  
159   -
160   - // joy joy, we need to store the file first, or PHP will (helpfully)
  159 +
  160 + // joy joy, we need to store the file first, or PHP will (helpfully)
161 161 // clean it up for us
162 162  
163 163 $oKTConfig =& KTConfig::getSingleton();
164   - $sBasedir = $oKTConfig->get("urls/tmpDirectory");
165   -
  164 + $sBasedir = $oKTConfig->get("urls/tmpDirectory");
  165 +
166 166 $sFilename = tempnam($sBasedir, 'kt_storecontents');
167   - $oContents = new KTFSFileLike($data['file']['tmp_name']);
168   - $oOutputFile = new KTFSFileLike($sFilename);
169   - $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile);
170   - $data['file']['tmp_name'] = $sFilename;
171 167  
172   - if (PEAR::isError($res)) {
173   - $oForm->handleError(sprintf(_kt("Failed to store file: %s"), $res->getMessage()));
174   - }
175   - $_SESSION['_add_data'] = array($key => $data);
176   -
  168 +// $oContents = new KTFSFileLike($data['file']['tmp_name']);
  169 +// $oOutputFile = new KTFSFileLike($sFilename);
  170 +// $res = KTFileLikeUtil::copy_contents($oContents, $oOutputFile);
  171 +//
  172 +// if (PEAR::isError($res)) {
  173 +// $oForm->handleError(sprintf(_kt("Failed to store file: %s"), $res->getMessage()));
  174 +// }
  175 +
  176 + $oStorage =& KTStorageManagerUtil::getSingleton();
  177 + $oStorage->uploadTmpFile($data['file']['tmp_name'], $sFilename);
  178 +
  179 + $data['file']['tmp_name'] = $sFilename;
  180 + $_SESSION['_add_data'] = array($key => $data);
  181 +
177 182 // if we don't need metadata
178 183 $fieldsets = $this->getFieldsetsForType($data['document_type']);
179 184 if (empty($fieldsets)) {
180   - return $this->successRedirectTo('finalise', _kt("File uploaded successfully. Processing."), sprintf("fFileKey=%s", $key));
  185 + return $this->successRedirectTo('finalise', _kt("File uploaded successfully. Processing."), sprintf("fFileKey=%s", $key));
181 186 }
182   -
  187 +
183 188 // if we need metadata
184   -
  189 +
185 190 $this->successRedirectTo('metadata', _kt("File uploaded successfully. Please fill in the metadata below."), sprintf("fFileKey=%s", $key));
186 191 }
187   -
  192 +
188 193 function form_metadata($sess_key) {
189 194 $oForm = new KTForm;
190 195 $oForm->setOptions(array(
... ... @@ -197,49 +202,49 @@ class KTFolderAddDocumentAction extends KTFolderAction {
197 202 'context' => &$this,
198 203 'extraargs' => $this->meldPersistQuery("","",true),
199 204 ));
200   -
  205 +
201 206 $oFReg =& KTFieldsetRegistry::getSingleton();
202   -
  207 +
203 208 $doctypeid = $_SESSION['_add_data'][$sess_key]['document_type'];
204   -
  209 +
205 210 $widgets = array();
206 211 $validators = array();
207 212 $fieldsets = $this->getFieldsetsForType($doctypeid);
208   -
  213 +
209 214 foreach ($fieldsets as $oFieldset) {
210 215 $widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument));
211   - $validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument));
  216 + $validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument));
212 217 }
213   -
  218 +
214 219 $oForm->setWidgets($widgets);
215   - $oForm->setValidators($validators);
216   -
  220 + $oForm->setValidators($validators);
  221 +
217 222 return $oForm;
218 223 }
219   -
  224 +
220 225 function do_metadata() {
221 226 $this->persistParams(array('fFileKey'));
222   -
  227 +
223 228 $oForm = $this->form_metadata($_REQUEST['fFileKey']);
224 229 return $oForm->render();
225 230 }
226   -
  231 +
227 232 function do_finalise() {
228   - $this->persistParams(array('fFileKey'));
  233 + $this->persistParams(array('fFileKey'));
229 234 $sess_key = $_REQUEST['fFileKey'];
230 235 $oForm = $this->form_metadata($sess_key);
231 236 $res = $oForm->validate();
232 237 if (!empty($res['errors'])) {
233   - return $oForm->handleError();
  238 + return $oForm->handleError();
234 239 }
235 240 $data = $res['results'];
236   -
  241 +
237 242 $extra_d = $_SESSION['_add_data'][$sess_key];
238 243 $doctypeid = $extra_d['document_type'];
239 244 $aGenericFieldsetIds = KTFieldset::getGenericFieldsets(array('ids' => false));
240 245 $aSpecificFieldsetIds = KTFieldset::getForDocumentType($doctypeid, array('ids' => false));
241 246 $fieldsets = kt_array_merge($aGenericFieldsetIds, $aSpecificFieldsetIds);
242   -
  247 +
243 248  
244 249 $MDPack = array();
245 250 foreach ($fieldsets as $oFieldset) {
... ... @@ -247,9 +252,9 @@ class KTFolderAddDocumentAction extends KTFolderAction {
247 252 $values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId());
248 253  
249 254 foreach ($fields as $oField) {
250   - $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId());
  255 + $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId());
251 256 // ALT.METADATA.LAYER.DIE.DIE.DIE
252   - if (!is_null($val)) {
  257 + if (!is_null($val)) {
253 258 $MDPack[] = array(
254 259 $oField,
255 260 $val
... ... @@ -258,14 +263,14 @@ class KTFolderAddDocumentAction extends KTFolderAction {
258 263  
259 264 }
260 265 }
261   - // older code
  266 + // older code
262 267  
263 268 $mpo =& new JavascriptObserver($this);
264 269 $oUploadChannel =& KTUploadChannel::getSingleton();
265 270 $oUploadChannel->addObserver($mpo);
266   -
  271 +
267 272 require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php');
268   - require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
  273 + //require_once(KT_LIB_DIR . '/filelike/fsfilelike.inc.php');
269 274 require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
270 275 require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
271 276 require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
... ... @@ -274,13 +279,14 @@ class KTFolderAddDocumentAction extends KTFolderAction {
274 279 'redirect_to' => array('main', sprintf('fFolderId=%d', $this->oFolder->getId())),
275 280 'max_str_len' => 200,
276 281 );
277   -
  282 +
278 283 $aFile = $this->oValidator->validateFile($extra_d['file'], $aErrorOptions);
279 284 $sTitle = sanitizeForSQL($extra_d['document_name']);
280   -
  285 +
281 286 $iFolderId = $this->oFolder->getId();
282 287 $aOptions = array(
283   - 'contents' => new KTFSFileLike($aFile['tmp_name']),
  288 + //'contents' => new KTFSFileLike($aFile['tmp_name']),
  289 + 'temp_file' => $aFile['tmp_name'],
284 290 'documenttype' => DocumentType::get($extra_d['document_type']),
285 291 'metadata' => $MDPack,
286 292 'description' => $sTitle,
... ... @@ -302,7 +308,7 @@ class KTFolderAddDocumentAction extends KTFolderAction {
302 308 exit(0);
303 309  
304 310 }
305   -
  311 +
306 312 }
307 313  
308 314 ?>
... ...