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;
|
|
|
|
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-19 21:38:18 +00:00
|
|
|
private static function getExif(File $file) {
|
2022-08-15 03:25:12 +00:00
|
|
|
// Attempt to read exif data
|
2022-08-19 21:38:18 +00:00
|
|
|
try {
|
2022-08-19 21:42:23 +00:00
|
|
|
// Start exiftool and output to json
|
2022-08-19 21:38:18 +00:00
|
|
|
$pipes = [];
|
|
|
|
$proc = proc_open('exiftool -json -', [
|
|
|
|
0 => array('pipe', 'rb'),
|
|
|
|
1 => array('pipe', 'w'),
|
|
|
|
2 => array('pipe', 'w'),
|
|
|
|
], $pipes);
|
|
|
|
|
2022-08-19 21:42:23 +00:00
|
|
|
// Write the file to exiftool's stdin
|
2022-08-19 21:53:57 +00:00
|
|
|
// Assume exif exists in the first 256 kb of the file
|
|
|
|
$handle = $file->fopen('rb');
|
|
|
|
stream_copy_to_stream($handle, $pipes[0], 256 * 1024);
|
|
|
|
fclose($handle);
|
2022-08-19 21:38:18 +00:00
|
|
|
fclose($pipes[0]);
|
2022-08-19 21:53:57 +00:00
|
|
|
|
|
|
|
// Get output from exiftool
|
2022-08-19 21:38:18 +00:00
|
|
|
$stdout = stream_get_contents($pipes[1]);
|
2022-08-19 21:42:23 +00:00
|
|
|
|
|
|
|
// Clean up
|
|
|
|
fclose($pipes[1]);
|
|
|
|
fclose($pipes[2]);
|
2022-08-19 21:38:18 +00:00
|
|
|
proc_close($proc);
|
|
|
|
|
2022-08-19 21:42:23 +00:00
|
|
|
// Parse the json
|
2022-08-19 21:38:18 +00:00
|
|
|
$json = json_decode($stdout, true);
|
|
|
|
if (empty($json)) {
|
|
|
|
throw new \Exception('Could not read exif data');
|
|
|
|
}
|
|
|
|
return $json[0];
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getDateTaken(File $file, array $exif) {
|
|
|
|
$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-16 02:33:15 +00:00
|
|
|
public function processFile(File $file): void {
|
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
|
|
|
|
$sql = 'SELECT COUNT(*) as e
|
2022-08-18 18:27:25 +00:00
|
|
|
FROM *PREFIX*memories
|
2022-08-18 00:35:14 +00:00
|
|
|
WHERE file_id = ? AND user_id = ? AND mtime = ?';
|
2022-08-17 23:51:48 +00:00
|
|
|
$exists = $this->connection->executeQuery($sql, [
|
2022-08-18 00:35:14 +00:00
|
|
|
$fileId, $user, $mtime,
|
2022-08-17 23:51:48 +00:00
|
|
|
], [
|
2022-08-18 00:35:14 +00:00
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_INT,
|
2022-08-17 23:51:48 +00:00
|
|
|
])->fetch();
|
|
|
|
if (intval($exists['e']) > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-19 21:38:18 +00:00
|
|
|
// Get exif data
|
|
|
|
$exif = self::getExif($file);
|
|
|
|
|
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-16 02:47:49 +00:00
|
|
|
$sql = 'INSERT
|
2022-08-18 18:27:25 +00:00
|
|
|
INTO *PREFIX*memories (day_id, date_taken, is_video, mtime, user_id, file_id)
|
2022-08-18 00:35:14 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
2022-08-16 02:47:49 +00:00
|
|
|
ON DUPLICATE KEY UPDATE
|
2022-08-18 00:35:14 +00:00
|
|
|
day_id = ?, date_taken = ?, is_video = ?, mtime = ?';
|
2022-08-16 02:47:49 +00:00
|
|
|
$this->connection->executeStatement($sql, [
|
2022-08-18 00:35:14 +00:00
|
|
|
$dayId, $dateTaken, $is_video, $mtime,
|
2022-08-16 00:46:36 +00:00
|
|
|
$user, $fileId,
|
2022-08-18 00:35:14 +00:00
|
|
|
$dayId, $dateTaken, $is_video, $mtime,
|
2022-08-16 00:46:36 +00:00
|
|
|
], [
|
2022-08-18 00:35:14 +00:00
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_BOOL, \PDO::PARAM_INT,
|
2022-08-16 00:46:36 +00:00
|
|
|
\PDO::PARAM_STR, \PDO::PARAM_INT,
|
2022-08-18 00:35:14 +00:00
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_STR, \PDO::PARAM_BOOL, \PDO::PARAM_INT,
|
2022-08-16 00:46:36 +00:00
|
|
|
]);
|
2022-08-13 01:58:37 +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-16 03:58:55 +00:00
|
|
|
public function processDays(&$days) {
|
|
|
|
foreach($days as &$row) {
|
|
|
|
$row["day_id"] = intval($row["day_id"]);
|
|
|
|
$row["count"] = intval($row["count"]);
|
|
|
|
}
|
|
|
|
return $days;
|
|
|
|
}
|
|
|
|
|
2022-08-15 23:43:10 +00:00
|
|
|
public function getDays(
|
2022-08-13 03:34:05 +00:00
|
|
|
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-18 00:35:14 +00:00
|
|
|
AND `*PREFIX*filecache`.`path` LIKE "files/Photos/%"
|
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';
|
|
|
|
$rows = $this->connection->executeQuery($sql, [$user], [
|
|
|
|
\PDO::PARAM_STR,
|
|
|
|
])->fetchAll();
|
2022-08-16 03:58:55 +00:00
|
|
|
return $this->processDays($rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDaysFolder(int $folderId) {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processDay(&$day) {
|
|
|
|
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-15 23:43:10 +00:00
|
|
|
public function getDay(
|
2022-08-13 03:34:05 +00:00
|
|
|
string $user,
|
2022-08-14 23:31:47 +00:00
|
|
|
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-18 00:35:14 +00:00
|
|
|
AND `*PREFIX*filecache`.`path` LIKE "files/Photos/%"
|
2022-08-15 23:14:24 +00:00
|
|
|
WHERE user_id = ? AND day_id = ?
|
|
|
|
ORDER BY date_taken DESC';
|
2022-08-15 23:43:10 +00:00
|
|
|
$rows = $this->connection->executeQuery($sql, [$user, $dayId], [
|
2022-08-15 23:14:24 +00:00
|
|
|
\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-16 03:58:55 +00:00
|
|
|
public function getDayFolder(
|
|
|
|
int $folderId,
|
|
|
|
int $dayId,
|
|
|
|
): 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
|
|
|
}
|