diff --git a/src/Classes/Response.php b/src/Classes/Response.php index 7c2fe4f..6e60b69 100644 --- a/src/Classes/Response.php +++ b/src/Classes/Response.php @@ -22,14 +22,14 @@ class Response { - public mixed $type; + public string $type; /** * Takes a string as an argument and sets the header to the appropriate content type * * @param $response_type string The type of response you want to return. Valid options are: csv, html, json, text. */ - public function __construct(string $response_type = "json") + public function __construct(string $response_type) { switch ($response_type) { case 'csv': diff --git a/src/Classes/Upload.php b/src/Classes/Upload.php index b092cfe..7c2f697 100644 --- a/src/Classes/Upload.php +++ b/src/Classes/Upload.php @@ -246,9 +246,9 @@ throw new Exception('Gave up trying to find an unused name!', 500); } $NEW_NAME = ''; - for ($i = 0; $i < $this->Connector->CONFIG['NAME_LENGTH']; ++$i) { - $NEW_NAME .= $this->Connector->CONFIG['ID_CHARSET'] - [mt_rand(0, strlen($this->Connector->CONFIG['ID_CHARSET']))]; + $count = strlen($this->Connector->CONFIG['ID_CHARSET']); + while ($this->Connector->CONFIG['NAME_LENGTH']--) { + $NEW_NAME .= $this->Connector->CONFIG['ID_CHARSET'][mt_rand(0, $count - 1)]; } if (!empty($extension)) { $NEW_NAME .= '.' . $extension; diff --git a/src/Classes/UploadGateway.php b/src/Classes/UploadGateway.php deleted file mode 100644 index 6e66aeb..0000000 --- a/src/Classes/UploadGateway.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - namespace Pomf\Uguu\Classes; - - use Exception; - - class UploadGateway extends Upload - { - /** - * It handles the file uploads. - * - * @param $output mixed The output format of the response. - * @param $files mixed The name of the file input field. - * - * @throws \Exception - */ - public function handleFile(mixed $output, mixed $files) - { - $type = 'json' ?? $output; - $response = (new Response($type)); - if (!empty($_FILES['files'])) { - $files = $this->reFiles($files); - try { - $this->fingerPrint(count($files)); - $res = []; - foreach ($files as $ignored) { - $res[] = $this->uploadFile(); - } - if (!empty($res)) { - $response->send($res); - } - } - catch (Exception $e) { - $response->error($e->getCode(), $e->getMessage()); - } - } else { - $response->error(400, 'No input file(s)'); - } - } - } \ No newline at end of file diff --git a/src/static/php/upload.php b/src/static/php/upload.php index c4f25a6..6d60800 100644 --- a/src/static/php/upload.php +++ b/src/static/php/upload.php @@ -20,10 +20,34 @@ require_once __DIR__ . '/../vendor/autoload.php'; - use Pomf\Uguu\Classes\UploadGateway; + use Pomf\Uguu\Classes\Upload; + use Pomf\Uguu\Classes\Response; - try { - (new UploadGateway())->handleFile($_GET['output'], $_FILES['files']); - } catch (Exception $e) { - throw new Exception($e->getMessage(), 500); + function handleFile(string $outputFormat, array $files) + { + $upload = new Upload($outputFormat); + $files = $upload->reFiles($files); + try { + $upload->fingerPrint(count($files)); + $res = []; + foreach ($files as $ignored) { + $res[] = $upload->uploadFile(); + } + if (!empty($res)) { + $upload->send($res); + } + } catch (Exception $e) { + $upload->error($e->getCode(), $e->getMessage()); + } } + + if (!isset($_FILES['files']) or empty($_FILES['files'])) { + $response = new Response('json'); + $response->error(400, 'No input file(s)'); + } + if (isset($_GET['output']) and !empty($_GET['output'])) { + $resType = filter_var($_GET['output'], FILTER_SANITIZE_SPECIAL_CHARS); + } else { + $resType = 'json'; + } + handleFile($resType, $_FILES['files']); \ No newline at end of file