Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Controller/PageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Mobizel\Bundle\MarkdownDocsBundle\Context\ReaderContextInterface;
use Mobizel\Bundle\MarkdownDocsBundle\DataProvider\PageItemDataProvider;
use Mobizel\Bundle\MarkdownDocsBundle\Dto\ImageResponse;
use Mobizel\Bundle\MarkdownDocsBundle\Event\PageEvent;
use Mobizel\Bundle\MarkdownDocsBundle\Helper\RouteHelperInterface;
use Mobizel\Bundle\MarkdownDocsBundle\PageEvents;
Expand Down Expand Up @@ -51,6 +52,12 @@ public function __invoke(Request $request, string $slug): Response
{
$context = $this->readerContext->getContext();

/// send image response if page ends with .jpeg ("foo/page.jpg" should be send as image )
if( $this->isJpegImage($slug)
&& file_exists($context->getDocsDir($request).DIRECTORY_SEPARATOR.$slug)){
$image = file_get_contents($context->getDocsDir($request).DIRECTORY_SEPARATOR.$slug);
return new ImageResponse($image,200);
}
// redirect a suffixed page ("foo/bar.md" should be redirected to "foo/bar")
if (false !== strpos($slug, '.md')) {
/** @var string $slug */
Expand All @@ -68,7 +75,6 @@ public function __invoke(Request $request, string $slug): Response
}

$page = $this->pageItemDataProvider->getPage($request);

if (null === $page) {
throw new NotFoundHttpException(sprintf('Page "%s" was not found', $slug));
}
Expand All @@ -79,4 +85,12 @@ public function __invoke(Request $request, string $slug): Response
'page' => $page,
]);
}


private function isJpegImage(string $slug): bool
{
$chunks = explode(".",$slug);
$extension = end($chunks);
return "jpeg" === $extension;
}
}
18 changes: 18 additions & 0 deletions src/Dto/ImageResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Mobizel\Bundle\MarkdownDocsBundle\Dto;

use Symfony\Component\HttpFoundation\Response;

class ImageResponse extends Response
{
public function __construct(?string $content = '', int $status = 200, array $headers = [])
{
$headers = array_merge($headers,
[
"Content-Type"=>"image/jpeg"
]
);
parent::__construct($content, $status, $headers);
}
}
12 changes: 12 additions & 0 deletions tests/Controller/PageActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public function testShowPage(): void
$this->assertSelectorTextSame('html h1', 'Documentation');
}

public function testImage():void
{
$client = static::createClient();

$client->request("GET","/current/products/books/stephen-king/img1.jpeg");
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$client->request("GET","/current/products/../products/books/stephen-king/img1.jpeg");
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$client->request("GET","/current/not-found.jpeg");
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}

public function testShowPageWithContextContainingOneRequirement(): void
{
$client = static::createClient();
Expand Down
Binary file added tests/docs/products/books/stephen-king/img1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.