Skip to content

Commit

Permalink
[5.2] Media manager fix default adapter handling (#44313)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik authored Nov 13, 2024
1 parent 49f6c13 commit e6173bd
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Media\Administrator\Exception;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Provider Account is empty or not set exception.
*
* @since __DEPLOY_VERSION__
*/
class ProviderAccountIsEmptyException extends \Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Media\Administrator\Exception;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Provider Account not found exception.
*
* @since __DEPLOY_VERSION__
*/
class ProviderAccountNotFoundException extends \Exception
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* @package Joomla.Administrator
* @subpackage com_media
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Component\Media\Administrator\Exception;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Provider not found exception.
*
* @since __DEPLOY_VERSION__
*/
class ProviderNotFoundException extends \Exception
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

use Joomla\CMS\Language\Text;
use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
use Joomla\Component\Media\Administrator\Exception\ProviderAccountIsEmptyException;
use Joomla\Component\Media\Administrator\Exception\ProviderAccountNotFoundException;
use Joomla\Component\Media\Administrator\Exception\ProviderNotFoundException;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand Down Expand Up @@ -97,7 +100,7 @@ public function unregisterProvider(?ProviderInterface $provider = null): void
public function getProvider($id)
{
if (!isset($this->providers[$id])) {
throw new \Exception(Text::_('COM_MEDIA_ERROR_MEDIA_PROVIDER_NOT_FOUND'));
throw new ProviderNotFoundException(Text::_('COM_MEDIA_ERROR_MEDIA_PROVIDER_NOT_FOUND'));
}

return $this->providers[$id];
Expand All @@ -119,13 +122,13 @@ public function getAdapter($name)
list($provider, $account) = array_pad(explode('-', $name, 2), 2, null);

if ($account == null) {
throw new \Exception(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_SET'));
throw new ProviderAccountIsEmptyException(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_SET'));
}

$adapters = $this->getProvider($provider)->getAdapters();

if (!isset($adapters[$account])) {
throw new \Exception(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_FOUND'));
throw new ProviderAccountNotFoundException(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_FOUND'));
}

return $adapters[$account];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
use Joomla\Component\Media\Administrator\Event\MediaProviderEvent;
use Joomla\Component\Media\Administrator\Exception\ProviderAccountNotFoundException;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand Down Expand Up @@ -148,14 +149,19 @@ protected function getDefaultAdapterName(): ?string
return $this->defaultAdapterName;
}

$defaultAdapter = $this->getAdapter('local-' . ComponentHelper::getParams('com_media')->get('file_path', 'images'));
try {
// Check for default path in Media config, and whether associated account exists, and use it as default adapter.
$defaultFilePath = ComponentHelper::getParams('com_media')->get('file_path', 'images');
$defaultAdapter = $this->getAdapter('local-' . $defaultFilePath);
// @TODO: Need a proper configuration for default adapter.
} catch (ProviderAccountNotFoundException $e) {
$defaultAdapter = null;
}

if (
!$defaultAdapter
&& $this->getProviderManager()->getProvider('local')
&& $this->getProviderManager()->getProvider('local')->getAdapters()
) {
$defaultAdapter = $this->getProviderManager()->getProvider('local')->getAdapters()[0];
if (!$defaultAdapter) {
// Default adapter were not found, pick the first available local adapter
$localAdapters = $this->getProviderManager()->getProvider('local')->getAdapters();
$defaultAdapter = $localAdapters ? reset($localAdapters) : null;
}

if (!$defaultAdapter) {
Expand Down

0 comments on commit e6173bd

Please sign in to comment.