From 7973540a9ebee826fd75e58f118bcc957b6a92da Mon Sep 17 00:00:00 2001 From: Raj Janorkar Date: Mon, 20 Mar 2023 14:13:09 +1000 Subject: [PATCH] Update Request.php Set the new value for given key in the metadata from the request header. This function will be useful to change metadata on the fly say in the middleware. --- src/Request.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Request.php b/src/Request.php index c6df760..fe71060 100644 --- a/src/Request.php +++ b/src/Request.php @@ -186,6 +186,37 @@ public function extractAllMeta(): array return $result; } + /** + * Set the new value for given key in the metadata from the request header. + * This function will be useful to change metadata on the fly say in the middleware. + * + * @param string $requestedKey + * @param string $newValue + * + * @return string + */ + public function setMeta(string $requestedKey, string $newValue): string + { + $uploadMetaData = $this->request->headers->get('Upload-Metadata'); + + if (empty($uploadMetaData)) { + return ''; + } + + $uploadMetaDataChunks = explode(',', $uploadMetaData); + + foreach ($uploadMetaDataChunks as $key => $chunk) { + $pieces = explode(' ', trim($chunk)); + if ($pieces[0] === $requestedKey) { + $pieces[1] = base64_encode($newValue); + $uploadMetaDataChunks[$key] = implode(' ', $pieces); + break; + } + } + + return implode(',', $uploadMetaDataChunks); + } + /** * Extract partials from header. *