memories/lib/Db/TimelineWrite.php

109 lines
3.6 KiB
PHP
Raw Normal View History

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 {
2022-09-09 07:31:42 +00:00
protected IDBConnection $connection;
2022-08-20 02:53:21 +00:00
2022-09-09 07:31:42 +00:00
public function __construct(IDBConnection $connection) {
$this->connection = $connection;
}
2022-08-20 02:53:21 +00:00
/**
* Process a file to insert Exif data into the database
* @param File $file
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
*/
public function processFile(File &$file, bool $force=false): 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
$mime = $file->getMimeType();
$is_image = in_array($mime, Application::IMAGE_MIMES);
$isvideo = in_array($mime, Application::VIDEO_MIMES);
if (!$is_image && !$isvideo) {
2022-08-23 09:35:51 +00:00
return 0;
2022-08-20 02:53:21 +00:00
}
// Get parameters
$mtime = $file->getMtime();
$user = $file->getOwner()->getUID();
$fileId = $file->getId();
// Check if need to update
2022-08-22 18:42:06 +00:00
$sql = 'SELECT `fileid`, `mtime`
2022-08-20 02:53:21 +00:00
FROM *PREFIX*memories
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 && !$force && intval($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 = [];
try {
$exif = Exif::getExifFromFile($file);
2022-08-22 18:16:05 +00:00
} catch (\Exception $e) {}
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);
if ($prevRow) {
// Update existing row
$sql = 'UPDATE *PREFIX*memories
SET `dayid` = ?, `datetaken` = ?, `isvideo` = ?, `mtime` = ?
WHERE `uid` = ? AND `fileid` = ?';
2022-08-20 02:53:21 +00:00
$this->connection->executeStatement($sql, [
$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 {
2022-08-22 18:42:06 +00:00
// Try to create new row
try {
$sql = 'INSERT
INTO *PREFIX*memories (`dayid`, `datetaken`, `isvideo`, `mtime`, `uid`, `fileid`)
VALUES (?, ?, ?, ?, ?, ?)';
$this->connection->executeStatement($sql, [
$dayId, $dateTaken, $isvideo, $mtime,
$user, $fileId,
], [
\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_BOOL, \PDO::PARAM_INT,
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
} catch (\Exception $ex) {
error_log("Failed to create memories record: " . $ex->getMessage());
}
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
}
/**
* Remove a file from the exif database
* @param File $file
*/
public function deleteFile(File &$file) {
$sql = 'DELETE
FROM *PREFIX*memories
WHERE `fileid` = ?';
2022-08-20 02:53:21 +00:00
$this->connection->executeStatement($sql, [$file->getId()], [\PDO::PARAM_INT]);
}
}