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-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-15 23:43:10 +00:00
|
|
|
if (in_array($file->getMimeType(), Application::IMAGE_MIMES)) {
|
|
|
|
$exif = exif_read_data($file->fopen('rb'));
|
|
|
|
$dt = $exif['DateTimeOriginal'];
|
|
|
|
if ($dt) {
|
|
|
|
$dt = \DateTime::createFromFormat('Y:m:d H:i:s', $dt);
|
|
|
|
if ($dt) {
|
|
|
|
return $dt->getTimestamp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-16 02:33:15 +00:00
|
|
|
$user = $file->getOwner()->getUID();
|
2022-08-14 23:19:13 +00:00
|
|
|
$fileId = $file->getId();
|
|
|
|
$dateTaken = $this->getDateTaken($file);
|
2022-08-14 23:31:47 +00:00
|
|
|
$dayId = floor($dateTaken / 86400);
|
2022-08-14 23:19:13 +00:00
|
|
|
|
2022-08-16 02:47:49 +00:00
|
|
|
$sql = 'INSERT
|
|
|
|
INTO oc_polaroid (day_id, date_taken, is_video, user_id, file_id)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
ON DUPLICATE KEY UPDATE
|
|
|
|
day_id = ?, date_taken = ?, is_video = ?';
|
|
|
|
$this->connection->executeStatement($sql, [
|
2022-08-16 00:46:36 +00:00
|
|
|
$dayId, $dateTaken, $is_video,
|
|
|
|
$user, $fileId,
|
2022-08-16 02:47:49 +00:00
|
|
|
$dayId, $dateTaken, $is_video,
|
2022-08-16 00:46:36 +00:00
|
|
|
], [
|
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_BOOL,
|
|
|
|
\PDO::PARAM_STR, \PDO::PARAM_INT,
|
2022-08-16 02:47:49 +00:00
|
|
|
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_BOOL,
|
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-16 01:19:43 +00:00
|
|
|
FROM oc_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
|
|
|
|
FROM `oc_polaroid`
|
|
|
|
WHERE user_id=?
|
|
|
|
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
|
|
|
|
FROM `oc_polaroid`
|
|
|
|
INNER JOIN `oc_filecache`
|
|
|
|
ON `oc_polaroid`.`file_id` = `oc_filecache`.`fileid`
|
|
|
|
AND (`oc_filecache`.`parent`=? OR `oc_filecache`.`fileid`=?)
|
|
|
|
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-16 00:46:36 +00:00
|
|
|
$sql = 'SELECT file_id, oc_filecache.etag, is_video
|
2022-08-16 01:19:43 +00:00
|
|
|
FROM oc_polaroid
|
2022-08-15 23:14:24 +00:00
|
|
|
LEFT JOIN oc_filecache
|
2022-08-16 01:19:43 +00:00
|
|
|
ON oc_filecache.fileid = oc_polaroid.file_id
|
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 {
|
|
|
|
$sql = 'SELECT file_id, oc_filecache.etag, is_video
|
|
|
|
FROM `oc_polaroid`
|
|
|
|
INNER JOIN `oc_filecache`
|
|
|
|
ON `oc_polaroid`.`day_id`=?
|
|
|
|
AND `oc_polaroid`.`file_id` = `oc_filecache`.`fileid`
|
|
|
|
AND (`oc_filecache`.`parent`=? OR `oc_filecache`.`fileid`=?);';
|
|
|
|
$rows = $this->connection->executeQuery($sql, [$dayId, $folderId, $folderId], [
|
|
|
|
\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
|
|
|
}
|