true, 'csrf_token' => $_SESSION['csrf_token'] ]); exit; } // // 2) Auth checks // if (empty($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) { http_response_code(401); echo json_encode(["error" => "Unauthorized"]); exit; } $userPerms = loadUserPermissions($_SESSION['username']); if (!empty($userPerms['disableUpload'])) { http_response_code(403); echo json_encode(["error" => "Upload disabled for this user."]); exit; } // // 3) Delegate the actual file handling // $result = UploadModel::handleUpload($_POST, $_FILES); // // 4) Respond // if (isset($result['error'])) { http_response_code(400); echo json_encode($result); exit; } if (isset($result['status'])) { echo json_encode($result); exit; } // full‐upload redirect $_SESSION['upload_message'] = "File uploaded successfully."; exit; } /** * @OA\Post( * path="/api/upload/removeChunks.php", * summary="Remove chunked upload temporary directory", * description="Removes the temporary directory used for chunked uploads, given a folder name matching the expected resumable pattern.", * operationId="removeChunks", * tags={"Uploads"}, * @OA\RequestBody( * required=true, * @OA\JsonContent( * required={"folder"}, * @OA\Property(property="folder", type="string", example="resumable_myupload123") * ) * ), * @OA\Response( * response=200, * description="Temporary folder removed successfully", * @OA\JsonContent( * @OA\Property(property="success", type="boolean", example=true), * @OA\Property(property="message", type="string", example="Temporary folder removed.") * ) * ), * @OA\Response( * response=400, * description="Invalid input (e.g., missing folder or invalid folder name)" * ), * @OA\Response( * response=403, * description="Invalid CSRF token" * ) * ) * * Removes the temporary upload folder for chunked uploads. * * @return void Outputs a JSON response. */ public function removeChunks(): void { header('Content-Type: application/json'); // CSRF Protection: Validate token from POST data. $receivedToken = isset($_POST['csrf_token']) ? trim($_POST['csrf_token']) : ''; if ($receivedToken !== $_SESSION['csrf_token']) { http_response_code(403); echo json_encode(["error" => "Invalid CSRF token"]); exit; } // Check that the folder parameter is provided. if (!isset($_POST['folder'])) { http_response_code(400); echo json_encode(["error" => "No folder specified"]); exit; } $folder = $_POST['folder']; $result = UploadModel::removeChunks($folder); echo json_encode($result); exit; } }