aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/lib/Listener/SyncLivePhotosListener.php
blob: 6334e5d16a62d60b52973caaa9135e13339426ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OCA\Files\Listener;

use Exception;
use OC\Files\Node\NonExistingFile;
use OC\Files\Node\NonExistingFolder;
use OC\Files\View;
use OC\FilesMetadata\Model\FilesMetadata;
use OCA\Files\Service\LivePhotosService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Exceptions\AbortedEventException;
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\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\FilesMetadata\IFilesMetadataManager;

/**
 * @template-implements IEventListener<Event>
 */
class SyncLivePhotosListener implements IEventListener {
	/** @var Array<int> */
	private array $pendingRenames = [];
	/** @var Array<int, bool> */
	private array $pendingDeletion = [];

	public function __construct(
		private ?Folder $userFolder,
		private IFilesMetadataManager $filesMetadataManager,
		private LivePhotosService $livePhotosService,
		private IRootFolder $rootFolder,
		private View $view,
	) {
	}

	public function handle(Event $event): void {
		if ($this->userFolder === null) {
			return;
		}

		if ($event instanceof BeforeNodeCopiedEvent || $event instanceof NodeCopiedEvent) {
			$this->handleCopyRecursive($event, $event->getSource(), $event->getTarget());
		} else {
			$peerFileId = null;

			if ($event instanceof BeforeNodeRenamedEvent) {
				$peerFileId = $this->livePhotosService->getLivePhotoPeerId($event->getSource()->getId());
			} elseif ($event instanceof BeforeNodeDeletedEvent) {
				$peerFileId = $this->livePhotosService->getLivePhotoPeerId($event->getNode()->getId());
			} elseif ($event instanceof CacheEntryRemovedEvent) {
				$peerFileId = $this->livePhotosService->getLivePhotoPeerId($event->getFileId());
			}

			if ($peerFileId === null) {
				return; // Not a live photo.
			}

			// Check the user's folder.
			$peerFile = $this->userFolder->getFirstNodeById($peerFileId);

			if ($peerFile === null) {
				return; // Peer file not found.
			}

			if ($event instanceof BeforeNodeRenamedEvent) {
				$this->runMoveOrCopyChecks($event->getSource(), $event->getTarget(), $peerFile);
				$this->handleMove($event->getSource(), $event->getTarget(), $peerFile);
			} elseif ($event instanceof BeforeNodeDeletedEvent) {
				$this->handleDeletion($event, $peerFile);
			} elseif ($event instanceof CacheEntryRemovedEvent) {
				$peerFile->delete();
			}
		}
	}

	private function runMoveOrCopyChecks(Node $sourceFile, Node $targetFile, Node $peerFile): void {
		$targetParent = $targetFile->getParent();
		$sourceExtension = $sourceFile->getExtension();
		$peerFileExtension = $peerFile->getExtension();
		$targetName = $targetFile->getName();
		$peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension;

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

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

		if (!($targetParent instanceof NonExistingFolder)) {
			try {
				$targetParent->get($peerTargetName);
				throw new AbortedEventException('A file already exist at destination path of the Live Photo');
			} catch (NotFoundException) {
			}
		}
	}

	/**
	 * During rename events, which also include move operations,
	 * we rename the peer file using the same name.
	 * The event listener being singleton, we can store the current state
	 * of pending renames inside the 'pendingRenames' property,
	 * to prevent infinite recursive.
	 */
	private function handleMove(Node $sourceFile, Node $targetFile, Node $peerFile): void {
		$targetParent = $targetFile->getParent();
		$sourceExtension = $sourceFile->getExtension();
		$peerFileExtension = $peerFile->getExtension();
		$targetName = $targetFile->getName();
		$peerTargetName = substr($targetName, 0, -strlen($sourceExtension)) . $peerFileExtension;

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

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

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


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


		if ($targetParent->nodeExists($peerTargetName)) {
			// If the copy was a folder copy, then the peer file already exists.
			$targetPeerFile = $targetParent->get($peerTargetName);
		} else {
			// If the copy was a file copy, then we need to create the peer file.
			$targetPeerFile = $peerFile->copy($targetParent->getPath() . '/' . $peerTargetName);
		}

		/** @var FilesMetadata $targetMetadata */
		$targetMetadata = $this->filesMetadataManager->getMetadata($targetFile->getId(), true);
		$targetMetadata->setStorageId($targetFile->getStorage()->getCache()->getNumericStorageId());
		$targetMetadata->setString('files-live-photo', (string)$targetPeerFile->getId());
		$this->filesMetadataManager->saveMetadata($targetMetadata);
		/** @var FilesMetadata $peerMetadata */
		$peerMetadata = $this->filesMetadataManager->getMetadata($targetPeerFile->getId(), true);
		$peerMetadata->setStorageId($targetPeerFile->getStorage()->getCache()->getNumericStorageId());
		$peerMetadata->setString('files-live-photo', (string)$targetFile->getId());
		$this->filesMetadataManager->saveMetadata($peerMetadata);
	}

	/**
	 * During deletion event, we trigger another recursive delete on the peer file.
	 * Delete operations on the .mov file directly are currently blocked.
	 * The event listener being singleton, we can store the current state
	 * of pending deletions inside the 'pendingDeletions' property,
	 * to prevent infinite recursivity.
	 */
	private function handleDeletion(BeforeNodeDeletedEvent $event, Node $peerFile): void {
		$deletedFile = $event->getNode();
		if ($deletedFile->getMimetype() === 'video/quicktime') {
			if (isset($this->pendingDeletion[$peerFile->getId()])) {
				unset($this->pendingDeletion[$peerFile->getId()]);
				return;
			} else {
				throw new AbortedEventException('Cannot delete the video part of a live photo');
			}
		} else {
			$this->pendingDeletion[$deletedFile->getId()] = true;
			try {
				$peerFile->delete();
			} catch (\Throwable $ex) {
				throw new AbortedEventException($ex->getMessage());
			}
		}
		return;
	}

	/*
	 * Recursively get all the peer ids of a live photo.
	 * Needed when coping a folder.
	 *
	 * @param BeforeNodeCopiedEvent|NodeCopiedEvent $event
	 */
	private function handleCopyRecursive(Event $event, Node $sourceNode, Node $targetNode): void {
		if ($sourceNode instanceof Folder && $targetNode instanceof Folder) {
			foreach ($sourceNode->getDirectoryListing() as $sourceChild) {
				if ($event instanceof BeforeNodeCopiedEvent) {
					if ($sourceChild instanceof Folder) {
						$targetChild = new NonExistingFolder($this->rootFolder, $this->view, $targetNode->getPath() . '/' . $sourceChild->getName(), null, $targetNode);
					} else {
						$targetChild = new NonExistingFile($this->rootFolder, $this->view, $targetNode->getPath() . '/' . $sourceChild->getName(), null, $targetNode);
					}
				} elseif ($event instanceof NodeCopiedEvent) {
					$targetChild = $targetNode->get($sourceChild->getName());
				} else {
					throw new Exception('Event is type is not supported');
				}

				$this->handleCopyRecursive($event, $sourceChild, $targetChild);
			}
		} elseif ($sourceNode instanceof File && $targetNode instanceof File) {
			$peerFileId = $this->livePhotosService->getLivePhotoPeerId($sourceNode->getId());
			if ($peerFileId === null) {
				return;
			}
			$peerFile = $this->userFolder->getFirstNodeById($peerFileId);
			if ($peerFile === null) {
				return;
			}

			if ($event instanceof BeforeNodeCopiedEvent) {
				$this->runMoveOrCopyChecks($sourceNode, $targetNode, $peerFile);
			} elseif ($event instanceof NodeCopiedEvent) {
				$this->handleCopy($sourceNode, $targetNode, $peerFile);
			}
		} else {
			throw new Exception('Source and target type are not matching');
		}
	}
}