mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-02 09:35:28 +00:00
update MIME type detection
This commit is contained in:
parent
6d3f669603
commit
7230bec4cf
1 changed files with 38 additions and 31 deletions
|
|
@ -9,6 +9,7 @@ use \Yggverse\Gemini\Client\Response;
|
||||||
use \Yggverse\Net\Address;
|
use \Yggverse\Net\Address;
|
||||||
|
|
||||||
use \Yggverse\Yoda\Model\Connection;
|
use \Yggverse\Yoda\Model\Connection;
|
||||||
|
use \Yggverse\Yoda\Model\Filesystem;
|
||||||
|
|
||||||
class Gemini
|
class Gemini
|
||||||
{
|
{
|
||||||
|
|
@ -55,44 +56,33 @@ class Gemini
|
||||||
|
|
||||||
case 20: // ok
|
case 20: // ok
|
||||||
|
|
||||||
|
// Update content data
|
||||||
$this->_connection->setData(
|
$this->_connection->setData(
|
||||||
$response->getBody()
|
$response->getBody()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Detect MIME type
|
||||||
switch (true)
|
switch (true)
|
||||||
{
|
{
|
||||||
case str_contains(
|
case $mime = self::getMimeByMeta(
|
||||||
$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
|
|
||||||
);
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
|
|
||||||
throw new \Exception(
|
|
||||||
sprintf(
|
|
||||||
_('MIME type not implemented for %s'),
|
|
||||||
$response->getMeta()
|
$response->getMeta()
|
||||||
)
|
): break;
|
||||||
);
|
|
||||||
|
case $mime = Filesystem::getMimeByPath(
|
||||||
|
$address->getPath()
|
||||||
|
): break;
|
||||||
|
|
||||||
|
case $mime = Filesystem::getMimeByData(
|
||||||
|
$response->getData()
|
||||||
|
): break;
|
||||||
|
|
||||||
|
default: $mime = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->_connection->setMime(
|
||||||
|
$mime
|
||||||
|
);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 31: // redirect
|
case 31: // redirect
|
||||||
|
|
@ -109,10 +99,6 @@ class Gemini
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->_connection->setMime(
|
|
||||||
$this->_connection::MIME_TEXT_GEMINI
|
|
||||||
);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
@ -139,4 +125,25 @@ class Gemini
|
||||||
true
|
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…
Add table
Add a link
Reference in a new issue