Skip to content

Commit

Permalink
using exception instead
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Mar 7, 2024
1 parent 88aa12f commit f81adce
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 112 deletions.
15 changes: 8 additions & 7 deletions apps/files/lib/Listener/SyncLivePhotosListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\Files_Trashbin\Trash\ITrashManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Exceptions\AbortedEventException;
use OCP\Files\Cache\CacheEntryRemovedEvent;
use OCP\Files\Events\Node\AbstractNodesEvent;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
Expand Down Expand Up @@ -119,20 +120,20 @@ private function handleMove(AbstractNodesEvent $event, Node $peerFile, bool $pre
$peerFileExtension = $peerFile->getExtension();
$targetName = $targetFile->getName();

if (!str_ends_with($targetName, ".".$sourceExtension)) {
$event->abortOperation(new NotPermittedException("Cannot change the extension of a Live Photo"));
if (!str_ends_with($targetName, "." . $sourceExtension)) {
throw new AbortedEventException('Cannot change the extension of a Live Photo');
}

try {
$targetParent->get($targetName);
$event->abortOperation(new NotPermittedException("A file already exist at destination path of the Live Photo"));
throw new AbortedEventException('A file already exist at destination path of the Live Photo');
} catch (NotFoundException) {
}

$peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension;
try {
$targetParent->get($peerTargetName);
$event->abortOperation(new NotPermittedException("A file already exist at destination path of the Live Photo"));
throw new AbortedEventException('A file already exist at destination path of the Live Photo');
} catch (NotFoundException) {
}

Expand All @@ -145,7 +146,7 @@ private function handleMove(AbstractNodesEvent $event, Node $peerFile, bool $pre
try {
$peerFile->move($targetParent->getPath() . '/' . $peerTargetName);
} catch (\Throwable $ex) {
$event->abortOperation($ex);
throw new AbortedEventException($ex->getMessage());
}

array_diff($this->pendingRenames, [$sourceFile->getId()]);
Expand Down Expand Up @@ -195,14 +196,14 @@ private function handleDeletion(BeforeNodeDeletedEvent $event, Node $peerFile):
unset($this->pendingDeletion[$peerFile->getId()]);
return;
} else {
$event->abortOperation(new NotPermittedException("Cannot delete the video part of a live photo"));
throw new AbortedEventException("Cannot delete the video part of a live photo");
}
} else {
$this->pendingDeletion[$deletedFile->getId()] = true;
try {
$peerFile->delete();
} catch (\Throwable $ex) {
$event->abortOperation($ex);
throw new AbortedEventException($ex->getMessage());
}
}
return;
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@
'OCP\\EventDispatcher\\GenericEvent' => $baseDir . '/lib/public/EventDispatcher/GenericEvent.php',
'OCP\\EventDispatcher\\IEventDispatcher' => $baseDir . '/lib/public/EventDispatcher/IEventDispatcher.php',
'OCP\\EventDispatcher\\IEventListener' => $baseDir . '/lib/public/EventDispatcher/IEventListener.php',
'OCP\\Exceptions\\AbortedEventException' => $baseDir . '/lib/public/Exceptions/AbortedEventException.php',
'OCP\\Exceptions\\AppConfigException' => $baseDir . '/lib/public/Exceptions/AppConfigException.php',
'OCP\\Exceptions\\AppConfigIncorrectTypeException' => $baseDir . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php',
'OCP\\Exceptions\\AppConfigTypeConflictException' => $baseDir . '/lib/public/Exceptions/AppConfigTypeConflictException.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\EventDispatcher\\GenericEvent' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/GenericEvent.php',
'OCP\\EventDispatcher\\IEventDispatcher' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventDispatcher.php',
'OCP\\EventDispatcher\\IEventListener' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventListener.php',
'OCP\\Exceptions\\AbortedEventException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AbortedEventException.php',
'OCP\\Exceptions\\AppConfigException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigException.php',
'OCP\\Exceptions\\AppConfigIncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php',
'OCP\\Exceptions\\AppConfigTypeConflictException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigTypeConflictException.php',
Expand Down
52 changes: 30 additions & 22 deletions lib/private/Files/Node/HookConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Maxence Lange <maxence@artificial-owl.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
Expand All @@ -30,6 +31,7 @@
use OC\Files\View;
use OCP\EventDispatcher\GenericEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Exceptions\AbortedEventException;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
use OCP\Files\Events\Node\BeforeNodeCreatedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
Expand All @@ -46,27 +48,18 @@
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Util;
use Psr\Log\LoggerInterface;

class HookConnector {
/** @var IRootFolder */
private $root;

/** @var View */
private $view;

/** @var FileInfo[] */
private $deleteMetaCache = [];

/** @var IEventDispatcher */
private $dispatcher;
private array $deleteMetaCache = [];

public function __construct(
IRootFolder $root,
View $view,
IEventDispatcher $dispatcher) {
$this->root = $root;
$this->view = $view;
$this->dispatcher = $dispatcher;
private IRootFolder $root,
private View $view,
private IEventDispatcher $dispatcher,
private LoggerInterface $logger
) {
}

public function viewToNode() {
Expand Down Expand Up @@ -133,8 +126,13 @@ public function delete($arguments) {
$this->root->emit('\OC\Files', 'preDelete', [$node]);
$this->dispatcher->dispatch('\OCP\Files::preDelete', new GenericEvent($node));

$event = new BeforeNodeDeletedEvent($node, $arguments['run']);
$this->dispatcher->dispatchTyped($event);
$event = new BeforeNodeDeletedEvent($node);
try {
$this->dispatcher->dispatchTyped($event);
} catch (AbortedEventException $e) {
$arguments['run'] = false;
$this->logger->warning('delete process aborted', ['exception' => $e]);
}
}

public function postDelete($arguments) {
Expand Down Expand Up @@ -171,8 +169,13 @@ public function rename($arguments) {
$this->root->emit('\OC\Files', 'preRename', [$source, $target]);
$this->dispatcher->dispatch('\OCP\Files::preRename', new GenericEvent([$source, $target]));

$event = new BeforeNodeRenamedEvent($source, $target, $arguments['run']);
$this->dispatcher->dispatchTyped($event);
$event = new BeforeNodeRenamedEvent($source, $target);
try {
$this->dispatcher->dispatchTyped($event);
} catch (AbortedEventException $e) {
$arguments['run'] = false;
$this->logger->warning('rename process aborted', ['exception' => $e]);
}
}

public function postRename($arguments) {
Expand All @@ -191,8 +194,13 @@ public function copy($arguments) {
$this->root->emit('\OC\Files', 'preCopy', [$source, $target]);
$this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));

$event = new BeforeNodeCopiedEvent($source, $target, $arguments['run']);
$this->dispatcher->dispatchTyped($event);
$event = new BeforeNodeCopiedEvent($source, $target);
try {
$this->dispatcher->dispatchTyped($event);
} catch (AbortedEventException $e) {
$arguments['run'] = false;
$this->logger->warning('copy process aborted', ['exception' => $e]);
}
}

public function postCopy($arguments) {
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ public function __construct($webRoot, \OC\Config $config) {
return new HookConnector(
$c->get(IRootFolder::class),
new View(),
$c->get(IEventDispatcher::class)
$c->get(IEventDispatcher::class),
$c->get(LoggerInterface::class)
);
});

Expand Down
34 changes: 34 additions & 0 deletions lib/public/Exceptions/AbortedEventException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);
/**
* @copyright 2023 Maxence Lange <maxence@artificial-owl.com>
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @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\Exceptions;

use Exception;

/**
* @since 29.0.0
*/
class AbortedEventException extends Exception {
}
8 changes: 3 additions & 5 deletions lib/public/Files/Events/Node/AbstractNodeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@
* @since 20.0.0
*/
abstract class AbstractNodeEvent extends Event {
/** @var Node */
private $node;

/**
* @since 20.0.0
*/
public function __construct(Node $node) {
$this->node = $node;
public function __construct(
private Node $node
) {
}

/**
Expand Down
12 changes: 4 additions & 8 deletions lib/public/Files/Events/Node/AbstractNodesEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,13 @@
* @since 20.0.0
*/
abstract class AbstractNodesEvent extends Event {
/** @var Node */
private $source;
/** @var Node */
private $target;

/**
* @since 20.0.0
*/
public function __construct(Node $source, Node $target) {
$this->source = $source;
$this->target = $target;
public function __construct(
private Node $source,
private Node $target
) {
}

/**
Expand Down
23 changes: 0 additions & 23 deletions lib/public/Files/Events/Node/BeforeNodeCopiedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,8 @@
*/
namespace OCP\Files\Events\Node;

use OCP\DB\Exception;
use OCP\Files\Node;

/**
* @since 20.0.0
*/
class BeforeNodeCopiedEvent extends AbstractNodesEvent {
/**
* @since 20.0.0
*/
public function __construct(Node $source, Node $target, private bool &$run) {
parent::__construct($source, $target);
}

/**
* @since 28.0.0
* @return never
*/
public function abortOperation(\Throwable $ex = null) {
$this->stopPropagation();
$this->run = false;
if ($ex !== null) {
throw $ex;
} else {
throw new Exception('Operation aborted');
}
}
}
23 changes: 0 additions & 23 deletions lib/public/Files/Events/Node/BeforeNodeDeletedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,8 @@
*/
namespace OCP\Files\Events\Node;

use Exception;
use OCP\Files\Node;

/**
* @since 20.0.0
*/
class BeforeNodeDeletedEvent extends AbstractNodeEvent {
/**
* @since 20.0.0
*/
public function __construct(Node $node, private bool &$run) {
parent::__construct($node);
}

/**
* @since 28.0.0
* @return never
*/
public function abortOperation(\Throwable $ex = null) {
$this->stopPropagation();
$this->run = false;
if ($ex !== null) {
throw $ex;
} else {
throw new Exception('Operation aborted');
}
}
}
23 changes: 0 additions & 23 deletions lib/public/Files/Events/Node/BeforeNodeRenamedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,8 @@
*/
namespace OCP\Files\Events\Node;

use Exception;
use OCP\Files\Node;

/**
* @since 20.0.0
*/
class BeforeNodeRenamedEvent extends AbstractNodesEvent {
/**
* @since 20.0.0
*/
public function __construct(Node $source, Node $target, private bool &$run) {
parent::__construct($source, $target);
}

/**
* @since 28.0.0
* @return never
*/
public function abortOperation(\Throwable $ex = null) {
$this->stopPropagation();
$this->run = false;
if ($ex !== null) {
throw $ex;
} else {
throw new Exception('Operation aborted');
}
}
}

0 comments on commit f81adce

Please sign in to comment.