Skip to content

Commit

Permalink
feat(files): copy live photos
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 8, 2024
1 parent 9522ef8 commit 467c84e
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 89 deletions.
4 changes: 4 additions & 0 deletions apps/files/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@
use OCP\Collaboration\Resources\IProviderManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryRemovedEvent;
use OCP\Files\Events\Node\BeforeNodeCopiedEvent;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\IConfig;
use OCP\IPreview;
use OCP\IRequest;
Expand Down Expand Up @@ -132,6 +134,8 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeNodeRestoredEvent::class, SyncLivePhotosListener::class);
$context->registerEventListener(CacheEntryRemovedEvent::class, SyncLivePhotosListener::class);
$context->registerEventListener(LoadSearchPlugins::class, LoadSearchPluginsListener::class);
$context->registerEventListener(BeforeNodeCopiedEvent::class, SyncLivePhotosListener::class);
$context->registerEventListener(NodeCopiedEvent::class, SyncLivePhotosListener::class);

$context->registerSearchProvider(FilesSearchProvider::class);

Expand Down
78 changes: 61 additions & 17 deletions apps/files/lib/Listener/SyncLivePhotosListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
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;
use OCP\Files\Events\Node\BeforeNodeDeletedEvent;
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
Expand All @@ -44,7 +48,7 @@
* @template-implements IEventListener<Event>
*/
class SyncLivePhotosListener implements IEventListener {
/** @var Array<int, string> */
/** @var Array<int> */
private array $pendingRenames = [];
/** @var Array<int, bool> */
private array $pendingDeletion = [];
Expand All @@ -65,7 +69,6 @@ public function handle(Event $event): void {
}

$peerFile = null;

if ($event instanceof BeforeNodeRenamedEvent) {
$peerFile = $this->getLivePhotoPeer($event->getSource()->getId());
} elseif ($event instanceof BeforeNodeRestoredEvent) {
Expand All @@ -74,20 +77,26 @@ public function handle(Event $event): void {
$peerFile = $this->getLivePhotoPeer($event->getNode()->getId());
} elseif ($event instanceof CacheEntryRemovedEvent) {
$peerFile = $this->getLivePhotoPeer($event->getFileId());
} elseif ($event instanceof BeforeNodeCopiedEvent || $event instanceof NodeCopiedEvent) {
$peerFile = $this->getLivePhotoPeer($event->getSource()->getId());
}

if ($peerFile === null) {
return; // not a Live Photo
}

if ($event instanceof BeforeNodeRenamedEvent) {
$this->handleMove($event, $peerFile);
$this->handleMove($event, $peerFile, false);
} elseif ($event instanceof BeforeNodeDeletedEvent) {
$this->handleDeletion($event, $peerFile);
} elseif ($event instanceof CacheEntryRemovedEvent) {
$peerFile->delete();
} elseif ($event instanceof BeforeNodeRestoredEvent) {
$this->handleRestore($event, $peerFile);
} elseif ($event instanceof BeforeNodeCopiedEvent) {
$this->handleMove($event, $peerFile, true);
} elseif ($event instanceof NodeCopiedEvent) {
$this->handleCopy($event, $peerFile);
}
}

Expand All @@ -98,44 +107,79 @@ public function handle(Event $event): void {
* of pending renames inside the 'pendingRenames' property,
* to prevent infinite recursive.
*/
private function handleMove(BeforeNodeRenamedEvent $event, Node $peerFile): void {
private function handleMove(AbstractNodesEvent $event, Node $peerFile, bool $prepForCopyOnly = false): void {
if (!($event instanceof BeforeNodeCopiedEvent) &&
!($event instanceof BeforeNodeRenamedEvent)) {
return;
}

$sourceFile = $event->getSource();
$targetFile = $event->getTarget();
$targetParent = $targetFile->getParent();
$sourceExtension = $sourceFile->getExtension();
$peerFileExtension = $peerFile->getExtension();
$targetName = $targetFile->getName();
$targetPath = $targetFile->getPath();

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"));
} catch (NotFoundException $ex) {
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"));
} catch (NotFoundException $ex) {
throw new AbortedEventException('A file already exist at destination path of the Live Photo');
} catch (NotFoundException) {
}

// in case the rename was initiated from this listener, we stop right now
if (array_key_exists($peerFile->getId(), $this->pendingRenames)) {
if ($prepForCopyOnly || in_array($peerFile->getId(), $this->pendingRenames)) {
return;
}

$this->pendingRenames[$sourceFile->getId()] = $targetPath;
$this->pendingRenames[] = $sourceFile->getId();
try {
$peerFile->move($targetParent->getPath() . '/' . $peerTargetName);
} catch (\Throwable $ex) {
$event->abortOperation($ex);
throw new AbortedEventException($ex->getMessage());
}
unset($this->pendingRenames[$sourceFile->getId()]);

array_diff($this->pendingRenames, [$sourceFile->getId()]);
}


/**
* handle copy, we already know if it is doable from BeforeNodeCopiedEvent, so we just copy the linked file
*
* @param NodeCopiedEvent $event
* @param Node $peerFile
*/
private function handleCopy(NodeCopiedEvent $event, Node $peerFile): void {
$sourceFile = $event->getSource();
$sourceExtension = $sourceFile->getExtension();
$peerFileExtension = $peerFile->getExtension();
$targetFile = $event->getTarget();
$targetParent = $targetFile->getParent();
$targetName = $targetFile->getName();
$peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension;

/**
* let's use freshly set variable.
* we copy the file and get its id. We already have the id of the current copy
* We have everything to update metadata and keep the link between the 2 copies.
*/
$newPeerFile = $peerFile->copy($targetParent->getPath() . '/' . $peerTargetName);
$targetMetadata = $this->filesMetadataManager->getMetadata($targetFile->getId(), true);
$targetMetadata->setString('files-live-photo', (string)$newPeerFile->getId());
$this->filesMetadataManager->saveMetadata($targetMetadata);
$peerMetadata = $this->filesMetadataManager->getMetadata($newPeerFile->getId(), true);
$peerMetadata->setString('files-live-photo', (string)$targetFile->getId());
$this->filesMetadataManager->saveMetadata($peerMetadata);
}

/**
Expand All @@ -152,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
50 changes: 29 additions & 21 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 @@ -192,7 +195,12 @@ public function copy($arguments) {
$this->dispatcher->dispatch('\OCP\Files::preCopy', new GenericEvent([$source, $target]));

$event = new BeforeNodeCopiedEvent($source, $target);
$this->dispatcher->dispatchTyped($event);
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

0 comments on commit 467c84e

Please sign in to comment.