Skip to content

Commit

Permalink
Merge pull request #442 from dftd/patch-440
Browse files Browse the repository at this point in the history
Throw exception in controller if AjaxUploader gives an error
  • Loading branch information
navjottomer authored Nov 25, 2021
2 parents f662cb4 + 8551bf9 commit 8202a5d
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions oc-includes/AjaxUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ public function getOriginalName()
public function handleUpload($uploadFilename, $replace = false)
{
if (!is_writable(dirname($uploadFilename))) {
return array('error' => __("Server error. Upload directory isn't writable."));
throw new Exception(__("Server error. Upload directory isn't writable."));
}
if (!$this->file) {
return array('error' => __('No files were uploaded.'));
throw new Exception(__('No files were uploaded.'));
}
$size = $this->file->getSize();
if ($size == 0) {
return array('error' => __('File is empty'));
throw new Exception(__('File is empty'));
}
if ($size > $this->sizeLimit) {
return array('error' => __('File is too large'));
throw new Exception(__('File is too large'));
}

$pathinfo = pathinfo($this->file->getOriginalName());
Expand All @@ -75,29 +75,25 @@ public function handleUpload($uploadFilename, $replace = false)
if ($this->allowedExtensions && stripos($this->allowedExtensions, strtolower($ext)) === false) {
@unlink($uploadFilename); // Wrong extension, remove it for security reasons

return array(
'error' => sprintf(
__('File has an invalid extension, it should be one of %s.'),
$this->allowedExtensions
)
);
throw new Exception(sprintf(
__('File has an invalid extension, it should be one of %s.'),
$this->allowedExtensions
));
}

if (!$replace && file_exists($uploadFilename)) {
return array('error' => 'Could not save uploaded file. File already exists');
throw new Exception(__('Could not save uploaded file. File already exists'));
}

if ($this->file->save($uploadFilename)) {
$result = $this->checkAllowedExt($uploadFilename);
if (!$result) {
@unlink($uploadFilename); // Wrong extension, remove it for security reasons

return array(
'error' => sprintf(
__('File has an invalid extension, it should be one of %s.'),
$this->allowedExtensions
)
);
throw new Exception(sprintf(
__('File has an invalid extension, it should be one of %s.'),
$this->allowedExtensions
));
}
$files = Session::newInstance()->_get('ajax_files');
if (!is_array($files)) {
Expand All @@ -109,7 +105,7 @@ public function handleUpload($uploadFilename, $replace = false)
return array('success' => true);
}

return array('error' => 'Could not save uploaded file. The upload was cancelled, or server error encountered');
throw new Exception('Could not save uploaded file. The upload was cancelled, or server error encountered');
}

/**
Expand Down

0 comments on commit 8202a5d

Please sign in to comment.