2022-08-13 01:58:37 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-08-16 01:19:43 +00:00
|
|
|
namespace OCA\Polaroid\Db;
|
2022-08-13 01:58:37 +00:00
|
|
|
|
2022-08-16 01:19:43 +00:00
|
|
|
use OCA\Polaroid\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-17 21:45:01 +00:00
|
|
|
private static function getExif($data, $field) {
|
|
|
|
$pipes = [];
|
|
|
|
$proc = proc_open('exiftool -b -' . $field . ' -', [
|
|
|
|
0 => array('pipe', 'rb'),
|
|
|
|
1 => array('pipe', 'w'),
|
|
|
|
2 => array('pipe', 'w'),
|
|
|
|
], $pipes);
|
|
|
|
|
|
|
|
fwrite($pipes[0], $data);
|
|
|
|
fclose($pipes[0]);
|
|
|
|
$stdout = stream_get_contents($pipes[1]);
|
|
|
|
proc_close($proc);
|
|
|
|
return $stdout;
|
|
|
|
}
|
|
|
|
|
2022-08-16 00:32:57 +00:00
|
|
|
public static function getDateTaken(File $file) {
|
2022-08-15 03:25:12 +00:00
|
|
|
// Attempt to read exif data
|
2022-08-17 21:45:01 +00:00
|
|
|
// Assume it exists in the first 256 kb of the file
|
|
|
|
$handle = $file->fopen('rb');
|
|
|
|
$data = stream_get_contents($handle, 256 * 1024);
|
|
|
|
fclose($handle);
|
|
|
|
|
|
|
|
// Try different formats
|
|
|
|
$dt = self::getExif($data, 'DateTimeOriginal');
|
|
|
|
if (empty($dt)) {
|
|
|
|
$dt = self::getExif($data, 'CreateDate');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if found something
|
|
|
|
if (!empty($dt)) {
|
|
|
|
$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-17 23:56:14 +00:00
|
|
|
FROM *PREFIX*polaroid
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get more parameters
|
2022-08-14 23:19:13 +00:00
|
|
|
$dateTaken = $this->getDateTaken($file);
|
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 00:35:14 +00:00
|
|
|
INTO *PREFIX*polaroid (day_id, date_taken, is_video, mtime, user_id, file_id)
|
|
|
|
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-17 23:56:14 +00:00
|
|
|
FROM *PREFIX*polaroid
|
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-17 23:56:14 +00:00
|
|
|
FROM `*PREFIX*polaroid`
|
2022-08-18 00:35:14 +00:00
|
|
|
INNER JOIN `*PREFIX*filecache`
|
2022-08-18 03:23:41 +00:00
|
|
|
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*polaroid`.`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-17 23:56:14 +00:00
|
|
|
FROM `*PREFIX*polaroid`
|
|
|
|
INNER JOIN `*PREFIX*filecache`
|
2022-08-18 03:23:41 +00:00
|
|
|
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*polaroid`.`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
|
|
|
|
FROM *PREFIX*polaroid
|
2022-08-18 03:23:41 +00:00
|
|
|
INNER JOIN *PREFIX*filecache
|
2022-08-18 00:35:14 +00:00
|
|
|
ON *PREFIX*filecache.fileid = *PREFIX*polaroid.file_id
|
|
|
|
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
|
|
|
|
FROM `*PREFIX*polaroid`
|
|
|
|
INNER JOIN `*PREFIX*filecache`
|
2022-08-18 03:23:41 +00:00
|
|
|
ON `*PREFIX*filecache`.`fileid` = `*PREFIX*polaroid`.`file_id`
|
|
|
|
AND (`*PREFIX*filecache`.`parent`=? OR `*PREFIX*filecache`.`fileid`=?)
|
|
|
|
WHERE `*PREFIX*polaroid`.`day_id`=?';
|
|
|
|
$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
|
|
|
}
|