You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.8 KiB
95 lines
2.8 KiB
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Container;
|
|
use App\Router\Request\IRequest;
|
|
use App\Router\Response\IResponse;
|
|
use App\Router\Response\RedirectResponse;
|
|
use App\Router\Response\Response;
|
|
use Manager\ActivityManager;
|
|
use Shared\Attributes\Route;
|
|
use Twig\Environment;
|
|
use Data\Core\Preferences;
|
|
use Shared\Log;
|
|
|
|
class HeartRateController extends BaseController
|
|
{
|
|
private ActivityManager $activityMgr;
|
|
|
|
public function __construct(ActivityManager $manager)
|
|
{
|
|
parent::__construct();
|
|
$this->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));
|
|
|
|
}
|
|
}
|