2022-08-13 01:58:37 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-08-18 18:27:25 +00:00
|
|
|
namespace OCA\Memories\Db;
|
2022-08-13 01:58:37 +00:00
|
|
|
|
2022-08-18 18:27:25 +00:00
|
|
|
use OCA\Memories\AppInfo\Application;
|
2022-08-13 01:58:37 +00:00
|
|
|
use OCP\Files\File;
|
2022-08-20 00:18:04 +00:00
|
|
|
use OCP\IConfig;
|
2022-08-13 01:58:37 +00:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
|
|
|
class Util {
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
2022-08-15 23:43:10 +00:00
|
|
|
public function __construct(IDBConnection $connection) {
|
2022-08-13 01:58:37 +00:00
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Get the path to the user's configured photos directory.
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param string $userId
|
|
|
|
*/
|
2022-08-20 02:12:55 +00:00
|
|
|
public static function getPhotosPath(IConfig &$config, string &$userId) {
|
2022-08-20 00:18:04 +00:00
|
|
|
$p = $config->getUserValue($userId, Application::APPNAME, 'timelinePath', '');
|
|
|
|
if (empty($p)) {
|
|
|
|
return '/Photos/';
|
|
|
|
}
|
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Get exif data as a JSON object from a Nextcloud file.
|
|
|
|
* @param File $file
|
|
|
|
*/
|
|
|
|
public static function getExifFromFile(File &$file) {
|
|
|
|
$handle = $file->fopen('rb');
|
2022-08-20 02:10:15 +00:00
|
|
|
if (!$handle) {
|
|
|
|
throw new \Exception('Could not open file');
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
$exif = self::getExifFromStream($handle);
|
|
|
|
fclose($handle);
|
|
|
|
return $exif;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get exif data as a JSON object from a stream.
|
|
|
|
* @param resource $handle
|
|
|
|
*/
|
|
|
|
public static function getExifFromStream(&$handle) {
|
|
|
|
// Start exiftool and output to json
|
|
|
|
$pipes = [];
|
|
|
|
$proc = proc_open('exiftool -json -', [
|
|
|
|
0 => array('pipe', 'rb'),
|
|
|
|
1 => array('pipe', 'w'),
|
|
|
|
2 => array('pipe', 'w'),
|
|
|
|
], $pipes);
|
|
|
|
|
|
|
|
// Write the file to exiftool's stdin
|
|
|
|
// Assume exif exists in the first 256 kb of the file
|
|
|
|
stream_copy_to_stream($handle, $pipes[0], 256 * 1024);
|
|
|
|
fclose($pipes[0]);
|
|
|
|
|
|
|
|
// Get output from exiftool
|
|
|
|
$stdout = stream_get_contents($pipes[1]);
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
fclose($pipes[1]);
|
|
|
|
fclose($pipes[2]);
|
|
|
|
proc_close($proc);
|
|
|
|
|
|
|
|
// Parse the json
|
|
|
|
$json = json_decode($stdout, true);
|
|
|
|
if (!$json) {
|
|
|
|
throw new \Exception('Could not read exif data');
|
2022-08-19 21:38:18 +00:00
|
|
|
}
|
2022-08-20 02:08:54 +00:00
|
|
|
return $json[0];
|
2022-08-19 21:38:18 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Get the date taken from either the file or exif data if available.
|
|
|
|
* @param File $file
|
|
|
|
* @param array $exif
|
|
|
|
*/
|
2022-08-20 02:12:55 +00:00
|
|
|
public static function getDateTaken(File &$file, array &$exif) {
|
2022-08-19 21:38:18 +00:00
|
|
|
$dt = $exif['DateTimeOriginal'];
|
|
|
|
if (!isset($dt) || empty($dt)) {
|
|
|
|
$dt = $exif['CreateDate'];
|
2022-08-17 21:45:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if found something
|
2022-08-19 21:38:18 +00:00
|
|
|
if (isset($dt) && !empty($dt)) {
|
2022-08-17 21:45:01 +00:00
|
|
|
$dt = \DateTime::createFromFormat('Y:m:d H:i:s', $dt);
|
2022-08-18 04:19:09 +00:00
|
|
|
if ($dt && $dt->getTimestamp() > -5364662400) { // 1800 A.D.
|
2022-08-17 21:45:01 +00:00
|
|
|
return $dt->getTimestamp();
|
2022-08-15 23:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-15 03:25:12 +00:00
|
|
|
|
|
|
|
// Fall back to creation time
|
2022-08-13 01:58:37 +00:00
|
|
|
$dateTaken = $file->getCreationTime();
|
2022-08-15 03:25:12 +00:00
|
|
|
|
2022-08-16 00:32:57 +00:00
|
|
|
// Fall back to upload time
|
|
|
|
if ($dateTaken == 0) {
|
|
|
|
$dateTaken = $file->getUploadTime();
|
|
|
|
}
|
|
|
|
|
2022-08-15 03:25:12 +00:00
|
|
|
// Fall back to modification time
|
2022-08-13 01:58:37 +00:00
|
|
|
if ($dateTaken == 0) {
|
|
|
|
$dateTaken = $file->getMtime();
|
|
|
|
}
|
2022-08-13 03:34:05 +00:00
|
|
|
return $dateTaken;
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Process a file to insert Exif data into the database
|
|
|
|
* @param File $file
|
|
|
|
*/
|
2022-08-20 02:12:55 +00:00
|
|
|
public function processFile(File &$file): void {
|
2022-08-20 01:39:17 +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-08-13 01:58:37 +00:00
|
|
|
$mime = $file->getMimeType();
|
2022-08-16 00:46:36 +00:00
|
|
|
$is_image = in_array($mime, Application::IMAGE_MIMES);
|
|
|
|
$is_video = in_array($mime, Application::VIDEO_MIMES);
|
|
|
|
if (!$is_image && !$is_video) {
|
2022-08-13 01:58:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:19:13 +00:00
|
|
|
// Get parameters
|
2022-08-17 23:51:48 +00:00
|
|
|
$mtime = $file->getMtime();
|
2022-08-16 02:33:15 +00:00
|
|
|
$user = $file->getOwner()->getUID();
|
2022-08-14 23:19:13 +00:00
|
|
|
$fileId = $file->getId();
|
2022-08-17 23:51:48 +00:00
|
|
|
|
|
|
|
// Check if need to update
|
2022-08-20 01:39:17 +00:00
|
|
|
$sql = 'SELECT `mtime`
|
2022-08-18 18:27:25 +00:00
|
|
|
FROM *PREFIX*memories
|
2022-08-20 01:39:17 +00:00
|
|
|
WHERE file_id = ? AND user_id = ?';
|
|
|
|
$prevRow = $this->connection->executeQuery($sql, [
|
|
|
|
$fileId, $user,
|
2022-08-17 23:51:48 +00:00
|
|
|
], [
|
2022-08-20 01:39:17 +00:00
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR,
|
2022-08-17 23:51:48 +00:00
|
|
|
])->fetch();
|
2022-08-20 01:39:17 +00:00
|
|
|
if ($prevRow && intval($prevRow['mtime']) === $mtime) {
|
2022-08-17 23:51:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:38:18 +00:00
|
|
|
// Get exif data
|
2022-08-20 02:08:54 +00:00
|
|
|
$exif = [];
|
|
|
|
try {
|
|
|
|
$exif = self::getExifFromFile($file);
|
|
|
|
} catch (\Exception) {}
|
2022-08-19 21:38:18 +00:00
|
|
|
|
2022-08-17 23:51:48 +00:00
|
|
|
// Get more parameters
|
2022-08-19 21:38:18 +00:00
|
|
|
$dateTaken = $this->getDateTaken($file, $exif);
|
2022-08-14 23:31:47 +00:00
|
|
|
$dayId = floor($dateTaken / 86400);
|
2022-08-16 04:31:09 +00:00
|
|
|
$dateTaken = gmdate('Y-m-d H:i:s', $dateTaken);
|
2022-08-14 23:19:13 +00:00
|
|
|
|
2022-08-20 01:39:17 +00:00
|
|
|
if ($prevRow) {
|
|
|
|
// Update existing row
|
|
|
|
$sql = 'UPDATE *PREFIX*memories
|
|
|
|
SET day_id = ?, date_taken = ?, is_video = ?, mtime = ?
|
|
|
|
WHERE user_id = ? AND file_id = ?';
|
|
|
|
$this->connection->executeStatement($sql, [
|
|
|
|
$dayId, $dateTaken, $is_video, $mtime,
|
|
|
|
$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
|
|
|
|
INTO *PREFIX*memories (day_id, date_taken, is_video, mtime, user_id, file_id)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)';
|
|
|
|
$this->connection->executeStatement($sql, [
|
|
|
|
$dayId, $dateTaken, $is_video, $mtime,
|
|
|
|
$user, $fileId,
|
|
|
|
], [
|
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_BOOL, \PDO::PARAM_INT,
|
|
|
|
\PDO::PARAM_STR, \PDO::PARAM_INT,
|
|
|
|
]);
|
|
|
|
}
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Remove a file from the exif database
|
|
|
|
* @param File $file
|
|
|
|
*/
|
2022-08-20 02:12:55 +00:00
|
|
|
public function deleteFile(File &$file) {
|
2022-08-14 23:19:13 +00:00
|
|
|
$sql = 'DELETE
|
2022-08-18 18:27:25 +00:00
|
|
|
FROM *PREFIX*memories
|
2022-08-16 02:47:49 +00:00
|
|
|
WHERE file_id = ?';
|
|
|
|
$this->connection->executeStatement($sql, [$file->getId()], [\PDO::PARAM_INT]);
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
2022-08-13 03:34:05 +00:00
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Process the days response
|
|
|
|
* @param array $days
|
|
|
|
*/
|
|
|
|
private function processDays(&$days) {
|
2022-08-16 03:58:55 +00:00
|
|
|
foreach($days as &$row) {
|
|
|
|
$row["day_id"] = intval($row["day_id"]);
|
|
|
|
$row["count"] = intval($row["count"]);
|
|
|
|
}
|
|
|
|
return $days;
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Get the days response from the database for the timeline
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param string $userId
|
|
|
|
*/
|
2022-08-15 23:43:10 +00:00
|
|
|
public function getDays(
|
2022-08-20 02:12:55 +00:00
|
|
|
IConfig &$config,
|
|
|
|
string &$user,
|
2022-08-14 23:31:47 +00:00
|
|
|
): array {
|
2022-08-16 02:47:49 +00:00
|
|
|
$sql = 'SELECT day_id, COUNT(file_id) AS count
|
2022-08-18 18:27:25 +00:00
|
|
|
FROM `*PREFIX*memories`
|
2022-08-18 00:35:14 +00:00
|
|
|
INNER JOIN `*PREFIX*filecache`
|
2022-08-18 18:27:25 +00:00
|
|
|
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`file_id`
|
2022-08-20 00:18:04 +00:00
|
|
|
AND `*PREFIX*filecache`.`path` LIKE ?
|
2022-08-18 03:23:41 +00:00
|
|
|
WHERE user_id=?
|
2022-08-16 02:47:49 +00:00
|
|
|
GROUP BY day_id
|
|
|
|
ORDER BY day_id DESC';
|
2022-08-20 00:18:04 +00:00
|
|
|
|
|
|
|
$path = "files" . self::getPhotosPath($config, $user) . "%";
|
|
|
|
$rows = $this->connection->executeQuery($sql, [$path, $user], [
|
|
|
|
\PDO::PARAM_STR, \PDO::PARAM_STR,
|
2022-08-16 02:47:49 +00:00
|
|
|
])->fetchAll();
|
2022-08-16 03:58:55 +00:00
|
|
|
return $this->processDays($rows);
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Get the days response from the database for one folder
|
|
|
|
* @param int $folderId
|
|
|
|
*/
|
2022-08-20 02:12:55 +00:00
|
|
|
public function getDaysFolder(int &$folderId) {
|
2022-08-16 03:58:55 +00:00
|
|
|
$sql = 'SELECT day_id, COUNT(file_id) AS count
|
2022-08-18 18:27:25 +00:00
|
|
|
FROM `*PREFIX*memories`
|
2022-08-17 23:56:14 +00:00
|
|
|
INNER JOIN `*PREFIX*filecache`
|
2022-08-18 18:27:25 +00:00
|
|
|
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`file_id`
|
2022-08-17 23:56:14 +00:00
|
|
|
AND (`*PREFIX*filecache`.`parent`=? OR `*PREFIX*filecache`.`fileid`=?)
|
2022-08-16 03:58:55 +00:00
|
|
|
GROUP BY day_id
|
|
|
|
ORDER BY day_id DESC';
|
|
|
|
$rows = $this->connection->executeQuery($sql, [$folderId, $folderId], [
|
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_INT,
|
|
|
|
])->fetchAll();
|
|
|
|
return $this->processDays($rows);
|
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Process the single day response
|
|
|
|
* @param array $day
|
|
|
|
*/
|
|
|
|
private function processDay(&$day) {
|
2022-08-16 03:58:55 +00:00
|
|
|
foreach($day as &$row) {
|
|
|
|
$row["file_id"] = intval($row["file_id"]);
|
|
|
|
$row["is_video"] = intval($row["is_video"]);
|
|
|
|
if (!$row["is_video"]) {
|
|
|
|
unset($row["is_video"]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $day;
|
2022-08-13 03:34:05 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Get a day response from the database for the timeline
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param string $userId
|
|
|
|
* @param int $dayId
|
|
|
|
*/
|
2022-08-15 23:43:10 +00:00
|
|
|
public function getDay(
|
2022-08-20 02:12:55 +00:00
|
|
|
IConfig &$config,
|
|
|
|
string &$user,
|
|
|
|
int &$dayId,
|
2022-08-13 03:34:05 +00:00
|
|
|
): array {
|
2022-08-17 23:56:14 +00:00
|
|
|
$sql = 'SELECT file_id, *PREFIX*filecache.etag, is_video
|
2022-08-18 18:27:25 +00:00
|
|
|
FROM *PREFIX*memories
|
2022-08-18 03:23:41 +00:00
|
|
|
INNER JOIN *PREFIX*filecache
|
2022-08-18 18:27:25 +00:00
|
|
|
ON *PREFIX*filecache.fileid = *PREFIX*memories.file_id
|
2022-08-20 00:18:04 +00:00
|
|
|
AND `*PREFIX*filecache`.`path` LIKE ?
|
2022-08-15 23:14:24 +00:00
|
|
|
WHERE user_id = ? AND day_id = ?
|
|
|
|
ORDER BY date_taken DESC';
|
2022-08-20 00:18:04 +00:00
|
|
|
|
|
|
|
$path = "files" . self::getPhotosPath($config, $user) . "%";
|
|
|
|
$rows = $this->connection->executeQuery($sql, [$path, $user, $dayId], [
|
|
|
|
\PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_INT,
|
2022-08-16 00:46:36 +00:00
|
|
|
])->fetchAll();
|
2022-08-16 03:58:55 +00:00
|
|
|
return $this->processDay($rows);
|
|
|
|
}
|
2022-08-16 00:46:36 +00:00
|
|
|
|
2022-08-20 02:08:54 +00:00
|
|
|
/**
|
|
|
|
* Get a day response from the database for one folder
|
|
|
|
* @param int $folderId
|
|
|
|
* @param int $dayId
|
|
|
|
*/
|
2022-08-16 03:58:55 +00:00
|
|
|
public function getDayFolder(
|
2022-08-20 02:12:55 +00:00
|
|
|
int &$folderId,
|
|
|
|
int &$dayId,
|
2022-08-16 03:58:55 +00:00
|
|
|
): array {
|
2022-08-17 23:56:14 +00:00
|
|
|
$sql = 'SELECT file_id, *PREFIX*filecache.etag, is_video
|
2022-08-18 18:27:25 +00:00
|
|
|
FROM `*PREFIX*memories`
|
2022-08-17 23:56:14 +00:00
|
|
|
INNER JOIN `*PREFIX*filecache`
|
2022-08-18 18:27:25 +00:00
|
|
|
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*memories`.`file_id`
|
2022-08-18 03:23:41 +00:00
|
|
|
AND (`*PREFIX*filecache`.`parent`=? OR `*PREFIX*filecache`.`fileid`=?)
|
2022-08-18 18:27:25 +00:00
|
|
|
WHERE `*PREFIX*memories`.`day_id`=?';
|
2022-08-18 03:23:41 +00:00
|
|
|
$rows = $this->connection->executeQuery($sql, [$folderId, $folderId, $dayId], [
|
2022-08-16 03:58:55 +00:00
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_INT,
|
|
|
|
])->fetchAll();
|
|
|
|
return $this->processDay($rows);
|
2022-08-13 03:34:05 +00:00
|
|
|
}
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|