- md5
- 25bef8f370101968e844ff6a49aa54c3
- sha1
- aa72f8958347107758b3fdc485be787cc80865d7
- sha256
- 33dc4c0a23d0389a9bd3caeab0c9d898ef45a2f2414ba0599941ff7a00834189
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
<?php
/**
* @package Joomla.API
* @subpackage com_media
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Media\Api\Model;
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;
use Joomla\Component\Media\Administrator\Exception\FileNotFoundException;
use Joomla\Component\Media\Administrator\Exception\InvalidPathException;
use Joomla\Component\Media\Administrator\Model\ApiModel;
use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* Media web service model supporting a single media item.
*
* @since 4.1.0
*/
class MediumModel extends BaseModel
{
use ProviderManagerHelperTrait;
/**
* Instance of com_media's ApiModel
*
* @var ApiModel
* @since 4.1.0
*/
private $mediaApiModel;
public function __construct($config = [])
{
parent::__construct($config);
$this->mediaApiModel = new ApiModel();
}
/**
* Method to get a single files or folder.
*
* @return \stdClass A file or folder object.
*
* @since 4.1.0
* @throws ResourceNotFound
*/
public function getItem()
{
$options = [
'path' => $this->getState('path', ''),
'url' => $this->getState('url', false),
'temp' => $this->getState('temp', false),
'content' => $this->getState('content', false),
];
['adapter' => $adapterName, 'path' => $path] = $this->resolveAdapterAndPath($this->getState('path', ''));
try {
return $this->mediaApiModel->getFile($adapterName, $path, $options);
} catch (FileNotFoundException $e) {
throw new ResourceNotFound(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $path),
404
);
}
}
/**
* Method to save a file or folder.
*
* @param string $path The primary key of the item (if exists)
*
* @return string The path
*
* @since 4.1.0
*
* @throws Save
*/
public function save($path = null): string
{
$path = $this->getState('path', '');
$oldPath = $this->getState('old_path', '');
$content = $this->getState('content', null);
$override = $this->getState('override', false);
['adapter' => $adapterName, 'path' => $path] = $this->resolveAdapterAndPath($path);
// Trim adapter information from path
if ($pos = strpos($path, ':/')) {
$path = substr($path, $pos + 1);
}
// Trim adapter information from old path
if ($pos = strpos($oldPath, ':/')) {
$oldPath = substr($oldPath, $pos + 1);
}
$resultPath = '';
/**
* If we have a (new) path and an old path, we want to move an existing
* file or folder. This must be done before updating the content of a file,
* if also requested (see below).
*/
if ($path && $oldPath) {
try {
// ApiModel::move() (or actually LocalAdapter::move()) returns a path with leading slash.
$resultPath = trim(
$this->mediaApiModel->move($adapterName, $oldPath, $path, $override),
'/'
);
} catch (FileNotFoundException $e) {
throw new Save(
Text::sprintf(
'WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND',
$oldPath
),
404
);
}
}
// If we have a (new) path but no old path, we want to create a
// new file or folder.
if ($path && !$oldPath) {
// com_media expects separate directory and file name.
// If we moved the file before, we must use the new path.
$basename = basename($resultPath ?: $path);
$dirname = dirname($resultPath ?: $path);
try {
// 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
);
$resultPath = $dirname . '/' . $name;
} catch (FileNotFoundException $e) {
throw new Save(
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
),
400
);
} catch (InvalidPathException $e) {
throw new Save(
Text::sprintf(
'WEBSERVICE_COM_MEDIA_BAD_FILE_TYPE',
$dirname . '/' . $basename
),
400
);
}
}
// If we have no (new) path but we do have an old path and we have content,
// we want to update the contents of an existing file.
if ($oldPath && $content) {
// com_media expects separate directory and file name.
// If we moved the file before, we must use the new path.
$basename = basename($resultPath ?: $oldPath);
$dirname = dirname($resultPath ?: $oldPath);
try {
$this->mediaApiModel->updateFile(
$adapterName,
$basename,
$dirname,
$content
);
} catch (FileNotFoundException $e) {
throw new Save(
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
),
400
);
}
$resultPath = $resultPath ?: $oldPath;
}
// If we still have no result path, something fishy is going on.
if (!$resultPath) {
throw new Save(
Text::_(
'WEBSERVICE_COM_MEDIA_UNSUPPORTED_PARAMETER_COMBINATION'
),
400
);
}
return $resultPath;
}
/**
* Method to delete an existing file or folder.
*
* @return void
*
* @since 4.1.0
* @throws Save
*/
public function delete(): void
{
['adapter' => $adapterName, 'path' => $path] = $this->resolveAdapterAndPath($this->getState('path', ''));
try {
$this->mediaApiModel->delete($adapterName, $path);
} catch (FileNotFoundException $e) {
throw new Save(
Text::sprintf('WEBSERVICE_COM_MEDIA_FILE_NOT_FOUND', $path),
404
);
}
}
}
The same content is in 60 releases, Joomla 4.3.0-beta1 to 5.0.0-beta3. See them all, or every version of this file.