]> jfr.im git - uguu.git/blob - src/static/php/upload.php
fixes
[uguu.git] / src / static / php / upload.php
1 <?php
2
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 */
21
22 require_once __DIR__ . '/../vendor/autoload.php';
23
24 use Pomf\Uguu\Classes\Upload;
25 use Pomf\Uguu\Classes\Response;
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 */
39 function handleFile(string $outputFormat, array $files): void
40 {
41 $fCount = count($files['size']);
42 $upload = new Upload($outputFormat);
43 $files = $upload->reFiles($files);
44 try {
45 $upload->fingerPrint($fCount);
46 $res = [];
47 foreach ($files as $ignored) {
48 $res[] = $upload->uploadFile();
49 }
50 if (!empty($res)) {
51 $upload->send($res);
52 }
53 } catch (Exception $e) {
54 $upload->error(500, $e->getMessage());
55 }
56 }
57
58 $response = new Response('json');
59
60 if (!isset($_FILES['files']) or empty($_FILES['files'])) {
61 $response->error(400, 'No input file(s)');
62 }
63 if (isset($_GET['output']) and !empty($_GET['output'])) {
64 $resType = strtolower(preg_replace('/[^a-zA-Z]/', '', $_GET['output']));
65 } else {
66 $resType = 'json';
67 }
68
69 try {
70 handleFile($resType, $_FILES['files']);
71 } catch (Exception $e) {
72 $response->error($e->getCode(), $e->getMessage());
73 }