Skip to content

Commit 14b57f1

Browse files
committed
fixup! fix(files): Do not require files_trashbin in live photo sync listener
1 parent 063d45c commit 14b57f1

File tree

1 file changed

+0
-110
lines changed

1 file changed

+0
-110
lines changed

apps/files/lib/Listener/SyncLivePhotosListener.php

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -161,114 +161,4 @@ private function handleDeletion(BeforeNodeDeletedEvent $event, Node $peerFile):
161161
}
162162
return;
163163
}
164-
165-
/**
166-
* During restore event, we trigger another recursive restore on the peer file.
167-
* Restore operations on the .mov file directly are currently blocked.
168-
* The event listener being singleton, we can store the current state
169-
* of pending restores inside the 'pendingRestores' property,
170-
* to prevent infinite recursivity.
171-
*/
172-
private function handleRestore(BeforeNodeRestoredEvent $event, Node $peerFile): void {
173-
$sourceFile = $event->getSource();
174-
175-
if ($sourceFile->getMimetype() === 'video/quicktime') {
176-
if (isset($this->pendingRestores[$peerFile->getId()])) {
177-
unset($this->pendingRestores[$peerFile->getId()]);
178-
return;
179-
} else {
180-
$event->abortOperation(new NotPermittedException("Cannot restore the video part of a live photo"));
181-
}
182-
} else {
183-
$user = $this->userSession->getUser();
184-
if ($user === null) {
185-
return;
186-
}
187-
188-
$peerTrashItem = $this->trashManager->getTrashNodeById($user, $peerFile->getId());
189-
// Peer file is not in the bin, no need to restore it.
190-
if ($peerTrashItem === null) {
191-
return;
192-
}
193-
194-
$trashRoot = $this->trashManager->listTrashRoot($user);
195-
$trashItem = $this->getTrashItem($trashRoot, $peerFile->getInternalPath());
196-
197-
if ($trashItem === null) {
198-
$event->abortOperation(new NotFoundException("Couldn't find peer file in trashbin"));
199-
}
200-
201-
$this->pendingRestores[$sourceFile->getId()] = true;
202-
try {
203-
$this->trashManager->restoreItem($trashItem);
204-
} catch (\Throwable $ex) {
205-
$event->abortOperation($ex);
206-
}
207-
}
208-
}
209-
210-
/**
211-
* Helper method to get the associated live photo file.
212-
* We first look for it in the user folder, and if we
213-
* cannot find it here, we look for it in the user's trashbin.
214-
*/
215-
private function getLivePhotoPeer(int $nodeId): ?Node {
216-
if ($this->userFolder === null || $this->userSession === null) {
217-
return null;
218-
}
219-
220-
try {
221-
$metadata = $this->filesMetadataManager->getMetadata($nodeId);
222-
} catch (FilesMetadataNotFoundException $ex) {
223-
return null;
224-
}
225-
226-
if (!$metadata->hasKey('files-live-photo')) {
227-
return null;
228-
}
229-
230-
$peerFileId = (int)$metadata->getString('files-live-photo');
231-
232-
// Check the user's folder.
233-
$nodes = $this->userFolder->getById($peerFileId);
234-
if (count($nodes) !== 0) {
235-
return $nodes[0];
236-
}
237-
238-
// Check the user's trashbin.
239-
$user = $this->userSession->getUser();
240-
if ($user !== null) {
241-
$peerFile = $this->trashManager->getTrashNodeById($user, $peerFileId);
242-
if ($peerFile !== null) {
243-
return $peerFile;
244-
}
245-
}
246-
247-
$metadata->unset('files-live-photo');
248-
return null;
249-
}
250-
251-
/**
252-
* There is currently no method to restore a file based on its fileId or path.
253-
* So we have to manually find a ITrashItem from the trash item list.
254-
* TODO: This should be replaced by a proper method in the TrashManager.
255-
*/
256-
private function getTrashItem(array $trashFolder, string $path): ?ITrashItem {
257-
foreach($trashFolder as $trashItem) {
258-
if (str_starts_with($path, "files_trashbin/files".$trashItem->getTrashPath())) {
259-
if ($path === "files_trashbin/files".$trashItem->getTrashPath()) {
260-
return $trashItem;
261-
}
262-
263-
if ($trashItem instanceof Folder) {
264-
$node = $this->getTrashItem($trashItem->getDirectoryListing(), $path);
265-
if ($node !== null) {
266-
return $node;
267-
}
268-
}
269-
}
270-
}
271-
272-
return null;
273-
}
274164
}

0 commit comments

Comments
 (0)