]> jfr.im git - uguu.git/blame - src/static/php/upload.php
fixes
[uguu.git] / src / static / php / upload.php
CommitLineData
d8c46ff7 1<?php
24383942 2
f0794be3
GJ
3 /**
4 * Uguu
5 *
6 * @copyright Copyright (c) 2022 Go Johansson (nokonoko) <neku@pomf.se>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
24383942 21
f0794be3 22 require_once __DIR__ . '/../vendor/autoload.php';
24383942 23
8be20956
GJ
24 use Pomf\Uguu\Classes\Upload;
25 use Pomf\Uguu\Classes\Response;
f0b5e51c
GJ
26
27 /**
28 * It takes a string and an array as arguments, creates a new Upload object,
29 * calls the reFiles method on the Upload object, calls the fingerPrint method on
30 * the Upload object, calls the uploadFile method on the Upload object,
31 * calls the send method on the Upload object, and calls the error method on the
32 * Upload object
33 *
34 * @param $outputFormat string The format of the output, json or xml
35 * @param $files array The file to be uploaded, which is an array.
36 *
37 * @throws \Exception
38 */
39function handleFile(string $outputFormat, array $files): void
40{
24383942 41 $fCount = count($files['size']);
f0b5e51c
GJ
42 $upload = new Upload($outputFormat);
43 $files = $upload->reFiles($files);
44 try {
24383942 45 $upload->fingerPrint($fCount);
f0b5e51c
GJ
46 $res = [];
47 foreach ($files as $ignored) {
48 $res[] = $upload->uploadFile();
49 }
50 if (!empty($res)) {
51 $upload->send($res);
8be20956 52 }
f0b5e51c 53 } catch (Exception $e) {
24383942 54 $upload->error(500, $e->getMessage());
f0794be3 55 }
f0b5e51c 56}
24383942 57
f0b5e51c
GJ
58 $response = new Response('json');
59
60if (!isset($_FILES['files']) or empty($_FILES['files'])) {
61 $response->error(400, 'No input file(s)');
62}
63if (isset($_GET['output']) and !empty($_GET['output'])) {
64 $resType = strtolower(preg_replace('/[^a-zA-Z]/', '', $_GET['output']));
65} else {
66 $resType = 'json';
67}
68
69try {
70 handleFile($resType, $_FILES['files']);
71} catch (Exception $e) {
72 $response->error($e->getCode(), $e->getMessage());
73}