Skip to content

Commit d2bb3ef

Browse files
committed
Optimized file uploader and fix file permissions
1 parent 5365cca commit d2bb3ef

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

src/FileUploader/FileUploader.php

Lines changed: 70 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace EWZ\SymfonyAdminBundle\FileUploader;
44

5+
use Symfony\Component\HttpFoundation\File\Exception\FileException;
56
use Symfony\Component\HttpFoundation\File\UploadedFile;
67
use Symfony\Component\HttpKernel\KernelInterface;
78
use Symfony\Component\Uid\Uuid;
@@ -31,9 +32,9 @@ public function __construct(
3132
int $maxFilesize,
3233
string $imageDriver = null
3334
) {
34-
$this->kernel = $kernel;
35-
3635
parent::__construct($validator, $translator, $mimeTypesExtensions, $mimeTypesTypes, $maxFilesize, $imageDriver);
36+
37+
$this->kernel = $kernel;
3738
}
3839

3940
/**
@@ -52,15 +53,16 @@ public function create(string $fileName, string $directory, string $fileContent
5253
}
5354

5455
// create folder if doesn't exists
55-
if (!is_dir($filePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $directory))) {
56-
mkdir($filePath, 0777, true);
57-
}
56+
$this->createFolder($directory);
5857

5958
// generate filename
6059
$newFileName = sprintf('%s/%s', $directory, basename($fileName));
6160

6261
// move file to ..
63-
rename($fileName, sprintf('%s/public/%s', $this->kernel->getProjectDir(), $newFileName));
62+
rename($fileName, $this->getFilePath($newFileName));
63+
64+
// fix permissions
65+
$this->setFilePermissions($newFileName);
6466

6567
return $newFileName;
6668
}
@@ -84,9 +86,7 @@ public function upload(
8486
$this->fixOrientate($file);
8587

8688
// create folder if doesn't exists
87-
if (!is_dir($filePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $directory))) {
88-
mkdir($filePath, 0777, true);
89-
}
89+
$this->createFolder($directory);
9090

9191
if ($prefix) {
9292
$prefix .= '__';
@@ -96,7 +96,10 @@ public function upload(
9696
$fileName = sprintf('%s/%s%s.%s', $directory, $prefix, Uuid::v4(), $file->guessExtension());
9797

9898
// move file to ..
99-
rename($file->getPathname(), sprintf('%s/public/%s', $this->kernel->getProjectDir(), $fileName));
99+
rename($file->getPathname(), $this->getFilePath($fileName));
100+
101+
// fix permissions
102+
$this->setFilePermissions($fileName);
100103

101104
// delete old file (if exists)
102105
if ($oldFileName) {
@@ -111,20 +114,21 @@ public function upload(
111114
*/
112115
public function move(string $fromDir, string $toDir, string $fileName): ?string
113116
{
114-
if (!file_exists($orgFilePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $fileName))) {
117+
if (!file_exists($orgFilePath = $this->getFilePath($fileName))) {
115118
return null;
116119
}
117120

118121
// create folder if doesn't exists
119-
if (!is_dir($filePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $toDir))) {
120-
mkdir($filePath, 0777, true);
121-
}
122+
$this->createFolder($toDir);
122123

123124
// generate filename
124125
$fileName = str_replace($fromDir, $toDir, $fileName);
125126

126127
// move file to ..
127-
rename($orgFilePath, sprintf('%s/public/%s', $this->kernel->getProjectDir(), $fileName));
128+
rename($orgFilePath, $this->getFilePath($fileName));
129+
130+
// fix permissions
131+
$this->setFilePermissions($fileName);
128132

129133
return $fileName;
130134
}
@@ -134,7 +138,7 @@ public function move(string $fromDir, string $toDir, string $fileName): ?string
134138
*/
135139
public function delete(string $fileName): void
136140
{
137-
$filePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $fileName);
141+
$filePath = $this->getFilePath($fileName);
138142

139143
if (file_exists($filePath)) {
140144
unlink($filePath);
@@ -146,7 +150,7 @@ public function delete(string $fileName): void
146150
*/
147151
public function getContent(string $fileName): ?string
148152
{
149-
$filePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $fileName);
153+
$filePath = $this->getFilePath($fileName);
150154

151155
return file_exists($filePath)
152156
? file_get_contents($filePath)
@@ -158,7 +162,7 @@ public function getContent(string $fileName): ?string
158162
*/
159163
public function getMimeType(string $fileName): ?string
160164
{
161-
$filePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $fileName);
165+
$filePath = $this->getFilePath($fileName);
162166

163167
return file_exists($filePath)
164168
? mime_content_type($filePath)
@@ -170,10 +174,57 @@ public function getMimeType(string $fileName): ?string
170174
*/
171175
public function getFileSize(string $fileName): ?int
172176
{
173-
$filePath = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $fileName);
177+
$filePath = $this->getFilePath($fileName);
174178

175179
return file_exists($filePath)
176180
? filesize($filePath)
177181
: null;
178182
}
183+
184+
/**
185+
* @param string $dirName
186+
*/
187+
private function createFolder(string $dirName): void
188+
{
189+
// create folder if doesn't exists
190+
if (!is_dir($path = sprintf('%s/public/%s', $this->kernel->getProjectDir(), $dirName))) {
191+
mkdir($path, 0777, true);
192+
}
193+
}
194+
195+
/**
196+
* @param string $dirName
197+
*
198+
* @return string
199+
*/
200+
private function getFilePath(string $dirName): string
201+
{
202+
return sprintf('%s/public/%s', $this->kernel->getProjectDir(), $dirName);
203+
}
204+
205+
/**
206+
* @param string $fileName
207+
*/
208+
private function setFilePermissions(string $fileName): void
209+
{
210+
try {
211+
$filePath = $this->getFilePath($fileName);
212+
213+
$userOwner = null;
214+
$groupOwner = null;
215+
216+
$director = str_replace(basename($filePath), null, $filePath);
217+
if (is_dir($director)) {
218+
$userOwner = posix_getpwuid(fileowner($director))['name'] ?? null;
219+
$groupOwner = posix_getgrgid(filegroup($director))['name'] ?? null;
220+
}
221+
222+
if (file_exists($filePath) && $userOwner && $groupOwner) {
223+
chmod($filePath, 0777);
224+
chown($filePath, $userOwner);
225+
chgrp($filePath, $groupOwner);
226+
}
227+
} catch (FileException $e) {
228+
}
229+
}
179230
}

src/FileUploader/S3Uploader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public function __construct(
4242
string $imageDriver = null,
4343
string $baseUrl = null
4444
) {
45+
parent::__construct($validator, $translator, $mimeTypesExtensions, $mimeTypesTypes, $maxFilesize, $imageDriver);
46+
4547
$this->s3Client = $s3Client;
4648
$this->s3Bucket = $s3Bucket;
4749
$this->baseUrl = $baseUrl;
48-
49-
parent::__construct($validator, $translator, $mimeTypesExtensions, $mimeTypesTypes, $maxFilesize, $imageDriver);
5050
}
5151

5252
/**

0 commit comments

Comments
 (0)