Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,16 @@ The app does not send any sensitive data to cloud providers or similar services.

<types>
<filesystem/>
<dav/>
</types>


<sabre>
<collections>
<collection>OCA\Recognize\Dav\RootCollection</collection>
</collections>
<plugins>
<plugin>OCA\Recognize\Dav\Faces\PropFindPlugin</plugin>
</plugins>
</sabre>
</info>
3 changes: 3 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace OCA\Recognize\AppInfo;

use OCA\DAV\Connector\Sabre\Principal;
use OCA\Recognize\Hooks\FileListener;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand All @@ -32,6 +33,8 @@ public function __construct() {

public function register(IRegistrationContext $context): void {
@include_once __DIR__ . '/../../vendor/autoload.php';
/** Register $principalBackend for the DAV collection */
$context->registerServiceAlias('principalBackend', Principal::class);
}

/**
Expand Down
151 changes: 151 additions & 0 deletions lib/Dav/Faces/FacePhoto.php
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;
}
}
138 changes: 138 additions & 0 deletions lib/Dav/Faces/FaceRoot.php
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;
}
}
Loading