Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/photos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRobin Appelman <robin@icewind.nl>2022-08-12 17:13:10 +0300
committerLouis Chemineau <louis@chmn.me>2022-08-22 21:03:52 +0300
commitf4dea80783c83b4d733ba4f3597e2977bcc3ddbe (patch)
tree2bde84e9073a936c8197db31179916028751a798 /lib
parentfe215a85754650cd2c085fe6ea813ce5365b6f0c (diff)
store album location and last added photo
Signed-off-by: Louis Chemineau <louis@chmn.me>
Diffstat (limited to 'lib')
-rw-r--r--lib/Album/AlbumFile.php9
-rw-r--r--lib/Album/AlbumInfo.php27
-rw-r--r--lib/Album/AlbumMapper.php66
-rw-r--r--lib/Migration/Version20000Date20220727125801.php13
4 files changed, 102 insertions, 13 deletions
diff --git a/lib/Album/AlbumFile.php b/lib/Album/AlbumFile.php
index babbddb0..86863e09 100644
--- a/lib/Album/AlbumFile.php
+++ b/lib/Album/AlbumFile.php
@@ -32,6 +32,7 @@ class AlbumFile {
private int $size;
private int $mtime;
private string $etag;
+ private int $added;
/** @var array<int, FileMetadata> */
private array $metaData = [];
@@ -41,7 +42,8 @@ class AlbumFile {
string $mimeType,
int $size,
int $mtime,
- string $etag
+ string $etag,
+ int $added
) {
$this->fileId = $fileId;
$this->name = $name;
@@ -49,6 +51,7 @@ class AlbumFile {
$this->size = $size;
$this->mtime = $mtime;
$this->etag = $etag;
+ $this->added = $added;
}
public function getFileId(): int {
@@ -86,4 +89,8 @@ class AlbumFile {
public function getMetadata(string $key): FileMetadata {
return $this->metaData[$key];
}
+
+ public function getAdded(): int {
+ return $this->added;
+ }
}
diff --git a/lib/Album/AlbumInfo.php b/lib/Album/AlbumInfo.php
index ced62ea0..315afde0 100644
--- a/lib/Album/AlbumInfo.php
+++ b/lib/Album/AlbumInfo.php
@@ -27,11 +27,24 @@ class AlbumInfo {
private int $id;
private string $userId;
private string $title;
+ private string $location;
+ private int $created;
+ private int $lastAdded;
- public function __construct(int $id, string $userId, string $title) {
+ public function __construct(
+ int $id,
+ string $userId,
+ string $title,
+ string $location,
+ int $created,
+ int $lastAdded
+ ) {
$this->id = $id;
$this->userId = $userId;
$this->title = $title;
+ $this->location = $location;
+ $this->created = $created;
+ $this->lastAdded = $lastAdded;
}
public function getId(): int {
@@ -45,4 +58,16 @@ class AlbumInfo {
public function getTitle(): string {
return $this->title;
}
+
+ public function getLocation(): string {
+ return $this->location;
+ }
+
+ public function getCreated(): int {
+ return $this->created;
+ }
+
+ public function getLastAddedPhoto(): int {
+ return $this->lastAdded;
+ }
}
diff --git a/lib/Album/AlbumMapper.php b/lib/Album/AlbumMapper.php
index b88c3731..f06f54d3 100644
--- a/lib/Album/AlbumMapper.php
+++ b/lib/Album/AlbumMapper.php
@@ -25,6 +25,7 @@ namespace OCA\Photos\Album;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCA\Photos\Exception\AlreadyInAlbumException;
+use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\IMimeTypeLoader;
use OCP\IDBConnection;
@@ -32,33 +33,39 @@ use OCP\IDBConnection;
class AlbumMapper {
private IDBConnection $connection;
private IMimeTypeLoader $mimeTypeLoader;
+ private ITimeFactory $timeFactory;
- public function __construct(IDBConnection $connection, IMimeTypeLoader $mimeTypeLoader) {
+ public function __construct(IDBConnection $connection, IMimeTypeLoader $mimeTypeLoader, ITimeFactory $timeFactory) {
$this->connection = $connection;
$this->mimeTypeLoader = $mimeTypeLoader;
+ $this->timeFactory = $timeFactory;
}
- public function create(string $userId, string $name): AlbumInfo {
+ public function create(string $userId, string $name, string $location = ""): AlbumInfo {
+ $created = $this->timeFactory->getTime();
$query = $this->connection->getQueryBuilder();
$query->insert("photos_albums")
->values([
'user' => $query->createNamedParameter($userId),
'name' => $query->createNamedParameter($name),
+ 'location' => $query->createNamedParameter($location),
+ 'created' => $query->createNamedParameter($created, IQueryBuilder::PARAM_INT),
+ 'last_added_photo' => $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT),
]);
$query->executeStatement();
$id = $query->getLastInsertId();
- return new AlbumInfo($id, $userId, $name);
+ return new AlbumInfo($id, $userId, $name, $location, $created, -1);
}
public function get(int $id): ?AlbumInfo {
$query = $this->connection->getQueryBuilder();
- $query->select("name", "user")
+ $query->select("name", "user", "location", "created", "last_added_photo")
->from("photos_albums")
->where($query->expr()->eq('album_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$row = $query->executeQuery()->fetch();
if ($row) {
- return new AlbumInfo($id, $row['user'], $row['name']);
+ return new AlbumInfo($id, $row['user'], $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']);
} else {
return null;
}
@@ -70,12 +77,12 @@ class AlbumMapper {
*/
public function getForUser(string $userId): array {
$query = $this->connection->getQueryBuilder();
- $query->select("album_id", "name")
+ $query->select("album_id", "name", "location", "created", "last_added_photo")
->from("photos_albums")
->where($query->expr()->eq('user', $query->createNamedParameter($userId)));
$rows = $query->executeQuery()->fetchAll();
return array_map(function (array $row) use ($userId) {
- return new AlbumInfo((int)$row['album_id'], $userId, $row['name']);
+ return new AlbumInfo((int)$row['album_id'], $userId, $row['name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']);
}, $rows);
}
@@ -87,6 +94,14 @@ class AlbumMapper {
$query->executeStatement();
}
+ public function setLocation(int $id, string $newLocation): void {
+ $query = $this->connection->getQueryBuilder();
+ $query->update("photos_albums")
+ ->set("location", $query->createNamedParameter($newLocation))
+ ->where($query->expr()->eq('album_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
+ $query->executeStatement();
+ }
+
public function delete(int $id): void {
$this->connection->beginTransaction();
$query = $this->connection->getQueryBuilder();
@@ -108,7 +123,7 @@ class AlbumMapper {
*/
public function getForUserWithFiles(string $userId): array {
$query = $this->connection->getQueryBuilder();
- $query->select("fileid", "mimetype", "a.album_id", "size", "mtime", "etag")
+ $query->select("fileid", "mimetype", "a.album_id", "size", "mtime", "etag", "location", "created", "last_added_photo", "added")
->selectAlias("f.name", "file_name")
->selectAlias("a.name", "album_name")
->from("photos_albums", "a")
@@ -124,11 +139,11 @@ class AlbumMapper {
if ($row['fileid']) {
$mimeId = $row['mimetype'];
$mimeType = $this->mimeTypeLoader->getMimetypeById($mimeId);
- $filesByAlbum[$albumId][] = new AlbumFile((int)$row['fileid'], $row['file_name'], $mimeType, (int)$row['size'], (int)$row['mtime'], $row['etag']);
+ $filesByAlbum[$albumId][] = new AlbumFile((int)$row['fileid'], $row['file_name'], $mimeType, (int)$row['size'], (int)$row['mtime'], $row['etag'], (int)$row['added']);
}
if (!isset($albumsById[$albumId])) {
- $albumsById[$albumId] = new AlbumInfo($albumId, $userId, $row['album_name']);
+ $albumsById[$albumId] = new AlbumInfo($albumId, $userId, $row['album_name'], $row['location'], (int)$row['created'], (int)$row['last_added_photo']);
}
}
@@ -140,17 +155,25 @@ class AlbumMapper {
}
public function addFile(int $albumId, int $fileId): void {
+ $added = $this->timeFactory->getTime();
try {
$query = $this->connection->getQueryBuilder();
$query->insert("photos_albums_files")
->values([
"album_id" => $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT),
- "file_id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)
+ "file_id" => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
+ "added" => $query->createNamedParameter($added, IQueryBuilder::PARAM_INT),
]);
$query->executeStatement();
} catch (UniqueConstraintViolationException $e) {
throw new AlreadyInAlbumException("File already in album", 0, $e);
}
+
+ $query = $this->connection->getQueryBuilder();
+ $query->update("photos_albums")
+ ->set('last_added_photo', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))
+ ->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)));
+ $query->executeStatement();
}
public function removeFile(int $albumId, int $fileId): void {
@@ -159,5 +182,26 @@ class AlbumMapper {
->where($query->expr()->eq("album_id", $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->eq("file_id", $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
$query->executeStatement();
+
+ $query = $this->connection->getQueryBuilder();
+ $query->update("photos_albums")
+ ->set('last_added_photo', $query->createNamedParameter($this->getLastAdded($albumId), IQueryBuilder::PARAM_INT))
+ ->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)));
+ $query->executeStatement();
+ }
+
+ private function getLastAdded(int $albumId): int {
+ $query = $this->connection->getQueryBuilder();
+ $query->select("file_id")
+ ->from("photos_albums_files")
+ ->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT)))
+ ->orderBy("added", "DESC")
+ ->setMaxResults(1);
+ $id = $query->executeQuery()->fetchOne();
+ if ($id === false) {
+ return -1;
+ } else {
+ return (int)$id;
+ }
}
}
diff --git a/lib/Migration/Version20000Date20220727125801.php b/lib/Migration/Version20000Date20220727125801.php
index 8f2ecbf7..81284e35 100644
--- a/lib/Migration/Version20000Date20220727125801.php
+++ b/lib/Migration/Version20000Date20220727125801.php
@@ -53,6 +53,16 @@ class Version20000Date20220727125801 extends SimpleMigrationStep {
'notnull' => true,
'length' => 255,
]);
+ $table->addColumn('created', 'bigint', [
+ 'notnull' => true,
+ ]);
+ $table->addColumn('location', 'string', [
+ 'notnull' => true,
+ 'length' => 255,
+ ]);
+ $table->addColumn('last_added_photo', 'bigint', [
+ 'notnull' => true,
+ ]);
$table->setPrimaryKey(['album_id']);
$table->addIndex(['user'], 'pa_user');
}
@@ -72,6 +82,9 @@ class Version20000Date20220727125801 extends SimpleMigrationStep {
'notnull' => true,
'length' => 20,
]);
+ $table->addColumn('added', 'bigint', [
+ 'notnull' => true,
+ ]);
$table->setPrimaryKey(['album_file_id']);
$table->addIndex(['album_id'], 'paf_folder');
$table->addUniqueIndex(['album_id', 'file_id'], 'paf_album_file');