diff --git a/bundles/CoreBundle/DependencyInjection/Configuration.php b/bundles/CoreBundle/DependencyInjection/Configuration.php index bdef82b81a4..9b94e51c435 100644 --- a/bundles/CoreBundle/DependencyInjection/Configuration.php +++ b/bundles/CoreBundle/DependencyInjection/Configuration.php @@ -209,20 +209,36 @@ private function addMaintenanceNode(ArrayNodeDefinition $rootNode) { $rootNode ->children() - ->arrayNode('maintenance') - ->addDefaultsIfNotSet() - ->children() - ->arrayNode('housekeeping') + ->arrayNode('maintenance') ->addDefaultsIfNotSet() - ->children() - ->integerNode('cleanup_tmp_files_atime_older_than') - ->info('Integer value in seconds.') - ->defaultValue(7_776_000) // 90 days - ->end() - ->integerNode('cleanup_profiler_files_atime_older_than') - ->info('Integer value in seconds.') - ->defaultValue(1800) + ->children() + ->arrayNode('housekeeping') + ->addDefaultsIfNotSet() + ->children() + ->integerNode('cleanup_profiler_files_atime_older_than') + ->info('Integer value in seconds.') + ->defaultValue(1800) + ->end() + ->end() + ->end() + ->arrayNode('cleanup_directory') + ->addDefaultsIfNotSet() + ->children() + ->integerNode('cleanup_files_atime_older_than') + ->info('Integer value in seconds.') + ->defaultValue(7_776_000) // 90 days + ->end() + ->arrayNode('directories') + ->info('Directories to cleanup.') + ->scalarPrototype()->end() + ->defaultValue([PIMCORE_PRIVATE_VAR. '/tmp', PIMCORE_WEB_ROOT . '/var/tmp']) + ->end() + ->end() + ->end() + ->end() ->end() + ->end() + ->end() ; } diff --git a/bundles/CoreBundle/DependencyInjection/PimcoreCoreExtension.php b/bundles/CoreBundle/DependencyInjection/PimcoreCoreExtension.php index 606e62bc747..95de39a6c90 100644 --- a/bundles/CoreBundle/DependencyInjection/PimcoreCoreExtension.php +++ b/bundles/CoreBundle/DependencyInjection/PimcoreCoreExtension.php @@ -88,9 +88,11 @@ public function loadInternal(array $config, ContainerBuilder $container) // @deprecated since Pimcore 10.1, parameter will be removed in Pimcore 11 $container->setParameter('pimcore.response_exception_listener.render_error_document', $config['error_handling']['render_error_document']); - $container->setParameter('pimcore.maintenance.housekeeping.cleanup_tmp_files_atime_older_than', $config['maintenance']['housekeeping']['cleanup_tmp_files_atime_older_than']); $container->setParameter('pimcore.maintenance.housekeeping.cleanup_profiler_files_atime_older_than', $config['maintenance']['housekeeping']['cleanup_profiler_files_atime_older_than']); + $container->setParameter('pimcore.maintenance.cleanup_directory.cleanup_files_atime_older_than', $config['maintenance']['cleanup_directory']['cleanup_files_atime_older_than']); + $container->setParameter('pimcore.maintenance.cleanup_directory.directories', $config['maintenance']['cleanup_directory']['directories']); + $container->setParameter('pimcore.documents.default_controller', $config['documents']['default_controller']); $container->setParameter('pimcore.documents.web_to_print.default_controller_print_page', $config['documents']['web_to_print']['default_controller_print_page']); $container->setParameter('pimcore.documents.web_to_print.default_controller_print_container', $config['documents']['web_to_print']['default_controller_print_container']); diff --git a/bundles/CoreBundle/Resources/config/maintenance.yaml b/bundles/CoreBundle/Resources/config/maintenance.yaml index c5049d342da..d7f7b23f054 100644 --- a/bundles/CoreBundle/Resources/config/maintenance.yaml +++ b/bundles/CoreBundle/Resources/config/maintenance.yaml @@ -90,11 +90,17 @@ services: Pimcore\Maintenance\Tasks\HousekeepingTask: arguments: - - '%pimcore.maintenance.housekeeping.cleanup_tmp_files_atime_older_than%' - '%pimcore.maintenance.housekeeping.cleanup_profiler_files_atime_older_than%' tags: - { name: pimcore.maintenance.task, type: housekeeping } + Pimcore\Maintenance\Tasks\CleanupDirectoryTask: + arguments: + - '%pimcore.maintenance.cleanup_directory.cleanup_files_atime_older_than%' + - '%pimcore.maintenance.cleanup_directory.directories%' + tags: + - { name: pimcore.maintenance.task, type: cleanupDirectory } + Pimcore\Maintenance\Tasks\LowQualityImagePreviewTask: arguments: - '@logger' diff --git a/lib/Maintenance/Tasks/CleanupDirectoryTask.php b/lib/Maintenance/Tasks/CleanupDirectoryTask.php new file mode 100644 index 00000000000..9fc907b807d --- /dev/null +++ b/lib/Maintenance/Tasks/CleanupDirectoryTask.php @@ -0,0 +1,65 @@ +cleanupDirectories as $directory) { + $this->deleteFilesInFolderOlderThanSeconds($directory, $this->tmpFileTime); + } + } + + /** + * @param string $folder + * @param int $seconds + */ + private function deleteFilesInFolderOlderThanSeconds(string $folder, int $seconds): void + { + if (!is_dir($folder)) { + return; + } + + $directory = new \RecursiveDirectoryIterator($folder); + $filter = new \RecursiveCallbackFilterIterator($directory, function (\SplFileInfo $current, $key, $iterator) use ($seconds) { + if (strpos($current->getFilename(), '-low-quality-preview.svg')) { + // do not delete low quality image previews + return false; + } + + if ($current->isFile()) { + if ($current->getATime() && $current->getATime() < (time() - $seconds)) { + return true; + } + } else { + return true; + } + + return false; + }); + + $iterator = new \RecursiveIteratorIterator($filter); + + foreach ($iterator as $file) { + /** + * @var \SplFileInfo $file + */ + if ($file->isFile()) { + @unlink($file->getPathname()); + } + if (is_dir_empty($file->getPath()) && $file->getPath() !== $directory->getPath()) { + @rmdir($file->getPath()); + } + } + } +} diff --git a/lib/Maintenance/Tasks/HousekeepingTask.php b/lib/Maintenance/Tasks/HousekeepingTask.php index 5122871ae06..3331d0745b6 100644 --- a/lib/Maintenance/Tasks/HousekeepingTask.php +++ b/lib/Maintenance/Tasks/HousekeepingTask.php @@ -33,12 +33,10 @@ class HousekeepingTask implements TaskInterface protected $profilerTime; /** - * @param int $tmpFileTime * @param int $profilerTime */ - public function __construct(int $tmpFileTime, int $profilerTime) + public function __construct(int $profilerTime) { - $this->tmpFileTime = $tmpFileTime; $this->profilerTime = $profilerTime; }