memories/lib/Db/Util.php

170 lines
5.2 KiB
PHP
Raw Normal View History

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 00:12:31 +00:00
// Get existing entry
2022-08-16 01:19:43 +00:00
$sql = 'SELECT * FROM oc_polaroid WHERE
2022-08-16 00:12:31 +00:00
user_id = ? AND file_id = ?';
$res = $this->connection->executeQuery($sql, [
$user, $fileId,
]);
$erow = $res->fetch();
$exists = (bool)$erow;
2022-08-14 23:19:13 +00:00
// Insert or update file
2022-08-16 00:12:31 +00:00
if ($exists) {
2022-08-16 01:19:43 +00:00
$sql = 'UPDATE oc_polaroid SET
2022-08-16 00:46:36 +00:00
day_id = ?, date_taken = ?, is_video = ?
2022-08-16 00:12:31 +00:00
WHERE user_id = ? AND file_id = ?';
} else {
$sql = 'INSERT
2022-08-16 01:19:43 +00:00
INTO oc_polaroid (day_id, date_taken, is_video, user_id, file_id)
2022-08-16 00:46:36 +00:00
VALUES (?, ?, ?, ?, ?)';
2022-08-16 00:12:31 +00:00
}
2022-08-14 23:19:13 +00:00
$res = $this->connection->executeStatement($sql, [
2022-08-16 00:46:36 +00:00
$dayId, $dateTaken, $is_video,
$user, $fileId,
], [
\PDO::PARAM_INT, \PDO::PARAM_INT, \PDO::PARAM_BOOL,
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
2022-08-14 23:19:13 +00:00
2022-08-16 00:12:31 +00:00
// Change of day
$dayChange = ($exists && intval($erow['day_id']) != $dayId);
2022-08-14 23:19:13 +00:00
// Update day table
2022-08-16 00:12:31 +00:00
if (!$exists || $dayChange) {
2022-08-14 23:19:13 +00:00
$sql = 'INSERT
2022-08-16 01:19:43 +00:00
INTO oc_polaroid_day (user_id, day_id, count)
2022-08-14 23:19:13 +00:00
VALUES (?, ?, 1)
ON DUPLICATE KEY
UPDATE count = count + 1';
$this->connection->executeStatement($sql, [
$user, $dayId,
]);
2022-08-16 00:12:31 +00:00
if ($dayChange) {
2022-08-16 01:19:43 +00:00
$sql = 'UPDATE oc_polaroid_day SET
2022-08-16 00:12:31 +00:00
count = count - 1
WHERE user_id = ? AND day_id = ?';
$this->connection->executeStatement($sql, [
$user, $erow['day_id'],
], [
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
}
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-14 23:19:13 +00:00
WHERE file_id = ?
RETURNING *';
$res = $this->connection->executeQuery($sql, [$file->getId()], [\PDO::PARAM_INT]);
$rows = $res->fetchAll();
foreach ($rows as $row) {
$dayId = $row['day_id'];
$userId = $row['user_id'];
2022-08-16 01:19:43 +00:00
$sql = 'UPDATE oc_polaroid_day
2022-08-14 23:19:13 +00:00
SET count = count - 1
WHERE user_id = ? AND day_id = ?';
$this->connection->executeStatement($sql, [$userId, $dayId], [
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
}
2022-08-13 01:58:37 +00:00
}
2022-08-13 03:34:05 +00:00
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-15 23:43:10 +00:00
$qb = $this->connection->getQueryBuilder();
2022-08-14 23:31:47 +00:00
$qb->select('day_id', 'count')
2022-08-16 01:19:43 +00:00
->from('polaroid_day')
2022-08-13 03:34:05 +00:00
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user)))
2022-08-14 23:31:47 +00:00
->orderBy('day_id', 'DESC');
$result = $qb->executeQuery();
$rows = $result->fetchAll();
return $rows;
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();
foreach($rows 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 $rows;
2022-08-13 03:34:05 +00:00
}
2022-08-13 01:58:37 +00:00
}