Skip to content

Commit

Permalink
Use oc_metadata to store location
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Feb 13, 2023
1 parent 540c329 commit 520924d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 141 deletions.
77 changes: 30 additions & 47 deletions lib/DB/Location/LocationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
use OCP\IDBConnection;

class LocationMapper {
public const TABLE_NAME = 'photos_metadata';
public const METADATA_TYPE = 'location';
public const METADATA_TYPE = 'photos_location';

public function __construct(
private IDBConnection $connection,
Expand All @@ -44,27 +43,33 @@ public function __construct(
public function findLocationsForUser(string $userId): array {
$qb = $this->connection->getQueryBuilder();

$rows = $qb->selectDistinct('value')
->from(self::TABLE_NAME)
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(self::METADATA_TYPE)))
$rows = $qb->selectDistinct('meta.metadata')
->from('mounts', 'mount')
->join('mount', 'filecache', 'file', $qb->expr()->eq('file.storage', 'mount.storage_id'))
->join('file', 'file_metadata', 'meta', $qb->expr()->eq('file.file_id', 'meta.id'))
->andWhere($qb->expr()->eq('mount.user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('file.mimepart', $qb->createNamedParameter(5, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_TYPE)))
->executeQuery()
->fetchAll();

return array_map(fn ($row) => new LocationInfo($userId, (int)$row['value']), $rows);
return array_map(fn ($row) => new LocationInfo($userId, (int)$row['metadata']), $rows);
}

/** @return LocationFile[] */
public function findFilesForUserAndLocation(string $userId, int $locationId) {
$qb = $this->connection->getQueryBuilder();

$rows = $qb->select("fileid", "mimetype", "size", "mtime", "etag", "m.value")
->from(self::TABLE_NAME, 'm')
->leftJoin("m", "filecache", "f", $qb->expr()->eq("m.file_id", "f.fileid"))
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('m.type', $qb->createNamedParameter(self::METADATA_TYPE)))
->andWhere($qb->expr()->eq('m.value', $qb->createNamedParameter($locationId)))
->executeQuery();
$rows = $qb->select('file.fileid', 'file.mimetype', 'file.size', 'file.mtime', 'file.etag', 'meta.value')
->from('mounts', 'mount')
->join('mount', 'filecache', 'file', $qb->expr()->eq('file.storage', 'mount.storage_id'))
->join('file', 'file_metadata', 'meta', $qb->expr()->eq('file.file_id', 'meta.id'))
->andWhere($qb->expr()->eq('mount.user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('file.mimepart', $qb->createNamedParameter(5, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('meta.group_name', $qb->createNamedParameter(self::METADATA_TYPE)))
->andWhere($qb->expr()->eq('meta.metadata', $qb->createNamedParameter($locationId)))
->executeQuery()
->fetchAll();

return array_map(
fn ($row) => new LocationFile(
Expand All @@ -74,21 +79,20 @@ public function findFilesForUserAndLocation(string $userId, int $locationId) {
(int)$row['size'],
(int)$row['mtime'],
$row['etag'],
(int)$row['value']
(int)$row['metadata']
),
$rows->fetchAll(),
$rows,
);
}

public function addLocationForFileAndUser(int $locationId, int $fileId, string $userId): void {
public function addLocationForFileAndUser(int $locationId, int $fileId): void {
try {
$query = $this->connection->getQueryBuilder();
$query->insert(self::TABLE_NAME)
$query->insert('file_metadata')
->values([
"user_id" => $query->createNamedParameter($userId),
"type" => $query->createNamedParameter(self::METADATA_TYPE),
"value" => $query->createNamedParameter($locationId),
"file_id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
"id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
"group_name" => $query->createNamedParameter(self::METADATA_TYPE),
"metadata" => $query->createNamedParameter($locationId),
])
->executeStatement();
} catch (\Exception $ex) {
Expand All @@ -100,31 +104,10 @@ public function addLocationForFileAndUser(int $locationId, int $fileId, string $

public function updateLocationForFile(int $locationId, int $fileId): void {
$query = $this->connection->getQueryBuilder();
$query->update(self::TABLE_NAME)
->set("value", $query->createNamedParameter($locationId))
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('type', $query->createNamedParameter(self::METADATA_TYPE)))
->executeStatement();
}

public function removeLocationForFile(int $fileId, ?string $userId = null): void {
$query = $this->connection->getQueryBuilder();
$query->delete(self::TABLE_NAME)
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('type', $query->createNamedParameter(self::METADATA_TYPE)));

if ($userId !== null) {
$query->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
}

$query->executeStatement();
}

public function removeLocationForUser(string $userId): void {
$query = $this->connection->getQueryBuilder();
$query->delete(self::TABLE_NAME)
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->eq('type', $query->createNamedParameter(self::METADATA_TYPE)))
$query->update('file_metadata')
->set("metadata", $query->createNamedParameter($locationId))
->where($query->expr()->eq('id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq('group_name', $query->createNamedParameter(self::METADATA_TYPE)))
->executeStatement();
}
}
10 changes: 0 additions & 10 deletions lib/Listener/LocationManagerEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ public function handle(Event $event): void {
return;
}

if ($event instanceof NodeDeletedEvent) {
if ($this->isCorrectPath($event->getNode()->getPath())) {
$this->mediaLocationManager->clearLocationForFile($event->getNode()->getId());
}
}

if ($event instanceof NodeWrittenEvent) {
if (!$this->isCorrectPath($event->getNode()->getPath())) {
return;
Expand All @@ -71,10 +65,6 @@ public function handle(Event $event): void {

$this->jobList->add(MapMediaToLocationJob::class, [$fileId, $ownerId]);
}

if ($event instanceof UserDeletedEvent) {
$this->mediaLocationManager->clearLocationForUser($event->getUser()->getUID());
}
}

private function isCorrectPath(string $path): bool {
Expand Down
84 changes: 0 additions & 84 deletions lib/Migration/Version20002Date20221012131022.php

This file was deleted.

0 comments on commit 520924d

Please sign in to comment.