123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 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 OC\Preview;
- use OC\BackgroundJob\TimedJob;
- use OC\Preview\Storage\Root;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\Files\IMimeTypeLoader;
- use OCP\Files\NotFoundException;
- use OCP\Files\NotPermittedException;
- use OCP\IDBConnection;
- class BackgroundCleanupJob extends TimedJob {
- /** @var IDBConnection */
- private $connection;
- /** @var Root */
- private $previewFolder;
- /** @var bool */
- private $isCLI;
- /** @var IMimeTypeLoader */
- private $mimeTypeLoader;
- public function __construct(IDBConnection $connection,
- Root $previewFolder,
- IMimeTypeLoader $mimeTypeLoader,
- bool $isCLI) {
- // Run at most once an hour
- $this->setInterval(3600);
- $this->connection = $connection;
- $this->previewFolder = $previewFolder;
- $this->isCLI = $isCLI;
- $this->mimeTypeLoader = $mimeTypeLoader;
- }
- public function run($argument) {
- foreach ($this->getDeletedFiles() as $fileId) {
- try {
- $preview = $this->previewFolder->getFolder((string)$fileId);
- $preview->delete();
- } catch (NotFoundException $e) {
- // continue
- } catch (NotPermittedException $e) {
- // continue
- }
- }
- }
- private function getDeletedFiles(): \Iterator {
- yield from $this->getOldPreviewLocations();
- yield from $this->getNewPreviewLocations();
- }
- private function getOldPreviewLocations(): \Iterator {
- $qb = $this->connection->getQueryBuilder();
- $qb->select('a.name')
- ->from('filecache', 'a')
- ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
- $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
- ))
- ->where(
- $qb->expr()->isNull('b.fileid')
- )->andWhere(
- $qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId()))
- )->andWhere(
- $qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
- );
- if (!$this->isCLI) {
- $qb->setMaxResults(10);
- }
- $cursor = $qb->execute();
- while ($row = $cursor->fetch()) {
- yield $row['name'];
- }
- $cursor->closeCursor();
- }
- private function getNewPreviewLocations(): \Iterator {
- $qb = $this->connection->getQueryBuilder();
- $qb->select('path', 'mimetype')
- ->from('filecache')
- ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($this->previewFolder->getId())));
- $cursor = $qb->execute();
- $data = $cursor->fetch();
- $cursor->closeCursor();
- if ($data === null) {
- return [];
- }
- /*
- * This lovely like is the result of the way the new previews are stored
- * We take the md5 of the name (fileid) and split the first 7 chars. That way
- * there are not a gazillion files in the root of the preview appdata.
- */
- $like = $this->connection->escapeLikeParameter($data['path']) . '/_/_/_/_/_/_/_/%';
- $qb = $this->connection->getQueryBuilder();
- $qb->select('a.name')
- ->from('filecache', 'a')
- ->leftJoin('a', 'filecache', 'b', $qb->expr()->eq(
- $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
- ))
- ->where(
- $qb->expr()->andX(
- $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
- $qb->expr()->isNull('b.fileid'),
- $qb->expr()->like('a.path', $qb->createNamedParameter($like)),
- $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
- )
- );
- if (!$this->isCLI) {
- $qb->setMaxResults(10);
- }
- $cursor = $qb->execute();
- while ($row = $cursor->fetch()) {
- yield $row['name'];
- }
- $cursor->closeCursor();
- }
- }
|