From 139cde0770c3f8bbc71db3f340689a2ca69cb918 Mon Sep 17 00:00:00 2001 From: Razvan Tanase Date: Tue, 11 Jun 2024 04:32:57 +0200 Subject: [PATCH] feat(Body): improving FormFiles --- src/lib/Core/File.php | 20 +++++++++++++++++++- src/lib/Web/Body.php | 3 +-- src/lib/Web/BodyParser.php | 8 +------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/lib/Core/File.php b/src/lib/Core/File.php index 971b340..f34004b 100644 --- a/src/lib/Core/File.php +++ b/src/lib/Core/File.php @@ -11,7 +11,6 @@ use function Amp\File\getStatus; use function Amp\File\openFile; use Dotenv\Dotenv; -use Error; use Throwable; readonly class File { @@ -173,6 +172,25 @@ public static function writeFile(string $filename, string $contents):Unsafe { $file->close(); return ok(); } + + /** + * Stream contents to a file.\ + * If the file doesn't exist it will be created. + * @param ReadableStream $readableStream + * @return Unsafe + */ + public static function writeStreamFile(string $filename, ReadableStream $readableStream):Unsafe { + $file = File::open($filename, 'w+')->unwrap($error); + if ($error) { + return error($error); + } + $file->writeStream($readableStream)->unwrap($error); + if ($error) { + return error($error); + } + $file->close(); + return ok(); + } /** * Read the contents of a file. diff --git a/src/lib/Web/Body.php b/src/lib/Web/Body.php index fcac4b9..5ef5b9b 100755 --- a/src/lib/Web/Body.php +++ b/src/lib/Web/Body.php @@ -6,13 +6,12 @@ use Amp\Http\Server\Request; use function CatPaw\Core\error; use function CatPaw\Core\ok; - use CatPaw\Core\Unsafe; use Throwable; class Body { private null|Cancellation $cancellation = null; - private int $sizeLimit = 1024 * 1024 * 120; + private int $sizeLimit = 1024 * 1024 * 1024 * 10; // 10GB public function __construct( public readonly Request $request, ) { diff --git a/src/lib/Web/BodyParser.php b/src/lib/Web/BodyParser.php index 33226f7..4b2d2bd 100755 --- a/src/lib/Web/BodyParser.php +++ b/src/lib/Web/BodyParser.php @@ -1,20 +1,14 @@ isFile()) { $result->{$name} = new FormFile( fileName: $field->getFilename(), - fileContents: $field->buffer($cancellation), + fileContents: $field->buffer() ); } else { $result->{$name} = $field->buffer($cancellation);