Skip to content

Commit

Permalink
Mail: Cleanup old files from attachment stage (for anonymous user)
Browse files Browse the repository at this point in the history
(cherry picked from commit 04f9348)
  • Loading branch information
mjansenDatabay committed Aug 9, 2024
1 parent 0b915bf commit 3100af0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
10 changes: 10 additions & 0 deletions components/ILIAS/Mail/classes/class.ilMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,16 @@ public function sendMail(
$this->deleteMails([$internalMessageId]);
}

if ($this->isSystemMail()) {
$random = new ilRandom();
if ($random->int(0, 50) === 2) {
(new ilMailAttachmentStageCleanup(
$this->logger,
$this->mfile
))->run();
}
}

return array_values($errors);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

final class ilMailAttachmentStageCleanup
{
private const OLD_FILE_MTIME_EXPRESSION = '1 day ago';

private ilLogger $logger;
private ilFileDataMail $mail_file_manager;

public function __construct(ilLogger $logger, ilFileDataMail $mail_file_manager)
{
$this->logger = $logger;
$this->mail_file_manager = $mail_file_manager;
}

public function run(): void
{
$right_interval = (new DateTimeImmutable(self::OLD_FILE_MTIME_EXPRESSION))->format('U');

$iter = new CallbackFilterIterator(
new RegexIterator(
new DirectoryIterator($this->mail_file_manager->getMailPath()),
'/^' . $this->mail_file_manager->user_id . '_/'
),
function (SplFileInfo $file) use ($right_interval): bool {
if (!$file->isFile()) {
return false;
}

return (int) $file->getMTime() < (int) $right_interval;
}
);

$filesystem = \ILIAS\Filesystem\Util\LegacyPathHelper::deriveFilesystemFrom(
$this->mail_file_manager->getMailPath()
);

foreach ($iter as $file) {
/** @var SplFileInfo $file */
if (strpos($file->getFilename(), $this->mail_file_manager->user_id . '_') === 0) {
try {
$relative_path = 'mail/' . $file->getFilename();
if ($filesystem->has($relative_path)) {
$filesystem->delete($relative_path);
$this->logger->info('Deleting file from attachment stage: ' . $file->getPath());
}
} catch (Exception $e) {
$this->logger->error('Error deleting file from attachment stage: ' . $file->getPath());
}
}
}
}
}

0 comments on commit 3100af0

Please sign in to comment.