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.
Merge pull request #21 from CodeIncHQ/2.x
v2.1.0
- Loading branch information
Showing
19 changed files
with
132 additions
and
378 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
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -3,86 +3,69 @@ | |
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. | | ||
// | Copyright (c) 2018 - 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 | | ||
// | 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 | ||
// Date: 08/10/2018 | ||
// Project: Psr7Responses | ||
// | ||
declare(strict_types = 1); | ||
declare(strict_types=1); | ||
namespace CodeInc\Psr7Responses; | ||
use CodeInc\MediaTypes\MediaTypes; | ||
use function GuzzleHttp\Psr7\stream_for; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
|
||
/** | ||
* Class FileResponse | ||
* | ||
* @see FileResponseTest | ||
* @package CodeInc\Psr7Responses | ||
* @author Joan Fabrégat <[email protected]> | ||
* @license MIT <https://github.com/CodeIncHQ/Psr7Responses/blob/master/LICENSE> | ||
* @link https://github.com/CodeIncHQ/Psr7Responses | ||
* @version 2 | ||
*/ | ||
class FileResponse extends StreamResponse | ||
{ | ||
/** | ||
* FileResponse constructor. | ||
* | ||
* @param string|resource|StreamInterface $file | ||
* @param string $fileName | ||
* @param $data | ||
* @param int $code | ||
* @param string $reasonPhrase | ||
* @param null|string $contentType | ||
* @param int|null $contentLength | ||
* @param bool $asAttachment | ||
* @param array $headers | ||
* @param string $version | ||
* @throws \CodeInc\MediaTypes\Exceptions\MediaTypesException | ||
*/ | ||
public function __construct($file, string $fileName, int $code = 200, string $reasonPhrase = '', | ||
?string $contentType = null, bool $asAttachment = true, array $headers = [], string $version = '1.1') | ||
{ | ||
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); | ||
public function __construct(string $fileName, $data, int $code = 200, string $reasonPhrase = '', | ||
?string $contentType = null, ?int $contentLength = null, bool $asAttachment = true, | ||
array $headers = [], string $version = '1.1') | ||
{ | ||
$stream = stream_for($data); | ||
|
||
// adding headers | ||
if (!isset($headers['Content-Type']) && $contentType) { | ||
$headers['Content-Type'] = $contentType | ||
?? MediaTypes::getFilenameMediaType($fileName) | ||
?? 'application/octet-stream'; | ||
} | ||
if (!isset($headers['Content-Disposition'])) { | ||
$headers['Content-Disposition'] = sprintf('%s; filename="%s"', | ||
($asAttachment ? 'attachment' : 'inline'), $fileName); | ||
} | ||
else { | ||
$stream = stream_for($file); | ||
if (!isset($headers['Content-Length']) && ($contentLength !== null || ($contentLength = $stream->getSize()) !== null)) { | ||
$headers['Content-Length'] = $contentLength; | ||
} | ||
|
||
parent::__construct( | ||
$stream, | ||
$code, | ||
$reasonPhrase, | ||
$contentType ?? MediaTypes::getFilenameMediaType($fileName, 'application/octet-stream'), | ||
null, | ||
$fileName, | ||
$asAttachment, | ||
$headers, | ||
$version | ||
); | ||
} | ||
parent::__construct($stream, $code, $reasonPhrase, $headers, $version); | ||
} | ||
} |
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
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
Oops, something went wrong.