Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
code improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
Joan Fabrégat committed Sep 14, 2018
1 parent 42778e4 commit 366da7a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
26 changes: 23 additions & 3 deletions src/FileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
declare(strict_types = 1);
namespace CodeInc\Psr7Responses;
use CodeInc\MediaTypes\MediaTypes;
use function GuzzleHttp\Psr7\stream_for;
use Psr\Http\Message\StreamInterface;


Expand All @@ -39,7 +40,7 @@ class FileResponse extends StreamResponse
/**
* FileResponse constructor.
*
* @param StreamInterface $fileStream
* @param string|resource|StreamInterface $file
* @param null|string $fileName
* @param null|string $contentType
* @param bool $asAttachment
Expand All @@ -49,12 +50,31 @@ class FileResponse extends StreamResponse
* @param null|string $reason
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException
*/
public function __construct(StreamInterface $fileStream, string $fileName, ?string $contentType = null,
public function __construct($file, string $fileName, ?string $contentType = null,
bool $asAttachment = true, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
if (is_string($file)) {
if (!is_file($file)) {
throw new ResponseException(
sprintf("The path \"%s\" is not a file or does not exist", $file),
$this
);
}
if (($handler = fopen($file, "r")) === false) {
throw new ResponseException(
sprintf("Unable to open the file \"%s\" for reading", $file),
$this
);
}
$stream = stream_for($handler);
}
else {
$stream = stream_for($file);
}

parent::__construct(
$fileStream,
$stream,
$contentType ?? MediaTypes::getFilenameMediaType($fileName, self::DEFAULT_MIME_TYPE),
null,
$fileName,
Expand Down
16 changes: 1 addition & 15 deletions src/LocalFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
declare(strict_types = 1);
namespace CodeInc\Psr7Responses;
use CodeInc\Psr7Responses\Tests\LocalFileResponseTest;
use function GuzzleHttp\Psr7\stream_for;


/**
Expand Down Expand Up @@ -52,21 +51,8 @@ public function __construct(string $filePath, ?string $fileName = null, ?string
bool $asAttachment = true, int $status = 200, array $headers = [],
string $version = '1.1', ?string $reason = null)
{
if (!is_file($filePath)) {
throw new ResponseException(
sprintf("The path \"%s\" is not a file or does not exist", $filePath),
$this
);
}
if (($f = fopen($filePath, "r")) === false) {
throw new ResponseException(
sprintf("Unable to open the file \"%s\" for reading", $filePath),
$this
);
}

parent::__construct(
stream_for($f),
$filePath,
$fileName ?? basename($filePath),
$contentType,
$asAttachment,
Expand Down

0 comments on commit 366da7a

Please sign in to comment.