Skip to content

Commit

Permalink
feat(Body): improving FormFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
razshare committed Jun 11, 2024
1 parent 6c021b2 commit 139cde0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
20 changes: 19 additions & 1 deletion src/lib/Core/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use function Amp\File\getStatus;
use function Amp\File\openFile;
use Dotenv\Dotenv;
use Error;
use Throwable;

readonly class File {
Expand Down Expand Up @@ -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<None>
*/
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.
Expand Down
3 changes: 1 addition & 2 deletions src/lib/Web/Body.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down
8 changes: 1 addition & 7 deletions src/lib/Web/BodyParser.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<?php

namespace CatPaw\Web;

use Amp\Cancellation;
use Amp\Http\Server\FormParser\StreamingFormParser;
use Amp\Http\Server\Request;

use function CatPaw\Core\error;
use function CatPaw\Core\ok;

use CatPaw\Core\Unsafe;
use function json_decode;

use stdClass;


use Throwable;

class BodyParser {
Expand Down Expand Up @@ -48,7 +42,7 @@ public static function parseAsObject(
if ($field->isFile()) {
$result->{$name} = new FormFile(
fileName: $field->getFilename(),
fileContents: $field->buffer($cancellation),
fileContents: $field->buffer()
);
} else {
$result->{$name} = $field->buffer($cancellation);
Expand Down

0 comments on commit 139cde0

Please sign in to comment.