ics/medias-bundle

File Manager for symfony

Installs: 156

Dependents: 2

Suggesters: 0

Security: 0

Stars: 3

Watchers: 3

Forks: 0

Open Issues: 0

Language:JavaScript

Type:symfony-bundle

0.1.3 2023-09-21 19:16 UTC

This package is auto-updated.

Last update: 2024-04-21 20:35:54 UTC


README

Bundle for file management in symfony

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

composer require ics/medias-bundle

Applications that don't use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

composer require ics/medias-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    ICS\MediaBundle\MediaBundle::class => ['all' => true],
];

Step 3: Install Database

For install database :

# Installer la base de données

php bin/console doctrine:schema:create

For update database :

# Mise a jour la base de données

php bin/console doctrine:schema:update -f

Configuration

Routing

#config/routes.yaml

#...

media_bundle:
  resource: '@MediaBundle/config/routes.yaml'
  prefix: /medias

#...

Medias configuration

You can configure relative path from the public directory. The medias files will be stored in this directory

# config/packages/medias.yaml

medias:
    path: 'medias' # Default Value

Usage

The Mediabundle propose two FormType MediaType that return One MediaFile and MediaCollectionType that retun Many MediaFile. You may precise the outputdir relative to medias path in the config.

Entity

use ICS\MediaBundle\Entity\MediaImage;

// Exemple entity

public class User
{
    /**
     * Avatar of user
     *
     * @var MediaImage
     * @ORM\ManyToOne(targetEntity=MediaImage::class, cascade={"persist","remove"})
     */
    private $avatar;

    /**
     * Gallery of user
     *
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity=MediaImage::class, cascade={"persist","remove"})
     */
    private $gallery;

    public function __construct()
    {
        $this->gallery=new ArrayCollection();
    }
}

FormType

    //...
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        // For One file
        $builder->add('avatar',MediaType::class,[
            'outputdir' => 'user/avatar'
        ]);
        // For Many files
        $builder->add('gallery',MediaCollectionType::class,[
            'outputdir' => 'user/gallery'
        ]);
    }
    //...

You can impose limitation of type for all and size for MediaCollectionType

    //...
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        // For One file
        $builder->add('avatar',MediaType::class,[
            'outputdir' => 'user/avatar',
            'required' => false,
            'acceptedFiles' => ['image/jpeg','.jpg'],
        ]);
        // For Many files
        $builder->add('gallery',MediaCollectionType::class,[
            'outputdir' => 'user/gallery'
            'required' => false,
            'acceptedFiles' => MediaImage::$mimes
            'maxFileSize' => '1024' // 1Ko
        ]);
    }
    //...

The maxFileSize is calculated based on system capabilities and your configuration. If your configuration is higher of system capabilities, system capabilities is used.

Commands

Two commands are implemented

Integrity verification

php bin/console media:file:verify

Duplicate search

php bin/console media:search:duplicate

Integration

Adding bundle to EasyAdmin

Step 1: Add entities to dashboard

Add this MenuItems in your dashboard Controller/Admin/DashboardController.php

    // Controller/Admin/DashboardController.php
    use ICS\SsiBundle\Entity\User;
    use ICS\SsiBundle\Entity\Log;

    class DashboardController extends AbstractDashboardController
    {
        public function configureMenuItems(): iterable
        {
            // ...
            yield MenuItem::section('Medias', 'fa fa-photo-video');
            yield MenuItem::linkToCrud('Files', 'fa fa-file', MediaFile::class);
            yield MenuItem::linkToCrud('Pictures', 'fa fa-photo', MediaImage::class);
            // ...
        }
    }

Step 2: Add twig widgets to dashboard

    {% extends "@EasyAdmin/page/content.html.twig" %}

    {% block page_content %}

        {{ mediaGraphData() }}

    {% endblock %}