Skip to content

Commit

Permalink
Merge pull request #14 from huzaifaarain/13-cannot-access-offset-of-t…
Browse files Browse the repository at this point in the history
…ype-string-on-string

feat: add support for multipart data
  • Loading branch information
huzaifaarain authored Oct 26, 2024
2 parents 28ad071 + c817ed0 commit 03bf02a
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions src/TelescopeGuzzleRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace MuhammadHuzaifa\TelescopeGuzzleWatcher;

use GuzzleHttp\TransferStats;
use Illuminate\Http\File;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laravel\Telescope\IncomingEntry;
Expand Down Expand Up @@ -185,33 +184,24 @@ protected function input(Request $request)
return $request->data();
}

return collect($request->data())->mapWithKeys(function ($data) {
if ($data['contents'] instanceof File) {
$value = [
'name' => $data['filename'] ?? $data['contents']->getClientOriginalName(),
'size' => ($data['contents']->getSize() / 1000).'KB',
'headers' => $data['headers'] ?? [],
];
} elseif (is_resource($data['contents'])) {
$filesize = @filesize(stream_get_meta_data($data['contents'])['uri']);

$value = [
'name' => $data['filename'] ?? null,
'size' => $filesize ? ($filesize / 1000).'KB' : null,
'headers' => $data['headers'] ?? [],
];
} elseif (json_encode($data['contents']) === false) {
$value = [
'name' => $data['filename'] ?? null,
'size' => (strlen($data['contents']) / 1000).'KB',
'headers' => $data['headers'] ?? [],
];
} else {
$value = $data['contents'];
}
return collect(preg_split("/--.*\r\n/", $request->data()))
->filter()
->values()
->mapWithKeys(function ($content): array {
$contentArray = collect(preg_split("/\r\n/", $content))
->filter()
->values();

return [$data['name'] => $value];
})->toArray();
$key = $contentArray->firstWhere(fn ($content) => str_contains($content, 'name='));

if ($hasContentType = $contentArray->search(fn ($contentItem) => str_contains($contentItem, 'Content-Type'))) {
$contentArray = $contentArray->filter(fn ($contentItem, $index) => $index <= $hasContentType);
}

$contentName = str($key)->match("/name\=[\',\"](\w*)[\',\"]/")->toString();

return [$contentName => $contentArray];
})->toArray();
}

/**
Expand Down

0 comments on commit 03bf02a

Please sign in to comment.