|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* @copyright Copyright (c) 2022 Robin Appelman <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\FaceRecognition\Album; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
27
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
28
|
|
|
use OCP\Files\IMimeTypeLoader; |
|
29
|
|
|
use OCP\IDBConnection; |
|
30
|
|
|
use OCP\IGroup; |
|
31
|
|
|
use OCP\IUser; |
|
32
|
|
|
use OCP\IUserManager; |
|
33
|
|
|
use OCP\IGroupManager; |
|
34
|
|
|
|
|
35
|
|
|
class AlbumMapper { |
|
36
|
|
|
|
|
37
|
|
|
private IDBConnection $connection; |
|
38
|
|
|
private ITimeFactory $timeFactory; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(IDBConnection $connection, |
|
41
|
|
|
ITimeFactory $timeFactory |
|
42
|
|
|
) { |
|
43
|
|
|
$this->connection = $connection; |
|
44
|
|
|
$this->timeFactory = $timeFactory; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function create(string $userId, string $name, string $location = 'Face Recognition'): int { |
|
48
|
|
|
$created = $this->timeFactory->getTime(); |
|
49
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
50
|
|
|
$query->insert("photos_albums") |
|
51
|
|
|
->values([ |
|
52
|
|
|
'user' => $query->createNamedParameter($userId), |
|
53
|
|
|
'name' => $query->createNamedParameter($name), |
|
54
|
|
|
'location' => $query->createNamedParameter($location), |
|
55
|
|
|
'created' => $query->createNamedParameter($created, IQueryBuilder::PARAM_INT), |
|
56
|
|
|
'last_added_photo' => $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT), |
|
57
|
|
|
]); |
|
58
|
|
|
$query->executeStatement(); |
|
59
|
|
|
|
|
60
|
|
|
return $query->getLastInsertId(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function get(string $userId, string $name, string $location = 'Face Recognition'): int { |
|
64
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
65
|
|
|
$qb->select('album_id') |
|
66
|
|
|
->from('photos_albums') |
|
67
|
|
|
->where($qb->expr()->eq('name', $qb->createNamedParameter($name))) |
|
68
|
|
|
->andWhere($qb->expr()->eq('location', $qb->createNamedParameter($location))) |
|
69
|
|
|
->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId))); |
|
70
|
|
|
|
|
71
|
|
|
$id = $qb->executeQuery()->fetchOne(); |
|
72
|
|
|
if ($id === false) { |
|
73
|
|
|
return -1; |
|
74
|
|
|
} else { |
|
75
|
|
|
return (int)$id; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getAll(string $userId, string $location = 'Face Recognition'): array { |
|
80
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
81
|
|
|
$qb->select('name') |
|
82
|
|
|
->from('photos_albums') |
|
83
|
|
|
->where($qb->expr()->eq('location', $qb->createNamedParameter($location))) |
|
84
|
|
|
->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId))); |
|
85
|
|
|
$rows = $qb->executeQuery()->fetchAll(); |
|
86
|
|
|
|
|
87
|
|
|
$result = []; |
|
88
|
|
|
foreach ($rows as $row) { |
|
89
|
|
|
$result[] = (string)$row['name']; |
|
90
|
|
|
} |
|
91
|
|
|
return $result; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function delete(int $albumId): void { |
|
95
|
|
|
$this->connection->beginTransaction(); |
|
96
|
|
|
|
|
97
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
98
|
|
|
$query->delete("photos_albums") |
|
99
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))); |
|
100
|
|
|
$query->executeStatement(); |
|
101
|
|
|
|
|
102
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
103
|
|
|
$query->delete("photos_albums_files") |
|
104
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))); |
|
105
|
|
|
$query->executeStatement(); |
|
106
|
|
|
|
|
107
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
108
|
|
|
$query->delete("photos_albums_collabs") |
|
109
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))); |
|
110
|
|
|
$query->executeStatement(); |
|
111
|
|
|
|
|
112
|
|
|
$this->connection->commit(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param int $albumId |
|
117
|
|
|
* @return int[] |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getFiles(int $albumId): array { |
|
120
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
121
|
|
|
$query->select('file_id') |
|
122
|
|
|
->from('photos_albums_files') |
|
123
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId))); |
|
124
|
|
|
$rows = $query->executeQuery()->fetchAll(); |
|
125
|
|
|
|
|
126
|
|
|
$result = []; |
|
127
|
|
|
foreach ($rows as $row) { |
|
128
|
|
|
$result[] = (int)$row['file_id']; |
|
129
|
|
|
} |
|
130
|
|
|
return $result; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function addFile(int $albumId, int $fileId, string $owner): void { |
|
134
|
|
|
$added = $this->timeFactory->getTime(); |
|
135
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
136
|
|
|
$query->insert('photos_albums_files') |
|
137
|
|
|
->values([ |
|
138
|
|
|
'album_id' => $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT), |
|
139
|
|
|
'file_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT), |
|
140
|
|
|
'added' => $query->createNamedParameter($added, IQueryBuilder::PARAM_INT), |
|
141
|
|
|
'owner' => $query->createNamedParameter($owner), |
|
142
|
|
|
]); |
|
143
|
|
|
$query->executeStatement(); |
|
144
|
|
|
|
|
145
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
146
|
|
|
$query->update('photos_albums') |
|
147
|
|
|
->set('last_added_photo', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)) |
|
148
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))); |
|
149
|
|
|
$query->executeStatement(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function removeFile(int $albumId, int $fileId): void { |
|
153
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
154
|
|
|
$query->delete('photos_albums_files') |
|
155
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))) |
|
156
|
|
|
->andWhere($query->expr()->eq('file_id', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
157
|
|
|
$query->executeStatement(); |
|
158
|
|
|
|
|
159
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
160
|
|
|
$query->update('photos_albums') |
|
161
|
|
|
->set('last_added_photo', $query->createNamedParameter($this->getLastAdded($albumId), IQueryBuilder::PARAM_INT)) |
|
162
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))); |
|
163
|
|
|
$query->executeStatement(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
private function getLastAdded(int $albumId): int { |
|
167
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
168
|
|
|
$query->select('file_id') |
|
169
|
|
|
->from('photos_albums_files') |
|
170
|
|
|
->where($query->expr()->eq('album_id', $query->createNamedParameter($albumId, IQueryBuilder::PARAM_INT))) |
|
171
|
|
|
->orderBy('added', 'DESC') |
|
172
|
|
|
->setMaxResults(1); |
|
173
|
|
|
$id = $query->executeQuery()->fetchOne(); |
|
174
|
|
|
if ($id === false) { |
|
175
|
|
|
return -1; |
|
176
|
|
|
} else { |
|
177
|
|
|
return (int)$id; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|