-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Konstantin Myakshin <[email protected]>
- Loading branch information
Showing
19 changed files
with
1,080 additions
and
91 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
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
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,66 @@ | ||
<?php | ||
|
||
namespace OCA\Forms\BackgroundJob; | ||
|
||
use OCA\Forms\Db\FormMapper; | ||
use OCA\Forms\Db\UploadedFileMapper; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\TimedJob; | ||
use OCP\Files\IRootFolder; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class CleanupUploadedFilesJob extends TimedJob { | ||
private const FILE_LIFETIME = '-2 days'; | ||
|
||
public function __construct( | ||
private IRootFolder $storage, | ||
private FormMapper $formMapper, | ||
private UploadedFileMapper $uploadedFileMapper, | ||
private LoggerInterface $logger, | ||
ITimeFactory $time) { | ||
parent::__construct($time); | ||
|
||
$this->setInterval(60 * 60 * 24); | ||
|
||
} | ||
|
||
/** | ||
* @param array $argument | ||
*/ | ||
public function run($argument): void { | ||
$dateTime = new \DateTimeImmutable(self::FILE_LIFETIME); | ||
$this->logger->info('Deleting files that were uploaded before {before} and still not submitted.', [ | ||
'before' => $dateTime->format(\DateTimeImmutable::ATOM), | ||
]); | ||
|
||
$uploadedFiles = $this->uploadedFileMapper->findUploadedEarlierThan($dateTime); | ||
|
||
$deleted = 0; | ||
foreach ($uploadedFiles as $uploadedFile) { | ||
$this->logger->info('Deleting uploaded file "{originalFileName}" for form {formId}.', [ | ||
'originalFileName' => $uploadedFile->getOriginalFileName(), | ||
'formId' => $uploadedFile->getFormId(), | ||
]); | ||
|
||
$form = $this->formMapper->findById($uploadedFile->getFormId()); | ||
$userFolder = $this->storage->getUserFolder($form->getOwnerId()); | ||
|
||
$nodes = $userFolder->getById($uploadedFile->getFileId()); | ||
|
||
if (!empty($nodes)) { | ||
$node = $nodes[0]; | ||
$node->delete(); | ||
} else { | ||
$this->logger->warning('Could not find uploaded file "{fileId}" for deletion.', [ | ||
'fileId' => $uploadedFile->getFileId(), | ||
]); | ||
} | ||
|
||
$this->uploadedFileMapper->delete($uploadedFile); | ||
|
||
$deleted++; | ||
} | ||
|
||
$this->logger->info('Deleted {deleted} uploaded files.', ['deleted' => $deleted]); | ||
} | ||
} |
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
Oops, something went wrong.