Skip to content

Commit

Permalink
Merge pull request #20 from ibnsultan/v2.x
Browse files Browse the repository at this point in the history
Add `store` and `storeAs` methods to the Request class
  • Loading branch information
ibnsultan authored Aug 8, 2024
2 parents d54f6dc + 6c5a221 commit c14b208
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"prefer-stable": true,
"require": {
"leafs/anchor": "*",
"leafs/form": "*"
"leafs/form": "*",
"leafs/fs": "*"
}
}
72 changes: 72 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,76 @@ public static function getUserAgent(): ?string
{
return Headers::get('HTTP_USER_AGENT');
}

/**
* Store a file from the request.
*
* @param string $key The name of the file input the request.
* @param string $destination The directory where the file should be stored.
* @param array $configs Optional configurations: max_file_size, file_type, extensions
* @return array An array containing the status, path, and error message.
*/
public static function store(string $key, string $destination, array $configs = []): object
{
$configs["unique"] = true;

# See PR notes #1
if(isset($configs["extensions"])) {
$file = self::get($key);
$fileExtension = pathinfo($file["name"], PATHINFO_EXTENSION);
if(!in_array($fileExtension, $configs["extensions"])) {
return (object) [
'status' => false,
'error' => 'Invalid file extension.'
];
}
}

$fileSystem = new \Leaf\FS;
$uploadedFile = $fileSystem::uploadFile(self::get($key), $destination, $configs);
if(!$uploadedFile)
return (object) [
'status' => false,
'error' => $fileSystem::$errorsArray['upload']
];

return (object) array_shift($fileSystem::$uploadInfo);
}

/**
* Store a file from the request with a specific name.
*
* @param string $key The name of the file input the request.
* @param string $destination The directory where the file should be stored.
* @param string $filename The name to give the stored file.
* @param array $configs Optional configurations: max_file_size, file_type, extensions
* @return array An array containing the status, path, and error message.
*/
public static function storeAs(string $key, string $destination, string $filename, array $configs = []): object
{
$configs["rename"] = true;
$configs["name"] = $filename;

# See PR notes #1
if(isset($configs["extensions"])) {
$file = self::get($key);
$fileExtension = pathinfo($file["name"], PATHINFO_EXTENSION);
if(!in_array($fileExtension, $configs["extensions"])) {
return (object) [
'status' => false,
'error' => 'Invalid file extension.'
];
}
}

$fileSystem = new \Leaf\FS;
$uploadedFile = $fileSystem::uploadFile(self::get($key), $destination, $configs);
if(!$uploadedFile)
return (object) [
'status' => false,
'error' => $fileSystem::$errorsArray['upload']
];

return (object) array_shift($fileSystem::$uploadInfo);
}
}

0 comments on commit c14b208

Please sign in to comment.