Fix date update

pull/37/head
Varun Patil 2022-08-16 00:12:31 +00:00
parent cfbbe0c567
commit 1481526402
3 changed files with 39 additions and 13 deletions

View File

@ -146,6 +146,6 @@ class Index extends Command {
private function parseFile(IUser $user, File $file): void { private function parseFile(IUser $user, File $file): void {
$this->output->writeln('Generating entry for ' . $file->getPath() . ' ' . $file->getId()); $this->output->writeln('Generating entry for ' . $file->getPath() . ' ' . $file->getId());
$this->util->processFile($user->getUID(), $file, false); $this->util->processFile($user->getUID(), $file);
} }
} }

View File

@ -37,7 +37,7 @@ class Util {
return $dateTaken; return $dateTaken;
} }
public function processFile(string $user, File $file, bool $update): void { public function processFile(string $user, File $file): void {
$mime = $file->getMimeType(); $mime = $file->getMimeType();
if (!in_array($mime, Application::IMAGE_MIMES) && !in_array($mime, Application::VIDEO_MIMES)) { if (!in_array($mime, Application::IMAGE_MIMES) && !in_array($mime, Application::VIDEO_MIMES)) {
return; return;
@ -48,17 +48,34 @@ class Util {
$dateTaken = $this->getDateTaken($file); $dateTaken = $this->getDateTaken($file);
$dayId = floor($dateTaken / 86400); $dayId = floor($dateTaken / 86400);
// Get existing entry
$sql = 'SELECT * FROM oc_betterphotos WHERE
user_id = ? AND file_id = ?';
$res = $this->connection->executeQuery($sql, [
$user, $fileId,
]);
$erow = $res->fetch();
$exists = (bool)$erow;
// Insert or update file // Insert or update file
// todo: update dateTaken and dayId if needed if ($exists) {
$sql = 'INSERT IGNORE $sql = 'UPDATE oc_betterphotos SET
INTO oc_betterphotos (user_id, file_id, date_taken, day_id) day_id = ?, date_taken = ?
VALUES (?, ?, ?, ?)'; WHERE user_id = ? AND file_id = ?';
} else {
$sql = 'INSERT
INTO oc_betterphotos (day_id, date_taken, user_id, file_id)
VALUES (?, ?, ?, ?)';
}
$res = $this->connection->executeStatement($sql, [ $res = $this->connection->executeStatement($sql, [
$user, $fileId, $dateTaken, $dayId, $dayId, $dateTaken, $user, $fileId,
]); ]);
// Change of day
$dayChange = ($exists && intval($erow['day_id']) != $dayId);
// Update day table // Update day table
if ($res === 1) { if (!$exists || $dayChange) {
$sql = 'INSERT $sql = 'INSERT
INTO oc_betterphotos_day (user_id, day_id, count) INTO oc_betterphotos_day (user_id, day_id, count)
VALUES (?, ?, 1) VALUES (?, ?, 1)
@ -67,6 +84,17 @@ class Util {
$this->connection->executeStatement($sql, [ $this->connection->executeStatement($sql, [
$user, $dayId, $user, $dayId,
]); ]);
if ($dayChange) {
$sql = 'UPDATE oc_betterphotos_day SET
count = count - 1
WHERE user_id = ? AND day_id = ?';
$this->connection->executeStatement($sql, [
$user, $erow['day_id'],
], [
\PDO::PARAM_STR, \PDO::PARAM_INT,
]);
}
} }
} }

View File

@ -28,7 +28,6 @@ use OCP\EventDispatcher\IEventListener;
use OCP\Files\Events\Node\NodeTouchedEvent; use OCP\Files\Events\Node\NodeTouchedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent; use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\Files\Folder; use OCP\Files\Folder;
use OCP\IPreview;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IUserManager; use OCP\IUserManager;
@ -37,10 +36,9 @@ class PostWriteListener implements IEventListener {
private \OCA\BetterPhotos\Db\Util $util; private \OCA\BetterPhotos\Db\Util $util;
public function __construct(IDBConnection $connection, public function __construct(IDBConnection $connection,
IUserManager $userManager, IUserManager $userManager) {
IPreview $previewGenerator) {
$this->userManager = $userManager; $this->userManager = $userManager;
$this->util = new \OCA\BetterPhotos\Db\Util($previewGenerator, $connection); $this->util = new \OCA\BetterPhotos\Db\Util($connection);
} }
public function handle(Event $event): void { public function handle(Event $event): void {
@ -56,6 +54,6 @@ class PostWriteListener implements IEventListener {
return; return;
} }
$this->util->processFile($owner, $node, true); $this->util->processFile($owner, $node);
} }
} }