-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#345 - Rename downloader to filedownloader and add support for
cache query parameter The cache query parameter allows to define the max-age in seconds.
- Loading branch information
1 parent
7e6d33e
commit 9fbf354
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
code/site/components/com_pages/event/subscriber/filedownloader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
/** | ||
* Joomlatools Pages | ||
* | ||
* @copyright Copyright (C) 2018 Johan Janssens and Timble CVBA. (http://www.timble.net) | ||
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> | ||
* @link https://github.com/joomlatools/joomlatools-pages for the canonical source repository | ||
*/ | ||
|
||
class ComPagesEventSubscriberFiledownloader extends ComPagesEventSubscriberAbstract | ||
{ | ||
protected function _initialize(KObjectConfig $config) | ||
{ | ||
$config->append(array( | ||
'priority' => KEvent::PRIORITY_HIGH, | ||
)); | ||
|
||
parent::_initialize($config); | ||
} | ||
|
||
public function onAfterApplicationRoute(KEventInterface $event) | ||
{ | ||
$request = $this->getObject('request'); | ||
$router = $this->getObject('com://site/pages.dispatcher.router.file', ['request' => $request]); | ||
|
||
if(false !== $route = $router->resolve()) | ||
{ | ||
//Set the location header | ||
$dispatcher = $this->getObject('com://site/pages.dispatcher.http'); | ||
$response = $dispatcher->getResponse(); | ||
|
||
//Force download the file | ||
if(isset($route->query['force-download'])) | ||
{ | ||
$request->query->set('force-download', true); | ||
unset($route->query['force-download']); | ||
} | ||
|
||
//Attach a different transport [stream or sendfile] | ||
if(isset($route->query['transport'])) | ||
{ | ||
$transport = $route->query['transport']; | ||
|
||
//Enable using the header | ||
if($transport == 'sendfile') { | ||
$response->getHeaders()->set('X-Sendfile', 1); | ||
} | ||
|
||
$response->attachTransport($transport); | ||
|
||
unset($route->query['transport']); | ||
} | ||
|
||
//Set the cache time | ||
if(isset($route->query['cache']) && ctype_alnum($route->query['cache'])) | ||
{ | ||
$response->setMaxAge($route->query['cache']); | ||
unset($route->query['cache']); | ||
} | ||
|
||
//Qualify the route | ||
$route = $router->qualify($route); | ||
|
||
//Get the file path | ||
$path = $route->getPath(); | ||
|
||
try { | ||
$response->setContent($path, @mime_content_type($path) ?? 'application/octet-stream'); | ||
} catch (InvalidArgumentException $e) { | ||
throw new KControllerExceptionResourceNotFound('File not found'); | ||
} | ||
|
||
$dispatcher->send(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters