Skip to content

Commit

Permalink
[WIP] Display links to files (3)
Browse files Browse the repository at this point in the history
Related to #17
  • Loading branch information
jbelien committed Aug 30, 2019
1 parent 16a3e62 commit 994b4b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions public/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

AddOutputFilterByType DEFLATE text/css text/javascript application/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE image/jpeg image/png
21 changes: 19 additions & 2 deletions src/App/Handler/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Diactoros\Stream;
use Zend\Expressive\Router\RouteResult;

class FileHandler implements RequestHandlerInterface
Expand Down Expand Up @@ -46,7 +48,22 @@ public function handle(ServerRequestInterface $request): ResponseInterface
return new EmptyResponse(404);
}

var_dump($route, $path, $filesystem->getMimetype($path));
exit();
$stream = new Stream($filesystem->readStream($path));
$mime = $filesystem->getMimetype($path);

$response = (new Response())
->withBody($stream)
->withStatus(200)
->withHeader('Content-Length', (string) $stream->getSize())
->withHeader('Content-Type', $mime);

if ($route === 'file.download') {
$response = $response->withHeader(
'Content-Disposition',
'attachment;' . sprintf('filename="%s"', basename($path))
);
}

return $response;
}
}

0 comments on commit 994b4b2

Please sign in to comment.