Skip to content

Commit fcc9076

Browse files
authored
Content Download (#27)
1 parent f1a66e3 commit fcc9076

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

src/Dispatch.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class Dispatch
6565
protected $_acceptableTypes;
6666
protected $_bits = 0;
6767

68-
public const BIT_WEBP = 0b1;
68+
public const FLAG_WEBP = 0b1;
69+
public const FLAG_CONTENT_ATTACHMENT = 0b10;
6970
/**
7071
* @var ResponseCacheConfig
7172
*/
@@ -288,7 +289,13 @@ public function handleRequest(Request $request): Response
288289
$resource->setOptions($this->config()->getSection('ext.' . $ext)->getItems());
289290
}
290291
}
291-
return ResourceFactory::create($resource, $contentHashMatch ? $this->_defaultCacheConfig : false);
292+
$response = ResourceFactory::create($resource, $contentHashMatch ? $this->_defaultCacheConfig : false);
293+
if(BitWise::has($this->getBits(), self::FLAG_CONTENT_ATTACHMENT))
294+
{
295+
$filename = pathinfo($fullPath, PATHINFO_FILENAME);
296+
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
297+
}
298+
return $response;
292299
}
293300

294301
public function config()
@@ -365,7 +372,7 @@ public function getBits()
365372
if(in_array('image/webp', $this->getAcceptableContentTypes())
366373
&& $this->config()->getItem('optimisation', 'webp', false))
367374
{
368-
$this->_bits = BitWise::add($this->_bits, self::BIT_WEBP);
375+
$this->_bits = BitWise::add($this->_bits, self::FLAG_WEBP);
369376
}
370377
return $this->_bits;
371378
}

src/ResourceManager.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,12 @@ protected function _requireInlineJs($javascript, ?array $options = [], int $prio
274274
*
275275
* @param bool $allowComponentBubble If the resource does not exist in a component, attempt to load from its parent
276276
*
277+
* @param null $flags
278+
*
277279
* @return string|null
278-
* @throws Exception
280+
* @throws \ReflectionException
279281
*/
280-
public function getResourceUri($relativeFullPath, bool $allowComponentBubble = true): ?string
282+
public function getResourceUri($relativeFullPath, bool $allowComponentBubble = true, $flags = null): ?string
281283
{
282284
if($this->_type == self::MAP_EXTERNAL || $this->isExternalUrl($relativeFullPath))
283285
{
@@ -303,6 +305,10 @@ public function getResourceUri($relativeFullPath, bool $allowComponentBubble = t
303305
$hash = $this->getFileHash($filePath);
304306

305307
$bits = Dispatch::instance()->getBits();
308+
if($flags !== null)
309+
{
310+
$bits = BitWise::add($bits, $flags);
311+
}
306312

307313
if(!$hash)
308314
{
@@ -323,7 +329,7 @@ protected function _optimisePath($path, $relativeFullPath)
323329
$this->_optimizeWebP = ValueAs::bool(Dispatch::instance()->config()->getItem('optimisation', 'webp', false));
324330
}
325331

326-
if($this->_optimizeWebP && BitWise::has(($this->_dispatch ?: Dispatch::instance())->getBits(), Dispatch::BIT_WEBP)
332+
if($this->_optimizeWebP && BitWise::has(($this->_dispatch ?: Dispatch::instance())->getBits(), Dispatch::FLAG_WEBP)
327333
&& in_array(substr($path, -4), ['.jpg', 'jpeg', '.png', '.gif', '.bmp', 'tiff', '.svg'])
328334
&& file_exists($path . '.webp'))
329335
{

tests/DispatchTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ public function testWebpReplacements()
159159

160160
$request = Request::create(ResourceManager::resources()->getResourceUri('css/webptest.css'));
161161
$response = $dispatch->handleRequest($request);
162-
$this->assertContains('url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png?abc=def#xyz)', $response->getContent());
162+
$this->assertContains(
163+
'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png?abc=def#xyz)',
164+
$response->getContent()
165+
);
163166
$this->assertNotContains(
164167
'url(http://assets.packaged.in/r/30c60da9f504/img/test-sample.png.webp?abc=def#xyz)',
165168
$response->getContent()
@@ -184,4 +187,27 @@ public function testWebpReplacements()
184187
Dispatch::destroy();
185188
}
186189

190+
public function testPdf()
191+
{
192+
$dispatch = new Dispatch(Path::system(__DIR__, '_root'), 'http://assets.packaged.in');
193+
Dispatch::bind($dispatch);
194+
195+
$request = Request::create(
196+
ResourceManager::resources()
197+
->getResourceUri('css/webptest.css')
198+
);
199+
200+
$response = $dispatch->handleRequest($request);
201+
$this->assertFalse($response->headers->has('Content-Disposition'));
202+
203+
$request = Request::create(
204+
ResourceManager::resources()
205+
->getResourceUri('css/webptest.css', true, Dispatch::FLAG_CONTENT_ATTACHMENT)
206+
);
207+
208+
$response = $dispatch->handleRequest($request);
209+
$this->assertTrue($response->headers->has('Content-Disposition'));
210+
Dispatch::destroy();
211+
}
212+
187213
}

tests/ResourceManagerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public function testComponent()
5555
'c/6/Packaged/Dispatch/Tests/TestComponents/DemoComponent/DemoComponent/1a9ffb748d31/style.css',
5656
$manager->getResourceUri('style.css')
5757
);
58+
$this->assertEquals(
59+
'c/6/Packaged/Dispatch/Tests/TestComponents/DemoComponent/DemoComponent/1a9ffb748d31-2/style.css',
60+
$manager->getResourceUri('style.css', true, Dispatch::FLAG_CONTENT_ATTACHMENT)
61+
);
62+
$this->assertEquals(
63+
'c/6/Packaged/Dispatch/Tests/TestComponents/DemoComponent/DemoComponent/1a9ffb748d31-3/style.css',
64+
$manager->getResourceUri('style.css', true, Dispatch::FLAG_CONTENT_ATTACHMENT | Dispatch::FLAG_WEBP)
65+
);
5866
Dispatch::instance()->addComponentAlias('\Packaged\Dispatch\Tests\TestComponents', '');
5967
$manager = ResourceManager::component($component);
6068
$this->assertEquals(

0 commit comments

Comments
 (0)