Skip to content

Commit

Permalink
Adds proper FileNotFound exception handling for GET requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pjdevries committed Jun 8, 2021
1 parent 03c413b commit d1967c5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 27 deletions.
17 changes: 15 additions & 2 deletions api/components/com_media/src/Model/MediaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

\defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\Exception\ResourceNotFound;
use Joomla\CMS\MVC\Model\BaseModel;
use Joomla\CMS\MVC\Model\ListModelInterface;
use Joomla\CMS\Pagination\Pagination;
use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
use Joomla\Component\Media\Administrator\Model\ApiModel;
use Joomla\Component\Media\Api\Helper\MediaHelper;

Expand Down Expand Up @@ -62,8 +65,18 @@ public function getItems()
'content' => $this->getState('content', false)
];

list('adapter' => $adapterName, 'path' => $path) = MediaHelper::adapterNameAndPath($this->getState('path', ''));
$files = $this->mediaApiModel->getFiles($adapterName, $path, $options);
['adapter' => $adapterName, 'path' => $path] = MediaHelper::adapterNameAndPath($this->getState('path', ''));
try
{
$files = $this->mediaApiModel->getFiles($adapterName, $path, $options);
}
catch (FileNotFoundException $e)
{
throw new ResourceNotFound(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $path),
404
);
}

// A hacky way to enable the standard jsonapiView::displayList() to create a Pagination object.
// Because com_media's ApiModel does not support pagination as we know from regular ListModel
Expand Down
99 changes: 74 additions & 25 deletions api/components/com_media/src/Model/MediumModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
\defined('_JEXEC') or die;

use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\Exception\ResourceNotFound;
use Joomla\CMS\MVC\Controller\Exception\Save;
use Joomla\CMS\MVC\Model\BaseModel;
use Joomla\Component\Media\Administrator\Exception\FileExistsException;
Expand All @@ -26,7 +27,8 @@
*
* @since 4.0
*/
class MediumModel extends BaseModel {
class MediumModel extends BaseModel
{

/**
* Instance of com_media's ApiModel
Expand All @@ -35,7 +37,8 @@ class MediumModel extends BaseModel {
*/
private $mediaApiModel;

public function __construct($config = []) {
public function __construct($config = [])
{
parent::__construct($config);

$this->mediaApiModel = new ApiModel();
Expand All @@ -48,20 +51,32 @@ public function __construct($config = []) {
*
* @since 4.0.0
*/
public function getItem() {
public function getItem()
{
$options = [
'path' => $this->getState('path', ''),
'url' => $this->getState('url', FALSE),
'temp' => $this->getState('temp', FALSE),
'content' => $this->getState('content', FALSE),
'url' => $this->getState('url', false),
'temp' => $this->getState('temp', false),
'content' => $this->getState('content', false),
];

[
'adapter' => $adapterName,
'path' => $path,
] = MediaHelper::adapterNameAndPath($this->getState('path', ''));
]
= MediaHelper::adapterNameAndPath($this->getState('path', ''));

return $this->mediaApiModel->getFile($adapterName, $path, $options);
try
{
return $this->mediaApiModel->getFile($adapterName, $path, $options);
}
catch (FileNotFoundException $e)
{
throw new ResourceNotFound(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $path),
404
);
}
}

/**
Expand All @@ -73,16 +88,18 @@ public function getItem() {
*
* @since 4.0.0
*/
public function save($path = NULL) {
public function save($path = null)
{
$path = $this->getState('path', '');
$oldPath = $this->getState('old_path', '');
$content = $this->getState('content', NULL);
$override = $this->getState('override', FALSE);
$content = $this->getState('content', null);
$override = $this->getState('override', false);

[
'adapter' => $adapterName,
'path' => $path,
] = MediaHelper::adapterNameAndPath($path);
]
= MediaHelper::adapterNameAndPath($path);

$resultPath = '';

Expand All @@ -95,12 +112,18 @@ public function save($path = NULL) {
{
// ApiModel::move() (or actually LocalAdapter::move()) returns a path
// with leading slash.
$resultPath = trim($this->mediaApiModel->move($adapterName, $oldPath, $path, $override), '/');
$resultPath = trim(
$this->mediaApiModel->move(
$adapterName, $oldPath, $path, $override
), '/'
);
}
catch (FileNotFoundException $e)
{
throw new Save(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $oldPath),
Text::sprintf(
'WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $oldPath
),
404
);
}
Expand All @@ -120,29 +143,42 @@ public function save($path = NULL) {
// If there is content, com_media's assumes the new item is a file.
// Otherwise a folder is assumed.
$name = $content
? $this->mediaApiModel->createFile($adapterName, $basename, $dirname, $content, $override)
: $this->mediaApiModel->createFolder($adapterName, $basename, $dirname, $override);
? $this->mediaApiModel->createFile(
$adapterName, $basename, $dirname, $content, $override
)
: $this->mediaApiModel->createFolder(
$adapterName, $basename, $dirname, $override
);

$resultPath = $dirname . '/' . $name;
}
catch (FileNotFoundException $e)
{
throw new Save(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $dirname . '/' . $basename),
Text::sprintf(
'WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND',
$dirname . '/' . $basename
),
404
);
}
catch (FileExistsException $e)
{
throw new Save(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_EXISTS', $dirname . '/' . $basename),
Text::sprintf(
'WEBSERVICE_COM_MEDIA_FILE_EXISTS',
$dirname . '/' . $basename
),
400
);
}
catch (InvalidPathException $e)
{
throw new Save(
Text::sprintf('WEBSERVICE_COM_MEDIA_BAD_FILE_TYPE', $dirname . '/' . $basename),
Text::sprintf(
'WEBSERVICE_COM_MEDIA_BAD_FILE_TYPE',
$dirname . '/' . $basename
),
400
);
}
Expand All @@ -159,19 +195,27 @@ public function save($path = NULL) {

try
{
$this->mediaApiModel->updateFile($adapterName, $basename, $dirname, $content);
$this->mediaApiModel->updateFile(
$adapterName, $basename, $dirname, $content
);
}
catch (FileNotFoundException $e)
{
throw new Save(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $dirname . '/' . $basename),
Text::sprintf(
'WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND',
$dirname . '/' . $basename
),
404
);
}
catch (InvalidPathException $e)
{
throw new Save(
Text::sprintf('WEBSERVICE_COM_MEDIA_BAD_FILE_TYPE', $dirname . '/' . $basename),
Text::sprintf(
'WEBSERVICE_COM_MEDIA_BAD_FILE_TYPE',
$dirname . '/' . $basename
),
400
);
}
Expand All @@ -183,7 +227,9 @@ public function save($path = NULL) {
if (!$resultPath)
{
throw new Save(
Text::_('WEBSERVICE_COM_MEDIA_UNSUPPORTED_PARAMETER_COMBINATION'),
Text::_(
'WEBSERVICE_COM_MEDIA_UNSUPPORTED_PARAMETER_COMBINATION'
),
400
);
}
Expand All @@ -198,13 +244,15 @@ public function save($path = NULL) {
*
* @since 4.0.0
*/
public function delete() {
public function delete()
{
$path = $this->getState('path', '');

[
'adapter' => $adapterName,
'path' => $path,
] = MediaHelper::adapterNameAndPath($path);
]
= MediaHelper::adapterNameAndPath($path);

try
{
Expand All @@ -218,4 +266,5 @@ public function delete() {
);
}
}

}

0 comments on commit d1967c5

Please sign in to comment.