Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  narrow down the max memory allowed to be used
  ignore the sample file when exporting the package
  fix loading multiple chunks in memory
  • Loading branch information
Baptouuuu committed Mar 27, 2024
2 parents 33d2040 + 56a6a07 commit 089371a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/samples export-ignore
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 24 additions & 0 deletions proofs/file/content.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
declare(strict_types = 1);
declare(ticks = 1);

use Innmind\Filesystem\File\{
Content as Model,
Expand Down Expand Up @@ -238,4 +239,27 @@ static function($assert, $a, $b) use ($io, $capabilities) {
$assert->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);
},
);
};
Binary file added samples/sample.pdf
Binary file not shown.
12 changes: 11 additions & 1 deletion src/File/Content/Chunks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Innmind\Filesystem\File\Content;

use Innmind\Stream\Stream\Size;
use Innmind\Immutable\{
Sequence,
SideEffect,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 089371a

Please sign in to comment.