改名
This commit is contained in:
@@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Server\Constrict;
|
||||||
|
|
||||||
|
|
||||||
|
use Kiri\Exception\NotFindClassException;
|
||||||
|
use ReflectionException;
|
||||||
|
use Server\ResponseInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class DownloadEmitter implements Emitter
|
||||||
|
{
|
||||||
|
|
||||||
|
const IMAGES = [
|
||||||
|
'png' => 'image/png',
|
||||||
|
'jpeg' => 'image/jpeg',
|
||||||
|
'gif' => 'image/gif',
|
||||||
|
'bmp' => 'image/bmp',
|
||||||
|
'ico' => 'image/vnd.microsoft.icon',
|
||||||
|
'tiff' => 'image/tiff',
|
||||||
|
'svg' => 'image/svg+xml',
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $response
|
||||||
|
* @param ResponseInterface $emitter
|
||||||
|
* @throws NotFindClassException
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||||
|
{
|
||||||
|
$content = $emitter->getContent();
|
||||||
|
|
||||||
|
$explode = explode('/', $content['path']);
|
||||||
|
|
||||||
|
$response->header('Pragma', 'public');
|
||||||
|
$response->header('Expires', '0');
|
||||||
|
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
|
||||||
|
$response->header('Content-Disposition', 'attachment;filename=' . end($explode));
|
||||||
|
$response->header('Content-Type', $type = get_file_extension($content['path']));
|
||||||
|
if (!in_array($type, self::IMAGES)) {
|
||||||
|
$response->header('Content-Transfer-Encoding', 'binary');
|
||||||
|
} else {
|
||||||
|
$response->end(file_get_contents($content['path']));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ($content['isChunk'] === false) {
|
||||||
|
$response->sendfile($content['path']);
|
||||||
|
} else {
|
||||||
|
$this->chunk($content, $response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $content
|
||||||
|
* @param $response
|
||||||
|
*/
|
||||||
|
private function chunk($content, $response): void
|
||||||
|
{
|
||||||
|
$resource = fopen($content['path'], 'r');
|
||||||
|
|
||||||
|
$state = fstat($resource);
|
||||||
|
|
||||||
|
$offset = $content['offset'];
|
||||||
|
|
||||||
|
$response->header('Content-length', $state['size']);
|
||||||
|
while ($file = fread($resource, $content['limit'])) {
|
||||||
|
$response->write($file);
|
||||||
|
fseek($resource, $offset);
|
||||||
|
if ($offset >= $state['size']) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$offset += $content['limit'];
|
||||||
|
}
|
||||||
|
$response->end();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,13 +3,10 @@
|
|||||||
namespace Server\Constrict;
|
namespace Server\Constrict;
|
||||||
|
|
||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use HttpServer\Http\HttpHeaders;
|
|
||||||
use HttpServer\Http\HttpParams;
|
|
||||||
use HttpServer\Http\Request as HttpResponse;
|
use HttpServer\Http\Request as HttpResponse;
|
||||||
use HttpServer\Http\Response;
|
use HttpServer\Http\Response;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\RequestInterface;
|
use Server\RequestInterface;
|
||||||
use Kiri\Abstracts\BaseObject;
|
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
|
|
||||||
@@ -71,7 +68,6 @@ class Request implements RequestInterface
|
|||||||
|
|
||||||
Context::setContext(HttpResponse::class, $sRequest);
|
Context::setContext(HttpResponse::class, $sRequest);
|
||||||
|
|
||||||
|
|
||||||
return Kiri::getDi()->get(Request::class);
|
return Kiri::getDi()->get(Request::class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ namespace Server\Constrict;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Http\Formatter\FileFormatter;
|
use HttpServer\Http\Formatter\FileFormatter;
|
||||||
use HttpServer\IInterface\IFormatter;
|
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\ResponseInterface;
|
use Server\ResponseInterface;
|
||||||
@@ -29,74 +28,11 @@ class ResponseEmitter implements Emitter
|
|||||||
{
|
{
|
||||||
$content = $emitter->configure($response)->getContent();
|
$content = $emitter->configure($response)->getContent();
|
||||||
if ($content instanceof FileFormatter) {
|
if ($content instanceof FileFormatter) {
|
||||||
$this->download($content->getData(), $response);
|
di(DownloadEmitter::class)->sender($response, $emitter);
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
$response->header('Content-Type', $emitter->getResponseFormat());
|
$response->header('Content-Type', $emitter->getResponseFormat());
|
||||||
$response->end($content->getData());
|
$response->end($content->getData());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const IMAGES = [
|
|
||||||
'png' => 'image/png',
|
|
||||||
'jpeg' => 'image/jpeg',
|
|
||||||
'gif' => 'image/gif',
|
|
||||||
'bmp' => 'image/bmp',
|
|
||||||
'ico' => 'image/vnd.microsoft.icon',
|
|
||||||
'tiff' => 'image/tiff',
|
|
||||||
'svg' => 'image/svg+xml',
|
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $content
|
|
||||||
* @param \Swoole\Http\Response $response
|
|
||||||
*/
|
|
||||||
private function download(array $content, \Swoole\Http\Response $response)
|
|
||||||
{
|
|
||||||
$explode = explode('/', $content['path']);
|
|
||||||
|
|
||||||
$response->header('Pragma', 'public');
|
|
||||||
$response->header('Expires', '0');
|
|
||||||
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
|
|
||||||
$response->header('Content-Disposition', 'attachment;filename=' . end($explode));
|
|
||||||
$response->header('Content-Type', $type = get_file_extension($content['path']));
|
|
||||||
if (!in_array($type, self::IMAGES)) {
|
|
||||||
$response->header('Content-Transfer-Encoding', 'binary');
|
|
||||||
} else {
|
|
||||||
$response->end(file_get_contents($content['path']));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ($content['isChunk'] === false) {
|
|
||||||
$response->sendfile($content['path']);
|
|
||||||
} else {
|
|
||||||
$this->chunk($content, $response);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $content
|
|
||||||
* @param $response
|
|
||||||
*/
|
|
||||||
private function chunk($content, $response): void
|
|
||||||
{
|
|
||||||
$resource = fopen($content['path'], 'r');
|
|
||||||
|
|
||||||
$state = fstat($resource);
|
|
||||||
|
|
||||||
$offset = $content['offset'];
|
|
||||||
|
|
||||||
$response->header('Content-length', $state['size']);
|
|
||||||
while ($file = fread($resource, $content['limit'])) {
|
|
||||||
$response->write($file);
|
|
||||||
fseek($resource, $offset);
|
|
||||||
if ($offset >= $state['size']) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$offset += $content['limit'];
|
|
||||||
}
|
|
||||||
$response->end();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user