ruvents/upload-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Ruwork Upload Bundle

Installs: 464

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 0

Type:symfony-bundle

0.4.0 2017-11-30 08:57 UTC

This package is not auto-updated.

Last update: 2020-08-22 05:49:12 UTC


README

This bundle provides an upload entity implementation.

Installation

composer require ruwork/upload-bundle.

Getting started

  1. Create your upload entity.
    <?php
    
    namespace App\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    use Ruwork\UploadBundle\Download\DownloadInterface;
    use Ruwork\UploadBundle\Entity\AbstractUpload;
    use Symfony\Component\HttpFoundation\File\UploadedFile;
    
    /**
     * @ORM\Entity()
     */
    class Upload extends AbstractUpload implements DownloadInterface
    {
        /**
         * @ORM\Column(type="string", nullable=true)
         *
         * @var null|string
         */
        private $name;
    
        public function __construct(UploadedFile $uploadedFile, string $path)
        {
            parent::__construct($uploadedFile, $path);
            $this->name = $uploadedFile->getClientOriginalName();
        }
    
        /**
         * {@inheritdoc}
         */
        public function getDownloadName(): string
        {
            return $this->name ?: basename($this->getPath());
        }
    }

Basic usage

<?php

namespace App\Controller;

use App\Entity\Upload;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Ruwork\UploadBundle\Form\Type\UploadType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;

class IndexController extends AbstractController
{
    /**
     * @Route("", name="index")
     * @Template()
     */
    public function indexAction(Request $request, EntityManagerInterface $em)
    {
        $user = new User();

        $builder = $this->createFormBuilder($user)
            ->add('upload', UploadType::class, [
                'factory' => function (UploadedFile $file, string $path) {
                    return new Upload($file, $path);
                },
            ])
            ->add('submit', SubmitType::class);

        $form = $builder
            ->getForm()
            ->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em->persist($user);
            $em->flush();

            return $this->redirectToRoute('index');
        }

        return [
            'user' => $user,
            'form' => $form->createView(),
        ];
    }
}

Serving upload entity for downloading

# config/routes.yaml
download:
    prefix: /download
    resource: '@RuworkUploadBundle/Resources/config/download_route.yaml'
    defaults:
        class: App\Entity\Upload
<a href="{{ path('ruwork_upload_download', {path: upload.path}) }}">Download</a>

Default configuration

ruwork_upload:
    public_dir: "%kernel.project_dir%/public"
    uploads_dir_name: uploads