|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MediaBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Alpixel\Bundle\MediaBundle\Entity\Media; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
|
|
13
|
|
|
class MediaController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @Route("/media/upload/wysiwyg", name="upload_wysiwyg") |
|
18
|
|
|
* |
|
19
|
|
|
* @Method({"POST"}) |
|
20
|
|
|
*/ |
|
21
|
|
|
public function uploadFilesWysiwygAction() |
|
22
|
|
|
{ |
|
23
|
|
|
foreach ($this->get('request')->files as $file) { |
|
24
|
|
|
$media = $this->get('media')->upload($file, $this->get('request')->get('folder'), null); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$file_uploaded = $this->get('media')->getSecretPath($media); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
return $this->render('AlpixelMediaBundle:admin:blocks/upload_wysiwyg.html.twig', array( |
|
30
|
|
|
'file_uploaded' => $file_uploaded, |
|
31
|
|
|
)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @Route("/media/upload", name="upload") |
|
36
|
|
|
* |
|
37
|
|
|
* @Method({"POST"}) |
|
38
|
|
|
*/ |
|
39
|
|
|
public function uploadAction() |
|
40
|
|
|
{ |
|
41
|
|
|
$returnData = []; |
|
42
|
|
|
|
|
43
|
|
|
if ($this->get('request')->get('lifetime') !== null) { |
|
44
|
|
|
$lifetime = new \DateTime($this->get('request')->get('lifetime')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (empty($lifetime) || $lifetime == new \DateTime('now')) { |
|
48
|
|
|
$lifetime = new \DateTime('+6 hours'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$mediaPreview = $this->container->get('twig.extension.media_preview_extension'); |
|
52
|
|
|
foreach ($this->get('request')->files as $files) { |
|
53
|
|
|
if (!is_array($files)) { |
|
54
|
|
|
$files = [$files]; |
|
55
|
|
|
} |
|
56
|
|
|
foreach ($files as $file) { |
|
57
|
|
|
$media = $this->get('alpixel_media.manager')->upload($file, $this->get('request')->get('folder'), $lifetime); |
|
58
|
|
|
$path = $mediaPreview->generatePathFromSecretKey($media->getSecretKey()); |
|
59
|
|
|
$returnData[] = [ |
|
60
|
|
|
'id' => $media->getSecretKey(), |
|
61
|
|
|
'path' => $path, |
|
62
|
|
|
'name' => $media->getName(), |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return new JsonResponse($returnData); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @Route("/media/download/{id}-{name}", name="media_download_public") |
|
72
|
|
|
* @Route("/media/download/{filter}/{id}-{name}", name="media_download_public_filters") |
|
73
|
|
|
* @Route("/media/download/{secretKey}/{filter}", name="media_download_private") |
|
74
|
|
|
* |
|
75
|
|
|
* @Method({"GET"}) |
|
76
|
|
|
*/ |
|
77
|
|
|
public function downloadMediaAction(Media $media) |
|
78
|
|
|
{ |
|
79
|
|
|
$response = new Response(); |
|
80
|
|
|
$response->setContent(file_get_contents($this->get('alpixel_media.manager')->getAbsolutePath($media))); |
|
81
|
|
|
$response->headers->set('Content-Type', 'application/force-download'); |
|
82
|
|
|
$response->headers->set('Content-disposition', 'filename='.$media->getName()); |
|
83
|
|
|
|
|
84
|
|
|
return $response; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @Route("/media/{id}-{name}", name="media_show_public") |
|
89
|
|
|
* @Route("/media/{filter}/{id}-{name}", name="media_show_public_filters") |
|
90
|
|
|
* @Route("/media/{secretKey}/{filter}", name="media_show_private") |
|
91
|
|
|
* |
|
92
|
|
|
* @Method({"GET"}) |
|
93
|
|
|
*/ |
|
94
|
|
|
public function showMediaAction(Media $media, $filter = null) |
|
95
|
|
|
{ |
|
96
|
|
|
$response = new Response(); |
|
97
|
|
|
$lastModified = new \DateTime('now'); |
|
98
|
|
|
|
|
99
|
|
|
//Checking if it is an image or not |
|
100
|
|
|
$src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
|
101
|
|
|
$isImage = @getimagesize($src); |
|
102
|
|
|
|
|
103
|
|
|
if ($isImage) { |
|
104
|
|
|
$response->headers->set('Content-disposition', 'inline;filename='.$media->getName()); |
|
105
|
|
|
if (!empty($filter) && $isImage) { |
|
106
|
|
|
$src = $this->get('alpixel_media.manager')->getAbsolutePath($media, $filter); |
|
107
|
|
|
$dataManager = $this->get('liip_imagine.data.manager'); // the data manager service |
|
108
|
|
|
$filterManager = $this->get('liip_imagine.filter.manager'); // the filter manager service |
|
109
|
|
|
$uploadDir = $this->get('alpixel_media.manager')->getUploadDir($filter); |
|
110
|
|
|
|
|
111
|
|
|
if (!is_file($src)) { |
|
112
|
|
|
$fs = new Filesystem(); |
|
113
|
|
|
if (!$fs->exists($uploadDir.$media->getFolder())) { |
|
114
|
|
|
$fs->mkdir($uploadDir.$media->getFolder()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$path = 'upload/'.$media->getUri(); |
|
118
|
|
|
|
|
119
|
|
|
// find the image and determine its type |
|
120
|
|
|
$image = $dataManager->find($filter, $path); |
|
121
|
|
|
|
|
122
|
|
|
// run the filter |
|
123
|
|
|
$responseData = $filterManager->applyFilter($image, $filter); |
|
124
|
|
|
$data = $responseData->getContent(); |
|
125
|
|
|
file_put_contents($uploadDir.$media->getUri(), $data); |
|
126
|
|
|
} else { |
|
127
|
|
|
$data = file_get_contents($src); |
|
128
|
|
|
$lastModified->setTimestamp(filemtime($src)); |
|
129
|
|
|
} |
|
130
|
|
|
} else { |
|
131
|
|
|
$src = $this->get('alpixel_media.manager')->getAbsolutePath($media); |
|
132
|
|
|
$lastModified->setTimestamp(filemtime($src)); |
|
133
|
|
|
$data = file_get_contents($src); |
|
134
|
|
|
} |
|
135
|
|
|
} else { |
|
136
|
|
|
$lastModified->setTimestamp(filemtime($src)); |
|
137
|
|
|
$data = file_get_contents($src); |
|
138
|
|
|
$response->headers->set('Content-disposition', 'attachment;filename='.basename($media->getUri())); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$response->setLastModified($lastModified); |
|
142
|
|
|
$response->setPublic(); |
|
143
|
|
|
$response->headers->set('Content-Type', $media->getMime()); |
|
144
|
|
|
|
|
145
|
|
|
if ($response->isNotModified($this->get('request'))) { |
|
146
|
|
|
return $response; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$response->setContent($data); |
|
150
|
|
|
|
|
151
|
|
|
return $response; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: