Skip to content

Commit

Permalink
Get the proper content-type
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
gisostallenberg committed Mar 3, 2016
1 parent effa9b3 commit 935efd3
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/FileServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function getResponse()
$filePath = $this->from.substr($requestPath, strlen($this->to));

if (is_file($filePath)) {

$file = new File($filePath);
$response = Response::create()
->setExpires(new DateTime('+1 week'))
Expand All @@ -84,13 +85,51 @@ public function getResponse()
return $response;
}

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

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

/**
* Sets the correct content-type
*
* @param File $file
* @param Response $response
*/
private function setContentType(File $file, Response $response)
{
$extension = pathinfo($file->getPathname(), PATHINFO_EXTENSION);

switch ($extension) {
case 'css':
$contentType = 'text/css';
break;
case 'js':
$contentType = 'application/javascript';
break;
case 'png':
$contentType = 'image/png';
break;
case 'jpg':
case 'jpeg':
$contentType = 'image/jpeg';
break;
case 'gif':
$contentType = 'image/gif';
break;
case 'txt':
$contentType = 'text/plain';
break;
default:
$contentType = $file->getMimeType();
break;
}

$response->headers->set('Content-Type', $contentType);
}

/**
* Serves the file.
*/
Expand Down

0 comments on commit 935efd3

Please sign in to comment.