add event for inserting cache entries

this provides a reliable way for apps to listen to new files
without the need to of cache wrappers to hook into inserts themselves
(something which isn't 100% reliable)

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/13748/head
Robin Appelman 6 years ago
parent a1aa6ee70d
commit 748bcd407b
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
  1. 1
      lib/composer/composer/autoload_classmap.php
  2. 1
      lib/composer/composer/autoload_static.php
  3. 24
      lib/private/Files/Cache/Cache.php
  4. 4
      lib/private/Files/Cache/Storage.php
  5. 72
      lib/public/Files/Cache/CacheInsertEvent.php

@ -172,6 +172,7 @@ return array(
'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php',
'OCP\\Files' => $baseDir . '/lib/public/Files.php',
'OCP\\Files\\AlreadyExistsException' => $baseDir . '/lib/public/Files/AlreadyExistsException.php',
'OCP\\Files\\Cache\\CacheInsertEvent' => $baseDir . '/lib/public/Files/Cache/CacheInsertEvent.php',
'OCP\\Files\\Cache\\ICache' => $baseDir . '/lib/public/Files/Cache/ICache.php',
'OCP\\Files\\Cache\\ICacheEntry' => $baseDir . '/lib/public/Files/Cache/ICacheEntry.php',
'OCP\\Files\\Cache\\IPropagator' => $baseDir . '/lib/public/Files/Cache/IPropagator.php',

@ -202,6 +202,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php',
'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php',
'OCP\\Files\\AlreadyExistsException' => __DIR__ . '/../../..' . '/lib/public/Files/AlreadyExistsException.php',
'OCP\\Files\\Cache\\CacheInsertEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/CacheInsertEvent.php',
'OCP\\Files\\Cache\\ICache' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICache.php',
'OCP\\Files\\Cache\\ICacheEntry' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEntry.php',
'OCP\\Files\\Cache\\IPropagator' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IPropagator.php',

@ -40,10 +40,12 @@ namespace OC\Files\Cache;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\CacheInsertEvent;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use \OCP\Files\IMimeTypeLoader;
use OCP\Files\Search\ISearchQuery;
use OCP\Files\Storage\IStorage;
use OCP\IDBConnection;
/**
@ -71,6 +73,8 @@ class Cache implements ICache {
*/
protected $storageId;
private $storage;
/**
* @var Storage $storageCache
*/
@ -84,18 +88,17 @@ class Cache implements ICache {
*/
protected $connection;
protected $eventDispatcher;
/** @var QuerySearchHelper */
protected $querySearchHelper;
/**
* @param \OC\Files\Storage\Storage|string $storage
* @param IStorage $storage
*/
public function __construct($storage) {
if ($storage instanceof \OC\Files\Storage\Storage) {
$this->storageId = $storage->getId();
} else {
$this->storageId = $storage;
}
public function __construct(IStorage $storage) {
$this->storageId = $storage->getId();
$this->storage = $storage;
if (strlen($this->storageId) > 64) {
$this->storageId = md5($this->storageId);
}
@ -103,6 +106,7 @@ class Cache implements ICache {
$this->storageCache = new Storage($storage);
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
$this->connection = \OC::$server->getDatabaseConnection();
$this->eventDispatcher = \OC::$server->getEventDispatcher();
$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
}
@ -283,9 +287,11 @@ class Cache implements ICache {
}
if ($builder->execute()) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
$fileId = (int)$this->connection->lastInsertId('*PREFIX*filecache');
$this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $file, $fileId));
return $fileId;
}
} catch(UniqueConstraintViolationException $e) {
} catch (UniqueConstraintViolationException $e) {
// entry exists already
}

@ -28,6 +28,8 @@
namespace OC\Files\Cache;
use OCP\Files\Storage\IStorage;
/**
* Handle the mapping between the string and numeric storage ids
*
@ -61,7 +63,7 @@ class Storage {
* @throws \RuntimeException
*/
public function __construct($storage, $isAvailable = true) {
if ($storage instanceof \OC\Files\Storage\Storage) {
if ($storage instanceof IStorage) {
$this->storageId = $storage->getId();
} else {
$this->storageId = $storage;

@ -0,0 +1,72 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.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 OCP\Files\Cache;
use OCP\Files\Storage\IStorage;
use Symfony\Component\EventDispatcher\Event;
/**
* @since 16.0.0
*/
class CacheInsertEvent extends Event {
private $storage;
private $path;
private $fileId;
/**
* CacheInsertEvent constructor.
*
* @param IStorage $storage
* @param string $path
* @param int $fileId
* @since 16.0.0
*/
public function __construct(IStorage $storage, string $path, int $fileId) {
$this->storage = $storage;
$this->path = $path;
$this->fileId = $fileId;
}
/**
* @return IStorage
* @since 16.0.0
*/
public function getStorage(): IStorage {
return $this->storage;
}
/**
* @return string
* @since 16.0.0
*/
public function getPath(): string {
return $this->path;
}
/**
* @return int
* @since 16.0.0
*/
public function getFileId(): int {
return $this->fileId;
}
}
Loading…
Cancel
Save