diff options
Diffstat (limited to 'src/model/entities/Asset.php')
| -rw-r--r-- | src/model/entities/Asset.php | 63 |
1 files changed, 24 insertions, 39 deletions
diff --git a/src/model/entities/Asset.php b/src/model/entities/Asset.php index e359e21..5400db4 100644 --- a/src/model/entities/Asset.php +++ b/src/model/entities/Asset.php | |||
| @@ -6,6 +6,7 @@ declare(strict_types=1); | |||
| 6 | namespace App\Entity; | 6 | namespace App\Entity; |
| 7 | 7 | ||
| 8 | use Doctrine\ORM\Mapping as ORM; | 8 | use Doctrine\ORM\Mapping as ORM; |
| 9 | use Doctrine\Common\Collections\Collection; | ||
| 9 | 10 | ||
| 10 | #[ORM\Entity] | 11 | #[ORM\Entity] |
| 11 | #[ORM\Table(name: TABLE_PREFIX . "asset")] | 12 | #[ORM\Table(name: TABLE_PREFIX . "asset")] |
| @@ -13,71 +14,55 @@ class Asset | |||
| 13 | { | 14 | { |
| 14 | const PATH = 'assets/'; | 15 | const PATH = 'assets/'; |
| 15 | const USER_PATH = 'user_data/assets/'; | 16 | const USER_PATH = 'user_data/assets/'; |
| 17 | // choisir un répertoire du genre /var/www/html/uploads/? ou au moins hors de /src? j'en sais rien | ||
| 16 | 18 | ||
| 17 | #[ORM\Id] | 19 | #[ORM\Id] |
| 18 | #[ORM\GeneratedValue] | 20 | #[ORM\GeneratedValue] |
| 19 | #[ORM\Column(type: "integer")] | 21 | #[ORM\Column(type: "integer")] |
| 20 | private int $id_asset; | 22 | private int $id_asset; |
| 21 | 23 | ||
| 22 | #[ORM\Column(type: "string", length: 255, unique: true)] // nom d'image UNIQUE | 24 | #[ORM\Column(type: "string", length: 255)] // nom de fichier modifié avec uniqid (fichiers différents de même nom) |
| 23 | private string $file_name; | 25 | private string $file_name; |
| 24 | 26 | ||
| 25 | // choisir un répertoire du genre /var/www/html/uploads/, au moins hors de /src | ||
| 26 | #[ORM\Column(type: "string", length: 255, unique: true, nullable: true)] | ||
| 27 | private ?string $file_path; | ||
| 28 | |||
| 29 | #[ORM\Column(type: "string", length: 255, nullable: true)] | 27 | #[ORM\Column(type: "string", length: 255, nullable: true)] |
| 30 | private string $mime_type; // image/jpeg, image/png, etc | 28 | private string $mime_type; // image/jpeg, image/png, etc |
| 31 | 29 | ||
| 32 | #[ORM\Column(type: "string", length: 255, nullable: true)] | 30 | #[ORM\Column(type: "string", length: 64, unique: true)] // doctrine n'a pas d'équivalent au type CHAR des BDD (on voulait CHAR(64)), c'est pas grave! |
| 33 | private string $alt; // texte alternatif | 31 | private string $hash; // pour détecter deux fichiers identiques, même si leurs noms et les métadonnées changent |
| 34 | |||
| 35 | // autre champs optionnels: file_size, date (default current timestamp) | ||
| 36 | |||
| 37 | /* étapes au téléchargement: | ||
| 38 | => Validation du type de fichier : On vérifie que le fichier est bien une image en utilisant le type MIME. On peut aussi vérifier la taille du fichier. | ||
| 39 | => Création d'un répertoire structuré : On génère un chemin dynamique basé sur la date (uploads/2024/12/24/) pour organiser les images. | ||
| 40 | => Génération d'un nom de fichier unique : On utilise uniqid() pour générer un nom unique et éviter les conflits de nom. | ||
| 41 | => Déplacement du fichier sur le serveur : Le fichier est déplacé depuis son emplacement temporaire vers le répertoire uploads/. | ||
| 42 | => Enregistrement dans la base de données : On enregistre les informations de l'image dans la base de données. */ | ||
| 43 | 32 | ||
| 44 | #[ORM\ManyToMany(targetEntity: NodeData::class, mappedBy: "assets")] | 33 | #[ORM\OneToMany(mappedBy: 'asset', targetEntity: NodeDataAsset::class)] |
| 45 | private $node_data; | 34 | private Collection $nda_collection; |
| 46 | 35 | ||
| 47 | public function __construct(string $name, ?string $path, string $mime_type, string $alt) | 36 | public function __construct(string $name, string $mime_type, string $hash) |
| 48 | { | 37 | { |
| 49 | $this->file_name = $name; | 38 | $this->file_name = $name; |
| 50 | $this->file_path = $path; | ||
| 51 | $this->mime_type = $mime_type; | 39 | $this->mime_type = $mime_type; |
| 52 | $this->alt = $alt; | 40 | $this->hash = $hash; |
| 53 | } | 41 | } |
| 54 | 42 | ||
| 55 | public function getFileName(): string | 43 | public function getFileName(): string |
| 56 | { | 44 | { |
| 57 | return $this->file_name; | 45 | return $this->file_name; |
| 58 | } | 46 | } |
| 59 | public function getFilePath(): string | 47 | public function setFileName(string $name): void |
| 60 | { | 48 | { |
| 61 | return $this->file_path; | 49 | $this->file_name = $name; |
| 62 | } | 50 | } |
| 63 | public function getAlt(): string | 51 | public function getMimeType(): string |
| 64 | { | 52 | { |
| 65 | return $this->alt; | 53 | return $this->mime_type; |
| 66 | } | 54 | } |
| 67 | 55 | public function setMimeType(string $mime_type): void | |
| 68 | |||
| 69 | // pour ViewBuilderController? | ||
| 70 | /*public function displayImage($imageId): void | ||
| 71 | { | 56 | { |
| 72 | //$imageId = 1; // Exemple d'ID d'image | 57 | $this->mime_type = $mime_type; |
| 73 | $stmt = $pdo->prepare("SELECT file_path FROM images WHERE id = ?"); | 58 | } |
| 74 | $stmt->execute([$imageId]); | 59 | public function getHash(): string |
| 75 | $image = $stmt->fetch(); | 60 | { |
| 61 | return $this->hash; | ||
| 62 | } | ||
| 76 | 63 | ||
| 77 | if ($image) { | 64 | public function getNodeDataAssets(): Collection |
| 78 | echo "<img src='" . $image['file_path'] . "' alt='Image'>"; | 65 | { |
| 79 | } else { | 66 | return $this->nda_collection; |
| 80 | echo "Image non trouvée."; | 67 | } |
| 81 | } | ||
| 82 | }*/ | ||
| 83 | } | 68 | } |
