2022-08-20 02:53:21 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Memories\Db;
|
|
|
|
|
|
|
|
use OCA\Memories\AppInfo\Application;
|
|
|
|
use OCA\Memories\Exif;
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
class TimelineWrite {
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
|
|
|
public function __construct(IDBConnection $connection) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a file to insert Exif data into the database
|
|
|
|
* @param File $file
|
|
|
|
*/
|
|
|
|
public function processFile(File &$file): void {
|
|
|
|
// 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
|
|
|
|
$mime = $file->getMimeType();
|
|
|
|
$is_image = in_array($mime, Application::IMAGE_MIMES);
|
2022-08-20 21:28:41 +00:00
|
|
|
$isvideo = in_array($mime, Application::VIDEO_MIMES);
|
|
|
|
if (!$is_image && !$isvideo) {
|
2022-08-20 02:53:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get parameters
|
|
|
|
$mtime = $file->getMtime();
|
|
|
|
$user = $file->getOwner()->getUID();
|
|
|
|
$fileId = $file->getId();
|
|
|
|
|
|
|
|
// Check if need to update
|
|
|
|
$sql = 'SELECT `mtime`
|
|
|
|
FROM *PREFIX*memories
|
2022-08-20 21:28:41 +00:00
|
|
|
WHERE `fileid` = ? AND `uid` = ?';
|
2022-08-20 02:53:21 +00:00
|
|
|
$prevRow = $this->connection->executeQuery($sql, [
|
|
|
|
$fileId, $user,
|
|
|
|
], [
|
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR,
|
|
|
|
])->fetch();
|
|
|
|
if ($prevRow && intval($prevRow['mtime']) === $mtime) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get exif data
|
|
|
|
$exif = [];
|
|
|
|
try {
|
|
|
|
$exif = Exif::getExifFromFile($file);
|
|
|
|
} catch (\Exception) {}
|
|
|
|
|
|
|
|
// Get more parameters
|
|
|
|
$dateTaken = Exif::getDateTaken($file, $exif);
|
|
|
|
$dayId = floor($dateTaken / 86400);
|
|
|
|
$dateTaken = gmdate('Y-m-d H:i:s', $dateTaken);
|
|
|
|
|
|
|
|
if ($prevRow) {
|
|
|
|
// Update existing row
|
|
|
|
$sql = 'UPDATE *PREFIX*memories
|
2022-08-20 21:28:41 +00:00
|
|
|
SET `dayid` = ?, `datetaken` = ?, `isvideo` = ?, `mtime` = ?
|
|
|
|
WHERE `uid` = ? AND `fileid` = ?';
|
2022-08-20 02:53:21 +00:00
|
|
|
$this->connection->executeStatement($sql, [
|
2022-08-20 21:28:41 +00:00
|
|
|
$dayId, $dateTaken, $isvideo, $mtime,
|
2022-08-20 02:53:21 +00:00
|
|
|
$user, $fileId,
|
|
|
|
], [
|
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_BOOL, \PDO::PARAM_INT,
|
|
|
|
\PDO::PARAM_STR, \PDO::PARAM_INT,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
// Create new row
|
|
|
|
$sql = 'INSERT
|
2022-08-20 21:28:41 +00:00
|
|
|
INTO *PREFIX*memories (`dayid`, `datetaken`, `isvideo`, `mtime`, `uid`, `fileid`)
|
2022-08-20 02:53:21 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?)';
|
|
|
|
$this->connection->executeStatement($sql, [
|
2022-08-20 21:28:41 +00:00
|
|
|
$dayId, $dateTaken, $isvideo, $mtime,
|
2022-08-20 02:53:21 +00:00
|
|
|
$user, $fileId,
|
|
|
|
], [
|
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_BOOL, \PDO::PARAM_INT,
|
|
|
|
\PDO::PARAM_STR, \PDO::PARAM_INT,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a file from the exif database
|
|
|
|
* @param File $file
|
|
|
|
*/
|
|
|
|
public function deleteFile(File &$file) {
|
|
|
|
$sql = 'DELETE
|
|
|
|
FROM *PREFIX*memories
|
2022-08-20 21:28:41 +00:00
|
|
|
WHERE `fileid` = ?';
|
2022-08-20 02:53:21 +00:00
|
|
|
$this->connection->executeStatement($sql, [$file->getId()], [\PDO::PARAM_INT]);
|
|
|
|
}
|
|
|
|
}
|