This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joan Fabrégat
committed
Mar 2, 2018
1 parent
b999d62
commit 1690f2b
Showing
3 changed files
with
79 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,6 @@ | |
// | ||
declare(strict_types = 1); | ||
namespace CodeInc\Psr7Responses; | ||
use GuzzleHttp\Psr7\Response; | ||
use function GuzzleHttp\Psr7\stream_for; | ||
|
||
|
||
/** | ||
|
@@ -31,7 +29,7 @@ | |
* @package CodeInc\Psr7Responses | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
class FileResponse extends Response { | ||
class FileResponse extends StreamResponse { | ||
/** | ||
* FileResponse constructor. | ||
* | ||
|
@@ -55,22 +53,23 @@ public function __construct(string $filePath, ?string $fileName = null, ?string | |
$this | ||
); | ||
} | ||
|
||
$headers["Content-Type"] = $mimeType ?? mime_content_type($filePath); | ||
$headers["Content-Disposition"] = sprintf("%s; filename=\"%s\"", | ||
$asAttachment !== false ? "attachment" : "inline", | ||
$fileName ?? basename($filePath)); | ||
if ($size = filesize($filePath)) { | ||
$headers["Content-Length"] = $size; | ||
} | ||
|
||
if (($f = fopen($filePath, "r")) === false) { | ||
throw new ResponseException( | ||
sprintf("Unable to open the file \"%s\" for reading", $filePath), | ||
$this | ||
); | ||
} | ||
|
||
parent::__construct($status, $headers, stream_for($f), $version, $reason); | ||
parent::__construct( | ||
$f, | ||
$mimeType ?? mime_content_type($filePath), | ||
filesize($filePath) ?: null, | ||
$fileName ?? basename($filePath), | ||
$asAttachment, | ||
$status, | ||
$headers, | ||
$version, | ||
$reason | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
// | ||
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. | | ||
// | Visit https://www.codeinc.fr for more information about licensing. | | ||
// +---------------------------------------------------------------------+ | ||
// | NOTICE: All information contained herein is, and remains the | | ||
// | property of Code Inc. SAS. The intellectual and technical concepts | | ||
// | contained herein are proprietary to Code Inc. SAS are protected by | | ||
// | trade secret or copyright law. Dissemination of this information or | | ||
// | reproduction of this material is strictly forbidden unless prior | | ||
// | written permission is obtained from Code Inc. SAS. | | ||
// +---------------------------------------------------------------------+ | ||
// | ||
// Author: Joan Fabrégat <[email protected]> | ||
// Date: 23/02/2018 | ||
// Time: 19:15 | ||
// Project: lib-psr7responses | ||
// | ||
declare(strict_types = 1); | ||
namespace CodeInc\Psr7Responses; | ||
use GuzzleHttp\Psr7\Response; | ||
use function GuzzleHttp\Psr7\stream_for; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
|
||
/** | ||
* Class StreamResponse | ||
* | ||
* @package CodeInc\Psr7Responses | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
class StreamResponse extends Response { | ||
/** | ||
* StreamResponse constructor. | ||
* | ||
* @param resource|string|null|int|float|bool|StreamInterface|callable $resource | ||
* @param null|string $fileName | ||
* @param null|string $mimeType | ||
* @param int|null $contentLength | ||
* @param bool|null $asAttachment | ||
* @param int $status | ||
* @param array $headers | ||
* @param string $version | ||
* @param null|string $reason | ||
*/ | ||
public function __construct($resource, ?string $mimeType = null, ?int $contentLength = null, | ||
?string $fileName = null, ?bool $asAttachment = null, int $status = 200, array $headers = [], | ||
string $version = '1.1', ?string $reason = null) | ||
{ | ||
if ($mimeType) { | ||
$headers["Content-Type"] = $mimeType; | ||
} | ||
$headers["Content-Disposition"] = $asAttachment !== false ? "attachment" : "inline"; | ||
if ($fileName) { | ||
$headers["Content-Disposition"] .= sprintf("; filename=\"%s\"", $fileName); | ||
} | ||
if ($contentLength !== null) { | ||
$headers["Content-Length"] = $contentLength; | ||
} | ||
|
||
parent::__construct($status, $headers, stream_for($resource), $version, $reason); | ||
} | ||
} |