-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.php
173 lines (142 loc) · 4.06 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* @author Robert Boloc <[email protected]>
* @copyright 2014 Servei de Recursos Educatius (http://www.sre.urv.cat)
*/
require_once __DIR__ . '/api_caller.php';
$config = include __DIR__ . '/config/local.php';
$token = filter_input(INPUT_POST, 'token');
if (empty($token)) {
api_response(array(
'status' => 'error',
'message' => 'error:accessdenied',
));
}
$api_caller = new api_caller($token, $config);
// validate the token before proceeding
$token_validation = $api_caller->get('token.validate');
if ($token_validation->status !== 'success') {
api_response(array(
'status' => 'error',
'message' => 'error:accessdenied'
));
}
/* Deal with the upload */
// Check upload exists (only file "video" is of interest)
if (!isset($_FILES['video'])) {
api_response(array(
'status' => 'error',
'message' => 'error:unknownfile',
));
}
// Check for errors
if (!isset($_FILES['video']['error']) ||
is_array($_FILES['video']['error'])) {
api_response(array(
'status' => 'error',
'message' => 'error:invalidparams',
));
}
// Check error value
switch ($_FILES['video']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
api_response(array(
'status' => 'error',
'message' => 'error:nofilesent',
));
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
api_response(array(
'status' => 'error',
'message' => 'error:filetoobig',
));
default:
api_response(array(
'status' => 'error',
'message' => 'error:unknownerror',
));
}
// Check the size
if ($_FILES['video']['size'] > $config['max_size']) {
api_response(array(
'status' => 'error',
'message' => 'error:filetoobig',
));
}
// Create the folder for the user
$token_user = $api_caller->get('token.user');
if ($token_user->status !== 'success') {
api_response(array(
'status' => 'error',
'message' => 'error:useroftokennotfound'
));
}
$user_dir = $config['uploads_dir'] . '/' . $token_user->data->userid;
if (!is_dir($user_dir)) {
mkdir($user_dir);
}
if (!is_dir($user_dir)) {
api_response(array(
'status' => 'error',
'message' => 'error:failedcreatinguserdir'
));
}
// Store the file reference into the db
$file = array(
'name' => $_FILES['video']['name'],
'hash' => sha1_file($_FILES['video']['tmp_name']),
'size' => $_FILES['video']['size']
);
$file_record = $api_caller->post('file.create', $file);
if ($file_record->status !== 'success') {
// Something went wrong
api_response(array(
'status' => 'error',
'message' => $file_record->message,
));
}
// Move the uploaded file
if (!move_uploaded_file(
$_FILES['video']['tmp_name'], $user_dir . '/' . $file['hash'])) {
api_response(array(
'status' => 'error',
'message' => 'error:movingfile'
));
//@TODO cleanup created file record if move fails
}
// Enqueue the video remotely
$queue_item = array(
'userid' => $token_user->data->userid,
'fileid' => $file_record->data->fileid,
);
$enqueued = $api_caller->post('queue.new', $queue_item);
if ($enqueued->status !== 'success') {
api_response(array(
'status' => 'error',
'message' => $enqueued->message,
));
}
// Enqueue the video locally
$local_queue_cache_file = $config['uploads_dir'] . '/.jobs';
if (!is_file($local_queue_cache_file)) {
file_put_contents($local_queue_cache_file, json_encode(array()));
}
$current_queue = json_decode(file_get_contents($local_queue_cache_file), true);
$current_queue[$enqueued->data->position] = array(
'queue_item_id' => $enqueued->data->queue_item_id,
'token' => $token,
'file' => $user_dir . '/' . $file['hash']
);
file_put_contents($local_queue_cache_file, json_encode($current_queue));
// Return success message
api_response(array(
'status' => 'success',
));
/* Functions */
function api_response(array $response)
{
echo json_encode($response);
exit;
}