<?php
namespace Moka\Cms\Controller;
use Moka\Cms\Entity\MediaInterface;
use Moka\Cms\Model\Model;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Vich\UploaderBundle\Handler\DownloadHandler;
class MediaController extends AbstractController
{
public function __construct(protected Model $model)
{
}
public function download(string $slug, DownloadHandler $downloadHandler)
{
[$slug] = explode('.', $slug, 2);
/** @var ?MediaInterface $media */
$media = $this->getDoctrine()
->getRepository($this->model->getMediaMetadata()->getClassName())
->findOneBy(['slug' => $slug]);
if (!$media) {
throw $this->createNotFoundException();
}
return $downloadHandler->downloadObject($media, field: 'file', fileName: $media->getFullSlug());
}
}