diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-07-15 18:11:54 +0300 |
---|---|---|
committer | Carl Schwan <carl@carlschwan.eu> | 2022-07-31 20:37:59 +0300 |
commit | 7b723813cef60e744ab14ab418c82e5ec67a9f2e (patch) | |
tree | eda4a044c9defc883c35d98e1ba5a9e261a3ecdf /lib | |
parent | ab1a20522b1b2730398869a764f672175a1abcb8 (diff) |
Multiple fixesenh/noid/share-attributes
- Fix tests
- Use non deprecated event stuff
- Add a bit of type hinting to the new stuff
- More safe handling of instanceOfStorage (share might not be the first
wrapper)
- Fix resharing
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 2 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | lib/private/Share20/DefaultShareProvider.php | 14 | ||||
-rw-r--r-- | lib/private/legacy/OC_Files.php | 30 | ||||
-rw-r--r-- | lib/public/Files/Events/BeforeDirectFileDownloadEvent.php | 84 | ||||
-rw-r--r-- | lib/public/Files/Events/BeforeZipCreatedEvent.php | 91 |
6 files changed, 199 insertions, 24 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index e17bbc96423..c55fb8080b0 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -266,8 +266,10 @@ return array( 'OCP\\Files\\Config\\IUserMountCache' => $baseDir . '/lib/public/Files/Config/IUserMountCache.php', 'OCP\\Files\\EmptyFileNameException' => $baseDir . '/lib/public/Files/EmptyFileNameException.php', 'OCP\\Files\\EntityTooLargeException' => $baseDir . '/lib/public/Files/EntityTooLargeException.php', + 'OCP\\Files\\Events\\BeforeDirectFileDownloadEvent' => $baseDir . '/lib/public/Files/Events/BeforeDirectFileDownloadEvent.php', 'OCP\\Files\\Events\\BeforeFileScannedEvent' => $baseDir . '/lib/public/Files/Events/BeforeFileScannedEvent.php', 'OCP\\Files\\Events\\BeforeFolderScannedEvent' => $baseDir . '/lib/public/Files/Events/BeforeFolderScannedEvent.php', + 'OCP\\Files\\Events\\BeforeZipCreatedEvent' => $baseDir . '/lib/public/Files/Events/BeforeZipCreatedEvent.php', 'OCP\\Files\\Events\\FileCacheUpdated' => $baseDir . '/lib/public/Files/Events/FileCacheUpdated.php', 'OCP\\Files\\Events\\FileScannedEvent' => $baseDir . '/lib/public/Files/Events/FileScannedEvent.php', 'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index dcca888aeb0..16b147814c4 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -299,8 +299,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Files\\Config\\IUserMountCache' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IUserMountCache.php', 'OCP\\Files\\EmptyFileNameException' => __DIR__ . '/../../..' . '/lib/public/Files/EmptyFileNameException.php', 'OCP\\Files\\EntityTooLargeException' => __DIR__ . '/../../..' . '/lib/public/Files/EntityTooLargeException.php', + 'OCP\\Files\\Events\\BeforeDirectFileDownloadEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeDirectFileDownloadEvent.php', 'OCP\\Files\\Events\\BeforeFileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeFileScannedEvent.php', 'OCP\\Files\\Events\\BeforeFolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeFolderScannedEvent.php', + 'OCP\\Files\\Events\\BeforeZipCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/BeforeZipCreatedEvent.php', 'OCP\\Files\\Events\\FileCacheUpdated' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileCacheUpdated.php', 'OCP\\Files\\Events\\FileScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FileScannedEvent.php', 'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php', diff --git a/lib/private/Share20/DefaultShareProvider.php b/lib/private/Share20/DefaultShareProvider.php index 627284da4e8..70f9b8665f9 100644 --- a/lib/private/Share20/DefaultShareProvider.php +++ b/lib/private/Share20/DefaultShareProvider.php @@ -1566,14 +1566,15 @@ class DefaultShareProvider implements IShareProvider { /** * Load from database format (JSON string) to IAttributes * - * @param IShare $share - * @param string|null $data - * @return IShare modified share + * @return IShare the modified share */ - private function updateShareAttributes(IShare $share, ?string $data) { + private function updateShareAttributes(IShare $share, ?string $data): IShare { if ($data !== null && $data !== '') { $attributes = new ShareAttributes(); $compressedAttributes = \json_decode($data, true); + if ($compressedAttributes === false || $compressedAttributes === null) { + return $share; + } foreach ($compressedAttributes as $compressedAttribute) { $attributes->setAttribute( $compressedAttribute[0], @@ -1589,11 +1590,8 @@ class DefaultShareProvider implements IShareProvider { /** * Format IAttributes to database format (JSON string) - * - * @param IAttributes|null $attributes - * @return string|null */ - private function formatShareAttributes($attributes) { + private function formatShareAttributes(?IAttributes $attributes): ?string { if ($attributes === null || empty($attributes->toArray())) { return null; } diff --git a/lib/private/legacy/OC_Files.php b/lib/private/legacy/OC_Files.php index 220fa0e3429..6a3a44d6cc0 100644 --- a/lib/private/legacy/OC_Files.php +++ b/lib/private/legacy/OC_Files.php @@ -44,12 +44,12 @@ use bantu\IniGetWrapper\IniGetWrapper; use OC\Files\View; use OC\Streamer; use OCP\Lock\ILockingProvider; -use OCP\EventDispatcher\GenericEvent; +use OCP\Files\Events\BeforeZipCreatedEvent; +use OCP\Files\Events\BeforeDirectFileDownloadEvent; use OCP\EventDispatcher\IEventDispatcher; /** * Class for file server access - * */ class OC_Files { public const FILE = 1; @@ -170,11 +170,11 @@ class OC_Files { } //Dispatch an event to see if any apps have problem with download - $event = new GenericEvent(null, ['dir' => $dir, 'files' => $files, 'run' => true]); - $dispatcher = \OC::$server->query(IEventDispatcher::class); - $dispatcher->dispatch('file.beforeCreateZip', $event); - if (($event->getArgument('run') === false) or ($event->hasArgument('errorMessage'))) { - throw new \OC\ForbiddenException($event->getArgument('errorMessage')); + $event = new BeforeZipCreatedEvent($dir, is_array($files) ? $files : [$files]); + $dispatcher = \OCP\Server::get(IEventDispatcher::class); + $dispatcher->dispatchTyped($event); + if ((!$event->isSuccessful()) || $event->getErrorMessage() !== null) { + throw new \OC\ForbiddenException($event->getErrorMessage()); } $streamer = new Streamer(\OC::$server->getRequest(), $fileSize, $numberOfFiles); @@ -222,8 +222,6 @@ class OC_Files { $streamer->finalize(); set_time_limit($executionTime); self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); - $event = new GenericEvent(null, ['result' => 'success', 'dir' => $dir, 'files' => $files]); - $dispatcher->dispatch('file.afterCreateZip', $event); } catch (\OCP\Lock\LockedException $ex) { self::unlockAllTheFiles($dir, $files, $getType, $view, $filename); OC::$server->getLogger()->logException($ex); @@ -240,8 +238,8 @@ class OC_Files { OC::$server->getLogger()->logException($ex); $l = \OC::$server->getL10N('lib'); $hint = method_exists($ex, 'getHint') ? $ex->getHint() : ''; - if ($event && $event->hasArgument('message')) { - $hint .= ' ' . $event->getArgument('message'); + if ($event && $event->getErrorMessage() !== null) { + $hint .= ' ' . $event->getErrorMessage(); } \OC_Template::printErrorPage($l->t('Cannot download file'), $hint, 200); } @@ -339,12 +337,12 @@ class OC_Files { } $dispatcher = \OC::$server->query(IEventDispatcher::class); - $event = new GenericEvent(null, ['path' => $filename]); - $dispatcher->dispatch('file.beforeGetDirect', $event); + $event = new BeforeDirectFileDownloadEvent($filename); + $dispatcher->dispatchTyped($event); - if (!\OC\Files\Filesystem::isReadable($filename) || $event->hasArgument('errorMessage')) { - if (!$event->hasArgument('errorMessage')) { - $msg = $event->getArgument('errorMessage'); + if (!\OC\Files\Filesystem::isReadable($filename) || $event->getErrorMessage()) { + if ($event->getErrorMessage()) { + $msg = $event->getErrorMessage(); } else { $msg = 'Access denied'; } diff --git a/lib/public/Files/Events/BeforeDirectFileDownloadEvent.php b/lib/public/Files/Events/BeforeDirectFileDownloadEvent.php new file mode 100644 index 00000000000..a32c95c6408 --- /dev/null +++ b/lib/public/Files/Events/BeforeDirectFileDownloadEvent.php @@ -0,0 +1,84 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2022 Carl Schwan <carl@carlschwan.eu> + * + * @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\Events; + +use OCP\EventDispatcher\Event; + +/** + * This event is triggered when a user tries to download a file + * directly. + * + * @since 25.0.0 + */ +class BeforeDirectFileDownloadEvent extends Event { + private string $path; + private bool $successful = true; + private ?string $errorMessage = null; + + /** + * @since 25.0.0 + */ + public function __construct(string $path) { + parent::__construct(); + $this->path = $path; + } + + /** + * @since 25.0.0 + */ + public function getPath(): string { + return $this->path; + } + + /** + * @since 25.0.0 + */ + public function isSuccessful(): bool { + return $this->successful; + } + + /** + * Set if the event was successful + * + * @since 25.0.0 + */ + public function setSuccessful(bool $successful): void { + $this->successful = $successful; + } + + /** + * Get the error message, if any + * @since 25.0.0 + */ + public function getErrorMessage(): ?string { + return $this->errorMessage; + } + + /** + * @since 25.0.0 + */ + public function setErrorMessage(string $errorMessage): void { + $this->errorMessage = $errorMessage; + } +} diff --git a/lib/public/Files/Events/BeforeZipCreatedEvent.php b/lib/public/Files/Events/BeforeZipCreatedEvent.php new file mode 100644 index 00000000000..18f41a42899 --- /dev/null +++ b/lib/public/Files/Events/BeforeZipCreatedEvent.php @@ -0,0 +1,91 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2022 Carl Schwan <carl@carlschwan.eu> + * + * @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\Events; + +use OCP\EventDispatcher\Event; + +/** + * @since 25.0.0 + */ +class BeforeZipCreatedEvent extends Event { + private string $directory; + private array $files; + private bool $successful = true; + private ?string $errorMessage = null; + + /** + * @since 25.0.0 + */ + public function __construct(string $directory, array $files) { + parent::__construct(); + $this->directory = $directory; + $this->files = $files; + } + + /** + * @since 25.0.0 + */ + public function getDirectory(): string { + return $this->directory; + } + + /** + * @since 25.0.0 + */ + public function getFiles(): array { + return $this->files; + } + + /** + * @since 25.0.0 + */ + public function isSuccessful(): bool { + return $this->successful; + } + + /** + * Set if the event was successful + * + * @since 25.0.0 + */ + public function setSuccessful(bool $successful): void { + $this->successful = $successful; + } + + /** + * Get the error message, if any + * @since 25.0.0 + */ + public function getErrorMessage(): ?string { + return $this->errorMessage; + } + + /** + * @since 25.0.0 + */ + public function setErrorMessage(string $errorMessage): void { + $this->errorMessage = $errorMessage; + } +} |