From 4804fbe1816ef4d99563448762697ca076b01bdf Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 26 Jan 2024 15:15:16 +0200 Subject: [PATCH] implement media viewer --- README.md | 2 +- composer.json | 3 ++- src/controller/media.php | 50 ++++++++++++++++++++++++++++++++++++ src/server.php | 55 ++++++++++++++++++++++++++++++---------- 4 files changed, 95 insertions(+), 15 deletions(-) create mode 100644 src/controller/media.php diff --git a/README.md b/README.md index fb2bef8..da2858c 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ KevaChat Application for Gemini Protocol * [ ] Post publication * [ ] Post replies * [ ] Rooms publication -* [ ] Media viewer +* [x] Media viewer * [ ] Users auth * [x] Error handlers diff --git a/composer.json b/composer.json index 6e4f5c8..23d6047 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ }, "require": { "yggverse/titan-ii": "^1.0", - "kevachat/kevacoin": "^1.6" + "kevachat/kevacoin": "^1.6", + "clitor-is-protocol/kevacoin": "^1.0" } } diff --git a/src/controller/media.php b/src/controller/media.php new file mode 100644 index 0000000..4a6613f --- /dev/null +++ b/src/controller/media.php @@ -0,0 +1,50 @@ +_config = $config; + + // Init kevacoin + $this->_kevacoin = new \Kevachat\Kevacoin\Client( + $this->_config->kevacoin->server->protocol, + $this->_config->kevacoin->server->host, + $this->_config->kevacoin->server->port, + $this->_config->kevacoin->server->username, + $this->_config->kevacoin->server->password + ); + } + + public function raw(string $namespace, ?string &$mime = null): mixed + { + if ($clitoris = $this->_kevacoin->kevaGet($namespace, '_CLITOR_IS_')) + { + $reader = new \ClitorIsProtocol\Kevacoin\Reader( + $clitoris['value'] + ); + + if ($reader->valid()) + { + if ($pieces = $this->_kevacoin->kevaFilter($namespace)) + { + if ($data = $reader->data($pieces)) + { + $mime = $reader->fileMime(); + + return $data; + } + } + } + } + + return null; + } +} \ No newline at end of file diff --git a/src/server.php b/src/server.php index 22830fc..299d901 100644 --- a/src/server.php +++ b/src/server.php @@ -104,24 +104,53 @@ foreach ((array) scandir(__DIR__ . '/../host') as $host) // Dynamical requests default: - // Room posts by namespace - if (preg_match('/^\/room\/(N[A-z0-9]{33})$/', $request->getPath(), $matches)) + if (preg_match('/^\/([A-z]+)\/(N[A-z0-9]{33})$/', $request->getPath(), $matches)) { - if (!empty($matches[1])) + if (!empty($matches[1]) && !empty($matches[2])) { - include_once __DIR__ . '/controller/room.php'; + switch ($matches[1]) + { + case 'room': - $room = new \Kevachat\Geminiapp\Controller\Room( - $config - ); + include_once __DIR__ . '/controller/room.php'; - if ($posts = $room->posts($matches[1])) - { - $response->setContent( - $posts - ); + $room = new \Kevachat\Geminiapp\Controller\Room( + $config + ); + + if ($posts = $room->posts($matches[2])) + { + $response->setContent( + $posts + ); + + return $response; + } + + break; + + case 'raw': + + include_once __DIR__ . '/controller/media.php'; + + $media = new \Kevachat\Geminiapp\Controller\Media( + $config + ); + + if ($data = $media->raw($matches[2], $mime)) + { + $response->setMeta( + $mime + ); + + $response->setContent( + $data + ); + + return $response; + } - return $response; + break; } } }