|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Helper\File; |
|
4
|
|
|
|
|
5
|
|
|
use Kunstmaan\MediaBundle\Entity\Folder; |
|
6
|
|
|
use Kunstmaan\MediaBundle\Entity\Media; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* FileHelper |
|
13
|
|
|
*/ |
|
14
|
|
|
class FileHelper |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Media |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $media; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var File |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $file; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $path; |
|
30
|
|
|
|
|
31
|
|
|
protected $mediaPath; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Media $media |
|
35
|
|
|
* @param string $mediaPath |
|
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
7 |
|
public function __construct(Media $media, string $mediaPath = null) |
|
38
|
|
|
{ |
|
39
|
7 |
|
$this->media = $media; |
|
40
|
|
|
|
|
41
|
7 |
|
if ($mediaPath === null) { |
|
42
|
|
|
@trigger_error(sprintf('Not passing the media path as the second argument of "%s" is deprecated since KunstmaanMediaBundle 5.7 and will be required in KunstmaanMediaBundle 6.0. Injected the required parameter in the constructor instead.', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
|
|
43
|
|
|
$mediaPath = '/uploads/media/'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
7 |
|
$this->mediaPath = $mediaPath; |
|
47
|
7 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function getName() |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->media->getName(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function setName($name) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->media->setName($name); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return Folder |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getFolder() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->media->getFolder(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Folder $folder |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function setFolder(Folder $folder) |
|
77
|
|
|
{ |
|
78
|
1 |
|
$this->media->setFolder($folder); |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function getCopyright() |
|
85
|
|
|
{ |
|
86
|
1 |
|
return $this->media->getCopyright(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $copyright |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function setCopyright($copyright) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$this->media->setCopyright($copyright); |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function getDescription() |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->media->getDescription(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $description |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function setDescription($description) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->media->setDescription($description); |
|
111
|
1 |
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
public function getOriginalFilename() |
|
114
|
|
|
{ |
|
115
|
1 |
|
return $this->media->getOriginalFilename(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $name |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function setOriginalFilename($name) |
|
122
|
|
|
{ |
|
123
|
1 |
|
$this->media->setOriginalFilename($name); |
|
124
|
1 |
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return UploadedFile |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function getFile() |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->file; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param File $file |
|
136
|
|
|
*/ |
|
137
|
1 |
|
public function setFile(File $file) |
|
138
|
|
|
{ |
|
139
|
1 |
|
$this->file = $file; |
|
140
|
1 |
|
if (\strlen($file->getPathname()) > 0) { |
|
141
|
1 |
|
$this->media->setContent($file); |
|
142
|
1 |
|
$this->media->setContentType($file->getMimeType()); |
|
143
|
1 |
|
$this->media->setUrl( |
|
144
|
1 |
|
$this->mediaPath . $this->media->getUuid() . '.' . $this->media->getContent()->getExtension() |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $mediaUrl |
|
151
|
|
|
* |
|
152
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getMediaFromUrl($mediaUrl) |
|
155
|
|
|
{ |
|
156
|
|
|
$path = tempnam(sys_get_temp_dir(), 'kuma_'); |
|
157
|
|
|
$saveFile = fopen($path, 'w'); |
|
158
|
|
|
$this->path = $path; |
|
159
|
|
|
|
|
160
|
|
|
$ch = curl_init($mediaUrl); |
|
161
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
|
162
|
|
|
curl_setopt($ch, CURLOPT_FILE, $saveFile); |
|
163
|
|
|
curl_exec($ch); |
|
164
|
|
|
$effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); |
|
165
|
|
|
curl_close($ch); |
|
166
|
|
|
|
|
167
|
|
|
fclose($saveFile); |
|
168
|
|
|
chmod($path, 0777); |
|
169
|
|
|
|
|
170
|
|
|
$url = parse_url($effectiveUrl); |
|
171
|
|
|
$info = pathinfo($url['path']); |
|
172
|
|
|
$filename = $info['filename'] . '.' . $info['extension']; |
|
173
|
|
|
|
|
174
|
|
|
$upload = new UploadedFile($path, $filename); |
|
175
|
|
|
$this->getMedia()->setContent($upload); |
|
176
|
|
|
|
|
177
|
|
|
if ($this->getMedia() === null) { |
|
178
|
|
|
unlink($path); |
|
179
|
|
|
|
|
180
|
|
|
throw new AccessDeniedException('Can not link file'); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return Media |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function getMedia() |
|
188
|
|
|
{ |
|
189
|
1 |
|
return $this->media; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* __destruct |
|
194
|
|
|
*/ |
|
195
|
|
|
public function __destruct() |
|
196
|
|
|
{ |
|
197
|
|
|
if ($this->path !== null) { |
|
198
|
|
|
unlink($this->path); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.