Skip to content
Prev Previous commit
Next Next commit
store album location and last added photo
Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
icewind1991 authored and artonge committed Aug 22, 2022
commit f4dea80783c83b4d733ba4f3597e2977bcc3ddbe
9 changes: 8 additions & 1 deletion lib/Album/AlbumFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -41,14 +42,16 @@ public function __construct(
string $mimeType,
int $size,
int $mtime,
string $etag
string $etag,
int $added
) {
$this->fileId = $fileId;
$this->name = $name;
$this->mimeType = $mimeType;
$this->size = $size;
$this->mtime = $mtime;
$this->etag = $etag;
$this->added = $added;
}

public function getFileId(): int {
Expand Down Expand Up @@ -86,4 +89,8 @@ public function hasMetadata(string $key): bool {
public function getMetadata(string $key): FileMetadata {
return $this->metaData[$key];
}

public function getAdded(): int {
return $this->added;
}
}
27 changes: 26 additions & 1 deletion lib/Album/AlbumInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -45,4 +58,16 @@ public function getUserId(): string {
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;
}
}
66 changes: 55 additions & 11 deletions lib/Album/AlbumMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,47 @@

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;

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;
}
Expand All @@ -70,12 +77,12 @@ public function get(int $id): ?AlbumInfo {
*/
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);
}

Expand All @@ -87,6 +94,14 @@ public function rename(int $id, string $newName): void {
$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();
Expand All @@ -108,7 +123,7 @@ public function delete(int $id): void {
*/
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")
Expand All @@ -124,11 +139,11 @@ public function getForUserWithFiles(string $userId): array {
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']);
}
}

Expand All @@ -140,17 +155,25 @@ public function getForUserWithFiles(string $userId): array {
}

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 {
Expand All @@ -159,5 +182,26 @@ public function removeFile(int $albumId, int $fileId): void {
->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;
}
}
}
13 changes: 13 additions & 0 deletions lib/Migration/Version20000Date20220727125801.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'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');
}
Expand All @@ -72,6 +82,9 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'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');
Expand Down
Loading