diff options
Diffstat (limited to 'lib')
48 files changed, 839 insertions, 163 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f49865ae6ed..439d4c819d1 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -169,6 +169,9 @@ return array( 'OCP\\Encryption\\IFile' => $baseDir . '/lib/public/Encryption/IFile.php', 'OCP\\Encryption\\IManager' => $baseDir . '/lib/public/Encryption/IManager.php', 'OCP\\Encryption\\Keys\\IStorage' => $baseDir . '/lib/public/Encryption/Keys/IStorage.php', + 'OCP\\EventDispatcher\\Event' => $baseDir . '/lib/public/EventDispatcher/Event.php', + 'OCP\\EventDispatcher\\IEventDispatcher' => $baseDir . '/lib/public/EventDispatcher/IEventDispatcher.php', + 'OCP\\EventDispatcher\\IEventListener' => $baseDir . '/lib/public/EventDispatcher/IEventListener.php', 'OCP\\Federation\\Exceptions\\ActionNotSupportedException' => $baseDir . '/lib/public/Federation/Exceptions/ActionNotSupportedException.php', 'OCP\\Federation\\Exceptions\\AuthenticationFailedException' => $baseDir . '/lib/public/Federation/Exceptions/AuthenticationFailedException.php', 'OCP\\Federation\\Exceptions\\BadRequestException' => $baseDir . '/lib/public/Federation/Exceptions/BadRequestException.php', @@ -351,6 +354,7 @@ return array( 'OCP\\OCS\\IDiscoveryService' => $baseDir . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\PreConditionNotMetException' => $baseDir . '/lib/public/PreConditionNotMetException.php', 'OCP\\Preview\\IProvider' => $baseDir . '/lib/public/Preview/IProvider.php', + 'OCP\\Preview\\IProviderV2' => $baseDir . '/lib/public/Preview/IProviderV2.php', 'OCP\\Remote\\Api\\IApiCollection' => $baseDir . '/lib/public/Remote/Api/IApiCollection.php', 'OCP\\Remote\\Api\\IApiFactory' => $baseDir . '/lib/public/Remote/Api/IApiFactory.php', 'OCP\\Remote\\Api\\ICapabilitiesApi' => $baseDir . '/lib/public/Remote/Api/ICapabilitiesApi.php', @@ -810,6 +814,9 @@ return array( 'OC\\Encryption\\Manager' => $baseDir . '/lib/private/Encryption/Manager.php', 'OC\\Encryption\\Update' => $baseDir . '/lib/private/Encryption/Update.php', 'OC\\Encryption\\Util' => $baseDir . '/lib/private/Encryption/Util.php', + 'OC\\EventDispatcher\\EventDispatcher' => $baseDir . '/lib/private/EventDispatcher/EventDispatcher.php', + 'OC\\EventDispatcher\\ServiceEventListener' => $baseDir . '/lib/private/EventDispatcher/ServiceEventListener.php', + 'OC\\EventDispatcher\\SymfonyAdapter' => $baseDir . '/lib/private/EventDispatcher/SymfonyAdapter.php', 'OC\\Federation\\CloudFederationFactory' => $baseDir . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => $baseDir . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => $baseDir . '/lib/private/Federation/CloudFederationProviderManager.php', @@ -1016,6 +1023,8 @@ return array( 'OC\\Preview\\Photoshop' => $baseDir . '/lib/private/Preview/Photoshop.php', 'OC\\Preview\\Postscript' => $baseDir . '/lib/private/Preview/Postscript.php', 'OC\\Preview\\Provider' => $baseDir . '/lib/private/Preview/Provider.php', + 'OC\\Preview\\ProviderV1Adapter' => $baseDir . '/lib/private/Preview/ProviderV1Adapter.php', + 'OC\\Preview\\ProviderV2' => $baseDir . '/lib/private/Preview/ProviderV2.php', 'OC\\Preview\\SVG' => $baseDir . '/lib/private/Preview/SVG.php', 'OC\\Preview\\StarOffice' => $baseDir . '/lib/private/Preview/StarOffice.php', 'OC\\Preview\\TIFF' => $baseDir . '/lib/private/Preview/TIFF.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 5f204e4e23e..7cfca806c06 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -203,6 +203,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Encryption\\IFile' => __DIR__ . '/../../..' . '/lib/public/Encryption/IFile.php', 'OCP\\Encryption\\IManager' => __DIR__ . '/../../..' . '/lib/public/Encryption/IManager.php', 'OCP\\Encryption\\Keys\\IStorage' => __DIR__ . '/../../..' . '/lib/public/Encryption/Keys/IStorage.php', + 'OCP\\EventDispatcher\\Event' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/Event.php', + 'OCP\\EventDispatcher\\IEventDispatcher' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventDispatcher.php', + 'OCP\\EventDispatcher\\IEventListener' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventListener.php', 'OCP\\Federation\\Exceptions\\ActionNotSupportedException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/ActionNotSupportedException.php', 'OCP\\Federation\\Exceptions\\AuthenticationFailedException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/AuthenticationFailedException.php', 'OCP\\Federation\\Exceptions\\BadRequestException' => __DIR__ . '/../../..' . '/lib/public/Federation/Exceptions/BadRequestException.php', @@ -385,6 +388,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\OCS\\IDiscoveryService' => __DIR__ . '/../../..' . '/lib/public/OCS/IDiscoveryService.php', 'OCP\\PreConditionNotMetException' => __DIR__ . '/../../..' . '/lib/public/PreConditionNotMetException.php', 'OCP\\Preview\\IProvider' => __DIR__ . '/../../..' . '/lib/public/Preview/IProvider.php', + 'OCP\\Preview\\IProviderV2' => __DIR__ . '/../../..' . '/lib/public/Preview/IProviderV2.php', 'OCP\\Remote\\Api\\IApiCollection' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiCollection.php', 'OCP\\Remote\\Api\\IApiFactory' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/IApiFactory.php', 'OCP\\Remote\\Api\\ICapabilitiesApi' => __DIR__ . '/../../..' . '/lib/public/Remote/Api/ICapabilitiesApi.php', @@ -844,6 +848,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Encryption\\Manager' => __DIR__ . '/../../..' . '/lib/private/Encryption/Manager.php', 'OC\\Encryption\\Update' => __DIR__ . '/../../..' . '/lib/private/Encryption/Update.php', 'OC\\Encryption\\Util' => __DIR__ . '/../../..' . '/lib/private/Encryption/Util.php', + 'OC\\EventDispatcher\\EventDispatcher' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/EventDispatcher.php', + 'OC\\EventDispatcher\\ServiceEventListener' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/ServiceEventListener.php', + 'OC\\EventDispatcher\\SymfonyAdapter' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/SymfonyAdapter.php', 'OC\\Federation\\CloudFederationFactory' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationNotification.php', 'OC\\Federation\\CloudFederationProviderManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationProviderManager.php', @@ -1050,6 +1057,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Preview\\Photoshop' => __DIR__ . '/../../..' . '/lib/private/Preview/Photoshop.php', 'OC\\Preview\\Postscript' => __DIR__ . '/../../..' . '/lib/private/Preview/Postscript.php', 'OC\\Preview\\Provider' => __DIR__ . '/../../..' . '/lib/private/Preview/Provider.php', + 'OC\\Preview\\ProviderV1Adapter' => __DIR__ . '/../../..' . '/lib/private/Preview/ProviderV1Adapter.php', + 'OC\\Preview\\ProviderV2' => __DIR__ . '/../../..' . '/lib/private/Preview/ProviderV2.php', 'OC\\Preview\\SVG' => __DIR__ . '/../../..' . '/lib/private/Preview/SVG.php', 'OC\\Preview\\StarOffice' => __DIR__ . '/../../..' . '/lib/private/Preview/StarOffice.php', 'OC\\Preview\\TIFF' => __DIR__ . '/../../..' . '/lib/private/Preview/TIFF.php', diff --git a/lib/private/DB/Migrator.php b/lib/private/DB/Migrator.php index 603f9259d6e..a853ab1ea1b 100644 --- a/lib/private/DB/Migrator.php +++ b/lib/private/DB/Migrator.php @@ -39,7 +39,7 @@ use Doctrine\DBAL\Types\StringType; use Doctrine\DBAL\Types\Type; use OCP\IConfig; use OCP\Security\ISecureRandom; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; class Migrator { @@ -53,7 +53,7 @@ class Migrator { /** @var IConfig */ protected $config; - /** @var EventDispatcher */ + /** @var EventDispatcherInterface */ private $dispatcher; /** @var bool */ @@ -63,12 +63,12 @@ class Migrator { * @param \Doctrine\DBAL\Connection|Connection $connection * @param ISecureRandom $random * @param IConfig $config - * @param EventDispatcher $dispatcher + * @param EventDispatcherInterface $dispatcher */ public function __construct(\Doctrine\DBAL\Connection $connection, ISecureRandom $random, IConfig $config, - EventDispatcher $dispatcher = null) { + EventDispatcherInterface $dispatcher = null) { $this->connection = $connection; $this->random = $random; $this->config = $config; diff --git a/lib/private/EventDispatcher/EventDispatcher.php b/lib/private/EventDispatcher/EventDispatcher.php new file mode 100644 index 00000000000..8db2f3101be --- /dev/null +++ b/lib/private/EventDispatcher/EventDispatcher.php @@ -0,0 +1,85 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\EventDispatcher; + +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\IContainer; +use OCP\ILogger; +use OCP\IServerContainer; +use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher; + +class EventDispatcher implements IEventDispatcher { + + /** @var SymfonyDispatcher */ + private $dispatcher; + + /** @var IContainer */ + private $container; + + /** @var ILogger */ + private $logger; + + public function __construct(SymfonyDispatcher $dispatcher, + IServerContainer $container, + ILogger $logger) { + $this->dispatcher = $dispatcher; + $this->container = $container; + $this->logger = $logger; + } + + public function addListener(string $eventName, + callable $listener, + int $priority = 0): void { + $this->dispatcher->addListener($eventName, $listener, $priority); + } + + public function addServiceListener(string $eventName, + string $className, + int $priority = 0): void { + $listener = new ServiceEventListener( + $this->container, + $className, + $this->logger + ); + + $this->addListener($eventName, $listener, $priority); + } + + public function dispatch(string $eventName, + Event $event): void { + + $this->dispatcher->dispatch($eventName, $event); + } + + /** + * @return SymfonyDispatcher + */ + public function getSymfonyDispatcher(): SymfonyDispatcher { + return $this->dispatcher; + } + +} diff --git a/lib/private/EventDispatcher/ServiceEventListener.php b/lib/private/EventDispatcher/ServiceEventListener.php new file mode 100644 index 00000000000..f5b6cd9b79d --- /dev/null +++ b/lib/private/EventDispatcher/ServiceEventListener.php @@ -0,0 +1,78 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\EventDispatcher; + +use OCP\AppFramework\QueryException; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\IContainer; +use OCP\ILogger; + +/** + * Lazy service event listener + * + * Makes it possible to lazy-route a dispatched event to a service instance + * created by the service container + */ +final class ServiceEventListener { + + /** @var IContainer */ + private $container; + + /** @var string */ + private $class; + + /** @var ILogger */ + private $logger; + + /** @var null|IEventListener */ + private $service; + + public function __construct(IContainer $container, + string $class, + ILogger $logger) { + $this->container = $container; + $this->class = $class; + $this->logger = $logger; + } + + public function __invoke(Event $event) { + if ($this->service === null) { + try { + $this->service = $this->container->query($this->class); + } catch (QueryException $e) { + $this->logger->logException($e, [ + 'level' => ILogger::ERROR, + 'message' => "Could not load event listener service " . $this->class, + ]); + return; + } + } + + $this->service->handle($event); + } + +} diff --git a/lib/private/EventDispatcher/SymfonyAdapter.php b/lib/private/EventDispatcher/SymfonyAdapter.php new file mode 100644 index 00000000000..f2f2fbf59fd --- /dev/null +++ b/lib/private/EventDispatcher/SymfonyAdapter.php @@ -0,0 +1,140 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\EventDispatcher; + +use function is_callable; +use OCP\EventDispatcher\Event; +use Symfony\Component\EventDispatcher\Event as SymfonyEvent; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class SymfonyAdapter implements EventDispatcherInterface { + + /** @var EventDispatcher */ + private $eventDispatcher; + + public function __construct(EventDispatcher $eventDispatcher) { + $this->eventDispatcher = $eventDispatcher; + } + + /** + * Dispatches an event to all registered listeners. + * + * @param string $eventName The name of the event to dispatch. The name of + * the event is the name of the method that is + * invoked on listeners. + * @param SymfonyEvent|null $event The event to pass to the event handlers/listeners + * If not supplied, an empty Event instance is created + * + * @return SymfonyEvent + */ + public function dispatch($eventName, SymfonyEvent $event = null) { + if ($event instanceof Event) { + $this->eventDispatcher->dispatch($eventName, $event); + } else { + // Legacy event + $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $event); + } + } + + /** + * Adds an event listener that listens on the specified events. + * + * @param string $eventName The event to listen on + * @param callable $listener The listener + * @param int $priority The higher this value, the earlier an event + * listener will be triggered in the chain (defaults to 0) + */ + public function addListener($eventName, $listener, $priority = 0) { + if (is_callable($listener)) { + $this->eventDispatcher->addListener($eventName, $listener, $priority); + } else { + // Legacy listener + $this->eventDispatcher->getSymfonyDispatcher()->addListener($eventName, $listener, $priority); + } + } + + /** + * Adds an event subscriber. + * + * The subscriber is asked for all the events it is + * interested in and added as a listener for these events. + */ + public function addSubscriber(EventSubscriberInterface $subscriber) { + $this->eventDispatcher->getSymfonyDispatcher()->addSubscriber($subscriber); + } + + /** + * Removes an event listener from the specified events. + * + * @param string $eventName The event to remove a listener from + * @param callable $listener The listener to remove + */ + public function removeListener($eventName, $listener) { + $this->eventDispatcher->getSymfonyDispatcher()->removeListener($eventName, $listener); + } + + public function removeSubscriber(EventSubscriberInterface $subscriber) { + $this->eventDispatcher->getSymfonyDispatcher()->removeSubscriber($subscriber); + } + + /** + * Gets the listeners of a specific event or all listeners sorted by descending priority. + * + * @param string|null $eventName The name of the event + * + * @return array The event listeners for the specified event, or all event listeners by event name + */ + public function getListeners($eventName = null) { + return $this->eventDispatcher->getSymfonyDispatcher()->getListeners($eventName); + } + + /** + * Gets the listener priority for a specific event. + * + * Returns null if the event or the listener does not exist. + * + * @param string $eventName The name of the event + * @param callable $listener The listener + * + * @return int|null The event listener priority + */ + public function getListenerPriority($eventName, $listener) { + return $this->eventDispatcher->getSymfonyDispatcher()->getListenerPriority($eventName, $listener); + } + + /** + * Checks whether an event has any registered listeners. + * + * @param string|null $eventName The name of the event + * + * @return bool true if the specified event has any listeners, false otherwise + */ + public function hasListeners($eventName = null) { + return $this->eventDispatcher->getSymfonyDispatcher()->hasListeners($eventName); + } + +} diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index fef6153fb9a..8322576c5c2 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -566,7 +566,12 @@ class View { $hooks[] = 'create'; $hooks[] = 'write'; } - $result = $this->basicOperation('touch', $path, $hooks, $mtime); + try { + $result = $this->basicOperation('touch', $path, $hooks, $mtime); + } catch (\Exception $e) { + $this->logger->logException($e, ['level' => ILogger::INFO, 'message' => 'Error while setting modified time']); + $result = false; + } if (!$result) { // If create file fails because of permissions on external storage like SMB folders, // check file exists and return false if not. @@ -1928,7 +1933,7 @@ class View { if ($mount) { try { $storage = $mount->getStorage(); - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { $storage->acquireLock( $mount->getInternalPath($absolutePath), $type, @@ -1969,7 +1974,7 @@ class View { if ($mount) { try { $storage = $mount->getStorage(); - if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { + if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { $storage->changeLock( $mount->getInternalPath($absolutePath), $type, diff --git a/lib/private/Preview/BMP.php b/lib/private/Preview/BMP.php index de08cfe0c8f..fed0c458d0e 100644 --- a/lib/private/Preview/BMP.php +++ b/lib/private/Preview/BMP.php @@ -26,7 +26,7 @@ class BMP extends Image { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/bmp/'; } } diff --git a/lib/private/Preview/Bitmap.php b/lib/private/Preview/Bitmap.php index 1fd42a0ea97..000b1f8277f 100644 --- a/lib/private/Preview/Bitmap.php +++ b/lib/private/Preview/Bitmap.php @@ -26,44 +26,43 @@ namespace OC\Preview; use Imagick; +use OCP\IImage; use OCP\ILogger; +use OCP\Files\File; /** * Creates a PNG preview using ImageMagick via the PECL extension * * @package OC\Preview */ -abstract class Bitmap extends Provider { +abstract class Bitmap extends ProviderV2 { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { - $tmpPath = $fileview->toTmpFile($path); - if (!$tmpPath) { - return false; - } + $tmpPath = $this->getLocalFile($file); // Creates \Imagick object from bitmap or vector file try { $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY); } catch (\Exception $e) { \OC::$server->getLogger()->logException($e, [ - 'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:', + 'message' => 'File: ' . $file->getPath() . ' Imagick says:', 'level' => ILogger::ERROR, 'app' => 'core', ]); - return false; + return null; } - unlink($tmpPath); + $this->cleanTmpFiles(); //new bitmap image object $image = new \OC_Image(); $image->loadFromData($bp); //check if image object is valid - return $image->valid() ? $image : false; + return $image->valid() ? $image : null; } /** diff --git a/lib/private/Preview/Font.php b/lib/private/Preview/Font.php index 4d5732eb880..3f24bf3e738 100644 --- a/lib/private/Preview/Font.php +++ b/lib/private/Preview/Font.php @@ -27,7 +27,7 @@ class Font extends Bitmap { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/(?:font-sfnt|x-font$)/'; } -}
\ No newline at end of file +} diff --git a/lib/private/Preview/GIF.php b/lib/private/Preview/GIF.php index 2189273b3b5..2652efc4ddd 100644 --- a/lib/private/Preview/GIF.php +++ b/lib/private/Preview/GIF.php @@ -26,7 +26,7 @@ class GIF extends Image { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/gif/'; } } diff --git a/lib/private/Preview/Generator.php b/lib/private/Preview/Generator.php index 1f7decf2b79..2f9401af385 100644 --- a/lib/private/Preview/Generator.php +++ b/lib/private/Preview/Generator.php @@ -35,6 +35,8 @@ use OCP\IConfig; use OCP\IImage; use OCP\IPreview; use OCP\Preview\IProvider; +use OCP\Preview\IProviderV2; +use OCP\Preview\IVersionedPreviewFile; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -178,9 +180,9 @@ class Generator { continue; } - foreach ($providers as $provider) { - $provider = $this->helper->getProvider($provider); - if (!($provider instanceof IProvider)) { + foreach ($providers as $providerClosure) { + $provider = $this->helper->getProvider($providerClosure); + if (!($provider instanceof IProviderV2)) { continue; } diff --git a/lib/private/Preview/GeneratorHelper.php b/lib/private/Preview/GeneratorHelper.php index 2f1a1f8aeff..7e35b5360d4 100644 --- a/lib/private/Preview/GeneratorHelper.php +++ b/lib/private/Preview/GeneratorHelper.php @@ -30,6 +30,7 @@ use OCP\IConfig; use OCP\IImage; use OCP\Image as OCPImage; use OCP\Preview\IProvider; +use OCP\Preview\IProviderV2; /** * Very small wrapper class to make the generator fully unit testable @@ -54,21 +55,8 @@ class GeneratorHelper { * @param int $maxHeight * @return bool|IImage */ - public function getThumbnail(IProvider $provider, File $file, $maxWidth, $maxHeight) { - list($view, $path) = $this->getViewAndPath($file); - return $provider->getThumbnail($path, $maxWidth, $maxHeight, false, $view); - } - - /** - * @param File $file - * @return array - * This is required to create the old view and path - */ - private function getViewAndPath(File $file) { - $view = new View($file->getParent()->getPath()); - $path = $file->getName(); - - return [$view, $path]; + public function getThumbnail(IProviderV2 $provider, File $file, $maxWidth, $maxHeight) { + return $provider->getThumbnail($file, $maxWidth, $maxHeight); } /** @@ -82,10 +70,14 @@ class GeneratorHelper { } /** - * @param $provider - * @return IProvider + * @param callable $providerClosure + * @return IProviderV2 */ - public function getProvider($provider) { - return $provider(); + public function getProvider($providerClosure) { + $provider = $providerClosure(); + if ($provider instanceof IProvider) { + $provider = new ProviderV1Adapter($provider); + } + return $provider; } } diff --git a/lib/private/Preview/HEIC.php b/lib/private/Preview/HEIC.php index 54bc212b6b5..d23e6c6bd1f 100644 --- a/lib/private/Preview/HEIC.php +++ b/lib/private/Preview/HEIC.php @@ -23,14 +23,16 @@ declare(strict_types=1); namespace OC\Preview; +use OCP\IImage; use OCP\ILogger; +use OCP\Files\File; /** * Creates a JPG preview using ImageMagick via the PECL extension * * @package OC\Preview */ -class HEIC extends Provider { +class HEIC extends ProviderV2 { /** * {@inheritDoc} */ @@ -48,11 +50,8 @@ class HEIC extends Provider { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { - $tmpPath = $fileview->toTmpFile($path); - if (!$tmpPath) { - return false; - } + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + $tmpPath = $this->getLocalFile($file); // Creates \Imagick object from the heic file try { @@ -60,20 +59,20 @@ class HEIC extends Provider { $bp->setFormat('jpg'); } catch (\Exception $e) { \OC::$server->getLogger()->logException($e, [ - 'message' => 'File: ' . $fileview->getAbsolutePath($path) . ' Imagick says:', + 'message' => 'File: ' . $file->getPath() . ' Imagick says:', 'level' => ILogger::ERROR, 'app' => 'core', ]); - return false; + return null; } - unlink($tmpPath); + $this->cleanTmpFiles(); //new bitmap image object $image = new \OC_Image(); $image->loadFromData($bp); //check if image object is valid - return $image->valid() ? $image : false; + return $image->valid() ? $image : null; } /** diff --git a/lib/private/Preview/Illustrator.php b/lib/private/Preview/Illustrator.php index 733b679f3f5..b8f29d4754f 100644 --- a/lib/private/Preview/Illustrator.php +++ b/lib/private/Preview/Illustrator.php @@ -28,7 +28,7 @@ class Illustrator extends Bitmap { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/illustrator/'; } } diff --git a/lib/private/Preview/Image.php b/lib/private/Preview/Image.php index 86ce3bcd071..6f82904a6a7 100644 --- a/lib/private/Preview/Image.php +++ b/lib/private/Preview/Image.php @@ -27,44 +27,37 @@ */ namespace OC\Preview; -abstract class Image extends Provider { +use OCP\Files\File; +use OCP\IImage; + +abstract class Image extends ProviderV2 { /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { - //get fileinfo - $fileInfo = $fileview->getFileInfo($path); - if (!$fileInfo) { - return false; - } - + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { $maxSizeForImages = \OC::$server->getConfig()->getSystemValue('preview_max_filesize_image', 50); - $size = $fileInfo->getSize(); + $size = $file->getSize(); if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) { - return false; + return null; } $image = new \OC_Image(); - $useTempFile = $fileInfo->isEncrypted() || !$fileInfo->getStorage()->isLocal(); - if ($useTempFile) { - $fileName = $fileview->toTmpFile($path); - } else { - $fileName = $fileview->getLocalFile($path); - } + $fileName = $this->getLocalFile($file); + $image->loadFromFile($fileName); $image->fixOrientation(); - if ($useTempFile) { - unlink($fileName); - } + + $this->cleanTmpFiles(); + if ($image->valid()) { $image->scaleDownToFit($maxX, $maxY); return $image; } - return false; + return null; } } diff --git a/lib/private/Preview/JPEG.php b/lib/private/Preview/JPEG.php index 1937d4a4a8a..9948d2ecfd5 100644 --- a/lib/private/Preview/JPEG.php +++ b/lib/private/Preview/JPEG.php @@ -26,7 +26,7 @@ class JPEG extends Image { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/jpeg/'; } } diff --git a/lib/private/Preview/MP3.php b/lib/private/Preview/MP3.php index 11a512286ec..f560f100109 100644 --- a/lib/private/Preview/MP3.php +++ b/lib/private/Preview/MP3.php @@ -28,23 +28,26 @@ namespace OC\Preview; use ID3Parser\ID3Parser; -class MP3 extends Provider { +use OCP\Files\File; +use OCP\IImage; + +class MP3 extends ProviderV2 { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/audio\/mpeg/'; } /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { $getID3 = new ID3Parser(); - $tmpPath = $fileview->toTmpFile($path); + $tmpPath = $this->getLocalFile($file); $tags = $getID3->analyze($tmpPath); - unlink($tmpPath); + $this->cleanTmpFiles(); $picture = isset($tags['id3v2']['APIC'][0]['data']) ? $tags['id3v2']['APIC'][0]['data'] : null; if(is_null($picture) && isset($tags['id3v2']['PIC'][0]['data'])) { $picture = $tags['id3v2']['PIC'][0]['data']; @@ -61,6 +64,6 @@ class MP3 extends Provider { } } - return false; + return null; } } diff --git a/lib/private/Preview/MSOffice2003.php b/lib/private/Preview/MSOffice2003.php index 39ba4bc3f5c..5af66f72720 100644 --- a/lib/private/Preview/MSOffice2003.php +++ b/lib/private/Preview/MSOffice2003.php @@ -26,7 +26,7 @@ class MSOffice2003 extends Office { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/vnd.ms-.*/'; } } diff --git a/lib/private/Preview/MSOffice2007.php b/lib/private/Preview/MSOffice2007.php index 2e033683c20..0d1177d9de9 100644 --- a/lib/private/Preview/MSOffice2007.php +++ b/lib/private/Preview/MSOffice2007.php @@ -26,7 +26,7 @@ class MSOffice2007 extends Office { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/vnd.openxmlformats-officedocument.*/'; } } diff --git a/lib/private/Preview/MSOfficeDoc.php b/lib/private/Preview/MSOfficeDoc.php index 7e9d22fcf59..24d1a9d5b6a 100644 --- a/lib/private/Preview/MSOfficeDoc.php +++ b/lib/private/Preview/MSOfficeDoc.php @@ -26,7 +26,7 @@ class MSOfficeDoc extends Office { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/msword/'; } } diff --git a/lib/private/Preview/MarkDown.php b/lib/private/Preview/MarkDown.php index f49efbaaa07..ae24c4f4419 100644 --- a/lib/private/Preview/MarkDown.php +++ b/lib/private/Preview/MarkDown.php @@ -25,7 +25,7 @@ class MarkDown extends TXT { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/text\/(x-)?markdown/'; } diff --git a/lib/private/Preview/Movie.php b/lib/private/Preview/Movie.php index 3576a889e56..2f64811b45e 100644 --- a/lib/private/Preview/Movie.php +++ b/lib/private/Preview/Movie.php @@ -25,38 +25,27 @@ */ namespace OC\Preview; -class Movie extends Provider { +use OCP\Files\File; +use OCP\IImage; + +class Movie extends ProviderV2 { public static $avconvBinary; public static $ffmpegBinary; /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/video\/.*/'; } /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { // TODO: use proc_open() and stream the source file ? - $fileInfo = $fileview->getFileInfo($path); - $useFileDirectly = (!$fileInfo->isEncrypted() && !$fileInfo->isMounted()); - - if ($useFileDirectly) { - $absPath = $fileview->getLocalFile($path); - } else { - $absPath = \OC::$server->getTempManager()->getTemporaryFile(); - - $handle = $fileview->fopen($path, 'rb'); - - // we better use 5MB (1024 * 1024 * 5 = 5242880) instead of 1MB. - // in some cases 1MB was no enough to generate thumbnail - $firstmb = stream_get_contents($handle, 5242880); - file_put_contents($absPath, $firstmb); - } + $absPath = $this->getLocalFile($file, 5242880); // only use the first 5MB $result = $this->generateThumbNail($maxX, $maxY, $absPath, 5); if ($result === false) { @@ -66,9 +55,7 @@ class Movie extends Provider { } } - if (!$useFileDirectly) { - unlink($absPath); - } + $this->cleanTmpFiles(); return $result; } @@ -78,9 +65,9 @@ class Movie extends Provider { * @param int $maxY * @param string $absPath * @param int $second - * @return bool|\OCP\IImage + * @return null|\OCP\IImage */ - private function generateThumbNail($maxX, $maxY, $absPath, $second) { + private function generateThumbNail($maxX, $maxY, $absPath, $second): ?IImage { $tmpPath = \OC::$server->getTempManager()->getTemporaryFile(); if (self::$avconvBinary) { @@ -109,6 +96,6 @@ class Movie extends Provider { } } unlink($tmpPath); - return false; + return null; } } diff --git a/lib/private/Preview/Office.php b/lib/private/Preview/Office.php index 4a2aa7f4953..f51023c5a83 100644 --- a/lib/private/Preview/Office.php +++ b/lib/private/Preview/Office.php @@ -25,21 +25,23 @@ */ namespace OC\Preview; +use OCP\IImage; use OCP\ILogger; +use OCP\Files\File; -abstract class Office extends Provider { +abstract class Office extends ProviderV2 { private $cmd; /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { $this->initCmd(); if (is_null($this->cmd)) { - return false; + return null; } - $absPath = $fileview->toTmpFile($path); + $absPath = $this->getLocalFile($file); $tmpDir = \OC::$server->getTempManager()->getTempBaseDir(); @@ -59,19 +61,19 @@ abstract class Office extends Provider { $png = new \imagick($pngPreview . '[0]'); $png->setImageFormat('jpg'); } catch (\Exception $e) { - unlink($absPath); + $this->cleanTmpFiles(); unlink($pngPreview); \OC::$server->getLogger()->logException($e, [ 'level' => ILogger::ERROR, 'app' => 'core', ]); - return false; + return null; } $image = new \OC_Image(); $image->loadFromData($png); - unlink($absPath); + $this->cleanTmpFiles(); unlink($pngPreview); if ($image->valid()) { @@ -79,7 +81,7 @@ abstract class Office extends Provider { return $image; } - return false; + return null; } diff --git a/lib/private/Preview/OpenDocument.php b/lib/private/Preview/OpenDocument.php index 40971a0951b..929fecffb21 100644 --- a/lib/private/Preview/OpenDocument.php +++ b/lib/private/Preview/OpenDocument.php @@ -26,7 +26,7 @@ class OpenDocument extends Office { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/vnd.oasis.opendocument.*/'; } } diff --git a/lib/private/Preview/PDF.php b/lib/private/Preview/PDF.php index 04d7c66e595..f23f58be365 100644 --- a/lib/private/Preview/PDF.php +++ b/lib/private/Preview/PDF.php @@ -28,7 +28,7 @@ class PDF extends Bitmap { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/pdf/'; } } diff --git a/lib/private/Preview/PNG.php b/lib/private/Preview/PNG.php index cafe081818d..1a1cf52c968 100644 --- a/lib/private/Preview/PNG.php +++ b/lib/private/Preview/PNG.php @@ -26,7 +26,7 @@ class PNG extends Image { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/png/'; } } diff --git a/lib/private/Preview/Photoshop.php b/lib/private/Preview/Photoshop.php index 243fd7052e5..975acb8a03d 100644 --- a/lib/private/Preview/Photoshop.php +++ b/lib/private/Preview/Photoshop.php @@ -28,7 +28,7 @@ class Photoshop extends Bitmap { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/x-photoshop/'; } } diff --git a/lib/private/Preview/Postscript.php b/lib/private/Preview/Postscript.php index c20347d74ff..4dd19484991 100644 --- a/lib/private/Preview/Postscript.php +++ b/lib/private/Preview/Postscript.php @@ -28,7 +28,7 @@ class Postscript extends Bitmap { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/postscript/'; } } diff --git a/lib/private/Preview/ProviderV1Adapter.php b/lib/private/Preview/ProviderV1Adapter.php new file mode 100644 index 00000000000..fc7245a5876 --- /dev/null +++ b/lib/private/Preview/ProviderV1Adapter.php @@ -0,0 +1,59 @@ +<?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 OC\Preview; + +use OC\Files\View; +use OCP\Files\File; +use OCP\Files\FileInfo; +use OCP\IImage; +use OCP\Preview\IProvider; +use OCP\Preview\IProviderV2; + +class ProviderV1Adapter implements IProviderV2 { + private $providerV1; + + public function __construct(IProvider $providerV1) { + $this->providerV1 = $providerV1; + } + + public function getMimeType(): string { + return $this->providerV1->getMimeType(); + } + + public function isAvailable(FileInfo $file): bool { + return $this->isAvailable($file); + } + + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + list($view, $path) = $this->getViewAndPath($file); + $thumbnail = $this->providerV1->getThumbnail($path, $maxX, $maxY, false, $view); + return $thumbnail === false ? null: $thumbnail; + } + + private function getViewAndPath(File $file) { + $view = new View($file->getParent()->getPath()); + $path = $file->getName(); + + return [$view, $path]; + } + +} diff --git a/lib/private/Preview/ProviderV2.php b/lib/private/Preview/ProviderV2.php new file mode 100644 index 00000000000..8a64ac6e3b8 --- /dev/null +++ b/lib/private/Preview/ProviderV2.php @@ -0,0 +1,105 @@ +<?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 OC\Preview; + +use OCP\Files\File; +use OCP\Files\FileInfo; +use OCP\IImage; +use OCP\Preview\IProviderV2; + +abstract class ProviderV2 implements IProviderV2 { + private $options; + + private $tmpFiles = []; + + /** + * Constructor + * + * @param array $options + */ + public function __construct(array $options = []) { + $this->options = $options; + } + + /** + * @return string Regex with the mimetypes that are supported by this provider + */ + abstract public function getMimeType(): string ; + + /** + * Check if a preview can be generated for $path + * + * @param FileInfo $file + * @return bool + */ + public function isAvailable(FileInfo $file): bool { + return true; + } + + /** + * get thumbnail for file at path $path + * + * @param File $file + * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image + * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image + * @return null|\OCP\IImage false if no preview was generated + * @since 17.0.0 + */ + abstract public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage; + + /** + * Get a path to either the local file or temporary file + * + * @param File $file + * @param int $maxSize maximum size for temporary files + * @return string + */ + protected function getLocalFile(File $file, int $maxSize = null): string { + $useTempFile = $file->isEncrypted() || !$file->getStorage()->isLocal(); + if ($useTempFile) { + $absPath = \OC::$server->getTempManager()->getTemporaryFile(); + + $content = $file->fopen('r'); + + if ($maxSize) { + $content = stream_get_contents($content, $maxSize); + } + + file_put_contents($absPath, $content); + $this->tmpFiles[] = $absPath; + return $absPath; + } else { + return $file->getStorage()->getLocalFile($file->getInternalPath()); + } + } + + /** + * Clean any generated temporary files + */ + protected function cleanTmpFiles() { + foreach ($this->tmpFiles as $tmpFile) { + unlink($tmpFile); + } + + $this->tmpFiles = []; + } +} diff --git a/lib/private/Preview/SVG.php b/lib/private/Preview/SVG.php index e283175c2e2..3f313f4bb9b 100644 --- a/lib/private/Preview/SVG.php +++ b/lib/private/Preview/SVG.php @@ -24,32 +24,34 @@ */ namespace OC\Preview; +use OCP\IImage; use OCP\ILogger; +use OCP\Files\File; -class SVG extends Provider { +class SVG extends ProviderV2 { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/svg\+xml/'; } /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { try { $svg = new \Imagick(); $svg->setBackgroundColor(new \ImagickPixel('transparent')); - $content = stream_get_contents($fileview->fopen($path, 'r')); + $content = stream_get_contents($file->fopen('r')); if (substr($content, 0, 5) !== '<?xml') { $content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content; } // Do not parse SVG files with references if (stripos($content, 'xlink:href') !== false) { - return false; + return null; } $svg->readImageBlob($content); @@ -59,7 +61,7 @@ class SVG extends Provider { 'level' => ILogger::ERROR, 'app' => 'core', ]); - return false; + return null; } //new image object @@ -71,6 +73,6 @@ class SVG extends Provider { return $image; } - return false; + return null; } } diff --git a/lib/private/Preview/StarOffice.php b/lib/private/Preview/StarOffice.php index 3537d4baddb..2c9542b22e6 100644 --- a/lib/private/Preview/StarOffice.php +++ b/lib/private/Preview/StarOffice.php @@ -26,7 +26,7 @@ class StarOffice extends Office { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/application\/vnd.sun.xml.*/'; } } diff --git a/lib/private/Preview/TIFF.php b/lib/private/Preview/TIFF.php index 9f03e3c79f6..0c667117339 100644 --- a/lib/private/Preview/TIFF.php +++ b/lib/private/Preview/TIFF.php @@ -28,7 +28,7 @@ class TIFF extends Bitmap { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/tiff/'; } } diff --git a/lib/private/Preview/TXT.php b/lib/private/Preview/TXT.php index 2bb121775d6..1ac98d62338 100644 --- a/lib/private/Preview/TXT.php +++ b/lib/private/Preview/TXT.php @@ -26,36 +26,40 @@ */ namespace OC\Preview; -class TXT extends Provider { +use OCP\Files\File; +use OCP\Files\FileInfo; +use OCP\IImage; + +class TXT extends ProviderV2 { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/text\/plain/'; } /** * {@inheritDoc} */ - public function isAvailable(\OCP\Files\FileInfo $file) { + public function isAvailable(FileInfo $file): bool { return $file->getSize() > 0; } /** * {@inheritDoc} */ - public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) { - $content = $fileview->fopen($path, 'r'); + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { + $content = $file->fopen('r'); if ($content === false) { - return false; + return null; } $content = stream_get_contents($content,3000); //don't create previews of empty text files if(trim($content) === '') { - return false; + return null; } $lines = preg_split("/\r\n|\n|\r/", $content); @@ -95,6 +99,6 @@ class TXT extends Provider { $imageObject = new \OC_Image(); $imageObject->setResource($image); - return $imageObject->valid() ? $imageObject : false; + return $imageObject->valid() ? $imageObject : null; } } diff --git a/lib/private/Preview/XBitmap.php b/lib/private/Preview/XBitmap.php index 7a282e5d1e1..ffec165a7b6 100644 --- a/lib/private/Preview/XBitmap.php +++ b/lib/private/Preview/XBitmap.php @@ -26,7 +26,7 @@ class XBitmap extends Image { /** * {@inheritDoc} */ - public function getMimeType() { + public function getMimeType(): string { return '/image\/x-xbitmap/'; } } diff --git a/lib/private/PreviewManager.php b/lib/private/PreviewManager.php index 931b0035c4f..6635d919fb7 100644 --- a/lib/private/PreviewManager.php +++ b/lib/private/PreviewManager.php @@ -97,7 +97,7 @@ class PreviewManager implements IPreview { * In order to improve lazy loading a closure can be registered which will be * called in case preview providers are actually requested * - * $callable has to return an instance of \OCP\Preview\IProvider + * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 * * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider * @param \Closure $callable diff --git a/lib/private/Server.php b/lib/private/Server.php index 408b457ec39..2b08b0bab15 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -128,7 +128,6 @@ use OCA\Theming\ThemingDefaults; use OCP\Accounts\IAccountManager; use OCP\App\IAppManager; -use OCP\AppFramework\Utility\ITimeFactory; use OCP\Collaboration\AutoComplete\IManager; use OCP\Contacts\ContactsMenu\IContactsStore; use OCP\Dashboard\IDashboardManager; @@ -158,7 +157,6 @@ use OCP\Remote\IInstanceFactory; use OCP\RichObjectStrings\IValidator; use OCP\Security\IContentSecurityPolicyManager; use OCP\Share\IShareHelper; -use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -989,11 +987,9 @@ class Server extends ServerContainer implements IServerContainer { $c->getLogger() ); }); - $this->registerService(EventDispatcher::class, function () { - return new EventDispatcher(); - }); - $this->registerAlias('EventDispatcher', EventDispatcher::class); - $this->registerAlias(EventDispatcherInterface::class, EventDispatcher::class); + $this->registerAlias(\OCP\EventDispatcher\IEventDispatcher::class, \OC\EventDispatcher\EventDispatcher::class); + $this->registerAlias('EventDispatcher', \OC\EventDispatcher\SymfonyAdapter::class); + $this->registerAlias(EventDispatcherInterface::class, \OC\EventDispatcher\SymfonyAdapter::class); $this->registerService('CryptoWrapper', function (Server $c) { // FIXME: Instantiiated here due to cyclic dependency @@ -1858,7 +1854,7 @@ class Server extends ServerContainer implements IServerContainer { * @since 8.2.0 */ public function getEventDispatcher() { - return $this->query('EventDispatcher'); + return $this->query(\OC\EventDispatcher\SymfonyAdapter::class); } /** diff --git a/lib/private/Share20/LegacyHooks.php b/lib/private/Share20/LegacyHooks.php index c3a4e9d999d..4cc748aa418 100644 --- a/lib/private/Share20/LegacyHooks.php +++ b/lib/private/Share20/LegacyHooks.php @@ -1,4 +1,5 @@ <?php + /** * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl> * @@ -21,24 +22,26 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OC\Share20; use OCP\Files\File; use OCP\Share\IShare; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; use OCP\Share; class LegacyHooks { - /** @var EventDispatcher */ + + /** @var EventDispatcherInterface */ private $eventDispatcher; /** * LegacyHooks constructor. * - * @param EventDispatcher $eventDispatcher + * @param EventDispatcherInterface $eventDispatcher */ - public function __construct(EventDispatcher $eventDispatcher) { + public function __construct(EventDispatcherInterface $eventDispatcher) { $this->eventDispatcher = $eventDispatcher; $this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']); diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index c2ea165955f..df9a06e3a96 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -62,7 +62,7 @@ use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; use OCP\Share\IProviderFactory; use OCP\Share\IShare; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; use OCP\Share\IShareProvider; use OCP\Share; @@ -96,7 +96,7 @@ class Manager implements IManager { private $rootFolder; /** @var CappedMemoryCache */ private $sharingDisabledForUsersCache; - /** @var EventDispatcher */ + /** @var EventDispatcherInterface */ private $eventDispatcher; /** @var LegacyHooks */ private $legacyHooks; @@ -122,7 +122,7 @@ class Manager implements IManager { * @param IProviderFactory $factory * @param IUserManager $userManager * @param IRootFolder $rootFolder - * @param EventDispatcher $eventDispatcher + * @param EventDispatcherInterface $eventDispatcher * @param IMailer $mailer * @param IURLGenerator $urlGenerator * @param \OC_Defaults $defaults @@ -139,7 +139,7 @@ class Manager implements IManager { IProviderFactory $factory, IUserManager $userManager, IRootFolder $rootFolder, - EventDispatcher $eventDispatcher, + EventDispatcherInterface $eventDispatcher, IMailer $mailer, IURLGenerator $urlGenerator, \OC_Defaults $defaults @@ -1430,6 +1430,9 @@ class Manager implements IManager { if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { $nodes = $userFolder->getById($path->getId()); $path = array_shift($nodes); + if ($path->getOwner() === null) { + return []; + } $owner = $path->getOwner()->getUID(); } diff --git a/lib/private/User/Database.php b/lib/private/User/Database.php index 27dcb2fc331..8c00060e196 100644 --- a/lib/private/User/Database.php +++ b/lib/private/User/Database.php @@ -67,7 +67,7 @@ use OCP\User\Backend\IGetDisplayNameBackend; use OCP\User\Backend\IGetHomeBackend; use OCP\User\Backend\ISetDisplayNameBackend; use OCP\User\Backend\ISetPasswordBackend; -use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; /** @@ -84,7 +84,7 @@ class Database extends ABackend /** @var CappedMemoryCache */ private $cache; - /** @var EventDispatcher */ + /** @var EventDispatcherInterface */ private $eventDispatcher; /** @var IDBConnection */ @@ -96,7 +96,7 @@ class Database extends ABackend /** * \OC\User\Database constructor. * - * @param EventDispatcher $eventDispatcher + * @param EventDispatcherInterface $eventDispatcher * @param string $table */ public function __construct($eventDispatcher = null, $table = 'users') { diff --git a/lib/private/legacy/util.php b/lib/private/legacy/util.php index 9bada5bb733..810f22fb9e5 100644 --- a/lib/private/legacy/util.php +++ b/lib/private/legacy/util.php @@ -204,7 +204,7 @@ class OC_Util { \OC\Files\Filesystem::initMountManager(); - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); + $prevLogging = \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(false); \OC\Files\Filesystem::addStorageWrapper('mount_options', function ($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { if ($storage->instanceOfStorage('\OC\Files\Storage\Common')) { /** @var \OC\Files\Storage\Common $storage */ @@ -279,7 +279,8 @@ class OC_Util { }); OC_Hook::emit('OC_Filesystem', 'preSetup', array('user' => $user)); - \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper(true); + + \OC\Files\Filesystem::logWarningWhenAddingStorageWrapper($prevLogging); //check if we are using an object storage $objectStore = \OC::$server->getSystemConfig()->getValue('objectstore', null); diff --git a/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php b/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php index 9a005c9cd5d..eb6eb5ca9af 100644 --- a/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php +++ b/lib/public/Authentication/TwoFactorAuth/RegistryEvent.php @@ -24,8 +24,8 @@ declare(strict_types=1); namespace OCP\Authentication\TwoFactorAuth; +use OCP\EventDispatcher\Event; use OCP\IUser; -use Symfony\Component\EventDispatcher\Event; /** * @since 15.0.0 @@ -42,6 +42,7 @@ class RegistryEvent extends Event { * @since 15.0.0 */ public function __construct(IProvider $provider, IUser $user) { + parent::__construct(); $this->provider = $provider; $this->user = $user; } diff --git a/lib/public/EventDispatcher/Event.php b/lib/public/EventDispatcher/Event.php new file mode 100644 index 00000000000..7f8554d7a86 --- /dev/null +++ b/lib/public/EventDispatcher/Event.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\EventDispatcher; + +use Symfony\Component\EventDispatcher\GenericEvent; + +/** + * Base event class for the event dispatcher service + * + * Typically this class isn't instantiated directly but sub classed for specific + * event types + * + * @since 17.0.0 + */ +class Event extends GenericEvent { + +} diff --git a/lib/public/EventDispatcher/IEventDispatcher.php b/lib/public/EventDispatcher/IEventDispatcher.php new file mode 100644 index 00000000000..af4d5316a7b --- /dev/null +++ b/lib/public/EventDispatcher/IEventDispatcher.php @@ -0,0 +1,61 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\EventDispatcher; + +/** + * Event dispatcher service of Nextcloud + * + * @since 17.0.0 + */ +interface IEventDispatcher { + + /** + * @param string $eventName preferably the fully-qualified class name of the Event sub class + * @param callable $listener the object that is invoked when a matching event is dispatched + * @param int $priority + * + * @since 17.0.0 + */ + public function addListener(string $eventName, callable $listener, int $priority = 0): void; + + /** + * @param string $eventName preferably the fully-qualified class name of the Event sub class to listen for + * @param string $className fully qualified class name (or ::class notation) of a \OCP\EventDispatcher\IEventListener that can be built by the DI container + * @param int $priority + * + * @since 17.0.0 + */ + public function addServiceListener(string $eventName, string $className, int $priority = 0): void; + + /** + * @param string $eventName + * @param Event $event + * + * @since 17.0.0 + */ + public function dispatch(string $eventName, Event $event): void; + +} diff --git a/lib/public/EventDispatcher/IEventListener.php b/lib/public/EventDispatcher/IEventListener.php new file mode 100644 index 00000000000..5ecf31abc4a --- /dev/null +++ b/lib/public/EventDispatcher/IEventListener.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @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\EventDispatcher; + +/** + * @since 17.0.0 + */ +interface IEventListener { + + /** + * @param Event $event + * + * @since 17.0.0 + */ + public function handle(Event $event): void; + +} diff --git a/lib/public/Preview/IProvider.php b/lib/public/Preview/IProvider.php index ca545d8eb76..5d62b3ce6d0 100644 --- a/lib/public/Preview/IProvider.php +++ b/lib/public/Preview/IProvider.php @@ -27,6 +27,7 @@ namespace OCP\Preview; * * @package OCP\Preview * @since 8.1.0 + * @deprecated 17.0.0 use IProviderV2 instead */ interface IProvider { /** diff --git a/lib/public/Preview/IProviderV2.php b/lib/public/Preview/IProviderV2.php new file mode 100644 index 00000000000..97fca21eaf5 --- /dev/null +++ b/lib/public/Preview/IProviderV2.php @@ -0,0 +1,57 @@ +<?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\Preview; + +use OCP\Files\File; +use OCP\Files\FileInfo; +use OCP\IImage; + +/** + * @since 17.0.0 + */ +interface IProviderV2 { + /** + * @return string Regex with the mimetypes that are supported by this provider + * @since 17.0.0 + */ + public function getMimeType(): string; + + /** + * Check if a preview can be generated for $path + * + * @param FileInfo $file + * @return bool + * @since 17.0.0 + */ + public function isAvailable(FileInfo $file): bool; + + /** + * get thumbnail for file at path $path + * + * @param File $file + * @param int $maxX The maximum X size of the thumbnail. It can be smaller depending on the shape of the image + * @param int $maxY The maximum Y size of the thumbnail. It can be smaller depending on the shape of the image + * @return null|\OCP\IImage null if no preview was generated + * @since 17.0.0 + */ + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage; +} |