Browse Source

update MIME type detection

PHP-GTK3
yggverse 4 months ago
parent
commit
7230bec4cf
  1. 69
      src/Model/Connection/Gemini.php

69
src/Model/Connection/Gemini.php

@ -9,6 +9,7 @@ use \Yggverse\Gemini\Client\Response; @@ -9,6 +9,7 @@ use \Yggverse\Gemini\Client\Response;
use \Yggverse\Net\Address;
use \Yggverse\Yoda\Model\Connection;
use \Yggverse\Yoda\Model\Filesystem;
class Gemini
{
@ -55,44 +56,33 @@ class Gemini @@ -55,44 +56,33 @@ class Gemini
case 20: // ok
// Update content data
$this->_connection->setData(
$response->getBody()
);
// Detect MIME type
switch (true)
{
case str_contains(
$response->getMeta(),
$this->_connection::MIME_TEXT_GEMINI
):
$this->_connection->setMime(
$this->_connection::MIME_TEXT_GEMINI
);
break;
case str_contains(
$response->getMeta(),
$this->_connection::MIME_TEXT_PLAIN
):
$this->_connection->setMime(
$this->_connection::MIME_TEXT_PLAIN
);
case $mime = self::getMimeByMeta(
$response->getMeta()
): break;
break;
case $mime = Filesystem::getMimeByPath(
$address->getPath()
): break;
default:
case $mime = Filesystem::getMimeByData(
$response->getData()
): break;
throw new \Exception(
sprintf(
_('MIME type not implemented for %s'),
$response->getMeta()
)
);
default: $mime = null;
}
$this->_connection->setMime(
$mime
);
break;
case 31: // redirect
@ -109,10 +99,6 @@ class Gemini @@ -109,10 +99,6 @@ class Gemini
)
);
$this->_connection->setMime(
$this->_connection::MIME_TEXT_GEMINI
);
break;
default:
@ -139,4 +125,25 @@ class Gemini @@ -139,4 +125,25 @@ class Gemini
true
);
}
public static function getMimeByMeta(
?string $meta = null
): ?string
{
if ($meta)
{
preg_match(
'/(?<mime>([\w]+\/[\w]+))/m',
$meta,
$match
);
if (isset($match['mime']))
{
return $match['mime'];
}
}
return null;
}
}
Loading…
Cancel
Save