FileDownloadDownloadController.php in File Download 8
File
src/Controller/FileDownloadDownloadController.php
View source
<?php
namespace Drupal\file_download\Controller;
use Drupal\system\FileDownloadController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\file\Entity\File;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class FileDownloadDownloadController extends FileDownloadController {
public function deliver(Request $request, $scheme, $fid) {
$file = File::load($fid);
if (!is_object($file)) {
throw new NotFoundHttpException();
}
$uri = $file
->getFileUri();
$filename = $file
->getFilename();
if (!file_exists($uri)) {
throw new NotFoundHttpException();
}
$headers = $this
->moduleHandler()
->invokeAll('file_download', [
$uri,
]);
foreach ($headers as $result) {
if ($result == -1) {
throw new AccessDeniedHttpException();
}
}
$mimetype = Unicode::mimeHeaderEncode($file
->getMimeType());
$headers = [
'Content-Type' => $mimetype,
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
'Content-Length' => $file
->getSize(),
'Content-Transfer-Encoding' => 'binary',
'Pragma' => 'no-cache',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Expires' => '0',
'Accept-Ranges' => 'bytes',
];
if (\Drupal::moduleHandler()
->moduleExists('file_download_counter')) {
$count_downloads = \Drupal::config('file_download_counter.settings')
->get('count_downloads');
if ($count_downloads) {
file_download_counter_increment_file($fid);
}
}
return new BinaryFileResponse($uri, 200, $headers, $scheme !== 'private');
}
}