-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
executable file
·115 lines (95 loc) · 2.45 KB
/
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Reference server for the H5P Caretaker library.
*
* PHP version 8
*
* @category Tool
* @package H5PCaretakerServer
* @author Oliver Tacke <[email protected]>
* @license MIT License
* @link https://github.com/ndlano/h5p-caretaker-server
*/
namespace Ndlano\H5PCaretakerServer;
require __DIR__ . '/vendor/autoload.php';
use Ndlano\H5PCaretaker\H5PCaretaker;
/**
* Convert a human-readable size to bytes.
*
* @param string $size The human-readable size.
*/
function convertToBytes($size) {
$unit = substr($size, -1);
$value = (int)$size;
switch (strtoupper($unit)) {
case 'G':
return $value * 1024 * 1024 * 1024;
case 'M':
return $value * 1024 * 1024;
case 'K':
return $value * 1024;
default:
return $value;
}
}
/**
* Exit the script with an optional HTTP status code.
*
* @param int $code The HTTP status code to send.
* @param string $message The message to display.
*
* @return void
*/
function done($code, $message)
{
if (isset($message)) {
echo $message;
}
if (isset($code)) {
http_response_code($code);
};
exit();
}
$maxFileSize = convertToBytes(min(ini_get('post_max_size'), ini_get('upload_max_filesize')));
if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
done( 405, _('Method Not Allowed') );
}
if (!isset($_FILES['file'])) {
done(
422,
sprintf(
_('It seems that no file was provided or it exceeds the file upload size limit of %s KB.'),
$max_file_size / 1024
)
);
}
$file = $_FILES['file'];
if ($file['error'] !== UPLOAD_ERR_OK) {
done( 500, _('Something went wrong with the file upload, but I dunno what.'));
}
if ($file['size'] > $maxFileSize) {
done(
413,
sprintf(
_('The file is larger than the allowed maximum file size of %s KB.'),
$max_file_size / 1024
)
);
}
$config = [
'uploadsPath' => './uploads',
'cachePath' => './cache',
];
if (isset($_POST['locale'])) {
$config['locale'] = $_POST['locale'];
}
$h5pCaretaker = new H5PCaretaker($config);
// TODO add an action parameter:
// - analyze (default)
// - write (with optional overwrite parameters)
$analysis = $h5pCaretaker->analyze(['file' => $file['tmp_name']]);
if (isset($analysis['error'])) {
done(422, $analysis['error']);
}
done(200, $analysis['result']);
?>