Linux SRV-16 6.18.44-paas #1 SMP PREEMPT_DYNAMIC Thu Aug 13 10:08:12 UTC 2026 x86_64
/
srv
/
data
/
web
/
vhosts
/
www.humitest.fr
/
htdocs
/
vendor
/
vich
/
uploader-bundle
/
Handler
/
/srv/data/web/vhosts/www.humitest.fr/htdocs/vendor/vich/uploader-bundle/Handler/DownloadHandler.php
<?php namespace Vich\UploaderBundle\Handler; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\HttpFoundation\StreamedResponse; use Vich\UploaderBundle\Exception\NoFileFoundException; use Vich\UploaderBundle\Util\Transliterator; /** * @author Kévin Gomez <contact@kevingomez.fr> */ class DownloadHandler extends AbstractHandler { /** * Create a response object that will trigger the download of a file. * * @param object|array $object * @param string $field * @param string $className * @param string|true $fileName True to return original file name * @param bool $forceDownload * * @return StreamedResponse * * @throws \Vich\UploaderBundle\Exception\MappingNotFoundException * @throws NoFileFoundException * @throws \InvalidArgumentException */ public function downloadObject($object, string $field, ?string $className = null, $fileName = null, bool $forceDownload = true): StreamedResponse { $mapping = $this->getMapping($object, $field, $className); $stream = $this->storage->resolveStream($object, $field, $className); if (null === $stream) { throw new NoFileFoundException(sprintf('No file found in field "%s".', $field)); } if (true === $fileName) { $fileName = $mapping->readProperty($object, 'originalName'); } $file = $mapping->getFile($object); return $this->createDownloadResponse( $stream, $fileName ?: $mapping->getFileName($object), null === $file ? null : $file->getMimeType(), $forceDownload ); } /** * @param resource $stream * @param string $filename * @param string|null $mimeType * @param bool $forceDownload * * @return StreamedResponse * * @throws \InvalidArgumentException */ private function createDownloadResponse($stream, string $filename, ?string $mimeType = 'application/octet-stream', bool $forceDownload = true): StreamedResponse { $response = new StreamedResponse(function () use ($stream): void { stream_copy_to_stream($stream, fopen('php://output', 'wb')); }); $disposition = $response->headers->makeDisposition( $forceDownload ? ResponseHeaderBag::DISPOSITION_ATTACHMENT : ResponseHeaderBag::DISPOSITION_INLINE, Transliterator::transliterate($filename) ); $response->headers->set('Content-Disposition', $disposition); $response->headers->set('Content-Type', $mimeType ?: 'application/octet-stream'); return $response; } }