Skip to content

Commit

Permalink
fix: Add compatibility to new FilesMetadata OCP API
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Nov 19, 2023
1 parent 2f7cff2 commit 3afb887
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 50 deletions.
13 changes: 2 additions & 11 deletions lib/Dav/Faces/FacePhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace OCA\Recognize\Dav\Faces;

use \Rubix\ML\Kernels\Distance\Euclidean;
use OC\Metadata\IMetadataManager;
use OCA\Recognize\Db\FaceDetection;
use OCA\Recognize\Db\FaceDetectionMapper;
use OCA\Recognize\Service\FaceClusterAnalyzer;
Expand All @@ -26,15 +25,13 @@ class FacePhoto implements IFile {
private Folder $userFolder;
private ?File $file = null;
private ITagManager $tagManager;
private IMetadataManager $metadataManager;
private IPreview $preview;

public function __construct(FaceDetectionMapper $detectionMapper, FaceDetection $faceDetection, Folder $userFolder, ITagManager $tagManager, IMetadataManager $metadataManager, IPreview $preview) {
public function __construct(FaceDetectionMapper $detectionMapper, FaceDetection $faceDetection, Folder $userFolder, ITagManager $tagManager, IPreview $preview) {
$this->detectionMapper = $detectionMapper;
$this->faceDetection = $faceDetection;
$this->userFolder = $userFolder;
$this->tagManager = $tagManager;
$this->metadataManager = $metadataManager;
$this->preview = $preview;
}

Expand Down Expand Up @@ -77,7 +74,7 @@ public function setName($name) {
public function put($data) {
throw new Forbidden('Can\'t write to photos trough the faces api');
}

public function getFile() : File {
if ($this->file === null) {
$nodes = $this->userFolder->getById($this->faceDetection->getFileId());
Expand Down Expand Up @@ -136,12 +133,6 @@ public function getLastModified() {
return $this->getFile()->getMTime();
}

public function getMetadata(): string {
$file = $this->getFile();
$sizeMetadata = $this->metadataManager->fetchMetadataFor('size', [$file->getId()])[$file->getId()];
return json_encode($sizeMetadata->getDecodedValue());
}

public function hasPreview(): bool {
return $this->preview->isAvailable($this->getFile());
}
Expand Down
9 changes: 3 additions & 6 deletions lib/Dav/Faces/FaceRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
declare(strict_types=1);
namespace OCA\Recognize\Dav\Faces;

use OC\Metadata\IMetadataManager;
use OCA\Recognize\Db\FaceCluster;
use OCA\Recognize\Db\FaceClusterMapper;
use OCA\Recognize\Db\FaceDetection;
Expand All @@ -28,21 +27,19 @@ class FaceRoot implements ICollection, IMoveTarget {
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) {
public function __construct(FaceClusterMapper $clusterMapper, FaceCluster $cluster, IUser $user, FaceDetectionMapper $detectionMapper, IRootFolder $rootFolder, 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;
}
Expand Down Expand Up @@ -91,7 +88,7 @@ public function getChildren(): array {
$detections = $this->detectionMapper->findByClusterId($this->cluster->getId());
$detectionsWithFile = array_filter($detections, fn (FaceDetection $detection): bool => current($this->rootFolder->getUserFolder($this->user->getUID())->getById($detection->getFileId())) !== false);
$this->children = array_map(function (FaceDetection $detection) {
return new FacePhoto($this->detectionMapper, $detection, $this->rootFolder->getUserFolder($this->user->getUID()), $this->tagManager, $this->metadataManager, $this->previewManager);
return new FacePhoto($this->detectionMapper, $detection, $this->rootFolder->getUserFolder($this->user->getUID()), $this->tagManager, $this->previewManager);
}, $detectionsWithFile);
}
return $this->children;
Expand All @@ -112,7 +109,7 @@ public function getChild($name): FacePhoto {
} catch (DoesNotExistException $e) {
throw new NotFound();
}
return new FacePhoto($this->detectionMapper, $detection, $this->rootFolder->getUserFolder($this->user->getUID()), $this->tagManager, $this->metadataManager, $this->previewManager);
return new FacePhoto($this->detectionMapper, $detection, $this->rootFolder->getUserFolder($this->user->getUID()), $this->tagManager, $this->previewManager);
}

public function childExists($name): bool {
Expand Down
9 changes: 3 additions & 6 deletions lib/Dav/Faces/FacesHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
declare(strict_types=1);
namespace OCA\Recognize\Dav\Faces;

use OC\Metadata\IMetadataManager;
use OCA\Recognize\Db\FaceCluster;
use OCA\Recognize\Db\FaceClusterMapper;
use OCA\Recognize\Db\FaceDetectionMapper;
Expand All @@ -26,16 +25,14 @@ class FacesHome implements ICollection {
private IRootFolder $rootFolder;
private array $children = [];
private ITagManager $tagManager;
private IMetadataManager $metadataManager;
private IPreview $previewManager;

public function __construct(FaceClusterMapper $faceClusterMapper, IUser $user, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IMetadataManager $metadataManager, IPreview $previewManager) {
public function __construct(FaceClusterMapper $faceClusterMapper, IUser $user, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IPreview $previewManager) {
$this->faceClusterMapper = $faceClusterMapper;
$this->user = $user;
$this->faceDetectionMapper = $faceDetectionMapper;
$this->rootFolder = $rootFolder;
$this->tagManager = $tagManager;
$this->metadataManager = $metadataManager;
$this->previewManager = $previewManager;
}

Expand Down Expand Up @@ -84,7 +81,7 @@ public function getChild($name) : FaceRoot {
}
}

return new FaceRoot($this->faceClusterMapper, $cluster, $this->user, $this->faceDetectionMapper, $this->rootFolder, $this->metadataManager, $this->tagManager, $this->previewManager);
return new FaceRoot($this->faceClusterMapper, $cluster, $this->user, $this->faceDetectionMapper, $this->rootFolder, $this->tagManager, $this->previewManager);
}

/**
Expand All @@ -95,7 +92,7 @@ public function getChildren(): array {
$clusters = $this->faceClusterMapper->findByUserId($this->user->getUID());
if (count($this->children) === 0) {
$this->children = array_map(function (FaceCluster $cluster) {
return new FaceRoot($this->faceClusterMapper, $cluster, $this->user, $this->faceDetectionMapper, $this->rootFolder, $this->metadataManager, $this->tagManager, $this->previewManager);
return new FaceRoot($this->faceClusterMapper, $cluster, $this->user, $this->faceDetectionMapper, $this->rootFolder, $this->tagManager, $this->previewManager);
}, $clusters);
}
return $this->children;
Expand Down
28 changes: 16 additions & 12 deletions lib/Dav/Faces/PropFindPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\Recognize\Db\FaceDetectionMapper;
use OCA\Recognize\Db\FaceDetectionWithTitle;
use OCP\Files\DavUtil;
use OCP\IPreview;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
Expand All @@ -25,10 +26,11 @@ class PropFindPlugin extends ServerPlugin {
public const FACE_PREVIEW_IMAGE_PROPERTYNAME = '{http://nextcloud.org/ns}face-preview-image';

private Server $server;
private FaceDetectionMapper $faceDetectionMapper;

public function __construct(FaceDetectionMapper $faceDetectionMapper) {
$this->faceDetectionMapper = $faceDetectionMapper;
public function __construct(
private FaceDetectionMapper $faceDetectionMapper,
private IPreview $previewManager,
) {
}

public function initialize(Server $server) {
Expand All @@ -40,10 +42,6 @@ public function initialize(Server $server) {

public function propFind(PropFind $propFind, INode $node) {
if ($node instanceof FacePhoto) {
$propFind->handle(self::FILE_NAME_PROPERTYNAME, function () use ($node) {
return $node->getFile()->getName();
});

$propFind->handle(self::FACE_DETECTIONS_PROPERTYNAME, function () use ($node) {
return json_encode(
array_map(
Expand All @@ -52,14 +50,20 @@ public function propFind(PropFind $propFind, INode $node) {
)
);
});

$propFind->handle(FilesPlugin::INTERNAL_FILEID_PROPERTYNAME, fn () => $node->getFile()->getId());
$propFind->handle(FilesPlugin::GETETAG_PROPERTYNAME, fn () => $node->getETag());
$propFind->handle(self::FILE_NAME_PROPERTYNAME, fn () => $node->getFile()->getName());
$propFind->handle(self::REALPATH_PROPERTYNAME, fn () => $node->getFile()->getPath());
$propFind->handle(FilesPlugin::FILE_METADATA_SIZE, fn () => $node->getMetadata());
$propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, fn () => json_encode($node->hasPreview()));
$propFind->handle(TagsPlugin::FAVORITE_PROPERTYNAME, fn () => $node->isFavorite() ? 1 : 0);
$propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, fn () => str_replace('G', '', DavUtil::getDavPermissions($node->getFile()->getFileInfo())));
$propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, fn () => json_encode($this->previewManager->isAvailable($node->getFile()->getFileInfo())));
$propFind->handle(FilesPlugin::PERMISSIONS_PROPERTYNAME, function () use ($node): string {
$permissions = DavUtil::getDavPermissions($node->getFile()->getFileInfo());
$filteredPermissions = str_replace('R', '', $permissions);
return $filteredPermissions;
});

foreach ($node->getFile()->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) {
$propFind->handle(FilesPlugin::FILE_METADATA_PREFIX.$metadataKey, $metadataValue);
}
}

if ($node instanceof FaceRoot || $node instanceof UnassignedFacesHome) {
Expand Down
5 changes: 2 additions & 3 deletions lib/Dav/Faces/UnassignedFacePhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
declare(strict_types=1);
namespace OCA\Recognize\Dav\Faces;

use OC\Metadata\IMetadataManager;
use OCA\Recognize\Db\FaceDetection;
use OCA\Recognize\Db\FaceDetectionMapper;
use OCP\Files\Folder;
Expand All @@ -16,8 +15,8 @@

class UnassignedFacePhoto extends FacePhoto {

public function __construct(FaceDetectionMapper $detectionMapper, FaceDetection $faceDetection, Folder $userFolder, ITagManager $tagManager, IMetadataManager $metadataManager, IPreview $preview) {
parent::__construct($detectionMapper, $faceDetection, $userFolder, $tagManager, $metadataManager, $preview);
public function __construct(FaceDetectionMapper $detectionMapper, FaceDetection $faceDetection, Folder $userFolder, ITagManager $tagManager, IPreview $preview) {
parent::__construct($detectionMapper, $faceDetection, $userFolder, $tagManager, $preview);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions lib/Dav/Faces/UnassignedFacesHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
declare(strict_types=1);
namespace OCA\Recognize\Dav\Faces;

use OC\Metadata\IMetadataManager;
use OCA\Recognize\Db\FaceDetection;
use OCA\Recognize\Db\FaceDetectionMapper;
use OCP\AppFramework\Db\DoesNotExistException;
Expand All @@ -24,10 +23,9 @@ class UnassignedFacesHome implements ICollection {
private IRootFolder $rootFolder;
private array $children = [];
private ITagManager $tagManager;
private IMetadataManager $metadataManager;
private IPreview $previewManager;

public function __construct(IUser $user, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IMetadataManager $metadataManager, IPreview $previewManager) {
public function __construct(IUser $user, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IPreview $previewManager) {
$this->user = $user;
$this->faceDetectionMapper = $faceDetectionMapper;
$this->rootFolder = $rootFolder;
Expand Down Expand Up @@ -110,7 +108,7 @@ public function getChildren(): array {
$this->children = array_map(function (FaceDetection $detection) {
return new UnassignedFacePhoto($this->faceDetectionMapper, $detection, $this->rootFolder->getUserFolder($this->user->getUID()), $this->tagManager, $this->metadataManager, $this->previewManager);
}, $detectionsWithFile);

return $this->children;
}

Expand Down
4 changes: 1 addition & 3 deletions lib/Dav/RecognizeHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
declare(strict_types=1);
namespace OCA\Recognize\Dav;

use OC\Metadata\IMetadataManager;
use OCA\Recognize\Dav\Faces\FacesHome;
use OCA\Recognize\Dav\Faces\UnassignedFacesHome;
use OCA\Recognize\Db\FaceClusterMapper;
Expand All @@ -26,10 +25,9 @@ class RecognizeHome implements ICollection {
private FaceDetectionMapper $faceDetectionMapper;
private IRootFolder $rootFolder;
private ITagManager $tagManager;
private IMetadataManager $metadataManager;
private IPreview $previewManager;

public function __construct(array $principalInfo, FaceClusterMapper $faceClusterMapper, IUser $user, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IMetadataManager $metadataManager, IPreview $previewManager) {
public function __construct(array $principalInfo, FaceClusterMapper $faceClusterMapper, IUser $user, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IPreview $previewManager) {
$this->principalInfo = $principalInfo;
$this->faceClusterMapper = $faceClusterMapper;
$this->user = $user;
Expand Down
7 changes: 2 additions & 5 deletions lib/Dav/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use \Sabre\DAVACL\AbstractPrincipalCollection;
use \Sabre\DAVACL\PrincipalBackend\BackendInterface;
use OC\Metadata\IMetadataManager;
use OCA\Recognize\Db\FaceClusterMapper;
use OCA\Recognize\Db\FaceDetectionMapper;
use OCP\Files\IRootFolder;
Expand All @@ -23,17 +22,15 @@ class RootCollection extends AbstractPrincipalCollection {
private FaceDetectionMapper $faceDetectionMapper;
private IRootFolder $rootFolder;
private ITagManager $tagManager;
private IMetadataManager $metadataManager;
private IPreview $previewManager;

public function __construct(BackendInterface $principalBackend, IUserSession $userSession, FaceClusterMapper $faceClusterMapper, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IMetadataManager $metadataManager, IPreview $previewManager) {
public function __construct(BackendInterface $principalBackend, IUserSession $userSession, FaceClusterMapper $faceClusterMapper, FaceDetectionMapper $faceDetectionMapper, IRootFolder $rootFolder, ITagManager $tagManager, IPreview $previewManager) {
parent::__construct($principalBackend, 'principals/users');
$this->userSession = $userSession;
$this->faceClusterMapper = $faceClusterMapper;
$this->faceDetectionMapper = $faceDetectionMapper;
$this->rootFolder = $rootFolder;
$this->tagManager = $tagManager;
$this->metadataManager = $metadataManager;
$this->previewManager = $previewManager;
}

Expand All @@ -43,7 +40,7 @@ public function getChildForPrincipal(array $principalInfo): RecognizeHome {
if (is_null($user) || $name !== $user->getUID()) {
throw new Forbidden();
}
return new RecognizeHome($principalInfo, $this->faceClusterMapper, $user, $this->faceDetectionMapper, $this->rootFolder, $this->tagManager, $this->metadataManager, $this->previewManager);
return new RecognizeHome($principalInfo, $this->faceClusterMapper, $user, $this->faceDetectionMapper, $this->rootFolder, $this->tagManager, $this->previewManager);
}

public function getName() {
Expand Down

0 comments on commit 3afb887

Please sign in to comment.