diff options
Diffstat (limited to 'src/controller/HeadFootController.php')
| -rw-r--r-- | src/controller/HeadFootController.php | 74 |
1 files changed, 54 insertions, 20 deletions
diff --git a/src/controller/HeadFootController.php b/src/controller/HeadFootController.php index 0429aac..cf3aed0 100644 --- a/src/controller/HeadFootController.php +++ b/src/controller/HeadFootController.php | |||
| @@ -3,8 +3,7 @@ | |||
| 3 | 3 | ||
| 4 | declare(strict_types=1); | 4 | declare(strict_types=1); |
| 5 | 5 | ||
| 6 | //use App\Entity\Node; | 6 | use App\Entity\NodeDataAsset; |
| 7 | //use App\Entity\NodeData; | ||
| 8 | use App\Entity\Asset; | 7 | use App\Entity\Asset; |
| 9 | use Doctrine\Common\Collections\ArrayCollection; | 8 | use Doctrine\Common\Collections\ArrayCollection; |
| 10 | use Doctrine\ORM\EntityManager; | 9 | use Doctrine\ORM\EntityManager; |
| @@ -38,41 +37,76 @@ class HeadFootController | |||
| 38 | echo json_encode(['success' => false]); | 37 | echo json_encode(['success' => false]); |
| 39 | } | 38 | } |
| 40 | else{ | 39 | else{ |
| 41 | $file = $_FILES['file']; | ||
| 42 | |||
| 43 | if(!is_dir(Asset::USER_PATH)){ | 40 | if(!is_dir(Asset::USER_PATH)){ |
| 44 | mkdir(Asset::USER_PATH, 0700, true); | 41 | mkdir(Asset::USER_PATH, 0700, true); |
| 45 | } | 42 | } |
| 46 | 43 | ||
| 44 | /* -- téléchargement -- */ | ||
| 45 | $file = $_FILES['file']; | ||
| 47 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif', 'ico', 'bmp']; // pas de SVG | 46 | $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'tiff', 'tif', 'ico', 'bmp']; // pas de SVG |
| 48 | $name = Security::secureFileName(pathinfo($file['name'], PATHINFO_FILENAME)); | ||
| 49 | $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); | 47 | $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); |
| 50 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ | 48 | if(!in_array($extension, $allowed_extensions) || $extension === 'jpg'){ |
| 51 | $extension = 'jpeg'; | 49 | $extension = 'jpeg'; |
| 52 | } | 50 | } |
| 53 | $file_path = uniqid($name . '_') . '.' . $extension; | 51 | $mime_type = mime_content_type($file['tmp_name']); |
| 52 | $hash = hash_file('sha256', $file['tmp_name']); | ||
| 54 | 53 | ||
| 55 | if(ImageUploadController::imagickCleanImage(file_get_contents($file['tmp_name']), Asset::USER_PATH . $file_path, $extension)){ // recréer l’image pour la nettoyer | 54 | /* -- instance d'Asset -- */ |
| 56 | $params_array = explode('_', $request_params); // favicon, header_logo, header_background, footer_logo | 55 | $model = new Model($entityManager); |
| 56 | $result = $model->getWhatever('App\Entity\Asset', 'hash', $hash); | ||
| 57 | 57 | ||
| 58 | $model = new Model($entityManager); | 58 | if(count($result) > 0){ // asset existant trouvé |
| 59 | if($model->findWhateverNode('name_node', $params_array[0])){ | 59 | $asset = $result[0]; |
| 60 | $node_data = $model->getNode()->getNodeData(); | ||
| 61 | $image = new Asset($name, $file_path, mime_content_type($file['tmp_name']), $request_params); | ||
| 62 | $node_data->addAsset($image); | ||
| 63 | 60 | ||
| 64 | $entityManager->persist($image); | 61 | // correction des informations |
| 65 | $entityManager->flush(); | 62 | $name = $asset->getFileName(); // permet à priori de réécrire par dessus le précédent fichier |
| 66 | echo json_encode(['success' => true, 'location' => Asset::USER_PATH . $file_path]); | 63 | //$asset->setFileName($name); |
| 67 | } | 64 | $asset->setMimeType($mime_type); |
| 68 | else{ | ||
| 69 | echo json_encode(['success' => false, 'message' => 'Erreur noeud non trouvé.']); | ||
| 70 | } | ||
| 71 | } | 65 | } |
| 72 | else{ | 66 | else{ |
| 67 | $name = Security::secureFileName(pathinfo($file['name'], PATHINFO_FILENAME)); | ||
| 68 | $name = uniqid($name . '_') . '.' . $extension; | ||
| 69 | $asset = new Asset($name, $mime_type, $hash); | ||
| 70 | } | ||
| 71 | |||
| 72 | /* -- écriture du fichier sur le disque -- */ | ||
| 73 | if(!ImageUploadController::imagickCleanImage(file_get_contents($file['tmp_name']), Asset::USER_PATH . $name, $extension)){ // recréer l’image pour la nettoyer | ||
| 73 | http_response_code(500); | 74 | http_response_code(500); |
| 74 | echo json_encode(['success' => false, 'message' => 'Erreur image non valide.']); | 75 | echo json_encode(['success' => false, 'message' => 'Erreur image non valide.']); |
| 75 | } | 76 | } |
| 77 | else{ | ||
| 78 | $params_array = explode('_', $request_params); // head_favicon, header_logo, header_background, footer_logo | ||
| 79 | |||
| 80 | /* -- table intermédiaire node_data/asset-- */ | ||
| 81 | if($model->findWhateverNode('name_node', $params_array[0])){ // noeud (head, header ou footer) | ||
| 82 | $node_data = $model->getNode()->getNodeData(); | ||
| 83 | |||
| 84 | // recherche à l'aide du rôle | ||
| 85 | $old_nda = null; | ||
| 86 | foreach($node_data->getNodeDataAssets() as $nda){ | ||
| 87 | if($nda->getRole() === $request_params){ | ||
| 88 | $old_nda = $nda; | ||
| 89 | $old_nda->setAsset($asset); | ||
| 90 | break; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | // entrée pas trouvée | ||
| 94 | if($old_nda === null){ | ||
| 95 | $new_nda = new NodeDataAsset($node_data, $asset, $request_params); // $request_params sera le rôle de l'asset | ||
| 96 | $entityManager->persist($new_nda); | ||
| 97 | } | ||
| 98 | |||
| 99 | if(count($result) === 0){ | ||
| 100 | $entityManager->persist($asset); | ||
| 101 | } | ||
| 102 | $entityManager->flush(); | ||
| 103 | echo json_encode(['success' => true, 'location' => Asset::USER_PATH . $name, 'mime_type' => $mime_type]); | ||
| 104 | } | ||
| 105 | else{ | ||
| 106 | http_response_code(500); | ||
| 107 | echo json_encode(['success' => false, 'message' => "Erreur noeud non trouvé, c'est pas du tout normal!"]); | ||
| 108 | } | ||
| 109 | } | ||
| 76 | } | 110 | } |
| 77 | die; | 111 | die; |
| 78 | } | 112 | } |
