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

Commit 0c101e5

Browse files
committed
ENH: refs #0377. Server side validation of upload quota for simpleupload
Also make sure we delete temporary upload file in both the error condition and the success condition.
1 parent 83be7a8 commit 0c101e5

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

core/controllers/UploadController.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,28 @@ public function saveuploadedAction()
412412
}
413413
$parent = $parentDao->getKey();
414414
}
415+
$validations = Zend_Registry::get('notifier')->callback('CALLBACK_CORE_VALIDATE_UPLOAD',
416+
array('filename' => $filename,
417+
'size' => $file_size,
418+
'path' => $path,
419+
'folderId' => $parent));
420+
foreach($validations as $validation)
421+
{
422+
if(!$validation['status'])
423+
{
424+
unlink($path);
425+
throw new Zend_Exception($validation['message']);
426+
}
427+
}
415428
$item = $this->Component->Upload->createUploadedItem($this->userSession->Dao, $filename, $path, $parent, $license);
429+
unlink($path);
416430
$this->userSession->uploaded[] = $item->getKey();
417431
}
418432

419433
$info = array();
420434
$info['name'] = basename($path);
421435
$info['size'] = $file_size;
422-
echo json_encode($info);
436+
echo JsonComponent::encode($info);
423437
}
424438
}//end saveuploaded
425439

modules/sizequota/Notification.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,38 @@ public function getSimpleuploadExtraHtml($args)
9595
/**
9696
* Return whether or not the upload is allowed. If uploading the file
9797
* will cause the size to surpass the quota, it will be rejected.
98+
* @param size Size of the uploaded file
99+
* @param folderId The id of the folder being uploaded into
100+
* @return array('status' => boolean, 'message' => 'error message if status is false')
98101
*/
99102
public function validateUpload($args)
100103
{
101-
return true;
104+
$modelLoader = new MIDAS_ModelLoader();
105+
$folderModel = $modelLoader->loadModel('Folder');
106+
$folderQuotaModel = $modelLoader->loadModel('FolderQuota', $this->moduleName);
107+
108+
$folder = $folderModel->load($args['folderId']);
109+
if(!$folder)
110+
{
111+
return array('status' => false, 'message' => 'Invalid folder id');
112+
}
113+
$rootFolder = $folderModel->getRoot($folder);
114+
$quota = $folderQuotaModel->getFolderQuota($rootFolder);
115+
if($quota == '')
116+
{
117+
return array('status' => true);
118+
}
119+
120+
$freeSpace = $quota - $folderModel->getSize($rootFolder);
121+
$uploadSize = $args['size'];
122+
if($uploadSize > $freeSpace)
123+
{
124+
return array('status' => false,
125+
'message' => 'Upload quota exceeded. Free space: '.$freeSpace.
126+
'. Attempted upload size: '.$uploadSize.
127+
' into folder '.$args['folderId']);
128+
}
129+
return array('status' => true);
102130
}
103131
} //end class
104132
?>

0 commit comments

Comments
 (0)