22
33namespace  EWZ \SymfonyAdminBundle \FileUploader ;
44
5+ use  Symfony \Component \HttpFoundation \File \Exception \FileException ;
56use  Symfony \Component \HttpFoundation \File \UploadedFile ;
67use  Symfony \Component \HttpKernel \KernelInterface ;
78use  Symfony \Component \Uid \Uuid ;
@@ -31,9 +32,9 @@ public function __construct(
3132        int  $ maxFilesize
3233        string  $ imageDrivernull 
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 ($ filePathsprintf ('%s/public/%s ' , $ this kernel ->getProjectDir (), $ directory
56-             mkdir ($ filePath0777 , true );
57-         }
56+         $ this createFolder ($ directory
5857
5958        // generate filename 
6059        $ newFileNamesprintf ('%s/%s ' , $ directorybasename ($ fileName
6160
6261        // move file to .. 
63-         rename ($ fileNamesprintf ('%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 ($ filePathsprintf ('%s/public/%s ' , $ this kernel ->getProjectDir (), $ directory
88-             mkdir ($ filePath0777 , true );
89-         }
89+         $ this createFolder ($ directory
9090
9191        if  ($ prefix
9292            $ prefix'__ ' ;
@@ -96,7 +96,10 @@ public function upload(
9696        $ fileNamesprintf ('%s/%s%s.%s ' , $ directory$ prefixv4 (), $ fileguessExtension ());
9797
9898        // move file to .. 
99-         rename ($ filegetPathname (), sprintf ('%s/public/%s ' , $ this kernel ->getProjectDir (), $ fileName
99+         rename ($ filegetPathname (), $ 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  $ fromDirstring  $ toDirstring  $ fileNamestring 
113116    {
114-         if  (!file_exists ($ orgFilePathsprintf ( ' %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 ($ filePathsprintf ('%s/public/%s ' , $ this kernel ->getProjectDir (), $ toDir
120-             mkdir ($ filePath0777 , true );
121-         }
122+         $ this createFolder ($ toDir
122123
123124        // generate filename 
124125        $ fileNamestr_replace ($ fromDir$ toDir$ fileName
125126
126127        // move file to .. 
127-         rename ($ orgFilePathsprintf ('%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  $ fileNamevoid 
136140    {
137-         $ filePathsprintf ( ' %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  $ fileNamestring 
148152    {
149-         $ filePathsprintf ( ' %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  $ fileNamestring 
160164    {
161-         $ filePathsprintf ( ' %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  $ fileNameint 
172176    {
173-         $ filePathsprintf ( ' %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  $ dirNamevoid 
188+     {
189+         // create folder if doesn't exists 
190+         if  (!is_dir ($ pathsprintf ('%s/public/%s ' , $ this kernel ->getProjectDir (), $ dirName
191+             mkdir ($ path0777 , true );
192+         }
193+     }
194+ 
195+     /** 
196+      * @param string $dirName 
197+      * 
198+      * @return string 
199+      */ 
200+     private  function  getFilePath (string  $ dirNamestring 
201+     {
202+         return  sprintf ('%s/public/%s ' , $ this kernel ->getProjectDir (), $ dirName
203+     }
204+ 
205+     /** 
206+      * @param string $fileName 
207+      */ 
208+     private  function  setFilePermissions (string  $ fileNamevoid 
209+     {
210+         try  {
211+             $ filePath$ this getFilePath ($ fileName
212+ 
213+             $ userOwnernull ;
214+             $ groupOwnernull ;
215+ 
216+             $ directorstr_replace (basename ($ filePathnull , $ filePath
217+             if  (is_dir ($ director
218+                 $ userOwnerposix_getpwuid (fileowner ($ director'name ' ] ?? null ;
219+                 $ groupOwnerposix_getgrgid (filegroup ($ director'name ' ] ?? null ;
220+             }
221+ 
222+             if  (file_exists ($ filePath$ userOwner$ groupOwner
223+                 chmod ($ filePath0777 );
224+                 chown ($ filePath$ userOwner
225+                 chgrp ($ filePath$ groupOwner
226+             }
227+         } catch  (FileException $ e
228+         }
229+     }
179230}
0 commit comments