Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ffc9a67
redo
laoneo Jun 28, 2022
134e9cd
Merge branch '4.2-dev' into j4/plugins/checkfiles
laoneo Jun 28, 2022
06372ed
Update plugins/task/checkfiles/checkfiles.xml
laoneo Jun 29, 2022
f461767
Update plugins/task/checkfiles/src/Extension/Checkfiles.php
laoneo Jun 29, 2022
c7b6375
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 29, 2022
5ea0370
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 29, 2022
7c06c04
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 29, 2022
a5bf970
Merge branch '4.2-dev' into j4/plugins/checkfiles
laoneo Jun 30, 2022
c1c9b84
Merge branch '4.2-dev' into j4/plugins/checkfiles
laoneo Jun 30, 2022
230c3bb
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
6200d00
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
2ba1beb
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
2c8ca31
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
399e140
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
7d1a534
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
f942d7f
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
7dab748
Update tests/Unit/Plugin/Task/Checkfiles/Extension/CheckfilesPluginTe…
laoneo Jun 30, 2022
c0bc995
Merge branch '4.2-dev' into j4/plugins/checkfiles
laoneo Jun 30, 2022
4c08de9
Merge branch '4.2-dev' into j4/plugins/checkfiles
laoneo Jul 1, 2022
3479ac6
Merge branch '4.2-dev' into j4/plugins/checkfiles
roland-d Jul 3, 2022
9acc2a8
Merge branch '4.2-dev' into j4/plugins/checkfiles
laoneo Jul 3, 2022
81c0c3b
translate
laoneo Jul 3, 2022
2feadf2
cs
laoneo Jul 3, 2022
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
5 changes: 3 additions & 2 deletions plugins/task/checkfiles/checkfiles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.1</version>
<description>PLG_TASK_CHECK_FILES_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Task\Checkfiles</namespace>
<files>
<filename plugin="checkfiles">checkfiles.php</filename>
<folder>language</folder>
<folder plugin="checkfiles">services</folder>
<folder>src</folder>
<folder>forms</folder>
</files>
<languages>
Expand Down
48 changes: 48 additions & 0 deletions plugins/task/checkfiles/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Task\Checkfiles\Extension\Checkfiles;

return new class implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container) {
$plugin = new Checkfiles(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('task', 'checkfiles'),
JPATH_ROOT . '/images/'
);
$plugin->setApplication(Factory::getApplication());

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@
*
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt

* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

use Joomla\CMS\Filesystem\Folder;
namespace Joomla\Plugin\Task\Checkfiles\Extension;

use Joomla\CMS\Image\Image;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status as TaskStatus;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Event\DispatcherInterface;
use Joomla\Event\SubscriberInterface;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\Path;
use LogicException;

/**
* Task plugin with routines that offer checks on files.
* At the moment, offers a single routine to check and resize image files in a directory.
*
* @since 4.1.0
*/
class PlgTaskCheckfiles extends CMSPlugin implements SubscriberInterface
final class Checkfiles extends CMSPlugin implements SubscriberInterface
{
use TaskPluginTrait;

Expand All @@ -43,12 +44,6 @@ class PlgTaskCheckfiles extends CMSPlugin implements SubscriberInterface
],
];

/**
* @var boolean
* @since 4.1.0
*/
protected $autoloadLanguage = true;

/**
* @inheritDoc
*
Expand All @@ -65,6 +60,36 @@ public static function getSubscribedEvents(): array
];
}

/**
* @var boolean
* @since 4.1.0
*/
protected $autoloadLanguage = true;

/**
* The root directory path
*
* @var string
* @since __DEPLOY_VERSION__
*/
private $rootDirectory;

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param string $rootDirectory The root directory to look for images
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DispatcherInterface $dispatcher, array $config, string $rootDirectory)
{
parent::__construct($dispatcher, $config);

$this->rootDirectory = $rootDirectory;
}

/**
* @param ExecuteTaskEvent $event The onExecuteTask event
*
Expand All @@ -76,22 +101,19 @@ public static function getSubscribedEvents(): array
*/
protected function checkImages(ExecuteTaskEvent $event): int
{
$params = $event->getArgument('params');

$path = Path::check(JPATH_ROOT . '/images/' . $params->path);
$params = $event->getArgument('params');
$path = Path::check($this->rootDirectory . $params->path);
$dimension = $params->dimension;
$limit = $params->limit;
$numImages = max(1, (int) $params->numImages ?? 1);

if (!Folder::exists($path)) {
$this->logTask(Text::_('PLG_TASK_CHECK_FILES_LOG_IMAGE_PATH_NA'), 'warning');
if (!is_dir($path)) {
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_IMAGE_PATH_NA'), 'warning');

return TaskStatus::NO_RUN;
}

$images = Folder::files($path, '^.*\.(jpg|jpeg|png|gif|webp)', 2, true);

foreach ($images as $imageFilename) {
foreach (Folder::files($path, '^.*\.(jpg|jpeg|png|gif|webp)', 2, true) as $imageFilename) {
$properties = Image::getImageFileProperties($imageFilename);
$resize = $properties->$dimension > $limit;

Expand All @@ -105,23 +127,29 @@ protected function checkImages(ExecuteTaskEvent $event): int
$newHeight = $dimension === 'height' ? $limit : $height * $limit / $width;
$newWidth = $dimension === 'width' ? $limit : $width * $limit / $height;

$this->logTask(Text::sprintf('PLG_TASK_CHECK_FILES_LOG_RESIZING_IMAGE', $width, $height, $newWidth, $newHeight, $imageFilename));
$this->logTask(sprintf(
$this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_RESIZING_IMAGE'),
$width,
$height,
$newWidth,
$newHeight,
$imageFilename
));

$image = new Image($imageFilename);

try {
$image->resize($newWidth, $newHeight, false);
} catch (LogicException $e) {
$this->logTask('PLG_TASK_CHECK_FILES_LOG_RESIZE_FAIL', 'error');
$resizeFail = true;
}
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_RESIZE_FAIL'), 'error');

if (!empty($resizeFail)) {
return TaskStatus::KNOCKOUT;
}

if (!$image->toFile($imageFilename, $properties->type)) {
$this->logTask('PLG_TASK_CHECK_FILES_LOG_IMAGE_SAVE_FAIL', 'error');
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_IMAGE_SAVE_FAIL'), 'error');

return TaskStatus::KNOCKOUT;
}

--$numImages;
Expand Down
Loading