This commit is contained in:
2021-03-05 11:57:58 +08:00
parent df0ead6364
commit 0654c676cb
+27 -1
View File
@@ -14,6 +14,7 @@ use HttpServer\Http\Formatter\HtmlFormatter;
use HttpServer\Http\Formatter\JsonFormatter;
use HttpServer\Http\Formatter\XmlFormatter;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Core\Help;
use Snowflake\Snowflake;
use Swoole\Http\Response as SResponse;
@@ -269,6 +270,31 @@ class Response extends HttpService
}
/**
* @param string $path
* @param int $offset
* @param int $limit
*/
public function sendFile(string $path, $offset = 0, $limit = 1024000)
{
$open = fopen($path, 'r');
$stat = fstat($open);
$this->response->setHeader('Content-Length', $stat['size']);
while ($file = fread($open, $limit)) {
$this->response->write($file);
fseek($open, $offset);
if ($offset >= $stat['size']) {
break;
}
$offset += $limit;
}
$this->response->end();
}
/**
* @throws Exception
*/
@@ -281,7 +307,7 @@ class Response extends HttpService
/**
* @return string
*/
public function getRuntime(): string
#[Pure] public function getRuntime(): string
{
return sprintf('%.5f', microtime(TRUE) - $this->startTime);
}