Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception in controller if AjaxUploader gives an error #442

Merged
merged 3 commits into from
Nov 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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