hhxsv5 /
laravel-s
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | namespace Hhxsv5\LaravelS\Swoole; |
||
| 4 | |||
| 5 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
||
|
0 ignored issues
–
show
The type
Symfony\Component\HttpFo...tion\BinaryFileResponse was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 6 | |||
| 7 | class StaticResponse extends Response |
||
|
0 ignored issues
–
show
|
|||
| 8 | { |
||
| 9 | /**@var BinaryFileResponse */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | protected $laravelResponse; |
||
| 11 | |||
| 12 | public function gzip() |
||
|
0 ignored issues
–
show
|
|||
| 13 | { |
||
| 14 | } |
||
| 15 | |||
| 16 | /** |
||
|
0 ignored issues
–
show
|
|||
| 17 | * @throws \Exception |
||
| 18 | */ |
||
|
0 ignored issues
–
show
|
|||
| 19 | public function sendContent() |
||
| 20 | { |
||
| 21 | $file = $this->laravelResponse->getFile(); |
||
| 22 | if (!$this->laravelResponse->headers->has('Content-Type')) { |
||
| 23 | $this->swooleResponse->header('Content-Type', $file->getMimeType()); |
||
| 24 | } |
||
| 25 | if ($this->laravelResponse->getStatusCode() == BinaryFileResponse::HTTP_NOT_MODIFIED) { |
||
| 26 | $this->swooleResponse->end(); |
||
| 27 | return; |
||
| 28 | } |
||
| 29 | |||
| 30 | $path = $file->getPathname(); |
||
| 31 | $size = filesize($path); |
||
| 32 | if ($size <= 0) { |
||
| 33 | $this->swooleResponse->end(); |
||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | // Support deleteFileAfterSend: https://github.com/symfony/http-foundation/blob/5.0/BinaryFileResponse.php#L305 |
||
| 38 | $reflection = new \ReflectionObject($this->laravelResponse); |
||
| 39 | if ($reflection->hasProperty('deleteFileAfterSend')) { |
||
| 40 | $deleteFileAfterSend = $reflection->getProperty('deleteFileAfterSend'); |
||
| 41 | $deleteFileAfterSend->setAccessible(true); |
||
| 42 | $deleteFile = $deleteFileAfterSend->getValue($this->laravelResponse); |
||
| 43 | } else { |
||
| 44 | $deleteFile = false; |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($deleteFile) { |
||
| 48 | $fp = fopen($path, 'rb'); |
||
| 49 | |||
| 50 | for ($offset = 0, $limit = (int)(0.99 * $this->chunkLimit); $offset < $size; $offset += $limit) { |
||
| 51 | fseek($fp, $offset, SEEK_SET); |
||
| 52 | $chunk = fread($fp, $limit); |
||
| 53 | $this->swooleResponse->write($chunk); |
||
| 54 | } |
||
| 55 | $this->swooleResponse->end(); |
||
| 56 | |||
| 57 | fclose($fp); |
||
| 58 | |||
| 59 | if (file_exists($path)) { |
||
| 60 | unlink($path); |
||
| 61 | } |
||
| 62 | } else { |
||
| 63 | if (version_compare(SWOOLE_VERSION, '1.7.21', '<')) { |
||
| 64 | throw new \RuntimeException('sendfile() require Swoole >= 1.7.21'); |
||
| 65 | } |
||
| 66 | $this->swooleResponse->sendfile($path); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 |