From 29dd777b5b6e48a06e45ee532f66f971e03b8e3e Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Thu, 19 Jan 2023 16:18:22 +0100 Subject: [PATCH] Turn the session file into a session directory --- examples/bot.php | 4 +- src/danog/MadelineProto/APIWrapper.php | 2 - .../MadelineProto/Db/Driver/Postgres.php | 2 +- src/danog/MadelineProto/Ipc/Client.php | 6 +-- src/danog/MadelineProto/Ipc/Runner/entry.php | 5 ++- src/danog/MadelineProto/SessionPaths.php | 42 ++++++++++++++----- src/danog/MadelineProto/TL/Types/Button.php | 2 +- 7 files changed, 42 insertions(+), 21 deletions(-) diff --git a/examples/bot.php b/examples/bot.php index 532f1b6da..8284d4aee 100755 --- a/examples/bot.php +++ b/examples/bot.php @@ -23,6 +23,8 @@ use danog\MadelineProto\EventHandler; use danog\MadelineProto\Logger; use danog\MadelineProto\Settings; use danog\MadelineProto\Settings\Database\Mysql; +use danog\MadelineProto\Settings\Database\Postgres; +use danog\MadelineProto\Settings\Database\Redis; /* * Various ways to load MadelineProto @@ -61,7 +63,7 @@ class MyEventHandler extends EventHandler /** * @var DbArray */ - protected $dataStoredOnDb; + protected DbArray $dataStoredOnDb; /** * Get peer(s) where to report errors. diff --git a/src/danog/MadelineProto/APIWrapper.php b/src/danog/MadelineProto/APIWrapper.php index 7cca21ed2..bb4407118 100644 --- a/src/danog/MadelineProto/APIWrapper.php +++ b/src/danog/MadelineProto/APIWrapper.php @@ -22,8 +22,6 @@ namespace danog\MadelineProto; use danog\MadelineProto\Ipc\Client; -use function Amp\File\openFile; - final class APIWrapper { /** diff --git a/src/danog/MadelineProto/Db/Driver/Postgres.php b/src/danog/MadelineProto/Db/Driver/Postgres.php index fbc3ff91e..4d9056f2b 100644 --- a/src/danog/MadelineProto/Db/Driver/Postgres.php +++ b/src/danog/MadelineProto/Db/Driver/Postgres.php @@ -46,7 +46,7 @@ final class Postgres $result = $connection->query("SELECT * FROM pg_database WHERE datname = '{$db}'"); // Replace with getRowCount once it gets fixed - if (!iterator_to_array($result)) { + if (!\iterator_to_array($result)) { $connection->query(" CREATE DATABASE {$db} OWNER {$user} diff --git a/src/danog/MadelineProto/Ipc/Client.php b/src/danog/MadelineProto/Ipc/Client.php index f65820d5b..9e6bc88a8 100644 --- a/src/danog/MadelineProto/Ipc/Client.php +++ b/src/danog/MadelineProto/Ipc/Client.php @@ -70,7 +70,7 @@ final class Client extends ClientAbstract $this->logger = $logger; $this->server = $server; $this->session = $session; - self::$instances[$session->getLegacySessionPath()] = $this; + self::$instances[$session->getSessionDirectoryPath()] = $this; async($this->loopInternal(...)); } /** @@ -94,8 +94,8 @@ final class Client extends ClientAbstract } catch (Throwable $e) { $this->logger("An error occurred while disconnecting the client: $e"); } - if (isset(self::$instances[$this->session->getLegacySessionPath()])) { - unset(self::$instances[$this->session->getLegacySessionPath()]); + if (isset(self::$instances[$this->session->getSessionDirectoryPath()])) { + unset(self::$instances[$this->session->getSessionDirectoryPath()]); } } /** diff --git a/src/danog/MadelineProto/Ipc/Runner/entry.php b/src/danog/MadelineProto/Ipc/Runner/entry.php index 09649f664..e6bd1440d 100644 --- a/src/danog/MadelineProto/Ipc/Runner/entry.php +++ b/src/danog/MadelineProto/Ipc/Runner/entry.php @@ -83,8 +83,8 @@ use Webmozart\Assert\Assert; include $autoloadPath; } if (MADELINE_WORKER_TYPE === 'madeline-ipc') { - $session = new SessionPaths(MADELINE_WORKER_ARGS[0]); - if (!file_exists($session->getSessionPath())) { + $session = MADELINE_WORKER_ARGS[0]; + if (!file_exists($session)) { trigger_error("IPC session $session does not exist!", E_USER_ERROR); exit(1); } @@ -102,6 +102,7 @@ use Webmozart\Assert\Assert; $runnerId = MADELINE_WORKER_ARGS[1]; Assert::numeric($runnerId); $runnerId = (int) $runnerId; + $session = new SessionPaths($session); try { Magic::start(); diff --git a/src/danog/MadelineProto/SessionPaths.php b/src/danog/MadelineProto/SessionPaths.php index 9cfcdc273..240818593 100644 --- a/src/danog/MadelineProto/SessionPaths.php +++ b/src/danog/MadelineProto/SessionPaths.php @@ -27,8 +27,12 @@ use const LOCK_SH; use const PHP_MAJOR_VERSION; use const PHP_MINOR_VERSION; use const PHP_VERSION; + +use function Amp\File\createDirectory; use function Amp\File\exists; use function Amp\File\getStatus; +use function Amp\File\isDirectory; +use function Amp\File\isFile; use function Amp\File\move; use function Amp\File\openFile; use function Amp\File\touch; @@ -43,7 +47,7 @@ final class SessionPaths /** * Legacy session path. */ - private string $legacySessionPath; + private string $sessionDirectoryPath; /** * Session path. */ @@ -81,13 +85,29 @@ final class SessionPaths public function __construct(string $session) { $session = Tools::absolute($session); - $this->legacySessionPath = $session; - $this->sessionPath = "$session.safe.php"; - $this->lightStatePath = "$session.lightState.php"; - $this->lockPath = "$session.lock"; - $this->ipcPath = "$session.ipc"; - $this->ipcCallbackPath = "$session.callback.ipc"; - $this->ipcStatePath = "$session.ipcState.php"; + $this->sessionDirectoryPath = $session; + $this->sessionPath = "$session/safe.php"; + $this->lightStatePath = "$session/lightState.php"; + $this->lockPath = "$session/lock"; + $this->ipcPath = "$session/ipc"; + $this->ipcCallbackPath = "$session/callback.ipc"; + $this->ipcStatePath = "$session/ipcState.php"; + if (!exists($session)) { + createDirectory($session); + return; + } + if (!isDirectory($session) && isFile("$session.safe.php")) { + \unlink($session); + createDirectory($session); + foreach (['safe.php', 'lightState.php', 'lock', 'ipc', 'callback.ipc', 'ipcState.php'] as $part) { + if (exists("$session.$part")) { + move("$session.$part", "$session/$part"); + } + if (exists("$session.$part.lock")) { + move("$session.$part.lock", "$session/$part.lock"); + } + } + } } /** * Serialize object to file. @@ -166,15 +186,15 @@ final class SessionPaths */ public function __toString(): string { - return $this->legacySessionPath; + return $this->sessionDirectoryPath; } /** * Get legacy session path. */ - public function getLegacySessionPath(): string + public function getSessionDirectoryPath(): string { - return $this->legacySessionPath; + return $this->sessionDirectoryPath; } /** diff --git a/src/danog/MadelineProto/TL/Types/Button.php b/src/danog/MadelineProto/TL/Types/Button.php index 8f30c33b1..db149141d 100644 --- a/src/danog/MadelineProto/TL/Types/Button.php +++ b/src/danog/MadelineProto/TL/Types/Button.php @@ -78,7 +78,7 @@ final class Button implements JsonSerializable, ArrayAccess $this->button = $button; $this->id = $message['id']; $this->API = $API; - $this->session = $API->getWrapper()->getSession()->getLegacySessionPath(); + $this->session = $API->getWrapper()->getSession()->getSessionDirectoryPath(); } /** * Sleep function.