-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Louis Chemineau <louis@chmn.me>
- Loading branch information
Showing
19 changed files
with
835 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
Oops, something went wrong.