Browse Source

integrate clitor-is-protocol

main
ghost 11 months ago
parent
commit
29017dd2bb
  1. 6
      src/Controller/ViewController.php
  2. 67
      src/Twig/AppExtension.php

6
src/Controller/ViewController.php

@ -58,13 +58,13 @@ class ViewController extends AbstractController @@ -58,13 +58,13 @@ class ViewController extends AbstractController
);
}
if ($length = $reader->fileLength())
if ($size = $reader->fileSize())
{
if ($length == strlen($data))
if ($size == strlen($data))
{
$response->headers->set(
'Content-length',
$length
$size
);
}
}

67
src/Twig/AppExtension.php

@ -222,8 +222,32 @@ class AppExtension extends AbstractExtension @@ -222,8 +222,32 @@ class AppExtension extends AbstractExtension
return $text;
}
// Replace with _KEVA_NS_ value if defined
foreach ($matches[1] as $namespace)
{
// Replace with _CLITOR_IS_ value
if ($meta = $this->_clitor($namespace))
{
$text = str_replace(
$namespace,
sprintf(
'[%s](%s) (%s)',
$meta['file']['name'],
$this->container->get('router')->generate(
'view_raw',
[
'namespace' => $namespace
]
),
$this->formatBytes(
$meta['file']['size']
)
),
$text
);
}
// Replace with _KEVA_NS_ value
else
{
$text = str_replace(
$namespace,
@ -244,6 +268,7 @@ class AppExtension extends AbstractExtension @@ -244,6 +268,7 @@ class AppExtension extends AbstractExtension
);
}
}
}
return $text;
}
@ -277,6 +302,46 @@ class AppExtension extends AbstractExtension @@ -277,6 +302,46 @@ class AppExtension extends AbstractExtension
return $namespace;
}
private function _clitor(
string $namespace
): ?array
{
// Validate namespace supported to continue
if (preg_match('/^N[A-z0-9]{33}$/', $namespace))
{
// Connect kevacoin
$client = new \Kevachat\Kevacoin\Client(
$this->container->getParameter('app.kevacoin.protocol'),
$this->container->getParameter('app.kevacoin.host'),
$this->container->getParameter('app.kevacoin.port'),
$this->container->getParameter('app.kevacoin.username'),
$this->container->getParameter('app.kevacoin.password')
);
// Get meta data by namespace
if ($meta = $client->kevaGet($namespace, '_CLITOR_IS_'))
{
$reader = new \ClitorIsProtocol\Kevacoin\Reader(
$meta['value']
);
if ($reader->valid())
{
return
[
'file' =>
[
'name' => $reader->fileName() ? $reader->fileName() : $namespace,
'size' => (int) $reader->fileSize(),
]
];
}
}
}
return null;
}
private function _plural(int $number, array $texts)
{
$cases = [2, 0, 1, 1, 1, 2];

Loading…
Cancel
Save