-
Notifications
You must be signed in to change notification settings - Fork 55
Allow managing face clusters through DAV #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0865eaa
Allow managing face clusters through DAV
marcelklehr 4b63241
PropFindPlugin: Add additional DAV properties
marcelklehr 3afde9e
Performance optimizations
marcelklehr f927125
Default cluster title: Just the id
marcelklehr ffab182
Do not query Fileinfo separately
marcelklehr 52095ba
Properly inject classes
marcelklehr e489def
PropFindPlugin: Use existing constants
marcelklehr 2b2bf46
FaceRoot: Cache children
marcelklehr 1aa642c
Update lib/Dav/Faces/FaceRoot.php
marcelklehr dfb3dbc
PropFindPlugin: Fix import
marcelklehr 94a2ff3
FacePhoto#getMetadata: Fix return type
marcelklehr 61e239a
Dav endpoints getChild: Do not query all children to get a single one
marcelklehr 1d4f9af
FacesHome dav endpoint: Avoid returning clusters that don't belong to…
marcelklehr 0b1f0d0
getChild: Use children cache if possible
marcelklehr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| <?php | ||
|
|
||
| namespace OCA\Recognize\Dav\Faces; | ||
|
|
||
| use OC\Metadata\IMetadataManager; | ||
| use OCA\Recognize\Db\FaceCluster; | ||
| use OCA\Recognize\Db\FaceDetection; | ||
| use OCA\Recognize\Db\FaceDetectionMapper; | ||
| use OCP\Files\File; | ||
| use OCP\Files\Folder; | ||
| use OCP\IPreview; | ||
| use OCP\ITagManager; | ||
| use OCP\ITags; | ||
| use Sabre\DAV\Exception\Forbidden; | ||
| use Sabre\DAV\Exception\NotFound; | ||
| use Sabre\DAV\IFile; | ||
|
|
||
| class FacePhoto implements IFile { | ||
| private FaceDetectionMapper $detectionMapper; | ||
| private FaceDetection $faceDetection; | ||
| private FaceCluster $cluster; | ||
| private Folder $userFolder; | ||
| private ?File $file = null; | ||
| private ITagManager $tagManager; | ||
| private IMetadataManager $metadataManager; | ||
| private IPreview $preview; | ||
|
|
||
| public function __construct(FaceDetectionMapper $detectionMapper, FaceCluster $cluster, FaceDetection $faceDetection, Folder $userFolder, ITagManager $tagManager, IMetadataManager $metadataManager, IPreview $preview) { | ||
| $this->detectionMapper = $detectionMapper; | ||
| $this->cluster = $cluster; | ||
| $this->faceDetection = $faceDetection; | ||
| $this->userFolder = $userFolder; | ||
| $this->tagManager = $tagManager; | ||
| $this->metadataManager = $metadataManager; | ||
| $this->preview = $preview; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getName() { | ||
| $file = $this->getFile(); | ||
| return $file->getId() . '-' . $file->getName(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| * @throws \OCP\DB\Exception | ||
| */ | ||
| public function delete() { | ||
| $this->faceDetection->setClusterId(null); | ||
| $this->detectionMapper->update($this->faceDetection); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function setName($name) { | ||
| throw new Forbidden('Cannot rename photos through faces API'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function put($data) { | ||
| throw new Forbidden('Can\'t write to photos trough the faces api'); | ||
| } | ||
|
|
||
| public function getCluster() : FaceCluster { | ||
| return $this->cluster; | ||
| } | ||
|
|
||
| public function getFile() : File { | ||
| if ($this->file === null) { | ||
| $nodes = $this->userFolder->getById($this->faceDetection->getFileId()); | ||
| $node = current($nodes); | ||
| if ($node) { | ||
| if ($node instanceof File) { | ||
| return $this->file = $node; | ||
| } else { | ||
| throw new NotFound("Photo is a folder"); | ||
| } | ||
| } else { | ||
| throw new NotFound("Photo not found for user"); | ||
| } | ||
| } else { | ||
| return $this->file; | ||
| } | ||
| } | ||
|
|
||
| public function getFaceDetection() : FaceDetection { | ||
| return $this->faceDetection; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| * @throws \Sabre\DAV\Exception\NotFound | ||
| */ | ||
| public function get() { | ||
| return $this->getFile()->fopen('r'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getContentType() { | ||
| return $this->getFile()->getMimeType(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getETag() { | ||
| return $this->getFile()->getEtag(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getSize() { | ||
| return $this->getFile()->getSize(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getLastModified() { | ||
| return $this->getFile()->getMTime(); | ||
| } | ||
|
|
||
| public function getMetadata(): array { | ||
| $file = $this->getFile(); | ||
| $sizeMetadata = $this->metadataManager->fetchMetadataFor('size', [$file->getId()])[$file->getId()]; | ||
| return $sizeMetadata->getMetadata(); | ||
| } | ||
|
|
||
| public function hasPreview(): bool { | ||
| return $this->preview->isAvailable($this->getFile()); | ||
| } | ||
|
|
||
| public function isFavorite(): bool { | ||
| $tagger = $this->tagManager->load('files'); | ||
| $tags = $tagger->getTagsForObjects([$this->getFile()->getId()]); | ||
|
|
||
| if ($tags === false || empty($tags)) { | ||
| return false; | ||
| } | ||
|
|
||
| return array_search(ITags::TAG_FAVORITE, current($tags)) !== false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| <?php | ||
|
|
||
| namespace OCA\Recognize\Dav\Faces; | ||
|
|
||
| use OC\Metadata\IMetadataManager; | ||
| use OCA\Recognize\Db\FaceCluster; | ||
| use OCA\Recognize\Db\FaceClusterMapper; | ||
| use OCA\Recognize\Db\FaceDetection; | ||
| use OCA\Recognize\Db\FaceDetectionMapper; | ||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\Files\IRootFolder; | ||
| use OCP\IPreview; | ||
| use OCP\ITagManager; | ||
| use OCP\IUser; | ||
| use Sabre\DAV\Exception\Forbidden; | ||
| use Sabre\DAV\Exception\NotFound; | ||
| use Sabre\DAV\ICollection; | ||
| use Sabre\DAV\IMoveTarget; | ||
| use Sabre\DAV\INode; | ||
|
|
||
| class FaceRoot implements ICollection, IMoveTarget { | ||
| private FaceClusterMapper $clusterMapper; | ||
| private FaceCluster $cluster; | ||
| private IUser $user; | ||
| private FaceDetectionMapper $detectionMapper; | ||
| private IRootFolder $rootFolder; | ||
| private IMetadataManager $metadataManager; | ||
| private ITagManager $tagManager; | ||
| private IPreview $previewManager; | ||
| /** | ||
| * @var \OCA\Recognize\Dav\Faces\FacePhoto[] | ||
| */ | ||
| private array $children = []; | ||
|
|
||
| public function __construct(FaceClusterMapper $clusterMapper, FaceCluster $cluster, IUser $user, FaceDetectionMapper $detectionMapper, IRootFolder $rootFolder, IMetadataManager $metadataManager, ITagManager $tagManager, IPreview $previewManager) { | ||
| $this->clusterMapper = $clusterMapper; | ||
| $this->cluster = $cluster; | ||
| $this->user = $user; | ||
| $this->detectionMapper = $detectionMapper; | ||
| $this->rootFolder = $rootFolder; | ||
| $this->metadataManager = $metadataManager; | ||
| $this->tagManager = $tagManager; | ||
| $this->previewManager = $previewManager; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getName() { | ||
| return $this->cluster->getTitle() !== '' ? $this->cluster->getTitle() : ''.$this->cluster->getId(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function setName($name) { | ||
| $this->cluster->setTitle(basename($name)); | ||
| $this->clusterMapper->update($this->cluster); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function createDirectory($name) { | ||
| throw new Forbidden('Not allowed to create directories in this folder'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function createFile($name, $data = null) { | ||
| throw new Forbidden('Not allowed to create files in this folder'); | ||
| } | ||
|
|
||
| /** | ||
| * @throws \OCP\Files\NotPermittedException | ||
| * @throws \OC\User\NoUserException | ||
| */ | ||
| public function getChildren(): array { | ||
| if (count($this->children) === 0) { | ||
| $this->children = array_map(function (FaceDetection $detection) { | ||
| return new FacePhoto($this->detectionMapper, $this->cluster, $detection, $this->rootFolder->getUserFolder($this->user->getUID()), $this->tagManager, $this->metadataManager, $this->previewManager); | ||
| }, $this->detectionMapper->findByClusterId($this->cluster->getId())); | ||
| } | ||
| return $this->children; | ||
| } | ||
|
|
||
| public function getChild($name): FacePhoto { | ||
| if (count($this->children) !== 0) { | ||
| foreach ($this->getChildren() as $child) { | ||
| if ($child->getName() === $name) { | ||
| return $child; | ||
| } | ||
| } | ||
| throw new NotFound("$name not found"); | ||
| } | ||
| [$fileId,] = explode('-', $name); | ||
| try { | ||
| $detection = $this->detectionMapper->findByFileIdAndClusterId((int)$fileId, $this->cluster->getId()); | ||
| } catch (DoesNotExistException $e) { | ||
| throw new NotFound(); | ||
| } | ||
| return new FacePhoto($this->detectionMapper, $this->cluster, $detection, $this->rootFolder->getUserFolder($this->user->getUID()), $this->tagManager, $this->metadataManager, $this->previewManager); | ||
| } | ||
|
|
||
| public function childExists($name): bool { | ||
| try { | ||
| $this->getChild($name); | ||
| return true; | ||
| } catch (NotFound $e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public function moveInto($targetName, $sourcePath, INode $sourceNode) { | ||
| if ($sourceNode instanceof FacePhoto) { | ||
| $sourceNode->getFaceDetection()->setClusterId($this->cluster->getId()); | ||
| $this->detectionMapper->update($sourceNode->getFaceDetection()); | ||
| return true; | ||
| } | ||
| throw new Forbidden('Not a photo with a detected face, you can only move photos from the faces collection here'); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| * @throws \OCP\DB\Exception | ||
| */ | ||
| public function delete() { | ||
| $this->clusterMapper->delete($this->cluster); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getLastModified() : int { | ||
| return 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.