View source
<?php
namespace Drupal\file_entity\Controller;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseDialogCommand;
use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Form\FormState;
use Drupal\file\FileInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FileController extends ControllerBase {
protected $fileSystem;
public function __construct(FileSystemInterface $file_system) {
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('file_system'));
}
public function FileAddUpload() {
}
public function FileAddUploadFile() {
}
public function FileUsage($file) {
}
public function download(FileInterface $file) {
if (!$this
->config('file_entity.settings')
->get('allow_insecure_download')) {
if (!isset($_GET['token']) || $_GET['token'] !== $file
->getDownloadToken()) {
return new Response(t('Access to file @url denied', array(
'@url' => $file
->getFileUri(),
)), 403);
}
}
$headers = array(
'Content-Type' => Unicode::mimeHeaderEncode($file
->getMimeType()),
'Content-Disposition' => 'attachment; filename="' . Unicode::mimeHeaderEncode($this->fileSystem
->basename($file
->getFileUri())) . '"',
'Content-Length' => $file
->getSize(),
'Content-Transfer-Encoding' => 'binary',
'Pragma' => 'no-cache',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Expires' => '0',
);
\Drupal::moduleHandler()
->alter('file_download_headers', $headers, $file);
\Drupal::moduleHandler()
->invokeAll('file_transfer', array(
$file
->getFileUri(),
$headers,
));
try {
return new BinaryFileResponse($file
->getFileUri(), 200, $headers);
} catch (FileNotFoundException $e) {
return new Response(t('File @uri not found', array(
'@uri' => $file
->getFileUri(),
)), 404);
}
}
public function inlineEdit(FileInterface $file) {
$form_object = $this
->entityTypeManager()
->getFormObject('file', 'inline_edit');
$form_object
->setEntity($file);
$form_state = (new FormState())
->setFormObject($form_object)
->disableRedirect();
$form = $this
->formBuilder()
->buildForm($form_object, $form_state);
$dialog_selector = '#file-entity-inline-edit-' . $file
->id();
if (!$form_state
->isExecuted()) {
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$title = $this
->t('Edit file @file', [
'@file' => $file
->label(),
]);
$response = AjaxResponse::create()
->addCommand(new OpenDialogCommand($dialog_selector, $title, $form, [
'width' => 800,
]));
return $response;
}
else {
return AjaxResponse::create()
->addCommand(new CloseDialogCommand($dialog_selector));
}
}
}