diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2018-04-23 22:32:41 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2018-04-26 10:35:37 +0200 |
commit | b6c58e75b754fc7a5f6873b51934be16a8365d8f (patch) | |
tree | 088d56a227ea0ec9990e4a8884a54008b378c734 /apps/dav/lib | |
parent | 042340ccf6e7d6408390b91f6904de0425bb3c07 (diff) | |
download | nextcloud-server-b6c58e75b754fc7a5f6873b51934be16a8365d8f.tar.gz nextcloud-server-b6c58e75b754fc7a5f6873b51934be16a8365d8f.zip |
Add backgroundjob to cleanup expired direct links
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/BackgroundJob/CleanupDirectLinksJob.php | 50 | ||||
-rw-r--r-- | apps/dav/lib/Db/DirectMapper.php | 18 |
2 files changed, 67 insertions, 1 deletions
diff --git a/apps/dav/lib/BackgroundJob/CleanupDirectLinksJob.php b/apps/dav/lib/BackgroundJob/CleanupDirectLinksJob.php new file mode 100644 index 00000000000..ba1049071ad --- /dev/null +++ b/apps/dav/lib/BackgroundJob/CleanupDirectLinksJob.php @@ -0,0 +1,50 @@ +<?php +declare(strict_types=1); +/** + * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\DAV\BackgroundJob; + +use OC\BackgroundJob\TimedJob; +use OCA\DAV\Db\DirectMapper; +use OCP\AppFramework\Utility\ITimeFactory; + +class CleanupDirectLinksJob extends TimedJob { + /** @var ITimeFactory */ + private $timeFactory; + + /** @var DirectMapper */ + private $mapper; + + public function __construct(ITimeFactory $timeFactory, DirectMapper $mapper) { + $this->setInterval(60*60*24); + + $this->timeFactory = $timeFactory; + $this->mapper = $mapper; + } + + protected function run($argument) { + // Delete all shares expired 24 hours ago + $this->mapper->deleteExpired($this->timeFactory->getTime() - 60*60*24); + } + +} diff --git a/apps/dav/lib/Db/DirectMapper.php b/apps/dav/lib/Db/DirectMapper.php index 806b145823e..d0db4b82879 100644 --- a/apps/dav/lib/Db/DirectMapper.php +++ b/apps/dav/lib/Db/DirectMapper.php @@ -34,6 +34,11 @@ class DirectMapper extends Mapper { parent::__construct($db, 'directlink', Direct::class); } + /** + * @param string $token + * @return Direct + * @throws DoesNotExistException + */ public function getByToken(string $token): Direct { $qb = $this->db->getQueryBuilder(); @@ -48,9 +53,20 @@ class DirectMapper extends Mapper { $cursor->closeCursor(); if ($data === false) { - throw new DoesNotExistException(); + throw new DoesNotExistException('Direct link with token does not exist'); } return Direct::fromRow($data); } + + public function deleteExpired(int $expiration) { + $qb = $this->db->getQueryBuilder(); + + $qb->delete('directlink') + ->where( + $qb->expr()->lt('expiration', $qb->createNamedParameter($expiration)) + ); + + $qb->execute(); + } } |