Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit fdda3b0

Browse files
committed
ENH: refs #446. Server side quota validation for uploading new revisions
1 parent 3ea0701 commit fdda3b0

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

core/controllers/UploadController.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,22 @@ public function saveuploadedAction()
417417
$changes = $this->_getParam('changes');
418418
$itemId = $itemId_itemRevisionNumber[0];
419419
$itemRevisionNumber = $itemId_itemRevisionNumber[1];
420+
421+
$validations = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_VALIDATE_UPLOAD_REVISION',
422+
array('filename' => $filename,
423+
'size' => $file_size,
424+
'path' => $path,
425+
'itemId' => $itemId,
426+
'changes' => $changes,
427+
'revisionNumber' => $itemRevisionNumber));
428+
foreach($validations as $validation)
429+
{
430+
if(!$validation['status'])
431+
{
432+
unlink($path);
433+
throw new Zend_Exception($validation['message']);
434+
}
435+
}
420436
$this->Component->Upload->createNewRevision($this->userSession->Dao, $filename, $path, $changes, $itemId, $itemRevisionNumber, $license);
421437
}
422438
else

modules/sizequota/Notification.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function init()
3636
$this->addCallBack('CALLBACK_CORE_GET_JAVAUPLOAD_EXTRA_HTML', 'getExtraHtmlSimple');
3737
$this->addCallBack('CALLBACK_CORE_GET_REVISIONUPLOAD_EXTRA_HTML', 'getExtraHtmlRevision');
3838
$this->addCallBack('CALLBACK_CORE_VALIDATE_UPLOAD', 'validateUpload');
39+
$this->addCallBack('CALLBACK_CORE_VALIDATE_UPLOAD_REVISION', 'validateUploadRevision');
3940

4041
$this->enableWebAPI($this->moduleName);
4142
}
@@ -175,5 +176,49 @@ public function validateUpload($args)
175176
}
176177
return array('status' => true);
177178
}
179+
180+
/**
181+
* Return whether or not the upload is allowed. If uploading the revision
182+
* will cause the size to surpass the quota, it will be rejected.
183+
* @param size Size of the uploaded file
184+
* @param itemId The id of the item being uploaded into
185+
* @return array('status' => boolean, 'message' => 'error message if status is false')
186+
*/
187+
public function validateUploadRevision($args)
188+
{
189+
$modelLoader = new MIDAS_ModelLoader();
190+
$folderModel = $modelLoader->loadModel('Folder');
191+
$itemModel = $modelLoader->loadModel('Item');
192+
$folderQuotaModel = $modelLoader->loadModel('FolderQuota', $this->moduleName);
193+
194+
$item = $itemModel->load($args['itemId']);
195+
if(!$item)
196+
{
197+
return array('status' => false, 'message' => 'Invalid item id');
198+
}
199+
$folders = $item->getFolders();
200+
if(count($folders) == 0)
201+
{
202+
return array('status' => false, 'message' => 'Cannot upload into an orphaned item');
203+
}
204+
$rootFolder = $folderModel->getRoot($folders[0]);
205+
$quota = $folderQuotaModel->getFolderQuota($rootFolder);
206+
if($quota == '')
207+
{
208+
return array('status' => true);
209+
}
210+
211+
$freeSpace = $quota - $folderModel->getSize($rootFolder);
212+
$uploadSize = $args['size'];
213+
if($uploadSize > $freeSpace)
214+
{
215+
return array('status' => false,
216+
'message' => 'Upload quota exceeded. Free space: '.$freeSpace.
217+
'. Attempted upload size: '.$uploadSize.
218+
' into item '.$args['itemId']);
219+
}
220+
return array('status' => true);
221+
}
222+
178223
} //end class
179224
?>

0 commit comments

Comments
 (0)