-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mail: Cleanup old files from attachment stage (for anonymous user)
(cherry picked from commit 04f9348)
- Loading branch information
1 parent
0b915bf
commit 3100af0
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
components/ILIAS/Mail/classes/class.ilMailAttachmentStageCleanup.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} | ||
} |