Skip to content

Commit

Permalink
add middleware for manage download file
Browse files Browse the repository at this point in the history
  • Loading branch information
ooghry committed Aug 11, 2016
1 parent f56c9ca commit 9209fa0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/autoload/middleware-pipeline.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
Helper\ServerUrlMiddleware::class,
App\Middleware\AuthMiddleware::class,
],
'priority' => 10000,
'priority' => PHP_INT_MAX,
],

'routing' => [
Expand Down
6 changes: 6 additions & 0 deletions config/autoload/routes.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@
'middleware' => App\Action\ListAction::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'download',
'path' => '/download/{file:[_0-9a-zA-z\-]+\.\w{1,}}',
'middleware' => App\Action\DownloadAction::class,
'allowed_methods' => ['GET'],
],
],
];
2 changes: 1 addition & 1 deletion data/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*
!.gitignore
!users.db
!users.db
23 changes: 23 additions & 0 deletions src/App/Action/DownloadAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Action;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class DownloadAction
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$file='data'.$request->getUri()->getPath();
if(is_readable($file)){
return $response
->write(file_get_contents($file))
->withHeader('Content-Disposition','inline; filename="' . pathinfo($file,PATHINFO_BASENAME) . '"')
->withHeader('Content-type',pathinfo($file,PATHINFO_EXTENSION))
->withStatus(200);
}else{
return $next($request, $response);
}
}
}

0 comments on commit 9209fa0

Please sign in to comment.