attachmentModel = new AttachmentModel(); } /** * Загрузить вложение */ public function upload(string $entityType, int $entityId, array $file, int $userId): ?int { $uploadPath = WRITEPATH . 'uploads/' . $entityType . '/' . $entityId . '/'; // Создаём директорию если не существует if (!is_dir($uploadPath)) { mkdir($uploadPath, 0755, true); } // Генерируем уникальное имя файла $originalName = $file['name']; $extension = pathinfo($originalName, PATHINFO_EXTENSION); $newFileName = uniqid() . '_' . time() . '.' . $extension; $filePath = $uploadPath . $newFileName; // Перемещаем загруженный файл if (!move_uploaded_file($file['tmp_name'], $filePath)) { return null; } $attachmentId = $this->attachmentModel->insert([ 'entity_type' => $entityType, 'entity_id' => $entityId, 'file_name' => $originalName, 'file_path' => $filePath, 'file_size' => $file['size'], 'file_type' => $file['type'] ?? '', 'uploaded_by' => $userId, ]); if ($attachmentId) { Events::trigger('attachment.uploaded', [ 'entity_type' => $entityType, 'entity_id' => $entityId, 'attachment_id' => $attachmentId, 'user_id' => $userId, ]); } return $attachmentId; } /** * Удалить вложение */ public function delete(string $entityType, int $entityId, int $attachmentId, int $userId): bool { if (!$this->attachmentModel->belongsToEntity($entityType, $entityId, $attachmentId)) { return false; } $result = $this->attachmentModel->deleteAttachment($attachmentId); if ($result) { Events::trigger('attachment.deleted', [ 'entity_type' => $entityType, 'entity_id' => $entityId, 'attachment_id' => $attachmentId, 'user_id' => $userId, ]); } return $result; } /** * Получить вложения сущности */ public function getByEntity(string $entityType, int $entityId): array { return $this->attachmentModel->getByEntity($entityType, $entityId); } /** * Получить количество вложений */ public function count(string $entityType, int $entityId): int { return $this->attachmentModel->countByEntity($entityType, $entityId); } /** * Получить иконку файла */ public function getFileIcon(string $fileType): string { return $this->attachmentModel->getFileIcon($fileType); } /** * Форматировать размер файла */ public function formatSize(int $bytes): string { return $this->attachmentModel->formatSize($bytes); } }