dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.85k stars 519 forks source link

Problem dowload using the DownloadHandler #1231

Closed pminetti closed 3 years ago

pminetti commented 3 years ago
Q A
Bundle version 1.18
Symfony version 5.3.9
PHP version 7.3.31

Support Question

Hi,

I need to download same certificates, so in development I have a link in my apache directory pointing to the certificate store. all is working fine when I contruct a link to the certificate. Problem start when I want to use the dowloadHandler. In the AbstractStorage, on function resolvePath $filename is always null, so it return a null and broke the code.

Entity

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity()
 * @Vich\Uploadable
 */
class ServiceDocFile
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @Vich\UploadableField(mapping="docs", fileNameProperty="docName", size="docSize")
     */
    private $docFile;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $docName;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     */
    private $docSize;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updateAt;

    /**
     * @ORM\ManyToOne(targetEntity="Service", inversedBy="serviceDocFiles")
     */
    private $service;

    public function setDocFile(?File $docFile = null): void
    {
        $this->docFile = $docFile;
        if (null !== $docFile) {
            $this->updateAt = new \DateTimeImmutable();
        }
    }

    public function getDocFile(): ?File
    {
        return $this->docFile;
    }

    public function setDocName(?string $docName): self
    {
        $this->docName = $docName;

        return $this;
    }

    public function getDocName(): ?string
    {
        return $this->docName;
    }

    public function setDocSize(?string $docSize): self
    {
        $this->docSize = $docSize;

        return $this;
    }

    public function getDocSize(): ?string
    {
        return $this->docSize;
    }

    public function setService(?Service $service): self
    {
        $this->service = $service;

        return $this;
    }

    public function getService(): ?Service
    {
        return $this->service;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUpdateAt(): ?\DateTimeInterface
    {
        return $this->updateAt;
    }

    public function setUpdateAt(?\DateTimeInterface $updateAt): self
    {
        $this->updateAt = $updateAt;

        return $this;
    }
}

The controller (yet no finished)

<?php
namespace App\Controller\CertificateDownload;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use Vich\UploaderBundle\Handler\DownloadHandler;
use App\Entity\ServiceDocFile;

/**
 * @Route("/certificate_download")
 */
class CertificateDownloadController extends AbstractController
{
    /**
     * @Route("/{reportName}")
     */
    public function index(DownloadHandler $handler, $reportName): Response
    {
        $rightReportName = substr($reportName, -3, 3);
        // Check if reportName is RM, or AL or AF or Generic name
        if ($rightReportName == '_RM' || $rightReportName == '_AL' || $rightReportName == '_AF') {
            $report = $this->getDoctrine()->getRepository(ServiceDocFile::class)->findByDocName($reportName . '.pdf');
            return $handler->downloadObject($report, 'docFile', ServiceDocFile::class);
        } 
    }
}
garak commented 3 years ago

I would start by checking what's inside $report object before returning.

pminetti commented 3 years ago

Hi garak,

thanks for your time.

Yes the problem was I use findByDocName and I have to use findOneByDocName, this return the objet not an array of objects

thank again for your time