Skip to content

Commit

Permalink
Content Download (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Aug 29, 2020
1 parent f1a66e3 commit fcc9076
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
13 changes: 10 additions & 3 deletions src/Dispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 9 additions & 3 deletions src/ResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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)
{
Expand All @@ -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'))
{
Expand Down
28 changes: 27 additions & 1 deletion tests/DispatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}

}
8 changes: 8 additions & 0 deletions tests/ResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit fcc9076

Please sign in to comment.