Skip to content

Commit

Permalink
Initial fileserver
Browse files Browse the repository at this point in the history
* Fileserver class
* README
* composer requirements
  • Loading branch information
gisostallenberg committed Mar 3, 2016
1 parent de51a9d commit a568798
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 11 deletions.
7 changes: 1 addition & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
composer.phar
/vendor/

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
/vendor/
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# file-serving
Serve files to the browser that are not available directly

## Installation
```bash
composer require gisostallenberg/file-serving
```

## Usage example
```php
<?php
require_once __DIR__ . '/../../vendor/autoload.php';

use GisoStallenberg\FileServing\FileServer;

$fileserver = new FileServer('../serve-me/', 'serve-it/');
$fileserver->serve(); // will server http://example.com/serve-it/example.txt when ../serve-me/example.txt exists, gives a 404 otherwise
```

```php
<?php
require_once __DIR__ . '/../../vendor/autoload.php';

use GisoStallenberg\FileServing\FileServer;
use Symfony\Component\HttpFoundation\Response;

$fileserver = new FileServer('../serve-me/', 'serve-it/');
$response = $fileserver->getResponse(); // do not serve yet

if ($response->getStatusCode() === Response::HTTP_NOT_FOUND) {
$fileserver = new FileServer('../serve-other-dir/', 'serve-it/'); // check another directory
$response = $fileserver->getResponse();
}

$response->send();
```

## Credits
Niels Nijens (https://github.com/niels-nijens/)
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"description": "Serve files to the browser that are not available directly",
"type": "package",
"require": {
"symfony/http-foundation": "^3.0"
"php": "^5.3.9",
"symfony/http-foundation": "^2.8|^3.0"
},
"license": "MIT",
"authors": [
Expand Down
8 changes: 5 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 100 additions & 1 deletion src/FileServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,109 @@

namespace GisoStallenberg\FileServing;

use DateTime;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class FileServer
{

/**
* The path to serve from.
*
* @var string
*/
private $from;

/**
* The server path to serve.
*
* @var string
*/
private $to;

/**
* The request object.
*
* @var Request
*/
private $request;

/**
* The base path to find files.
*
* @var string
*/
private $basePath;

/**
* Constructor.
*
* @param string $from
* @param string $to
* @param Request $request
*/
public function __construct($from, $to, Request $request = null)
{
$this->from = $from;
$this->to = $to;

if (is_null($request)) {
$request = Request::createFromGlobals();
}
$this->request = $request;

$this->basePath = $this->request->server->get('DOCUMENT_ROOT');
}

/**
* Creates a new instance.
*
* @param string $from
* @param string $to
* @param Request $request
*
* @return \static
*/
public static function create($from, $to, Request $request = null)
{
return new static($from, $to, $request);
}

/**
* Creates the response object.
*
* @return Response
*/
public function getResponse()
{
$requestPath = parse_url($this->request->server->get('REQUEST_URI'), PHP_URL_PATH);

$filePath = $this->basePath.$this->from.substr($requestPath, strlen($this->to));

if (is_file($filePath)) {
$file = new File($filePath);
$response = Response::create()
->setExpires(new DateTime('+1 week'))
->setLastModified(DateTime::createFromFormat('U', $file->getMTime()));;

if ($response->isNotModified($this->request)) {

return $response;
}

$response->headers->set('Content-Type', $file->getMimeType() ?: 'application/octet-stream');
return $response->setContent(file_get_contents($file->getPathname()));
}

return Response::create('', Response::HTTP_NOT_FOUND);
}

/**
* Serves the file.
*/
public function serve()
{
$this->getResponse()->send();
}
}

0 comments on commit a568798

Please sign in to comment.