From 520924d0a7f95ca5b8a74f73af9fa57570c38fb8 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Mon, 13 Feb 2023 17:13:01 +0100 Subject: [PATCH] Use oc_metadata to store location Signed-off-by: Louis Chemineau --- lib/DB/Location/LocationMapper.php | 77 +++++++---------- lib/Listener/LocationManagerEventListener.php | 10 --- .../Version20002Date20221012131022.php | 84 ------------------- 3 files changed, 30 insertions(+), 141 deletions(-) delete mode 100644 lib/Migration/Version20002Date20221012131022.php diff --git a/lib/DB/Location/LocationMapper.php b/lib/DB/Location/LocationMapper.php index eb7e628bfb..b213a51205 100644 --- a/lib/DB/Location/LocationMapper.php +++ b/lib/DB/Location/LocationMapper.php @@ -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, @@ -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( @@ -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) { @@ -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(); } } diff --git a/lib/Listener/LocationManagerEventListener.php b/lib/Listener/LocationManagerEventListener.php index 41047e7933..65ea476bf5 100644 --- a/lib/Listener/LocationManagerEventListener.php +++ b/lib/Listener/LocationManagerEventListener.php @@ -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; @@ -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 { diff --git a/lib/Migration/Version20002Date20221012131022.php b/lib/Migration/Version20002Date20221012131022.php deleted file mode 100644 index 83352e6adc..0000000000 --- a/lib/Migration/Version20002Date20221012131022.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * @author Your name - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - */ - -namespace OCA\Photos\Migration; - -use Closure; -use OCP\DB\ISchemaWrapper; -use OCP\DB\Types; -use OCP\Migration\IOutput; -use OCP\Migration\SimpleMigrationStep; - -/** - * Auto-generated migration step: Please modify to your needs! - */ -class Version20002Date20221012131022 extends SimpleMigrationStep { - /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options - * @return null|ISchemaWrapper - */ - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { - /** @var ISchemaWrapper $schema */ - $schema = $schemaClosure(); - $modified = false; - - if (!$schema->hasTable("photos_metadata")) { - $modified = true; - $table = $schema->createTable("photos_metadata"); - $table->addColumn('id', Types::BIGINT, [ - 'autoincrement' => true, - 'notnull' => true, - 'length' => 20, - ]); - $table->addColumn('user_id', Types::STRING, [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('file_id', Types::BIGINT, [ - 'notnull' => true, - ]); - $table->addColumn('type', Types::STRING, [ - 'notnull' => true, - 'length' => 64, - ]); - $table->addColumn('value', Types::STRING, [ - 'notnull' => true, - 'length' => 64, - ]); - - $table->setPrimaryKey(['id']); - $table->addUniqueConstraint(['user_id', 'file_id', 'type'], 'photos_metadata_unique_idx'); - } - - if ($modified) { - return $schema; - } else { - return null; - } - } -}