-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathTimelineWrite.php
323 lines (275 loc) · 11.8 KB
/
TimelineWrite.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
declare(strict_types=1);
namespace OCA\Memories\Db;
use OCA\Memories\Exif;
use OCA\Memories\Service\Index;
use OCA\Memories\Util;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\File;
use OCP\IDBConnection;
use OCP\Lock\ILockingProvider;
use Psr\Log\LoggerInterface;
const DELETE_TABLES = ['memories', 'memories_livephoto', 'memories_places', 'memories_failures'];
const TRUNCATE_TABLES = ['memories_mapclusters'];
class TimelineWrite
{
use TimelineWriteFailures;
use TimelineWriteMap;
use TimelineWriteOrphans;
use TimelineWritePlaces;
use TimelineWriteTags;
public function __construct(
protected IDBConnection $connection,
protected LivePhoto $livePhoto,
protected ILockingProvider $lockingProvider,
protected LoggerInterface $logger,
) {}
/**
* Process a file to insert Exif data into the database.
*
* @param File $file File node to process
* @param bool $lock Lock the file before processing
* @param bool $force Update the record even if the file has not changed
*
* @return bool True if the file was processed
*
* @throws \OCP\Lock\LockedException If the file is locked
* @throws \OCP\DB\Exception If the database query fails
*/
public function processFile(
File $file,
bool $lock = true,
bool $force = false,
): bool {
// Check if we want to process this file
// https://github.com/pulsejet/memories/issues/933 (zero-byte files)
if ($file->getSize() <= 0 || !Index::isSupported($file) || !Index::isPathAllowed($file->getPath())) {
return false;
}
// Check if we need to lock the file
if ($lock) {
$lockKey = 'memories/'.$file->getId();
$lockType = ILockingProvider::LOCK_EXCLUSIVE;
// Throw directly to caller if we can't get the lock
// This way we don't release someone else's lock
$this->lockingProvider->acquireLock($lockKey, $lockType);
try {
return $this->processFile($file, false, $force);
} finally {
$this->lockingProvider->releaseLock($lockKey, $lockType);
}
}
// Get parameters
$mtime = $file->getMtime();
$fileId = $file->getId();
$isvideo = Index::isVideo($file);
// Get previous row
$prevRow = $this->getCurrentRow($fileId);
// Skip if all of the following:
// - not forced
// - the record exists
// - the file has not changed
// - the record is not an orphan
if (!$force && $prevRow && ((int) $prevRow['mtime'] === $mtime) && (!(bool) $prevRow['orphan'])) {
return false;
}
// Get exif data
$exif = Exif::getExifFromFile($file);
// Check if EXIF is blank, which is probably wrong
if (0 === \count($exif)) {
throw new \Exception('No EXIF data could be read');
}
// Check if MIMEType was not detected
if (empty($exif['MIMEType'] ?? null)) {
throw new \Exception('No MIMEType in EXIF data');
}
// Hand off if Live Photo video part
if ($isvideo && $this->livePhoto->isVideoPart($exif)) {
return Util::transaction(fn () => $this->livePhoto->processVideoPart($file, $exif));
}
if (!empty($exif['TagsList'])){
Util::transaction(fn () => $this->processTags($file, $exif));
}
// If control reaches here, it's not a Live Photo video part
// But if prevRow exists and dayid is not set, it *was* a live video part
// In this case delete that entry (very rare edge case)
if ($prevRow && !\array_key_exists('dayid', $prevRow)) {
$this->livePhoto->deleteVideoPart($file);
$prevRow = null;
}
// Video parameters
$videoDuration = round((float) ($isvideo ? ($exif['Duration'] ?? $exif['TrackDuration'] ?? 0) : 0));
// Process location data
// This also modifies the exif array in-place to set the LocationTZID
// and drop the GPS data if it is not valid
[$lat, $lon, $mapCluster] = $this->processExifLocation($fileId, $exif, $prevRow);
// Get date parameters (after setting timezone offset)
$dateTaken = Exif::getDateTaken($file, $exif);
// Store the acutal epoch with the EXIF data
$epoch = $exif['DateTimeEpoch'] = $dateTaken->getTimestamp();
// Store the date taken in the database as UTC (local date) only
// Basically, assume everything happens in Greenwich
$dateLocalUtc = Exif::forgetTimezone($dateTaken)->getTimestamp();
$dateTakenStr = gmdate('Y-m-d H:i:s', $dateLocalUtc);
// We need to use the local time in UTC for the dayId
// This way two photos in different timezones on the same date locally
// end up in the same dayId group
$dayId = floor($dateLocalUtc / 86400);
// Get size of image
[$w, $h] = Exif::getDimensions($exif);
// Get live photo ID of video part
$liveid = $this->livePhoto->getLivePhotoId($file, $exif);
// Get BUID from ImageUniqueId if not present
$buid = $prevRow ? $prevRow['buid'] : '';
if (empty($buid)) {
$buid = Exif::getBUID($file->getName(), $exif['ImageUniqueID'] ?? null, (int) $file->getSize());
}
// Get exif json
$exifJson = $this->getExifJson($exif);
// Parameters for insert or update
$query = $this->connection->getQueryBuilder();
$params = [
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
'objectid' => $query->createNamedParameter((string) $fileId, IQueryBuilder::PARAM_STR),
'dayid' => $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT),
'datetaken' => $query->createNamedParameter($dateTakenStr, IQueryBuilder::PARAM_STR),
'epoch' => $query->createNamedParameter($epoch, IQueryBuilder::PARAM_INT),
'mtime' => $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT),
'isvideo' => $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT),
'video_duration' => $query->createNamedParameter($videoDuration, IQueryBuilder::PARAM_INT),
'w' => $query->createNamedParameter($w, IQueryBuilder::PARAM_INT),
'h' => $query->createNamedParameter($h, IQueryBuilder::PARAM_INT),
'exif' => $query->createNamedParameter($exifJson, IQueryBuilder::PARAM_STR),
'liveid' => $query->createNamedParameter($liveid, IQueryBuilder::PARAM_STR),
'lat' => $query->createNamedParameter($lat, IQueryBuilder::PARAM_STR),
'lon' => $query->createNamedParameter($lon, IQueryBuilder::PARAM_STR),
'mapcluster' => $query->createNamedParameter($mapCluster, IQueryBuilder::PARAM_INT),
'orphan' => $query->createNamedParameter(false, IQueryBuilder::PARAM_BOOL),
'buid' => $query->createNamedParameter($buid, IQueryBuilder::PARAM_STR),
'parent' => $query->createNamedParameter($file->getParent()->getId(), IQueryBuilder::PARAM_INT),
];
// There is no easy way to UPSERT in standard SQL
// https://stackoverflow.com/questions/15252213/sql-standard-upsert-call
if ($prevRow) {
$query->update('memories')
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
;
foreach ($params as $key => $value) {
$query->set($key, $value);
}
} else {
$query->insert('memories')->values($params);
}
// Execute query
$updated = Util::transaction(static fn () => $query->executeStatement() > 0);
// Clear failures if successful
if ($updated) {
$this->clearFailures($file);
}
return $updated;
}
/**
* Remove a file from the exif database.
*/
public function deleteFile(File $file): void
{
Util::transaction(function () use ($file): void {
// Get full record
$query = $this->connection->getQueryBuilder();
$record = $query->select('*')
->from('memories')
->where($query->expr()->eq('fileid', $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT)))
->executeQuery()
->fetch()
;
// Delete all records regardless of existence
foreach (DELETE_TABLES as $table) {
$query = $this->connection->getQueryBuilder();
$query->delete($table)
->where($query->expr()->eq('fileid', $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT)))
->executeStatement()
;
}
// Delete from map cluster
if ($record && ($cid = (int) $record['mapcluster']) > 0) {
$this->mapRemoveFromCluster($cid, (float) $record['lat'], (float) $record['lon']);
}
});
}
/**
* Clean up the table for entries not present in filecache.
*/
public function cleanupStale(): void
{
// Delete all stale records
foreach (DELETE_TABLES as $table) {
$clause = $this->connection->getQueryBuilder();
$clause->select($clause->expr()->literal(1))
->from('filecache', 'f')
->where($clause->expr()->eq('f.fileid', "*PREFIX*{$table}.fileid"))
;
$query = $this->connection->getQueryBuilder();
$query->delete($table)
->where(SQL::notExists($query, $clause))
->executeStatement()
;
}
}
/**
* Clear the entire index. Does not need confirmation!
*/
public function clear(): void
{
$p = $this->connection->getDatabasePlatform();
foreach (array_merge(DELETE_TABLES, TRUNCATE_TABLES) as $table) {
$this->connection->executeStatement($p->getTruncateTableSQL('*PREFIX*'.$table, false));
}
}
/**
* Get the current row for a file_id, from either table.
*/
private function getCurrentRow(int $fileId): ?array
{
$fetch = function (string $table) use ($fileId): false|null|array {
$query = $this->connection->getQueryBuilder();
return $query->select('*')
->from($table)
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
->executeQuery()
->fetch()
;
};
return Util::transaction(static fn () => $fetch('memories') ?: $fetch('memories_livephoto') ?: null);
}
/**
* Convert EXIF data to filtered JSON string.
*
* @param array<string, mixed> $exif EXIF data
*/
private function getExifJson(array $exif): string
{
// Clean up EXIF to keep only useful metadata
$filteredExif = [];
foreach ($exif as $key => $value) {
// Truncate any fields > 2048 chars
if (\is_string($value) && \strlen($value) > 2048) {
$value = substr($value, 0, 2048);
}
// Only keep fields in the whitelist
if (\array_key_exists($key, ExifFields::EXIF_FIELDS_LIST)) {
$filteredExif[$key] = $value;
}
}
// Store JSON string
$exifJson = json_encode($filteredExif);
// Store error if data > 64kb
if (\is_string($exifJson)) {
if (\strlen($exifJson) > 65535) {
$exifJson = json_encode(['error' => 'Exif data too large']);
}
} else {
$exifJson = json_encode(['error' => 'Exif data encoding error']);
}
return $exifJson;
}
}