Skip to content

Commit 192092d

Browse files
committed
feat: update upload & fix request issues
1 parent 103d4bc commit 192092d

File tree

1 file changed

+61
-86
lines changed

1 file changed

+61
-86
lines changed

src/Request.php

Lines changed: 61 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Request
2929
* @var array
3030
*/
3131
protected static $formDataMediaTypes = ['application/x-www-form-urlencoded'];
32+
protected static $errors = [];
3233

3334
/**
3435
* Get HTTP method
@@ -285,7 +286,7 @@ public static function isFormData(): bool
285286
{
286287
$method = static::getMethod();
287288

288-
return ($method === self::METHOD_POST && is_null(static::getContentType())) || in_array(static::getMediaType(), self::$formDataMediaTypes);
289+
return ($method === static::METHOD_POST && is_null(static::getContentType())) || in_array(static::getMediaType(), static::$formDataMediaTypes);
289290
}
290291

291292
/**
@@ -318,10 +319,6 @@ public static function validate(array $rules, bool $returnFullData = false)
318319
{
319320
$data = \Leaf\Form::validate(static::body(false), $rules);
320321

321-
if ($data === false) {
322-
return false;
323-
}
324-
325322
return $returnFullData ? $data : static::get(array_keys($rules));
326323
}
327324

@@ -332,14 +329,16 @@ public static function validate(array $rules, bool $returnFullData = false)
332329
protected static function auth()
333330
{
334331
if (!class_exists('\Leaf\Auth')) {
335-
throw new \Exception('You need to install the leafs/auth package to use the auth helper');
332+
throw new \Exception('You need to install the leafs/auth module to use the auth helper');
336333
}
337334

338-
if (!(\Leaf\Config::get('auth.instance'))) {
339-
\Leaf\Config::set('auth.instance', new \Leaf\Auth());
335+
if (!(\Leaf\Config::getStatic('auth'))) {
336+
\Leaf\Config::singleton('auth', function () {
337+
return new \Leaf\Auth;
338+
});
340339
}
341340

342-
return \Leaf\Config::get('auth.instance');
341+
return \Leaf\Config::get('auth');
343342
}
344343

345344
/**
@@ -351,15 +350,50 @@ public static function user()
351350
}
352351

353352
/**
354-
* Handle errors from validation
355-
* @return array
353+
* Store a file from the request.
354+
*
355+
* @param string $key The name of the file input the request.
356+
* @param string $destination The directory where the file should be stored.
357+
* @param array $config Optional configurations: max_file_size, file_type, extensions
358+
*
359+
* @return array|false An array containing the status or false for a failure.
356360
*/
357-
public static function errors()
361+
public static function upload(string $key, string $destination, array $config = [])
358362
{
359-
return array_merge(
360-
\Leaf\Form::errors(),
361-
static::auth()->errors()
363+
$file = static::files($key);
364+
365+
if (!$file) {
366+
static::$errors['upload'] = 'No file was uploaded.';
367+
return false;
368+
}
369+
370+
if (isset($config['extensions'])) {
371+
$fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
372+
373+
if (!in_array($fileExtension, $config['extensions'])) {
374+
static::$errors['upload'] = 'Invalid file extension.';
375+
return false;
376+
}
377+
}
378+
379+
$config['unique'] = true;
380+
$fileSystem = new \Leaf\FS;
381+
$uploadedFile = $fileSystem->uploadFile(
382+
$file,
383+
preg_replace(
384+
'/\/$/',
385+
'',
386+
$destination
387+
) . '/',
388+
$config
362389
);
390+
391+
if (!$uploadedFile) {
392+
static::$errors = $fileSystem->errors();
393+
return false;
394+
}
395+
396+
return $fileSystem->uploadInfo($uploadedFile);
363397
}
364398

365399
/**
@@ -378,6 +412,7 @@ public static function getContentType(): ?string
378412
public static function getMediaType(): ?string
379413
{
380414
$contentType = static::getContentType();
415+
381416
if ($contentType) {
382417
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
383418

@@ -393,8 +428,8 @@ public static function getMediaType(): ?string
393428
*/
394429
public static function getMediaTypeParams(): array
395430
{
396-
$contentType = static::getContentType();
397431
$contentTypeParams = [];
432+
$contentType = static::getContentType();
398433

399434
if ($contentType) {
400435
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
@@ -444,7 +479,6 @@ public static function getHost(): string
444479
return $matches[1];
445480
} else if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
446481
$hostParts = explode(':', $_SERVER['HTTP_HOST']);
447-
448482
return $hostParts[0];
449483
}
450484

@@ -568,74 +602,15 @@ public static function getUserAgent(): ?string
568602
}
569603

570604
/**
571-
* Store a file from the request.
572-
*
573-
* @param string $key The name of the file input the request.
574-
* @param string $destination The directory where the file should be stored.
575-
* @param array $configs Optional configurations: max_file_size, file_type, extensions
576-
* @return array An array containing the status, path, and error message.
577-
*/
578-
public static function store(string $key, string $destination, array $configs = []): object
579-
{
580-
$configs["unique"] = true;
581-
582-
# See PR notes #1
583-
if(isset($configs["extensions"])) {
584-
$file = self::get($key);
585-
$fileExtension = pathinfo($file["name"], PATHINFO_EXTENSION);
586-
if(!in_array($fileExtension, $configs["extensions"])) {
587-
return (object) [
588-
'status' => false,
589-
'error' => 'Invalid file extension.'
590-
];
591-
}
592-
}
593-
594-
$fileSystem = new \Leaf\FS;
595-
$uploadedFile = $fileSystem::uploadFile(self::get($key), StoragePath($destination), $configs);
596-
if(!$uploadedFile)
597-
return (object) [
598-
'status' => false,
599-
'error' => $fileSystem::$errorsArray['upload']
600-
];
601-
602-
return (object) array_shift($fileSystem::$uploadInfo);
603-
}
604-
605-
/**
606-
* Store a file from the request with a specific name.
607-
*
608-
* @param string $key The name of the file input the request.
609-
* @param string $destination The directory where the file should be stored.
610-
* @param string $filename The name to give the stored file.
611-
* @param array $configs Optional configurations: max_file_size, file_type, extensions
612-
* @return array An array containing the status, path, and error message.
613-
*/
614-
public static function storeAs(string $key, string $destination, string $filename, array $configs = []): object
615-
{
616-
$configs["rename"] = true;
617-
$configs["name"] = $filename;
618-
619-
# See PR notes #1
620-
if(isset($configs["extensions"])) {
621-
$file = self::get($key);
622-
$fileExtension = pathinfo($file["name"], PATHINFO_EXTENSION);
623-
if(!in_array($fileExtension, $configs["extensions"])) {
624-
return (object) [
625-
'status' => false,
626-
'error' => 'Invalid file extension.'
627-
];
628-
}
629-
}
630-
631-
$fileSystem = new \Leaf\FS;
632-
$uploadedFile = $fileSystem::uploadFile(self::get($key), storagePath($destination), $configs);
633-
if(!$uploadedFile)
634-
return (object) [
635-
'status' => false,
636-
'error' => $fileSystem::$errorsArray['upload']
637-
];
638-
639-
return (object) array_shift($fileSystem::$uploadInfo);
605+
* Handle errors from validation/auth/upload
606+
* @return array
607+
*/
608+
public static function errors()
609+
{
610+
return array_merge(
611+
static::$errors,
612+
\Leaf\Form::errors(),
613+
class_exists('\Leaf\Auth') ? static::auth()->errors() : []
614+
);
640615
}
641616
}

0 commit comments

Comments
 (0)