4
4
5
5
namespace App \Twig ;
6
6
7
+ use App \Model \Filesystem ;
8
+ use Exception ;
7
9
use Twig \Extension \AbstractExtension ;
8
10
use Twig \TwigFunction ;
11
+ use Zend \Expressive \Helper \UrlHelper ;
9
12
10
13
class FileExtension extends AbstractExtension
11
14
{
15
+ /** @var AbstractAdapter */
16
+ private $ adapter ;
17
+
18
+ /** @var Filesystem */
19
+ private $ filesystem ;
20
+
21
+ /** @var array */
22
+ private $ routeParams ;
23
+
24
+ /** @var UrlHelper */
25
+ private $ urlHelper ;
26
+
27
+ public function __construct (UrlHelper $ urlHelper )
28
+ {
29
+ $ this ->urlHelper = $ urlHelper ;
30
+ }
31
+
12
32
public function getFunctions (): array
13
33
{
14
34
return [
15
- new TwigFunction ('file ' , [$ this , 'file ' ], ['is_safe ' => ['html ' ]]),
35
+ new TwigFunction ('file ' , [$ this , 'file ' ], ['is_safe ' => ['html ' ]]),
16
36
];
17
37
}
18
38
19
- public function file ($ path , bool $ preview , bool $ download ): string
39
+ public function file ($ adapter , $ path, array $ routeParams , bool $ preview , bool $ download ): string
20
40
{
41
+ $ this ->filesystem = new Filesystem ($ adapter );
42
+ $ this ->routeParams = $ routeParams ;
43
+
21
44
if (!is_null ($ path )) {
22
45
$ path = trim ($ path );
23
46
}
24
47
25
- if (is_null ($ path ) || strlen ($ path ) === 0 || ! file_exists ($ path )) {
48
+ if (is_null ($ path ) || strlen ($ path ) === 0 || $ this -> filesystem -> has ($ path ) !== true ) {
26
49
$ output = '<td ' ;
27
50
$ output .= ' class="text-nowrap" ' ;
28
51
$ output .= ' colspan="2" ' ;
@@ -32,8 +55,10 @@ public function file($path, bool $preview, bool $download): string
32
55
$ output .= ValueExtension::null ();
33
56
} elseif (strlen ($ path ) === 0 ) {
34
57
$ output .= '' ;
35
- } elseif (! file_exists ($ path )) {
58
+ } elseif ($ this -> filesystem -> has ($ path ) !== true ) {
36
59
$ output .= self ::notexists ($ path );
60
+ } else {
61
+ $ output .= '123 ' ;
37
62
}
38
63
39
64
$ output .= '</td> ' ;
@@ -72,15 +97,27 @@ private function notexists($path) : string
72
97
73
98
private function preview ($ path , bool $ displayFilename ) : string
74
99
{
75
- return '<a href="#" style="text-decoration: none;"> '
100
+ $ mime = $ this ->filesystem ->getMimetype ($ path );
101
+ $ href = $ this ->urlHelper ->generate ('file.view ' , $ this ->routeParams );
102
+
103
+ if ($ mime === 'image/jpeg ' || $ mime === 'image/png ' ) {
104
+ return '<a href=" ' .$ href .'" style="text-decoration: none;"> '
76
105
. '<i class="far fa-fw fa-eye"></i> '
77
106
. ($ displayFilename ? ' ' . basename ($ path ) : '' )
78
107
. '</a> ' ;
108
+ } else {
109
+ return '<a href=" ' .$ href .'" style="text-decoration: none;" target="_blank"> '
110
+ . '<i class="far fa-fw fa-file"></i> '
111
+ . ($ displayFilename ? ' ' . basename ($ path ) : '' )
112
+ . '</a> ' ;
113
+ }
79
114
}
80
115
81
116
private function download ($ path , bool $ displayFilename ) : string
82
117
{
83
- return '<a href="#" style="text-decoration: none;"> '
118
+ $ href = $ this ->urlHelper ->generate ('file.download ' , $ this ->routeParams );
119
+
120
+ return '<a href=" ' .$ href .'" style="text-decoration: none;"> '
84
121
. '<i class="fas fa-fw fa-file-download"></i> '
85
122
. ($ displayFilename ? ' ' . basename ($ path ) : '' )
86
123
. '</a> ' ;
0 commit comments