fix(FileStream): improve stream validation in constructor

Ensure the stream is readable and metadata is correctly checked.
This commit is contained in:
Jérémy Munsch 2026-11-03 16:44:42 +01:00
parent f956a47c35
commit bb1129c209
Signed by: forge-joniko
GPG key ID: D7EFE2F5CB6CC0E4

View file

@ -20,8 +20,11 @@ final readonly class FileStream
throw new UnexpectedStorageException('Resource is not a valid stream.');
}
if (! stream_get_meta_data($this->resource)) {
throw new UnexpectedStorageException('Unable to read stream metadata.');
$meta = stream_get_meta_data($this->resource);
$mode = $meta['mode'];
if (! str_contains($mode, 'r') && ! str_contains($mode, '+')) {
throw new UnexpectedStorageException('Stream is not readable.');
}
}
@ -66,7 +69,7 @@ final readonly class FileStream
}
$meta = stream_get_meta_data($this->resource);
if ($meta['seekable'] ?? false) {
if ($meta['seekable']) {
rewind($this->resource);
}