From fcc907690cd8e0cfc0f08d8eaea798f4aecb4259 Mon Sep 17 00:00:00 2001 From: Brooke Bryan Date: Sat, 29 Aug 2020 08:44:35 +0100 Subject: [PATCH] Content Download (#27) --- src/Dispatch.php | 13 ++++++++++--- src/ResourceManager.php | 12 +++++++++--- tests/DispatchTest.php | 28 +++++++++++++++++++++++++++- tests/ResourceManagerTest.php | 8 ++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/Dispatch.php b/src/Dispatch.php index 848e578..cb76873 100644 --- a/src/Dispatch.php +++ b/src/Dispatch.php @@ -65,7 +65,8 @@ class Dispatch protected $_acceptableTypes; protected $_bits = 0; - public const BIT_WEBP = 0b1; + public const FLAG_WEBP = 0b1; + public const FLAG_CONTENT_ATTACHMENT = 0b10; /** * @var ResponseCacheConfig */ @@ -288,7 +289,13 @@ public function handleRequest(Request $request): Response $resource->setOptions($this->config()->getSection('ext.' . $ext)->getItems()); } } - return ResourceFactory::create($resource, $contentHashMatch ? $this->_defaultCacheConfig : false); + $response = ResourceFactory::create($resource, $contentHashMatch ? $this->_defaultCacheConfig : false); + if(BitWise::has($this->getBits(), self::FLAG_CONTENT_ATTACHMENT)) + { + $filename = pathinfo($fullPath, PATHINFO_FILENAME); + $response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); + } + return $response; } public function config() @@ -365,7 +372,7 @@ public function getBits() if(in_array('image/webp', $this->getAcceptableContentTypes()) && $this->config()->getItem('optimisation', 'webp', false)) { - $this->_bits = BitWise::add($this->_bits, self::BIT_WEBP); + $this->_bits = BitWise::add($this->_bits, self::FLAG_WEBP); } return $this->_bits; } diff --git a/src/ResourceManager.php b/src/ResourceManager.php index 44f5114..f2e5a23 100644 --- a/src/ResourceManager.php +++ b/src/ResourceManager.php @@ -274,10 +274,12 @@ protected function _requireInlineJs($javascript, ?array $options = [], int $prio * * @param bool $allowComponentBubble If the resource does not exist in a component, attempt to load from its parent * + * @param null $flags + * * @return string|null - * @throws Exception + * @throws \ReflectionException */ - public function getResourceUri($relativeFullPath, bool $allowComponentBubble = true): ?string + public function getResourceUri($relativeFullPath, bool $allowComponentBubble = true, $flags = null): ?string { if($this->_type == self::MAP_EXTERNAL || $this->isExternalUrl($relativeFullPath)) { @@ -303,6 +305,10 @@ public function getResourceUri($relativeFullPath, bool $allowComponentBubble = t $hash = $this->getFileHash($filePath); $bits = Dispatch::instance()->getBits(); + if($flags !== null) + { + $bits = BitWise::add($bits, $flags); + } if(!$hash) { @@ -323,7 +329,7 @@ protected function _optimisePath($path, $relativeFullPath) $this->_optimizeWebP = ValueAs::bool(Dispatch::instance()->config()->getItem('optimisation', 'webp', false)); } - if($this->_optimizeWebP && BitWise::has(($this->_dispatch ?: Dispatch::instance())->getBits(), Dispatch::BIT_WEBP) + if($this->_optimizeWebP && BitWise::has(($this->_dispatch ?: Dispatch::instance())->getBits(), Dispatch::FLAG_WEBP) && in_array(substr($path, -4), ['.jpg', 'jpeg', '.png', '.gif', '.bmp', 'tiff', '.svg']) && file_exists($path . '.webp')) { diff --git a/tests/DispatchTest.php b/tests/DispatchTest.php index e604c39..2ec7af1 100644 --- a/tests/DispatchTest.php +++ b/tests/DispatchTest.php @@ -159,7 +159,10 @@ public function testWebpReplacements() $request = Request::create(ResourceManager::resources()->getResourceUri('css/webptest.css')); $response = $dispatch->handleRequest($request); - $this->assertContains('url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png?abc=def#xyz)', $response->getContent()); + $this->assertContains( + 'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png?abc=def#xyz)', + $response->getContent() + ); $this->assertNotContains( 'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png.webp?abc=def#xyz)', $response->getContent() @@ -184,4 +187,27 @@ public function testWebpReplacements() Dispatch::destroy(); } + public function testPdf() + { + $dispatch = new Dispatch(Path::system(__DIR__, '_root'), 'http://assets.packaged.in'); + Dispatch::bind($dispatch); + + $request = Request::create( + ResourceManager::resources() + ->getResourceUri('css/webptest.css') + ); + + $response = $dispatch->handleRequest($request); + $this->assertFalse($response->headers->has('Content-Disposition')); + + $request = Request::create( + ResourceManager::resources() + ->getResourceUri('css/webptest.css', true, Dispatch::FLAG_CONTENT_ATTACHMENT) + ); + + $response = $dispatch->handleRequest($request); + $this->assertTrue($response->headers->has('Content-Disposition')); + Dispatch::destroy(); + } + } diff --git a/tests/ResourceManagerTest.php b/tests/ResourceManagerTest.php index 98204cc..9899e79 100644 --- a/tests/ResourceManagerTest.php +++ b/tests/ResourceManagerTest.php @@ -55,6 +55,14 @@ public function testComponent() 'c/6/Packaged/Dispatch/Tests/TestComponents/DemoComponent/DemoComponent/1a9ffb748d31/style.css', $manager->getResourceUri('style.css') ); + $this->assertEquals( + 'c/6/Packaged/Dispatch/Tests/TestComponents/DemoComponent/DemoComponent/1a9ffb748d31-2/style.css', + $manager->getResourceUri('style.css', true, Dispatch::FLAG_CONTENT_ATTACHMENT) + ); + $this->assertEquals( + 'c/6/Packaged/Dispatch/Tests/TestComponents/DemoComponent/DemoComponent/1a9ffb748d31-3/style.css', + $manager->getResourceUri('style.css', true, Dispatch::FLAG_CONTENT_ATTACHMENT | Dispatch::FLAG_WEBP) + ); Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents', ''); $manager = ResourceManager::component($component); $this->assertEquals(