activityMgr = $manager; } #[Route(path: '/import', name: 'import', methods: ['GET'])] public function import(): Response { return $this->render('./page/import.html.twig', [ 'css' => $this->preference->getCookie(), 'pp' => "test2", 'user' => "Doe", 'role' => "Athlète", 'friendship' => [], 'analyzes' => [], 'mails' => [], 'users' => [], 'infoUser' => [], 'exos' => [], 'member' => [] ]); } #[Route(path: '/upload', name: 'upload', methods: ['POST'])] public function uploadFile(string $activityType, int $effort, IRequest $req): IResponse { $error = $this->validateRequest($effort); if (!empty($error)) { return $this->renderError($error); } $tmp_file = $_FILES['uploaded_file']['tmp_name']; if (!$this->isValidFile($tmp_file)) { return $this->renderError(['Failed to get file be sure that you provide the file']); } $content = file_get_contents($tmp_file); try { if ($this->activityMgr->uploadFile($activityType, 5, $content)) { return new RedirectResponse('/home'); } } catch (\Exception $e) { return $this->renderError([$e->getMessage()]); } return $this->renderError(['Failed to save activity.']); } private function validateRequest(int $effort): array { $error = []; if ($effort < 0 || $effort > 5) { $error[] = 'Invalid effort level.'; } $fileExtension = pathinfo($_FILES['uploaded_file']['name'], PATHINFO_EXTENSION); if ($fileExtension !== 'fit') { $error[] = 'Invalid file type. Only .fit files are allowed.'; } return $error; } private function isValidFile(string $tmp_file): bool { return file_exists($tmp_file) && is_uploaded_file($tmp_file); } private function renderError(array $error): Response { // Consolidez la logique de rendu ici return $this->render('./error/error.html.twig', ['title'=> "Failed" , "code" => 400, "name" => "error import", "descr" => $error[0] ], new Response('$error', 400)); } }