Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 82 additions & 79 deletions administrator/components/com_media/src/Model/ApiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,24 @@ public function createFolder($adapter, $name, $path, $override)
throw new FileExistsException;
}

$object = $this->triggerEvent(
$adapter,
$name,
$path,
0,
function ($object)
{
$object->name = $this->getAdapter($object->adapter)->createFolder($object->name, $object->path);
}
);
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->name = $name;
$object->path = $path;

PluginHelper::importPlugin('content');

$result = $app->triggerEvent('onContentBeforeSave', ['com_media.folder', $object, true, $object]);

if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}

$object->name = $this->getAdapter($object->adapter)->createFolder($object->name, $object->path);

$app->triggerEvent('onContentAfterSave', ['com_media.folder', $object, true, $object]);

return $object->name;
}
Expand Down Expand Up @@ -281,16 +289,29 @@ public function createFile($adapter, $name, $path, $data, $override)
throw new InvalidPathException;
}

$object = $this->triggerEvent(
$adapter,
$name,
$path,
$data,
function ($object)
{
$object->name = $this->getAdapter($object->adapter)->createFile($object->name, $object->path, $object->data);
}
);
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->name = $name;
$object->path = $path;
$object->data = $data;
$object->extension = strtolower(File::getExt($name));

PluginHelper::importPlugin('content');

// Also include the filesystem plugins, perhaps they support batch processing too
PluginHelper::importPlugin('media-action');

$result = $app->triggerEvent('onContentBeforeSave', ['com_media.file', $object, true, $object]);

if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}

$object->name = $this->getAdapter($object->adapter)->createFile($object->name, $object->path, $object->data);

$app->triggerEvent('onContentAfterSave', ['com_media.file', $object, true, $object]);

return $object->name;
}
Expand Down Expand Up @@ -318,16 +339,29 @@ public function updateFile($adapter, $name, $path, $data)
throw new InvalidPathException;
}

$this->triggerEvent(
$adapter,
$name,
$path,
$data,
function ($object)
{
$this->getAdapter($object->adapter)->updateFile($object->name, $object->path, $object->data);
}
);
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->name = $name;
$object->path = $path;
$object->data = $data;
$object->extension = strtolower(File::getExt($name));

PluginHelper::importPlugin('content');

// Also include the filesystem plugins, perhaps they support batch processing too
PluginHelper::importPlugin('media-action');

$result = $app->triggerEvent('onContentBeforeSave', ['com_media.file', $object, false, $object]);

if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}

$this->getAdapter($object->adapter)->updateFile($object->name, $object->path, $object->data);

$app->triggerEvent('onContentAfterSave', ['com_media.file', $object, false, $object]);
}

/**
Expand All @@ -353,7 +387,24 @@ public function delete($adapter, $path)
throw new InvalidPathException;
}

$this->getAdapter($adapter)->delete($path);
$type = $file->type === 'file' ? 'file' : 'folder';
$app = Factory::getApplication();
$object = new CMSObject;
$object->adapter = $adapter;
$object->path = $path;

PluginHelper::importPlugin('content');

$result = $app->triggerEvent('onContentBeforeDelete', ['com_media.' . $type, $object]);

if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}

$this->getAdapter($object->adapter)->delete($object->path);

$app->triggerEvent('onContentAfterDelete', ['com_media.' . $type, $object]);
}

/**
Expand Down Expand Up @@ -494,52 +545,4 @@ private function isMediaFile($path)
// Check if the extension exists in the allowed extensions
return in_array($extension, $this->allowedExtensions);
}

/**
* Triggers the onContentBeforeSave and onContentAfterSave event when calling the
* given callable.
*
* If the onContentBeforeSave contains false, the operation will be aborted and an exception thrown.
*
* The object will be returned which got sent as part of the event.
*
* @param string $adapter The adapter
* @param string $name The name
* @param string $path The path
* @param binary $data The binary data
* @param callable $callback The callback
*
* @return CMSObject
*
* @throws \Exception
* @since 4.0.0
*/
private function triggerEvent(string $adapter, string $name, string $path, $data, callable $callback)
{
$app = Factory::getApplication();

$object = new CMSObject;
$object->adapter = $adapter;
$object->name = $name;
$object->path = $path;
$object->data = $data;
$object->extension = strtolower(File::getExt($name));
$object->type = $object->extension ? 'file' : 'dir';

// Also include the filesystem plugins, perhaps they support batch processing too
PluginHelper::importPlugin('media-action');

$result = $app->triggerEvent('onContentBeforeSave', ['com_media.' . $object->type, $object, true]);

if (in_array(false, $result, true))
{
throw new \Exception($object->getError());
}

$callback($object);

$app->triggerEvent('onContentAfterSave', ['com_media.' . $object->type, $object, true]);

return $object;
}
}