|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the BenGorFile package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace BenGorFile\FileBundle\Twig; |
|
14
|
|
|
|
|
15
|
|
|
use BenGorFile\File\Application\Query\FileOfIdQuery; |
|
16
|
|
|
use BenGorFile\File\Domain\Model\FileDoesNotExistException; |
|
17
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* File download by id Twig function. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Beñat Espiña <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class DownloadExtension extends \Twig_Extension |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The URL generator. |
|
28
|
|
|
* |
|
29
|
|
|
* @var UrlGeneratorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $urlGenerator; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The file handlers. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $handlers; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param UrlGeneratorInterface $anUrlGenerator The URL generator |
|
44
|
|
|
* @param array $handlers The file handlers |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(UrlGeneratorInterface $anUrlGenerator, array $handlers) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->urlGenerator = $anUrlGenerator; |
|
49
|
|
|
$this->handlers = $handlers; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getFunctions() |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
new \Twig_SimpleFunction($this->getName(), [$this, 'download'], ['is_safe' => ['html']]), |
|
59
|
|
|
]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Generates the url that returns the file of given file type and file. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $fileClass The file type type |
|
66
|
|
|
* @param string $q The file query |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function download($fileClass, $q) |
|
71
|
|
|
{ |
|
72
|
|
|
try { |
|
73
|
|
|
$file = $this->handlers[$fileClass]->__invoke(new FileOfIdQuery($q)); |
|
74
|
|
|
$filename = $file['file_name']; |
|
75
|
|
|
} catch (FileDoesNotExistException $exception) { |
|
76
|
|
|
$filename = $q; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $this->urlGenerator->generate( |
|
80
|
|
|
'bengor_file_' . $fileClass . '_download', |
|
81
|
|
|
['filename' => $filename], |
|
82
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getName() |
|
90
|
|
|
{ |
|
91
|
|
|
return 'bengor_file_download'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|