2022-08-13 01:58:37 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-08-13 03:34:05 +00:00
|
|
|
namespace OCA\BetterPhotos\Db;
|
2022-08-13 01:58:37 +00:00
|
|
|
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IPreview;
|
|
|
|
|
|
|
|
class Util {
|
|
|
|
protected IPreview $previewGenerator;
|
|
|
|
protected IDBConnection $connection;
|
|
|
|
|
|
|
|
public function __construct(IPreview $previewGenerator,
|
|
|
|
IDBConnection $connection) {
|
|
|
|
$this->previewGenerator = $previewGenerator;
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
|
|
|
|
2022-08-15 03:25:12 +00:00
|
|
|
public static function getDateTaken($file) {
|
|
|
|
// Attempt to read exif data
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to creation time
|
2022-08-13 01:58:37 +00:00
|
|
|
$dateTaken = $file->getCreationTime();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function processFile(string $user, File $file, bool $update): void {
|
|
|
|
$mime = $file->getMimeType();
|
|
|
|
if (!str_starts_with($mime, 'image/')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->previewGenerator->isMimeSupported($file->getMimeType())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-14 23:19:13 +00:00
|
|
|
// Get parameters
|
|
|
|
$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
|
|
|
|
|
|
|
// Insert or update file
|
|
|
|
// todo: update dateTaken and dayId if needed
|
|
|
|
$sql = 'INSERT IGNORE
|
|
|
|
INTO oc_betterphotos (user_id, file_id, date_taken, day_id)
|
|
|
|
VALUES (?, ?, ?, ?)';
|
|
|
|
$res = $this->connection->executeStatement($sql, [
|
|
|
|
$user, $fileId, $dateTaken, $dayId,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Update day table
|
|
|
|
if ($res === 1) {
|
|
|
|
$sql = 'INSERT
|
|
|
|
INTO oc_betterphotos_day (user_id, day_id, count)
|
|
|
|
VALUES (?, ?, 1)
|
|
|
|
ON DUPLICATE KEY
|
|
|
|
UPDATE count = count + 1';
|
|
|
|
$this->connection->executeStatement($sql, [
|
|
|
|
$user, $dayId,
|
|
|
|
]);
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteFile(File $file) {
|
2022-08-14 23:19:13 +00:00
|
|
|
$sql = 'DELETE
|
|
|
|
FROM oc_betterphotos
|
|
|
|
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'];
|
|
|
|
$sql = 'UPDATE oc_betterphotos_day
|
|
|
|
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-14 23:31:47 +00:00
|
|
|
public static function getDays(
|
2022-08-13 03:34:05 +00:00
|
|
|
IDBConnection $connection,
|
|
|
|
string $user,
|
2022-08-14 23:31:47 +00:00
|
|
|
): array {
|
2022-08-13 03:34:05 +00:00
|
|
|
$qb = $connection->getQueryBuilder();
|
2022-08-14 23:31:47 +00:00
|
|
|
$qb->select('day_id', 'count')
|
|
|
|
->from('betterphotos_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-14 23:31:47 +00:00
|
|
|
public static function getDay(
|
2022-08-13 03:34:05 +00:00
|
|
|
IDBConnection $connection,
|
|
|
|
string $user,
|
2022-08-14 23:31:47 +00:00
|
|
|
int $dayId,
|
2022-08-13 03:34:05 +00:00
|
|
|
): array {
|
2022-08-14 23:31:47 +00:00
|
|
|
$qb = $connection->getQueryBuilder();
|
2022-08-15 21:41:05 +00:00
|
|
|
$qb->select('file_id')
|
2022-08-14 23:31:47 +00:00
|
|
|
->from('betterphotos')
|
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user)))
|
|
|
|
->andWhere($qb->expr()->eq('day_id', $qb->createNamedParameter($dayId)))
|
|
|
|
->orderBy('date_taken', 'DESC');
|
2022-08-13 03:34:05 +00:00
|
|
|
$result = $qb->executeQuery();
|
|
|
|
$rows = $result->fetchAll();
|
|
|
|
return $rows;
|
|
|
|
}
|
2022-08-13 01:58:37 +00:00
|
|
|
}
|