bug fixes
This commit is contained in:
parent
923cc54dbc
commit
8be2095622
4 changed files with 34 additions and 68 deletions
|
@ -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':
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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']);
|
Loading…
Reference in a new issue