2022-08-20 02:53:21 +00:00
|
|
|
<?php
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-08-20 02:53:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
|
|
|
use OCA\Memories\AppInfo\Application;
|
|
|
|
use OCA\Memories\Exif;
|
2022-10-19 17:10:36 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2022-08-20 02:53:21 +00:00
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\IDBConnection;
|
2022-10-25 00:47:25 +00:00
|
|
|
use OCP\IPreview;
|
2023-02-06 02:29:09 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-08-20 02:53:21 +00:00
|
|
|
|
2023-01-23 00:51:26 +00:00
|
|
|
require_once __DIR__.'/../ExifFields.php';
|
|
|
|
|
2023-02-06 02:34:54 +00:00
|
|
|
const DELETE_TABLES = ['memories', 'memories_livephoto', 'memories_places'];
|
|
|
|
|
2022-10-19 17:10:36 +00:00
|
|
|
class TimelineWrite
|
|
|
|
{
|
2022-09-09 07:31:42 +00:00
|
|
|
protected IDBConnection $connection;
|
2022-10-25 00:47:25 +00:00
|
|
|
protected IPreview $preview;
|
2022-11-22 11:10:25 +00:00
|
|
|
protected LivePhoto $livePhoto;
|
2022-08-20 02:53:21 +00:00
|
|
|
|
2022-12-04 17:40:58 +00:00
|
|
|
public function __construct(IDBConnection $connection)
|
2022-10-19 17:10:36 +00:00
|
|
|
{
|
2022-09-09 07:31:42 +00:00
|
|
|
$this->connection = $connection;
|
2022-12-04 17:57:31 +00:00
|
|
|
$this->preview = \OC::$server->get(IPreview::class);
|
2022-11-22 11:10:25 +00:00
|
|
|
$this->livePhoto = new LivePhoto($connection);
|
2022-09-09 07:31:42 +00:00
|
|
|
}
|
2022-08-20 02:53:21 +00:00
|
|
|
|
2022-09-11 00:50:08 +00:00
|
|
|
/**
|
2022-10-19 17:10:36 +00:00
|
|
|
* Check if a file has a valid mimetype for processing.
|
|
|
|
*
|
2022-09-11 00:50:08 +00:00
|
|
|
* @return int 0 for invalid, 1 for image, 2 for video
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function getFileType(File $file): int
|
|
|
|
{
|
2022-09-11 00:50:08 +00:00
|
|
|
$mime = $file->getMimeType();
|
2022-10-19 17:10:36 +00:00
|
|
|
if (\in_array($mime, Application::IMAGE_MIMES, true)) {
|
2022-10-25 17:25:26 +00:00
|
|
|
// Make sure preview generator supports the mime type
|
|
|
|
if (!$this->preview->isMimeSupported($mime)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-09-11 00:50:08 +00:00
|
|
|
return 1;
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|
|
|
|
if (\in_array($mime, Application::VIDEO_MIMES, true)) {
|
2022-09-11 00:50:08 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-09-11 00:50:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:53:21 +00:00
|
|
|
/**
|
2022-10-19 17:10:36 +00:00
|
|
|
* Process a file to insert Exif data into the database.
|
|
|
|
*
|
2022-08-23 09:35:51 +00:00
|
|
|
* @return int 2 if processed, 1 if skipped, 0 if not valid
|
2022-08-20 02:53:21 +00:00
|
|
|
*/
|
2022-09-13 17:39:38 +00:00
|
|
|
public function processFile(
|
|
|
|
File &$file,
|
2022-10-19 17:10:36 +00:00
|
|
|
bool $force = false
|
2022-09-13 17:39:38 +00:00
|
|
|
): int {
|
2022-08-20 02:53:21 +00:00
|
|
|
// There is no easy way to UPSERT in a standard SQL way, so just
|
|
|
|
// do multiple calls. The worst that can happen is more updates,
|
|
|
|
// but that's not a big deal.
|
|
|
|
// https://stackoverflow.com/questions/15252213/sql-standard-upsert-call
|
|
|
|
|
|
|
|
// Check if we want to process this file
|
2022-09-11 00:50:08 +00:00
|
|
|
$fileType = $this->getFileType($file);
|
2022-10-19 17:10:36 +00:00
|
|
|
$isvideo = (2 === $fileType);
|
2022-09-11 00:50:08 +00:00
|
|
|
if (!$fileType) {
|
2022-08-23 09:35:51 +00:00
|
|
|
return 0;
|
2022-08-20 02:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get parameters
|
|
|
|
$mtime = $file->getMtime();
|
|
|
|
$fileId = $file->getId();
|
|
|
|
|
|
|
|
// Check if need to update
|
2022-09-11 02:22:57 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('fileid', 'mtime')
|
|
|
|
->from('memories')
|
2022-10-19 17:10:36 +00:00
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
2022-09-25 11:30:28 +00:00
|
|
|
$cursor = $query->executeQuery();
|
|
|
|
$prevRow = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
2022-11-22 11:10:25 +00:00
|
|
|
|
|
|
|
// Check in live-photo table in case this is a video part of a live photo
|
|
|
|
if (!$prevRow) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('fileid', 'mtime')
|
|
|
|
->from('memories_livephoto')
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
$cursor = $query->executeQuery();
|
|
|
|
$prevRow = $cursor->fetch();
|
|
|
|
$cursor->closeCursor();
|
|
|
|
}
|
|
|
|
|
2022-10-19 17:15:14 +00:00
|
|
|
if ($prevRow && !$force && (int) $prevRow['mtime'] === $mtime) {
|
2022-08-23 09:35:51 +00:00
|
|
|
return 1;
|
2022-08-20 02:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get exif data
|
|
|
|
$exif = [];
|
2022-10-19 17:10:36 +00:00
|
|
|
|
2022-08-20 02:53:21 +00:00
|
|
|
try {
|
|
|
|
$exif = Exif::getExifFromFile($file);
|
2022-10-19 17:10:36 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
}
|
2022-08-20 02:53:21 +00:00
|
|
|
|
2022-11-22 11:10:25 +00:00
|
|
|
// Hand off if live photo video part
|
|
|
|
if ($isvideo && $this->livePhoto->isVideoPart($exif)) {
|
|
|
|
$this->livePhoto->processVideoPart($file, $exif);
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:53:21 +00:00
|
|
|
// Get more parameters
|
|
|
|
$dateTaken = Exif::getDateTaken($file, $exif);
|
|
|
|
$dayId = floor($dateTaken / 86400);
|
|
|
|
$dateTaken = gmdate('Y-m-d H:i:s', $dateTaken);
|
2022-10-15 19:15:07 +00:00
|
|
|
[$w, $h] = Exif::getDimensions($exif);
|
2023-01-26 19:52:19 +00:00
|
|
|
$liveid = $this->livePhoto->getLivePhotoId($file, $exif);
|
2022-08-20 02:53:21 +00:00
|
|
|
|
2022-11-10 03:48:03 +00:00
|
|
|
// Video parameters
|
|
|
|
$videoDuration = 0;
|
|
|
|
if ($isvideo) {
|
2022-12-22 18:49:28 +00:00
|
|
|
$videoDuration = round((float) ($exif['Duration'] ?? $exif['TrackDuration'] ?? 0));
|
2022-11-10 03:48:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 10:36:04 +00:00
|
|
|
// Clean up EXIF to keep only useful metadata
|
2023-01-28 16:53:01 +00:00
|
|
|
$filteredExif = [];
|
|
|
|
foreach ($exif as $key => $value) {
|
2022-11-22 10:36:04 +00:00
|
|
|
// Truncate any fields > 2048 chars
|
2022-11-07 21:13:16 +00:00
|
|
|
if (\is_string($value) && \strlen($value) > 2048) {
|
2023-01-28 16:53:01 +00:00
|
|
|
$value = substr($value, 0, 2048);
|
2022-11-07 21:13:16 +00:00
|
|
|
}
|
2022-11-22 10:36:04 +00:00
|
|
|
|
2023-01-28 16:53:01 +00:00
|
|
|
// Only keep fields in the whitelist
|
|
|
|
if (\array_key_exists($key, EXIF_FIELDS_LIST)) {
|
|
|
|
$filteredExif[$key] = $value;
|
2022-11-22 10:36:04 +00:00
|
|
|
}
|
2022-11-07 21:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store JSON string
|
2023-01-28 16:53:01 +00:00
|
|
|
$exifJson = json_encode($filteredExif);
|
2022-11-07 21:13:16 +00:00
|
|
|
|
|
|
|
// Store error if data > 64kb
|
2022-11-21 09:43:17 +00:00
|
|
|
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']);
|
2022-11-07 21:13:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 21:43:25 +00:00
|
|
|
// Store location data
|
|
|
|
if (\array_key_exists('GPSLatitude', $exif) && \array_key_exists('GPSLongitude', $exif)) {
|
|
|
|
$lat = $exif['GPSLatitude'];
|
|
|
|
$lon = $exif['GPSLongitude'];
|
2023-02-06 02:29:09 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$this->updateGeoData($file, (float) $lat, (float) $lon);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$logger = \OC::$server->get(LoggerInterface::class);
|
|
|
|
$logger->log(3, 'Error updating geo data: '.$e->getMessage(), ['app' => 'memories']);
|
|
|
|
}
|
2023-02-05 21:43:25 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 02:53:21 +00:00
|
|
|
if ($prevRow) {
|
|
|
|
// Update existing row
|
2022-11-01 03:56:26 +00:00
|
|
|
// No need to set objectid again
|
2022-09-11 02:22:57 +00:00
|
|
|
$query->update('memories')
|
|
|
|
->set('dayid', $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT))
|
|
|
|
->set('datetaken', $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR))
|
|
|
|
->set('mtime', $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT))
|
|
|
|
->set('isvideo', $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT))
|
2022-11-10 03:48:03 +00:00
|
|
|
->set('video_duration', $query->createNamedParameter($videoDuration, IQueryBuilder::PARAM_INT))
|
2022-10-15 19:15:07 +00:00
|
|
|
->set('w', $query->createNamedParameter($w, IQueryBuilder::PARAM_INT))
|
|
|
|
->set('h', $query->createNamedParameter($h, IQueryBuilder::PARAM_INT))
|
2022-11-07 21:13:16 +00:00
|
|
|
->set('exif', $query->createNamedParameter($exifJson, IQueryBuilder::PARAM_STR))
|
2022-11-22 11:10:25 +00:00
|
|
|
->set('liveid', $query->createNamedParameter($liveid, IQueryBuilder::PARAM_STR))
|
2022-10-19 17:10:36 +00:00
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
2022-09-11 02:22:57 +00:00
|
|
|
$query->executeStatement();
|
2022-08-20 02:53:21 +00:00
|
|
|
} else {
|
2022-08-22 18:42:06 +00:00
|
|
|
// Try to create new row
|
|
|
|
try {
|
2022-09-11 02:22:57 +00:00
|
|
|
$query->insert('memories')
|
|
|
|
->values([
|
|
|
|
'fileid' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
|
2022-11-01 03:56:26 +00:00
|
|
|
'objectid' => $query->createNamedParameter((string) $fileId, IQueryBuilder::PARAM_STR),
|
2022-09-11 02:22:57 +00:00
|
|
|
'dayid' => $query->createNamedParameter($dayId, IQueryBuilder::PARAM_INT),
|
|
|
|
'datetaken' => $query->createNamedParameter($dateTaken, IQueryBuilder::PARAM_STR),
|
|
|
|
'mtime' => $query->createNamedParameter($mtime, IQueryBuilder::PARAM_INT),
|
|
|
|
'isvideo' => $query->createNamedParameter($isvideo, IQueryBuilder::PARAM_INT),
|
2022-11-10 03:48:03 +00:00
|
|
|
'video_duration' => $query->createNamedParameter($videoDuration, IQueryBuilder::PARAM_INT),
|
2022-10-15 19:15:07 +00:00
|
|
|
'w' => $query->createNamedParameter($w, IQueryBuilder::PARAM_INT),
|
|
|
|
'h' => $query->createNamedParameter($h, IQueryBuilder::PARAM_INT),
|
2022-11-07 21:13:16 +00:00
|
|
|
'exif' => $query->createNamedParameter($exifJson, IQueryBuilder::PARAM_STR),
|
2022-11-22 11:10:25 +00:00
|
|
|
'liveid' => $query->createNamedParameter($liveid, IQueryBuilder::PARAM_STR),
|
2022-10-19 17:10:36 +00:00
|
|
|
])
|
|
|
|
;
|
2022-09-11 02:22:57 +00:00
|
|
|
$query->executeStatement();
|
2022-08-22 18:42:06 +00:00
|
|
|
} catch (\Exception $ex) {
|
2022-10-19 17:10:36 +00:00
|
|
|
error_log('Failed to create memories record: '.$ex->getMessage());
|
2022-08-22 18:42:06 +00:00
|
|
|
}
|
2022-08-20 02:53:21 +00:00
|
|
|
}
|
2022-08-23 09:35:51 +00:00
|
|
|
|
|
|
|
return 2;
|
2022-08-20 02:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-19 17:10:36 +00:00
|
|
|
* Remove a file from the exif database.
|
2022-08-20 02:53:21 +00:00
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function deleteFile(File &$file)
|
|
|
|
{
|
2023-02-06 02:34:54 +00:00
|
|
|
foreach(DELETE_TABLES as $table) {
|
2022-11-23 18:40:41 +00:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete($table)
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
2023-02-06 02:34:54 +00:00
|
|
|
}
|
2022-08-20 02:53:21 +00:00
|
|
|
}
|
2022-09-11 00:15:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the entire index. Does not need confirmation!
|
2022-10-19 17:10:36 +00:00
|
|
|
*
|
2022-09-11 00:15:40 +00:00
|
|
|
* @param File $file
|
|
|
|
*/
|
2022-10-19 17:10:36 +00:00
|
|
|
public function clear()
|
|
|
|
{
|
2022-11-23 09:49:00 +00:00
|
|
|
$p = $this->connection->getDatabasePlatform();
|
2023-02-06 02:34:54 +00:00
|
|
|
foreach (DELETE_TABLES as $table) {
|
|
|
|
$this->connection->executeStatement($p->getTruncateTableSQL('*PREFIX*'.$table, false));
|
|
|
|
}
|
2022-09-11 00:15:40 +00:00
|
|
|
}
|
2023-01-18 04:52:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a file as not orphaned.
|
|
|
|
*/
|
|
|
|
public function unorphan(File &$file)
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->update('memories')
|
|
|
|
->set('orphan', $query->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
|
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all files in the table as orphaned.
|
|
|
|
*
|
|
|
|
* @return int Number of rows affected
|
|
|
|
*/
|
|
|
|
public function orphanAll(): int
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->update('memories')
|
|
|
|
->set('orphan', $query->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $query->executeStatement();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all entries that are orphans.
|
|
|
|
*
|
|
|
|
* @return int Number of rows affected
|
|
|
|
*/
|
|
|
|
public function removeOrphans(): int
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete('memories')
|
|
|
|
->where($query->expr()->eq('orphan', $query->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
|
|
|
|
;
|
|
|
|
|
|
|
|
return $query->executeStatement();
|
|
|
|
}
|
2023-02-05 21:43:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add geolocation data for a file.
|
|
|
|
*/
|
|
|
|
public function updateGeoData(File &$file, float $lat, float $lon): void
|
|
|
|
{
|
2023-02-06 02:29:09 +00:00
|
|
|
// Get GIS type
|
|
|
|
$config = \OC::$server->get(\OCP\IConfig::class);
|
|
|
|
$gisType = $config->getSystemValue('memories.gis_type', 0);
|
|
|
|
|
|
|
|
// Construct WHERE clause depending on GIS type
|
|
|
|
$where = null;
|
|
|
|
if (1 === $gisType) {
|
|
|
|
$where = "ST_Contains(geometry, ST_GeomFromText('POINT({$lon} {$lat})'))";
|
|
|
|
} elseif (2 === $gisType) {
|
|
|
|
$where = "POINT('{$lon},{$lat}') <@ geometry";
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:43:25 +00:00
|
|
|
// Make query to memories_planet table
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2023-02-06 01:15:18 +00:00
|
|
|
$query->select($query->createFunction('DISTINCT(osm_id)'))
|
|
|
|
->from('memories_planet_geometry')
|
2023-02-06 02:29:09 +00:00
|
|
|
->where($query->createFunction($where))
|
2023-02-05 21:43:25 +00:00
|
|
|
;
|
|
|
|
|
2023-02-06 02:29:09 +00:00
|
|
|
// memories_planet_geometry has no *PREFIX*
|
|
|
|
$sql = str_replace('*PREFIX*memories_planet_geometry', 'memories_planet_geometry', $query->getSQL());
|
2023-02-05 21:43:25 +00:00
|
|
|
|
|
|
|
// Run query
|
|
|
|
$result = $this->connection->executeQuery($sql);
|
|
|
|
$rows = $result->fetchAll();
|
|
|
|
|
|
|
|
// Delete previous records
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2023-02-06 01:15:18 +00:00
|
|
|
$query->delete('memories_places')
|
2023-02-05 21:43:25 +00:00
|
|
|
->where($query->expr()->eq('fileid', $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT)))
|
|
|
|
;
|
|
|
|
|
|
|
|
// Insert records
|
|
|
|
foreach ($rows as $row) {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
2023-02-06 01:15:18 +00:00
|
|
|
$query->insert('memories_places')
|
2023-02-05 21:43:25 +00:00
|
|
|
->values([
|
|
|
|
'fileid' => $query->createNamedParameter($file->getId(), IQueryBuilder::PARAM_INT),
|
|
|
|
'osm_id' => $query->createNamedParameter($row['osm_id'], IQueryBuilder::PARAM_INT),
|
|
|
|
])
|
|
|
|
;
|
|
|
|
$query->executeStatement();
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 17:10:36 +00:00
|
|
|
}
|