first commit
This commit is contained in:
22
plugins/task/checkfiles/checkfiles.xml
Normal file
22
plugins/task/checkfiles/checkfiles.xml
Normal file
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="task" method="upgrade">
|
||||
<name>plg_task_check_files</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2021-08</creationDate>
|
||||
<copyright>(C) 2021 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<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>
|
||||
<folder plugin="checkfiles">services</folder>
|
||||
<folder>src</folder>
|
||||
<folder>forms</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_task_checkfiles.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_task_checkfiles.sys.ini</language>
|
||||
</languages>
|
||||
</extension>
|
||||
50
plugins/task/checkfiles/forms/image_size.xml
Normal file
50
plugins/task/checkfiles/forms/image_size.xml
Normal file
@ -0,0 +1,50 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form>
|
||||
<fields name="params">
|
||||
<fieldset name="task_params">
|
||||
<field
|
||||
name="path"
|
||||
type="folderlist"
|
||||
label="PLG_TASK_CHECK_FILES_LABEL_DIRECTORY"
|
||||
directory="images"
|
||||
hide_default="true"
|
||||
hide_none="true"
|
||||
required="true"
|
||||
validate="options"
|
||||
>
|
||||
<option value="">JOPTION_DO_NOT_USE</option>
|
||||
</field>
|
||||
<field
|
||||
name="dimension"
|
||||
type="list"
|
||||
label="PLG_TASK_CHECK_FILES_LABEL_IMAGE_DIMENSION"
|
||||
required="true"
|
||||
default="width"
|
||||
>
|
||||
<option value="width">JFIELD_MEDIA_WIDTH_LABEL</option>
|
||||
<option value="height">JFIELD_MEDIA_HEIGHT_LABEL</option>
|
||||
</field>
|
||||
<field
|
||||
name="limit"
|
||||
type="number"
|
||||
label="PLG_TASK_CHECK_FILES_LABEL_DIMENSION_LIMIT"
|
||||
required="true"
|
||||
default="1080"
|
||||
min="1"
|
||||
step="1"
|
||||
filter="int"
|
||||
/>
|
||||
<field
|
||||
name="numImages"
|
||||
type="number"
|
||||
label="PLG_TASK_CHECK_FILES_LABEL_MAXIMAGES"
|
||||
description="PLG_TASK_CHECK_FILES_LABEL_MAXIMAGES_DESC"
|
||||
required="true"
|
||||
min="1"
|
||||
step="1"
|
||||
default="1"
|
||||
filter="int"
|
||||
/>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</form>
|
||||
47
plugins/task/checkfiles/services/provider.php
Normal file
47
plugins/task/checkfiles/services/provider.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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 4.2.0
|
||||
*/
|
||||
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;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
168
plugins/task/checkfiles/src/Extension/Checkfiles.php
Normal file
168
plugins/task/checkfiles/src/Extension/Checkfiles.php
Normal file
@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugins
|
||||
* @subpackage Task.CheckFiles
|
||||
*
|
||||
* @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\Plugin\Task\Checkfiles\Extension;
|
||||
|
||||
use Joomla\CMS\Image\Image;
|
||||
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;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
final class Checkfiles extends CMSPlugin implements SubscriberInterface
|
||||
{
|
||||
use TaskPluginTrait;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*
|
||||
* @since 4.1.0
|
||||
*/
|
||||
protected const TASKS_MAP = [
|
||||
'checkfiles.imagesize' => [
|
||||
'langConstPrefix' => 'PLG_TASK_CHECK_FILES_TASK_IMAGE_SIZE',
|
||||
'form' => 'image_size',
|
||||
'method' => 'checkImages',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return string[]
|
||||
*
|
||||
* @since 4.1.0
|
||||
*/
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
'onTaskOptionsList' => 'advertiseRoutines',
|
||||
'onExecuteTask' => 'standardRoutineHandler',
|
||||
'onContentPrepareForm' => 'enhanceTaskItemForm',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
* @since 4.1.0
|
||||
*/
|
||||
protected $autoloadLanguage = true;
|
||||
|
||||
/**
|
||||
* The root directory path
|
||||
*
|
||||
* @var string
|
||||
* @since 4.2.0
|
||||
*/
|
||||
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 4.2.0
|
||||
*/
|
||||
public function __construct(DispatcherInterface $dispatcher, array $config, string $rootDirectory)
|
||||
{
|
||||
parent::__construct($dispatcher, $config);
|
||||
|
||||
$this->rootDirectory = $rootDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExecuteTaskEvent $event The onExecuteTask event
|
||||
*
|
||||
* @return integer The exit code
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @throws \RuntimeException
|
||||
* @throws \LogicException
|
||||
*/
|
||||
protected function checkImages(ExecuteTaskEvent $event): int
|
||||
{
|
||||
$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 (!is_dir($path)) {
|
||||
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_IMAGE_PATH_NA'), 'warning');
|
||||
|
||||
return TaskStatus::NO_RUN;
|
||||
}
|
||||
|
||||
foreach (Folder::files($path, '^.*\.(jpg|jpeg|png|gif|webp)', 2, true) as $imageFilename) {
|
||||
$properties = Image::getImageFileProperties($imageFilename);
|
||||
$resize = $properties->$dimension > $limit;
|
||||
|
||||
if (!$resize) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$height = $properties->height;
|
||||
$width = $properties->width;
|
||||
|
||||
$newHeight = $dimension === 'height' ? $limit : $height * $limit / $width;
|
||||
$newWidth = $dimension === 'width' ? $limit : $width * $limit / $height;
|
||||
|
||||
$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($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_RESIZE_FAIL'), 'error');
|
||||
|
||||
return TaskStatus::KNOCKOUT;
|
||||
}
|
||||
|
||||
if (!$image->toFile($imageFilename, $properties->type)) {
|
||||
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_CHECK_FILES_LOG_IMAGE_SAVE_FAIL'), 'error');
|
||||
|
||||
return TaskStatus::KNOCKOUT;
|
||||
}
|
||||
|
||||
--$numImages;
|
||||
|
||||
// We do a limited number of resize per execution
|
||||
if ($numImages == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return TaskStatus::OK;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user