Skip to content

Commit c5a0497

Browse files
committed
read kb
1 parent 3ea13f3 commit c5a0497

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Bandwidth.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,35 @@ public function __construct(int $bucketSize = 0, int $tokensPerInterval = 1024,
2929
}
3030
}
3131

32-
public function file(string $path, $p = 0, $length = -1)
32+
public function file(string $path, $p = 0, $length = -1, $readKB = 0)
3333
{
3434
$stream = new \React\Stream\ThroughStream();
3535

36+
$readKB = $readKB > 0 ? min($readKB, $this->KB) : $this->KB;
37+
3638
$this->filesystem->detect($path)->then(function ($node) use ($path) {
3739
if ($node instanceof \React\Filesystem\Node\FileInterface) {
3840
return $node->stat();
3941
} else {
4042
throw new \RuntimeException($path. ' is not a file');
4143
}
42-
})->then(function ($stat) use ($stream, $p, $length) {
44+
})->then(function ($stat) use ($stream, $p, $length, $readKB) {
4345
if ($this->queue) {
44-
return $this->concurrent->concurrent(function () use ($stream, $stat, $p, $length) {
46+
return $this->concurrent->concurrent(function () use ($stream, $stat, $p, $length, $readKB) {
4547
$file = $this->filesystem->file($stat->path());
4648
$size = $stat->size();
4749
if ($length > 0) {
4850
$size = min($size, $p + $length);
4951
}
50-
return $this->fileStream($file, $stream, $p, $size);
52+
return $this->fileStream($file, $stream, $p, $size, $readKB);
5153
});
5254
} else {
5355
$file = $this->filesystem->file($stat->path());
5456
$size = $stat->size();
5557
if ($length > 0) {
5658
$size = min($size, $p + $length);
5759
}
58-
return $this->fileStream($file, $stream, $p, $size);
60+
return $this->fileStream($file, $stream, $p, $size, $readKB);
5961
}
6062
}, function ($e) use ($stream) {
6163
$stream->emit('error', [$e]);
@@ -80,7 +82,7 @@ public function stream($stream)
8082
return $_stream;
8183
}
8284

83-
protected function fileStream($file, $stream, $p, $size)
85+
protected function fileStream($file, $stream, $p, $size, $readKB)
8486
{
8587

8688
if (!$stream->isWritable()) {
@@ -89,23 +91,23 @@ protected function fileStream($file, $stream, $p, $size)
8991

9092
$currentSize = $size - $p;
9193

92-
if ($currentSize/1024 < $this->KB) {
94+
if ($currentSize/1024 < $readKB) {
9395
return $this->bucket->removeTokens(1024 * 1024 * ceil($currentSize/1024))->then(function () use ($file, $stream, $p, $currentSize) {
9496
return $file->getContents($p, $currentSize)->then(function ($contents) use ($stream) {
9597
$stream->end($contents);
9698
return null;
9799
});
98100
});
99101
} else {
100-
return $this->bucket->removeTokens(1024 * 1024 * $this->KB)->then(function () use ($file, $stream, $p, $size) {
101-
return $file->getContents($p, 1024 * 1024 * $this->KB)->then(function ($contents) use ($stream, $file, $p, $size) {
102+
return $this->bucket->removeTokens(1024 * 1024 * $readKB)->then(function () use ($file, $stream, $p, $size, $readKB) {
103+
return $file->getContents($p, 1024 * 1024 * $readKB)->then(function ($contents) use ($stream, $file, $p, $size, $readKB) {
102104
$p += strlen($contents);
103105
if ($p >= $size) {
104106
$stream->end($contents);
105107
return null;
106108
} else {
107109
$stream->write($contents);
108-
return $this->fileStream($file, $stream, $p, $size);
110+
return $this->fileStream($file, $stream, $p, $size, $readKB);
109111
}
110112
});
111113
});

0 commit comments

Comments
 (0)