Skip to content

Commit

Permalink
Throw in AjaxUploader
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksh committed Nov 19, 2021
1 parent 3d0e64f commit 8551bf9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 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
4 changes: 0 additions & 4 deletions oc-includes/osclass/classes/controller/CWebAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,6 @@ public function doModel()
try {
$result =
$uploader->handleUpload(osc_content_path() . 'uploads/temp/' . $filename);

if (isset($result['error'])) {
throw new Exception($result['error']);
}
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_WARNING);
echo json_encode(array('success' => false));
Expand Down

0 comments on commit 8551bf9

Please sign in to comment.