Skip to content

Commit

Permalink
fixup! fix(files): Do not require files_trashbin in live photo sync l…
Browse files Browse the repository at this point in the history
…istener
  • Loading branch information
artonge committed Apr 3, 2024
1 parent 063d45c commit 14b57f1
Showing 1 changed file with 0 additions and 110 deletions.
110 changes: 0 additions & 110 deletions apps/files/lib/Listener/SyncLivePhotosListener.php
Expand Up @@ -161,114 +161,4 @@ private function handleDeletion(BeforeNodeDeletedEvent $event, Node $peerFile):
}
return;
}

/**
* During restore event, we trigger another recursive restore on the peer file.
* Restore operations on the .mov file directly are currently blocked.
* The event listener being singleton, we can store the current state
* of pending restores inside the 'pendingRestores' property,
* to prevent infinite recursivity.
*/
private function handleRestore(BeforeNodeRestoredEvent $event, Node $peerFile): void {
$sourceFile = $event->getSource();

if ($sourceFile->getMimetype() === 'video/quicktime') {
if (isset($this->pendingRestores[$peerFile->getId()])) {
unset($this->pendingRestores[$peerFile->getId()]);
return;
} else {
$event->abortOperation(new NotPermittedException("Cannot restore the video part of a live photo"));
}
} else {
$user = $this->userSession->getUser();
if ($user === null) {
return;
}

$peerTrashItem = $this->trashManager->getTrashNodeById($user, $peerFile->getId());
// Peer file is not in the bin, no need to restore it.
if ($peerTrashItem === null) {
return;
}

$trashRoot = $this->trashManager->listTrashRoot($user);
$trashItem = $this->getTrashItem($trashRoot, $peerFile->getInternalPath());

if ($trashItem === null) {
$event->abortOperation(new NotFoundException("Couldn't find peer file in trashbin"));
}

$this->pendingRestores[$sourceFile->getId()] = true;
try {
$this->trashManager->restoreItem($trashItem);
} catch (\Throwable $ex) {
$event->abortOperation($ex);
}
}
}

/**
* Helper method to get the associated live photo file.
* We first look for it in the user folder, and if we
* cannot find it here, we look for it in the user's trashbin.
*/
private function getLivePhotoPeer(int $nodeId): ?Node {
if ($this->userFolder === null || $this->userSession === null) {
return null;
}

try {
$metadata = $this->filesMetadataManager->getMetadata($nodeId);
} catch (FilesMetadataNotFoundException $ex) {
return null;
}

if (!$metadata->hasKey('files-live-photo')) {
return null;
}

$peerFileId = (int)$metadata->getString('files-live-photo');

// Check the user's folder.
$nodes = $this->userFolder->getById($peerFileId);
if (count($nodes) !== 0) {
return $nodes[0];
}

// Check the user's trashbin.
$user = $this->userSession->getUser();
if ($user !== null) {
$peerFile = $this->trashManager->getTrashNodeById($user, $peerFileId);
if ($peerFile !== null) {
return $peerFile;
}
}

$metadata->unset('files-live-photo');
return null;
}

/**
* There is currently no method to restore a file based on its fileId or path.
* So we have to manually find a ITrashItem from the trash item list.
* TODO: This should be replaced by a proper method in the TrashManager.
*/
private function getTrashItem(array $trashFolder, string $path): ?ITrashItem {
foreach($trashFolder as $trashItem) {
if (str_starts_with($path, "files_trashbin/files".$trashItem->getTrashPath())) {
if ($path === "files_trashbin/files".$trashItem->getTrashPath()) {
return $trashItem;
}

if ($trashItem instanceof Folder) {
$node = $this->getTrashItem($trashItem->getDirectoryListing(), $path);
if ($node !== null) {
return $node;
}
}
}
}

return null;
}
}

0 comments on commit 14b57f1

Please sign in to comment.