Skip to content

Commit

Permalink
Add DB mapping files
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Oct 13, 2022
1 parent ff6e5d5 commit 6bb4679
Show file tree
Hide file tree
Showing 19 changed files with 835 additions and 137 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>Photos</name>
<summary>Your memories under your control</summary>
<description>Your memories under your control</description>
<version>1.8.0</version>
<version>1.9.0</version>
<licence>agpl</licence>
<author mail="skjnldsv@protonmail.com">John Molakvoæ</author>
<namespace>Photos</namespace>
Expand Down
3 changes: 2 additions & 1 deletion lib/Album/AlbumFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
namespace OCA\Photos\Album;

use OC\Metadata\FileMetadata;
use OCA\Photos\DB\PhotosFile;

class AlbumFile {
class AlbumFile extends PhotosFile{
private int $fileId;
private string $name;
private string $mimeType;
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/DownloadReverseGeocodingFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function configure() {
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
try {
$this->rgcService->initCities1000KdTree(true);
$this->rgcService->createCities1000KdTree(true);
} catch (\Exception $ex) {
$output->writeln('<error>Failed to update reverse geocoding files</error>');
$output->writeln($ex->getMessage());
Expand Down
14 changes: 5 additions & 9 deletions lib/Command/ReverseGeoCodeMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,30 @@
use OCP\Files\IRootFolder;
use OCP\Files\Folder;
use OCP\BackgroundJob\IJobList;
use OCA\Photos\Service\ReverseGeoCoderService;
use OCA\Photos\Service\LocationTagService;
use OCA\Photos\Service\MediaLocationManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ReverseGeoCodeMedia extends Command {
private ReverseGeoCoderService $rgcService;
private IRootFolder $rootFolder;
private LocationTagService $locationTagService;
private MediaLocationManager $mediaLocationManager;
private IConfig $config;
private IUserManager $userManager;

public function __construct(
ReverseGeoCoderService $rgcService,
IJobList $jobList,
IRootFolder $rootFolder,
LocationTagService $locationTagService,
MediaLocationManager $mediaLocationManager,
IConfig $config,
IUserManager $userManager
) {
parent::__construct();
$this->rgcService = $rgcService;
$this->config = $config;
$this->jobList = $jobList;
$this->rootFolder = $rootFolder;
$this->locationTagService = $locationTagService;
$this->mediaLocationManager = $mediaLocationManager;
$this->userManager = $userManager;
}

Expand Down Expand Up @@ -120,7 +116,7 @@ private function scanFolder(Folder $folder) {
continue;
}

$this->locationTagService->tag($node->getId());
$this->mediaLocationManager->addLocationForFileAndUser($node->getOwner()->getUID(), $node->getId());
}
}
}
57 changes: 57 additions & 0 deletions lib/DB/Location/LocationFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me>
*
* @author Louis Chemineau <louis@chmn.me>
*
* @license AGPL-3.0-or-later
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\DB\Location;

use OCA\Photos\DB\PhotosFile;

class LocationFile extends PhotosFile {
private int $locationId;

public function __construct(
int $fileId,
string $name,
string $mimeType,
int $size,
int $mtime,
string $etag,
int $locationId,
) {
parent::__construct(
$fileId,
$name,
$mimeType,
$size,
$mtime,
$etag
);

$this->locationId = $locationId;
}

public function getLocationId(): int {
return $this->locationId;
}
}
47 changes: 47 additions & 0 deletions lib/DB/Location/LocationInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me>
*
* @author Louis Chemineau <louis@chmn.me>
*
* @license AGPL-3.0-or-later
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\DB\Location;

class LocationInfo {
private string $userId;
private int $locationId;

public function __construct(
string $userId,
int $locationId,
) {
$this->userId = $userId;
$this->locationId = $locationId;
}

public function getUserId(): string {
return $this->userId;
}

public function getLocationId(): int {
return $this->locationId;
}
}
109 changes: 109 additions & 0 deletions lib/DB/Location/LocationMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me>
*
* @author Louis Chemineau <louis@chmn.me>
*
* @license AGPL-3.0-or-later
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\DB\Location;

use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

class LocationMapper {
const PHOTOS_LOCATION = 'photos_locations';

private IDBConnection $connection;

public function __construct(
IDBConnection $connection,
) {
$this->connection = $connection;
}

/** @return array<string, LocationInfo> */
public function findLocationForUser(string $userId): array {
$qb = $this->connection->getQueryBuilder();

$rows = $qb->selectDistinct('location_id')
->from(self::PHOTOS_LOCATION)
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->executeQuery()
->fetchAll();

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

/** @return array<string, LocationFiles> */
public function findFilesForUserAndLocation(string $userId, int $locationId) {
$qb = $this->db->getQueryBuilder();

$rows = $qb->select("fileid", "name", "mimetype", "size", "mtime", "etag", "location_id")
->from(self::PHOTOS_LOCATION, 'l')
->leftJoin("p", "filecache", "f", $qb->expr()->eq("l.file_id", "f.fileid"))
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
->andWhere($qb->expr()->eq('location_id', $qb->createNamedParameter($locationId, IQueryBuilder::PARAM_INT)));

return array_map(
fn ($row) => new LocationFile(
(int)$row['fileid'],
$row['name'],
$this->mimeTypeLoader->getMimetypeById($row['mimetype']),
(int)$row['size'],
(int)$row['mtime'],
$row['etag'],
(int)$row['location_id']
),
$rows
);
}

public function addFile(string $userId, int $locationId, int $fileId): void {
try {
$query = $this->connection->getQueryBuilder();
$query->insert(self::PHOTOS_LOCATION)
->values([
"user_id" => $query->createNamedParameter($userId),
"location_id" => $query->createNamedParameter($locationId, IQueryBuilder::PARAM_INT),
"file_id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
])
->executeStatement();
} catch (UniqueConstraintViolationException $ex) {
$this->updateFileLocationForUser($userId, $locationId, $fileId);
}
}

public function updateFileLocation(int $locationId, int $fileId): void {
$query = $this->connection->getQueryBuilder();
$query->update(self::PHOTOS_LOCATION)
->set("location_id", $query->createNamedParameter($locationId, IQueryBuilder::PARAM_INT))
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->executeStatement();
}

public function removeFileLocation(int $fileId): void {
$query = $this->connection->getQueryBuilder();
$query->delete(self::PHOTOS_LOCATION)
->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->executeStatement();
}
}
91 changes: 91 additions & 0 deletions lib/DB/PhotosFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2022 Louis Chemineau <louis@chmn.me>
*
* @author Louis Chemineau <louis@chmn.me>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Photos\DB;

use OC\Metadata\FileMetadata;

class PhotosFile {
private int $fileId;
private string $name;
private string $mimeType;
private int $size;
private int $mtime;
private string $etag;
/** @var array<string, FileMetadata> */
private array $metaData = [];

public function __construct(
int $fileId,
string $name,
string $mimeType,
int $size,
int $mtime,
string $etag,
) {
$this->fileId = $fileId;
$this->name = $name;
$this->mimeType = $mimeType;
$this->size = $size;
$this->mtime = $mtime;
$this->etag = $etag;
}

public function getFileId(): int {
return $this->fileId;
}

public function getName(): string {
return $this->name;
}

public function getMimeType(): string {
return $this->mimeType;
}

public function getSize(): int {
return $this->size;
}

public function getMTime(): int {
return $this->mtime;
}

public function getEtag(): string {
return $this->etag;
}

public function setMetadata(string $key, FileMetadata $value): void {
$this->metaData[$key] = $value;
}

public function hasMetadata(string $key): bool {
return isset($this->metaData[$key]);
}

public function getMetadata(string $key): FileMetadata {
return $this->metaData[$key];
}
}
Loading

0 comments on commit 6bb4679

Please sign in to comment.