|
3 | 3 | $db = new DB();
|
4 | 4 |
|
5 | 5 | $data = null;
|
6 |
| - |
7 |
| -if (!isset($_FILES['file'])) { |
8 |
| - echo json_encode(['message' => 'No file found', 'data' => file_get_contents( 'php://input' )]); |
| 6 | +if (!isset($_FILES) || count($_FILES) === 0) { |
| 7 | + echo json_encode(['message' => 'No files found', 'data' => file_get_contents( 'php://input' )]); |
9 | 8 | return;
|
10 | 9 | }
|
11 | 10 | if (!isset($_POST['track_id'])) {
|
12 | 11 | echo json_encode(['message' => 'Missing track id']);
|
13 | 12 | return;
|
14 | 13 | }
|
15 | 14 |
|
16 |
| -$existing = $db->get_file_by_name($_FILES['file']['name']); |
17 |
| -if ($existing) { |
18 |
| - $db->add_file_to_track($existing, htmlspecialchars($_POST['track_id'])); |
19 |
| - header("Content-Type: application/json"); |
20 |
| - http_response_code(200); |
21 |
| - echo json_encode($existing); |
22 |
| - exit(); |
23 |
| -} |
| 15 | +$results = []; |
| 16 | +for ($i = 0; $i < count($_FILES); $i++) { |
24 | 17 |
|
25 |
| -$allowedFormats = ['mp3', 'flac', 'ogg', 'vorbis', 'wav', 'mp4']; |
26 |
| -$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); |
27 |
| -if (in_array($extension, $allowedFormats)) { |
28 |
| - $path = $_SERVER['DOCUMENT_ROOT'] . "/" . $db->get_media_folder(); |
29 |
| - $filename = $_FILES['file']['name']; |
30 |
| - $pathAndFilename = $path . "/" . $filename; |
31 |
| - move_uploaded_file($_FILES['file']['tmp_name'], $pathAndFilename); |
32 |
| - header("Content-Type: application/json"); |
33 |
| - http_response_code(200); |
34 |
| - echo json_encode($db->create_file(htmlspecialchars($filename), htmlspecialchars($_POST['track_id']))); |
35 |
| -} else { |
36 |
| - header("Content-Type: application/json"); |
37 |
| - http_response_code(400); |
38 |
| - echo json_encode($extension . " not supported"); |
39 |
| -} |
| 18 | + $existing = $db->get_file_by_name($_FILES["file-$i"]['name']); |
| 19 | + if ($existing) { |
| 20 | + $results['files'][] = [ |
| 21 | + 'id' => $db->add_file_to_track($existing, htmlspecialchars($_POST['track_id'])), |
| 22 | + 'name' => $existing, |
| 23 | + ]; |
| 24 | + $results['messages'][] = "File $existing already exists. Adding to track."; |
| 25 | + } |
40 | 26 |
|
| 27 | + $allowedFormats = ['mp3', 'flac', 'ogg', 'vorbis', 'wav', 'mp4']; |
| 28 | + $extension = pathinfo($_FILES["file-$i"]['name'], PATHINFO_EXTENSION); |
| 29 | + if (in_array($extension, $allowedFormats)) { |
| 30 | + $path = $_SERVER['DOCUMENT_ROOT'] . "/" . $db->get_media_folder(); |
| 31 | + $filename = $_FILES["file-$i"]['name']; |
| 32 | + $pathAndFilename = $path . "/" . $filename; |
| 33 | + move_uploaded_file($_FILES["file-$i"]['tmp_name'], $pathAndFilename); |
| 34 | + $db->create_file(htmlspecialchars($filename), htmlspecialchars($_POST['track_id'])); |
| 35 | + $results['files'][] = [ |
| 36 | + 'id' => $db->create_file(htmlspecialchars($filename), htmlspecialchars($_POST['track_id'])), |
| 37 | + 'name' => $filename, |
| 38 | + ]; |
| 39 | + } else { |
| 40 | + $results['messages'][] = "$extension not supported, in file " . $_FILES["file-$i"]['name']; |
| 41 | + } |
| 42 | +} |
| 43 | +header("Content-Type: application/json"); |
| 44 | +http_response_code(200); |
| 45 | +echo json_encode($results); |
41 | 46 | exit();
|
| 47 | + |
| 48 | + |
0 commit comments