[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/api/components/com_media/src/Model/ -> MediumModel.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.API
   4   * @subpackage  com_media
   5   *
   6   * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\Component\Media\Api\Model;
  11  
  12  \defined('_JEXEC') or die;
  13  
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\MVC\Controller\Exception\ResourceNotFound;
  16  use Joomla\CMS\MVC\Controller\Exception\Save;
  17  use Joomla\CMS\MVC\Model\BaseModel;
  18  use Joomla\Component\Media\Administrator\Exception\FileExistsException;
  19  use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
  20  use Joomla\Component\Media\Administrator\Exception\InvalidPathException;
  21  use Joomla\Component\Media\Administrator\Model\ApiModel;
  22  use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait;
  23  
  24  /**
  25   * Media web service model supporting a single media item.
  26   *
  27   * @since  4.1.0
  28   */
  29  class MediumModel extends BaseModel
  30  {
  31      use ProviderManagerHelperTrait;
  32  
  33      /**
  34       * Instance of com_media's ApiModel
  35       *
  36       * @var ApiModel
  37       * @since  4.1.0
  38       */
  39      private $mediaApiModel;
  40  
  41  	public function __construct($config = [])
  42      {
  43          parent::__construct($config);
  44  
  45          $this->mediaApiModel = new ApiModel();
  46      }
  47  
  48      /**
  49       * Method to get a single files or folder.
  50       *
  51       * @return  \stdClass  A file or folder object.
  52       *
  53       * @since   4.1.0
  54       * @throws  ResourceNotFound
  55       */
  56  	public function getItem()
  57      {
  58          $options = [
  59              'path'    => $this->getState('path', ''),
  60              'url'     => $this->getState('url', false),
  61              'temp'    => $this->getState('temp', false),
  62              'content' => $this->getState('content', false),
  63          ];
  64  
  65          ['adapter' => $adapterName, 'path' => $path] = $this->resolveAdapterAndPath($this->getState('path', ''));
  66  
  67          try
  68          {
  69              return $this->mediaApiModel->getFile($adapterName, $path, $options);
  70          }
  71          catch (FileNotFoundException $e)
  72          {
  73              throw new ResourceNotFound(
  74                  Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $path),
  75                  404
  76              );
  77          }
  78      }
  79  
  80      /**
  81       * Method to save a file or folder.
  82       *
  83       * @param   string  $path  The primary key of the item (if exists)
  84       *
  85       * @return  string   The path
  86       *
  87       * @since   4.1.0
  88       *
  89       * @throws  Save
  90       */
  91  	public function save($path = null): string
  92      {
  93          $path     = $this->getState('path', '');
  94          $oldPath  = $this->getState('old_path', '');
  95          $content  = $this->getState('content', null);
  96          $override = $this->getState('override', false);
  97  
  98          ['adapter' => $adapterName, 'path' => $path] = $this->resolveAdapterAndPath($path);
  99  
 100          // Trim adapter information from path
 101          if ($pos = strpos($path, ':/'))
 102          {
 103              $path = substr($path, $pos + 1);
 104          }
 105  
 106          // Trim adapter information from old path
 107          if ($pos = strpos($oldPath, ':/'))
 108          {
 109              $oldPath = substr($oldPath, $pos + 1);
 110          }
 111  
 112          $resultPath = '';
 113  
 114          /**
 115           * If we have a (new) path and an old path, we want to move an existing
 116           * file or folder. This must be done before updating the content of a file,
 117           * if also requested (see below).
 118           */
 119          if ($path && $oldPath)
 120          {
 121              try
 122              {
 123                  // ApiModel::move() (or actually LocalAdapter::move()) returns a path with leading slash.
 124                  $resultPath = trim(
 125                      $this->mediaApiModel->move($adapterName, $oldPath, $path, $override),
 126                      '/'
 127                  );
 128              }
 129              catch (FileNotFoundException $e)
 130              {
 131                  throw new Save(
 132                      Text::sprintf(
 133                          'WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND',
 134                          $oldPath
 135                      ),
 136                      404
 137                  );
 138              }
 139          }
 140  
 141          // If we have a (new) path but no old path, we want to create a
 142          // new file or folder.
 143          if ($path && !$oldPath)
 144          {
 145              // com_media expects separate directory and file name.
 146              // If we moved the file before, we must use the new path.
 147              $basename = basename($resultPath ?: $path);
 148              $dirname  = dirname($resultPath ?: $path);
 149  
 150              try
 151              {
 152                  // If there is content, com_media's assumes the new item is a file.
 153                  // Otherwise a folder is assumed.
 154                  $name = $content
 155                      ? $this->mediaApiModel->createFile(
 156                          $adapterName,
 157                          $basename,
 158                          $dirname,
 159                          $content,
 160                          $override
 161                      )
 162                      : $this->mediaApiModel->createFolder(
 163                          $adapterName,
 164                          $basename,
 165                          $dirname,
 166                          $override
 167                      );
 168  
 169                  $resultPath = $dirname . '/' . $name;
 170              }
 171              catch (FileNotFoundException $e)
 172              {
 173                  throw new Save(
 174                      Text::sprintf(
 175                          'WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND',
 176                          $dirname . '/' . $basename
 177                      ),
 178                      404
 179                  );
 180              }
 181              catch (FileExistsException $e)
 182              {
 183                  throw new Save(
 184                      Text::sprintf(
 185                          'WEBSERVICE_COM_MEDIA_FILE_EXISTS',
 186                          $dirname . '/' . $basename
 187                      ),
 188                      400
 189                  );
 190              }
 191              catch (InvalidPathException $e)
 192              {
 193                  throw new Save(
 194                      Text::sprintf(
 195                          'WEBSERVICE_COM_MEDIA_BAD_FILE_TYPE',
 196                          $dirname . '/' . $basename
 197                      ),
 198                      400
 199                  );
 200              }
 201          }
 202  
 203          // If we have no (new) path but we do have an old path and we have content,
 204          // we want to update the contents of an existing file.
 205          if ($oldPath && $content)
 206          {
 207              // com_media expects separate directory and file name.
 208              // If we moved the file before, we must use the new path.
 209              $basename = basename($resultPath ?: $oldPath);
 210              $dirname  = dirname($resultPath ?: $oldPath);
 211  
 212              try
 213              {
 214                  $this->mediaApiModel->updateFile(
 215                      $adapterName,
 216                      $basename,
 217                      $dirname,
 218                      $content
 219                  );
 220              }
 221              catch (FileNotFoundException $e)
 222              {
 223                  throw new Save(
 224                      Text::sprintf(
 225                          'WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND',
 226                          $dirname . '/' . $basename
 227                      ),
 228                      404
 229                  );
 230              }
 231              catch (InvalidPathException $e)
 232              {
 233                  throw new Save(
 234                      Text::sprintf(
 235                          'WEBSERVICE_COM_MEDIA_BAD_FILE_TYPE',
 236                          $dirname . '/' . $basename
 237                      ),
 238                      400
 239                  );
 240              }
 241  
 242              $resultPath = $resultPath ?: $oldPath;
 243          }
 244  
 245          // If we still have no result path, something fishy is going on.
 246          if (!$resultPath)
 247          {
 248              throw new Save(
 249                  Text::_(
 250                      'WEBSERVICE_COM_MEDIA_UNSUPPORTED_PARAMETER_COMBINATION'
 251                  ),
 252                  400
 253              );
 254          }
 255  
 256          return $resultPath;
 257      }
 258  
 259      /**
 260       * Method to delete an existing file or folder.
 261       *
 262       * @return  void
 263       *
 264       * @since   4.1.0
 265       * @throws  Save
 266       */
 267  	public function delete(): void
 268      {
 269          ['adapter' => $adapterName, 'path' => $path] = $this->resolveAdapterAndPath($this->getState('path', ''));
 270  
 271          try
 272          {
 273              $this->mediaApiModel->delete($adapterName, $path);
 274          }
 275          catch (FileNotFoundException $e)
 276          {
 277              throw new Save(
 278                  Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $path),
 279                  404
 280              );
 281          }
 282      }
 283  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer