代码拉取完成,页面将自动刷新
<?php
namespace Kiri\Message;
use Kiri\Message\Constrict\OnDownloadInterface;
class OnDownload extends Response implements OnDownloadInterface
{
use Message;
private string $path;
private bool $isChunk;
private int $size;
private int $offset;
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 string $path
* @param false $isChunk
* @param int $size
* @param int $offset
* @return $this
*/
public function path(string $path, bool $isChunk = false, int $size = -1, int $offset = 0): OnDownload
{
$this->path = $path;
$this->isChunk = $isChunk;
$this->size = $size;
$this->offset = $offset;
return $this->emitter();
}
/**
* @return $this
*/
public function emitter(): static
{
$explode = explode('/', $this->path);
$this->withHeader('Pragma', 'public');
$this->withHeader('Expires', '0');
$this->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$this->withHeader('Content-Disposition', 'attachment;filename=' . end($explode));
$this->withHeader('Content-Type', $type = get_file_extension($this->path));
if (!in_array($type, self::IMAGES)) {
$this->withHeader('Content-Transfer-Encoding', 'binary');
}
if ($this->isChunk) {
$resource = fopen($this->path, 'r');
$state = fstat($resource);
$this->withHeader('Content-length', $state['size']);
}
return $this;
}
/**
* @param \Swoole\Http\Response $response
*/
public function dispatch(mixed $response)
{
if (!$this->isChunk) {
$response->sendfile($this->path);
} else {
$this->chunk($response);
}
}
/**
* @param \Swoole\Http\Response $response
*/
private function chunk(\Swoole\Http\Response $response): void
{
$resource = fopen($this->path, 'r');
$state = fstat($resource);
$offset = $this->offset;
while ($file = fread($resource, $this->size)) {
$response->write($file);
fseek($resource, $offset);
if ($offset >= $state['size']) {
break;
}
$offset += $this->size;
}
$response->end();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。