|
| 1 | +<?php |
| 2 | +// In-memory logger |
| 3 | +$debug_log = []; |
| 4 | +function log_upload_debug($msg) { |
| 5 | + global $debug_log; |
| 6 | + $ts = date('Y-m-d H:i:s'); |
| 7 | + $debug_log[] = "[$ts] $msg"; |
| 8 | +} |
| 9 | + |
| 10 | +header('Access-Control-Allow-Origin: *'); |
| 11 | +header('Access-Control-Allow-Methods: POST, OPTIONS'); |
| 12 | +header('Access-Control-Allow-Headers: Content-Type'); |
| 13 | +header('Content-Type: application/json'); |
| 14 | + |
| 15 | +// Handle chunked audio upload |
| 16 | +if ( |
| 17 | + $_SERVER['REQUEST_METHOD'] === 'POST' && |
| 18 | + isset($_GET['action']) && $_GET['action'] === 'upload_chunk' |
| 19 | +) { |
| 20 | + log_upload_debug('--- New chunked upload request ---'); |
| 21 | + $uploadDir = __DIR__ . '/../assets/recordings/chunks/'; |
| 22 | + if (!file_exists($uploadDir)) { |
| 23 | + mkdir($uploadDir, 0755, true); |
| 24 | + } |
| 25 | + $chunkNumber = isset($_POST['chunkNumber']) ? intval($_POST['chunkNumber']) : 0; |
| 26 | + $totalChunks = isset($_POST['totalChunks']) ? intval($_POST['totalChunks']) : 1; |
| 27 | + $fileName = isset($_POST['fileName']) ? preg_replace('/[^a-zA-Z0-9._-]/', '_', $_POST['fileName']) : uniqid('audio_'); |
| 28 | + $recordingId = isset($_POST['recordingId']) ? preg_replace('/[^a-zA-Z0-9-_]/', '_', $_POST['recordingId']) : 'unknown'; |
| 29 | + $uploadId = isset($_POST['uploadId']) ? preg_replace('/[^a-zA-Z0-9_-]/', '_', $_POST['uploadId']) : uniqid('upload_'); |
| 30 | + $chunkFile = $uploadDir . $uploadId . '_' . $chunkNumber; |
| 31 | + log_upload_debug("Chunk number: $chunkNumber / $totalChunks, fileName: $fileName, recordingId: $recordingId, uploadId: $uploadId"); |
| 32 | + |
| 33 | + if (!isset($_FILES['chunk']) || $_FILES['chunk']['error'] !== UPLOAD_ERR_OK) { |
| 34 | + log_upload_debug('No chunk uploaded or upload error.'); |
| 35 | + http_response_code(400); |
| 36 | + echo json_encode(['success' => false, 'error' => 'No chunk uploaded', 'uploadId' => $uploadId, 'debug' => $debug_log]); |
| 37 | + exit; |
| 38 | + } |
| 39 | + |
| 40 | + if (!move_uploaded_file($_FILES['chunk']['tmp_name'], $chunkFile)) { |
| 41 | + log_upload_debug('Failed to save chunk.'); |
| 42 | + http_response_code(500); |
| 43 | + echo json_encode(['success' => false, 'error' => 'Failed to save chunk', 'uploadId' => $uploadId, 'debug' => $debug_log]); |
| 44 | + exit; |
| 45 | + } |
| 46 | + |
| 47 | + log_upload_debug('Chunk saved to: ' . $chunkFile); |
| 48 | + |
| 49 | + if ($chunkNumber === $totalChunks - 1) { |
| 50 | + log_upload_debug('This is the final chunk, beginning assembly...'); |
| 51 | + $finalDir = __DIR__ . '/../assets/recordings/' . $recordingId . '/'; |
| 52 | + log_upload_debug('__DIR__ is: ' . __DIR__); |
| 53 | + log_upload_debug('Final directory (relative): ' . $finalDir); |
| 54 | + |
| 55 | + $parentDir = dirname($finalDir); |
| 56 | + log_upload_debug('Parent directory: ' . $parentDir); |
| 57 | + log_upload_debug('Parent directory exists: ' . (file_exists($parentDir) ? 'yes' : 'no')); |
| 58 | + log_upload_debug('Parent directory writable: ' . (is_writable($parentDir) ? 'yes' : 'no')); |
| 59 | + |
| 60 | + if (!file_exists($finalDir)) { |
| 61 | + log_upload_debug('Final directory does not exist, attempting to create...'); |
| 62 | + $mkdirResult = @mkdir($finalDir, 0755, true); |
| 63 | + $mkdirError = error_get_last(); |
| 64 | + log_upload_debug('mkdir result: ' . ($mkdirResult ? 'success' : 'failed')); |
| 65 | + if (!$mkdirResult && $mkdirError) { |
| 66 | + log_upload_debug('mkdir error: ' . json_encode($mkdirError)); |
| 67 | + } |
| 68 | + log_upload_debug('Final directory exists after mkdir: ' . (file_exists($finalDir) ? 'yes' : 'no')); |
| 69 | + } else { |
| 70 | + log_upload_debug('Final directory already exists'); |
| 71 | + } |
| 72 | + |
| 73 | + log_upload_debug('Final directory writable: ' . (is_writable($finalDir) ? 'yes' : 'no')); |
| 74 | + log_upload_debug('Final directory permissions: ' . (file_exists($finalDir) ? substr(sprintf('%o', fileperms($finalDir)), -4) : 'N/A')); |
| 75 | + |
| 76 | + $finalPath = $finalDir . $fileName; |
| 77 | + log_upload_debug('Final file path: ' . $finalPath); |
| 78 | + |
| 79 | + if (!file_exists($finalDir)) { |
| 80 | + log_upload_debug('ERROR: Final directory still does not exist, cannot create file'); |
| 81 | + http_response_code(500); |
| 82 | + echo json_encode(['success' => false, 'error' => 'Failed to create directory', 'uploadId' => $uploadId, 'debug' => $debug_log]); |
| 83 | + exit; |
| 84 | + } |
| 85 | + |
| 86 | + $out = @fopen($finalPath, 'wb'); |
| 87 | + if (!$out) { |
| 88 | + $fopenError = error_get_last(); |
| 89 | + log_upload_debug('fopen failed for final file: ' . json_encode($fopenError)); |
| 90 | + http_response_code(500); |
| 91 | + echo json_encode(['success' => false, 'error' => 'Failed to create final file', 'uploadId' => $uploadId, 'debug' => $debug_log]); |
| 92 | + exit; |
| 93 | + } |
| 94 | + log_upload_debug('Opened final file for writing'); |
| 95 | + |
| 96 | + for ($i = 0; $i < $totalChunks; $i++) { |
| 97 | + $part = $uploadDir . $uploadId . '_' . $i; |
| 98 | + if (!file_exists($part)) { |
| 99 | + log_upload_debug('Missing chunk ' . $i . ' at path: ' . $part); |
| 100 | + fclose($out); |
| 101 | + http_response_code(500); |
| 102 | + echo json_encode(['success' => false, 'error' => 'Missing chunk ' . $i, 'uploadId' => $uploadId, 'debug' => $debug_log]); |
| 103 | + exit; |
| 104 | + } |
| 105 | + log_upload_debug('Processing chunk ' . $i . ', size: ' . filesize($part) . ' bytes'); |
| 106 | + $in = fopen($part, 'rb'); |
| 107 | + stream_copy_to_stream($in, $out); |
| 108 | + fclose($in); |
| 109 | + unlink($part); |
| 110 | + log_upload_debug('Chunk ' . $i . ' copied and deleted'); |
| 111 | + } |
| 112 | + fclose($out); |
| 113 | + |
| 114 | + $finalFileSize = file_exists($finalPath) ? filesize($finalPath) : 0; |
| 115 | + log_upload_debug('All chunks assembled. Final file size: ' . $finalFileSize . ' bytes'); |
| 116 | + log_upload_debug('Final file exists: ' . (file_exists($finalPath) ? 'yes' : 'no')); |
| 117 | + |
| 118 | + http_response_code(200); |
| 119 | + echo json_encode([ |
| 120 | + 'success' => true, |
| 121 | + 'uploadId' => $uploadId, |
| 122 | + 'filePath' => '/assets/recordings/' . $recordingId . '/' . $fileName, |
| 123 | + 'fileSize' => $finalFileSize, |
| 124 | + 'absolutePath' => realpath($finalPath), |
| 125 | + 'directoryPath' => realpath($finalDir), |
| 126 | + 'debug' => $debug_log |
| 127 | + ]); |
| 128 | + exit; |
| 129 | + } |
| 130 | + |
| 131 | + http_response_code(200); |
| 132 | + echo json_encode(['success' => true, 'uploadId' => $uploadId, 'chunkNumber' => $chunkNumber, 'debug' => $debug_log]); |
| 133 | + exit; |
| 134 | +} |
| 135 | + |
| 136 | +log_upload_debug('--- New request ---'); |
| 137 | +log_upload_debug('Request method: ' . $_SERVER['REQUEST_METHOD']); |
| 138 | + |
| 139 | +if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { |
| 140 | + http_response_code(200); |
| 141 | + echo json_encode(['success' => true, 'debug' => $debug_log]); |
| 142 | + exit; |
| 143 | +} |
| 144 | + |
| 145 | +if ($_SERVER['REQUEST_METHOD'] !== 'POST') { |
| 146 | + log_upload_debug('Not a POST request.'); |
| 147 | + http_response_code(405); |
| 148 | + echo json_encode(['success' => false, 'error' => 'Method not allowed', 'debug' => $debug_log]); |
| 149 | + exit; |
| 150 | +} |
| 151 | + |
| 152 | +log_upload_debug('POST data: ' . json_encode($_POST)); |
| 153 | +log_upload_debug('FILES: ' . json_encode($_FILES)); |
| 154 | + |
| 155 | +log_upload_debug('Checking for title and audio...'); |
| 156 | +$title = isset($_POST['title']) ? $_POST['title'] : ''; |
| 157 | +if (!$title || !isset($_FILES['audio'])) { |
| 158 | + log_upload_debug('Missing title or audio file.'); |
| 159 | + http_response_code(400); |
| 160 | + echo json_encode(['success' => false, 'error' => 'Title and audio file required', 'debug' => $debug_log]); |
| 161 | + exit; |
| 162 | +} |
| 163 | +log_upload_debug('Title and audio present.'); |
| 164 | + |
| 165 | + |
| 166 | +log_upload_debug('Sanitizing folder name...'); |
| 167 | +$folder = preg_replace('/[^a-zA-Z0-9-_]/', '_', $title); |
| 168 | +$baseDir = __DIR__ . '/../public/assets/recordings/'; |
| 169 | +$targetDir = $baseDir . $folder . '/'; |
| 170 | +log_upload_debug('Target directory: ' . $targetDir); |
| 171 | +if (!file_exists($targetDir)) { |
| 172 | + $mk = mkdir($targetDir, 0755, true); |
| 173 | + log_upload_debug("mkdir $targetDir result: " . ($mk ? 'success' : 'fail')); |
| 174 | +} else { |
| 175 | + log_upload_debug('Target directory already exists.'); |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +log_upload_debug('Checking file extension...'); |
| 180 | +$file = $_FILES['audio']; |
| 181 | +$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); |
| 182 | +log_upload_debug('File extension: ' . $ext); |
| 183 | +$allowed = ['mp3', 'wav', 'm4a', 'mp4']; |
| 184 | +if (!in_array($ext, $allowed)) { |
| 185 | + log_upload_debug('Invalid file type: ' . $ext); |
| 186 | + http_response_code(400); |
| 187 | + echo json_encode(['success' => false, 'error' => 'Invalid file type', 'debug' => $debug_log]); |
| 188 | + exit; |
| 189 | +} |
| 190 | +log_upload_debug('File type allowed.'); |
| 191 | + |
| 192 | +$targetPath = $targetDir . basename($file['name']); |
| 193 | +log_upload_debug('Target path: ' . $targetPath); |
| 194 | +log_upload_debug('Checking if tmp_name exists: ' . (file_exists($file['tmp_name']) ? 'yes' : 'no')); |
| 195 | +if (move_uploaded_file($file['tmp_name'], $targetPath)) { |
| 196 | + log_upload_debug('move_uploaded_file success'); |
| 197 | + echo json_encode([ |
| 198 | + 'success' => true, |
| 199 | + 'path' => '/assets/recordings/' . $folder . '/' . basename($file['name']), |
| 200 | + 'debug' => $debug_log |
| 201 | + ]); |
| 202 | +} else { |
| 203 | + $err = error_get_last(); |
| 204 | + log_upload_debug('move_uploaded_file failed. Error: ' . json_encode($err)); |
| 205 | + log_upload_debug('$_FILES entry: ' . json_encode($file)); |
| 206 | + log_upload_debug('Permissions on target dir: ' . substr(sprintf('%o', fileperms($targetDir)), -4)); |
| 207 | + log_upload_debug('Current user: ' . get_current_user()); |
| 208 | + http_response_code(500); |
| 209 | + echo json_encode([ |
| 210 | + 'success' => false, |
| 211 | + 'error' => 'Failed to save file', |
| 212 | + 'debug' => $debug_log |
| 213 | + ]); |
| 214 | +} |
0 commit comments