]> jfr.im git - uguu.git/commitdiff
bug fixes
authorGo Johansson <redacted>
Sat, 24 Dec 2022 16:58:22 +0000 (17:58 +0100)
committerGo Johansson <redacted>
Sat, 24 Dec 2022 16:58:22 +0000 (17:58 +0100)
src/Classes/Response.php
src/Classes/Upload.php
src/Classes/UploadGateway.php [deleted file]
src/static/php/upload.php

index 7c2fe4f16f9de7230d65c163617a777c477b1c96..6e60b69965430fcf1e4446968a92bc206acb9726 100644 (file)
     
     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':
index b092cfec47fae6be81304560ff3536ceb2eb824e..7c2f697ace0aacbddcef1a0e57cd7584d4359276 100644 (file)
                         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 (file)
index 6e66aeb..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-<?php
-    /**
-     * Uguu
-     *
-     * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
-     *
-     * 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 <https://www.gnu.org/licenses/>.
-     */
-    
-    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
index c4f25a6201e2917a1e2d2ad4db557b3e3a455911..6d608009eacd5ac2bf32633af6c5b1debef5e086 100644 (file)
     
     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