-
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
26 changed files
with
1,300 additions
and
109 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,92 @@ | ||
<?php | ||
|
||
namespace OCA\Forms\BackgroundJob; | ||
|
||
use OCA\Forms\Constants; | ||
use OCA\Forms\Db\FormMapper; | ||
use OCA\Forms\Db\UploadedFileMapper; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\TimedJob; | ||
use OCP\Files\Folder; | ||
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; | ||
$usersToCleanup = []; | ||
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()); | ||
$usersToCleanup[$form->getOwnerId()] = true; | ||
$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]); | ||
|
||
// now delete empty folders in user folders | ||
$deleted = 0; | ||
foreach (array_keys($usersToCleanup) as $userId) { | ||
$this->logger->info('Cleaning up empty folders for user {userId}.', ['userId' => $userId]); | ||
$userFolder = $this->storage->getUserFolder($userId); | ||
|
||
$unsubmittedFilesFolder = $userFolder->get(Constants::UNSUBMITTED_FILES_FOLDER); | ||
if (!$unsubmittedFilesFolder instanceof Folder) { | ||
continue; | ||
} | ||
|
||
foreach ($unsubmittedFilesFolder->getDirectoryListing() as $node) { | ||
if ($node->getName() < $dateTime->getTimestamp()) { | ||
$node->delete(); | ||
$deleted++; | ||
} | ||
} | ||
} | ||
|
||
$this->logger->info('Deleted {deleted} folders.', ['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.