diff --git a/.gitattributes b/.gitattributes index 4160131..7fe7bdf 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,3 +2,4 @@ /.gitignore export-ignore /phpunit.xml.dist export-ignore /tests export-ignore +/samples export-ignore diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a40423..5f75d95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 7.5.1 - 2024-03-27 + +### Fixed + +- `Innmind\Filesystem\File\Content::ofChunks()->size()` no longer load more than one chunk size in memory + ## 7.5.0 - 2024-03-16 ### Changed diff --git a/proofs/file/content.php b/proofs/file/content.php index c40bf7d..3a41124 100644 --- a/proofs/file/content.php +++ b/proofs/file/content.php @@ -1,5 +1,6 @@ throws(static fn() => $b($content)); }, ); + + yield test( + 'Content::ofChunks()->size() does not load the whole file in memory', + static function($assert) use ($io, $capabilities) { + $atPath = Model::atPath( + $capabilities->readable(), + $io, + Path::of('samples/sample.pdf'), + ); + $content = Model::ofChunks($atPath->chunks()); + + $assert + ->memory(static function() use ($assert, $content) { + $size = $content->size()->match( + static fn($size) => $size->toInt(), + static fn() => null, + ); + $assert->same(5951532, $size); + }) + ->inLessThan() + ->kilobytes(537); + }, + ); }; diff --git a/samples/sample.pdf b/samples/sample.pdf new file mode 100644 index 0000000..50c970a Binary files /dev/null and b/samples/sample.pdf differ diff --git a/src/File/Content/Chunks.php b/src/File/Content/Chunks.php index a9a5b0b..8f9e128 100644 --- a/src/File/Content/Chunks.php +++ b/src/File/Content/Chunks.php @@ -3,6 +3,7 @@ namespace Innmind\Filesystem\File\Content; +use Innmind\Stream\Stream\Size; use Innmind\Immutable\{ Sequence, SideEffect, @@ -81,7 +82,16 @@ public function reduce($carry, callable $reducer) public function size(): Maybe { - return $this->content()->size(); + return Maybe::just( + $this + ->chunks + ->map(static fn($chunk) => $chunk->toEncoding(Str\Encoding::ascii)) + ->map(static fn($chunk) => $chunk->length()) + ->reduce( + 0, + static fn(int $total, int $chunk) => $total + $chunk, + ), + )->map(static fn($size) => new Size($size)); } public function toString(): string