44
55namespace Tamedevelopers \File \Traits ;
66
7+ use Tamedevelopers \Support \Tame ;
78use Tamedevelopers \Support \Time ;
9+ use Tamedevelopers \Support \Server ;
810
911
1012/**
@@ -30,6 +32,9 @@ protected function getFolderStorage($structure, $uploadDirectory, $newGenFileNam
3032 // Creating our folder structure
3133 $ folder = $ this ->createTimeBaseFolder ($ uploadDirectory );
3234
35+ // if directory is empty
36+ $ uploadDirectory = empty ($ uploadDirectory ) ? "" : "{$ uploadDirectory }/ " ;
37+
3338 switch ($ structure )
3439 {
3540 case 'year ' :
@@ -45,7 +50,7 @@ protected function getFolderStorage($structure, $uploadDirectory, $newGenFileNam
4550 $ path = str_replace ($ this ->config ['baseDir ' ], '' , $ fullPath );
4651 break ;
4752 default :
48- $ fullPath = "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }/ {$ newGenFileName }" ;
53+ $ fullPath = "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }{$ newGenFileName }" ;
4954 $ path = str_replace ($ this ->config ['baseDir ' ], '' , $ fullPath );
5055 break ;
5156 }
@@ -82,28 +87,30 @@ protected function createBaseDirectory()
8287 */
8388 protected function createParentFolder ($ uploadDirectory )
8489 {
85- // explode path using `/`
86- // this way we're able tpo separate all [dir] and [subdir]
87- $ directorySegments = explode ('/ ' , $ uploadDirectory );
88-
89- $ segmentPath = "" ;
90-
91- // loop through each directory path
92- foreach ($ directorySegments as $ key => $ segment ){
93-
94- $ separator = $ key === 0 ? '' : '/ ' ;
95- $ segmentPath .= $ separator . $ segment ;
96-
97- // create absolute path
98- $ fullPath = "{$ this ->config ['baseDir ' ]}{$ segmentPath }" ;
99-
100- // Create folder if not exist
101- if (!is_dir ($ fullPath ))
102- {
103- @mkdir ($ fullPath , 0777 );
104-
105- // Create index file
106- $ this ->createDefaultRestrictedFiles ($ fullPath );
90+ if (!empty ($ uploadDirectory )){
91+ // explode path using `/`
92+ // this way we're able tpo separate all [dir] and [subdir]
93+ $ directorySegments = explode ('/ ' , $ uploadDirectory );
94+
95+ $ segmentPath = "" ;
96+
97+ // loop through each directory path
98+ foreach ($ directorySegments as $ key => $ segment ){
99+
100+ $ separator = $ key === 0 ? '' : '/ ' ;
101+ $ segmentPath .= $ separator . $ segment ;
102+
103+ // create absolute path
104+ $ fullPath = "{$ this ->config ['baseDir ' ]}{$ segmentPath }" ;
105+
106+ // Create folder if not exist
107+ if (!is_dir ($ fullPath ))
108+ {
109+ @mkdir ($ fullPath , 0777 );
110+
111+ // Create index file
112+ $ this ->createDefaultRestrictedFiles ($ fullPath );
113+ }
107114 }
108115 }
109116 }
@@ -164,6 +171,9 @@ protected function createTimeBaseFolder($uploadDirectory = null)
164171 {
165172 $ now = strtotime ("now " );
166173
174+ // if directory is empty
175+ $ uploadDirectory = empty ($ uploadDirectory ) ? "" : "{$ uploadDirectory }/ " ;
176+
167177 $ time = [
168178 "year " => Time::timestamp ($ now , 'Y ' ),
169179 "month " => Time::timestamp ($ now , 'n ' ),
@@ -172,9 +182,9 @@ protected function createTimeBaseFolder($uploadDirectory = null)
172182 ];
173183
174184 return [
175- 'year ' => "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }/ {$ time ['year ' ]}" ,
176- 'month ' => "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }/ {$ time ['year ' ]}/ {$ time ['month ' ]}" ,
177- 'day ' => "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }/ {$ time ['year ' ]}/ {$ time ['month ' ]}/ {$ time ['day ' ]}" ,
185+ 'year ' => "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }{$ time ['year ' ]}" ,
186+ 'month ' => "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }{$ time ['year ' ]}/ {$ time ['month ' ]}" ,
187+ 'day ' => "{$ this ->config ['baseDir ' ]}{$ uploadDirectory }{$ time ['year ' ]}/ {$ time ['month ' ]}/ {$ time ['day ' ]}" ,
178188 'now ' => $ time ['now ' ]
179189 ];
180190 }
@@ -187,23 +197,72 @@ protected function createTimeBaseFolder($uploadDirectory = null)
187197 */
188198 protected function createDefaultRestrictedFiles ($ path )
189199 {
190- //Create index file
191- if (!file_exists ("{$ path }/index.html " ) ) {
192- @$ fsource = fopen ("{$ path }/index.html " , 'w+ ' );
193- if (is_resource ($ fsource )){
194- fwrite ($ fsource , "Restricted Access " );
195- fclose ($ fsource );
196- }
200+ $ dummyPath = $ this ->pathToDummy ($ path );
201+
202+ // create for htaccess
203+ $ this ->createHtaccess ($ dummyPath );
204+
205+ // create for html
206+ // $this->createHtml($dummyPath);
207+ }
208+
209+ /**
210+ * Create HTaccess File
211+ *
212+ * @param mixed $dummyPath
213+ * @return void
214+ */
215+ private function createHtaccess ($ dummyPath )
216+ {
217+ // create for htaccess
218+ if (!Tame::exists ($ dummyPath ['htaccess ' ]['path ' ])){
219+ // Read the contents of the dummy file
220+ $ dummyContent = file_get_contents ($ dummyPath ['htaccess ' ]['dummy ' ]);
221+
222+ // Write the contents to the new file
223+ file_put_contents ($ dummyPath ['htaccess ' ]['path ' ], $ dummyContent );
197224 }
225+ }
226+
227+ /**
228+ * Create HTML File
229+ *
230+ * @param mixed $dummyPath
231+ * @return void
232+ */
233+ private function createHtml ($ dummyPath )
234+ {
235+ // create for html
236+ if (!Tame::exists ($ dummyPath ['html ' ]['path ' ])){
237+ // Read the contents of the dummy file
238+ $ dummyContent = file_get_contents ($ dummyPath ['html ' ]['dummy ' ]);
198239
199- //Create apache file -- .htaccess
200- if (!file_exists ("{$ path }/.htaccess " ) ) {
201- @$ fsource = fopen ("{$ path }/.htaccess " , 'w+ ' );
202- if (is_resource ($ fsource )){
203- fwrite ($ fsource , "" );
204- fclose ($ fsource );
205- }
240+ // Write the contents to the new file
241+ file_put_contents ($ dummyPath ['html ' ]['path ' ], $ dummyContent );
206242 }
207243 }
244+
245+ /**
246+ * Path to dummy files
247+ * @param mixed $path
248+ * @return array
249+ */
250+ private function pathToDummy ($ path )
251+ {
252+ $ packageDummyPath = Server::cleanServerPath (
253+ dirname (__DIR__ ) . "/Dummy/ "
254+ );
255+
256+ return [
257+ 'htaccess ' => [
258+ 'path ' => "{$ path }/.htaccess " ,
259+ 'dummy ' => "{$ packageDummyPath }dummyHtaccess.dum " ,
260+ ],
261+ 'html ' => [
262+ 'path ' => "{$ path }/index.html " ,
263+ 'dummy ' => "{$ packageDummyPath }dummyHtml.dum " ,
264+ ]
265+ ];
266+ }
208267
209268}
0 commit comments