Skip to content

Commit 6b6b4ae

Browse files
author
Fredrick Peter
committed
upgrade and dummy data
1 parent 8f64066 commit 6b6b4ae

File tree

5 files changed

+113
-49
lines changed

5 files changed

+113
-49
lines changed

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ $file->getClass();
198198

199199
- `or`
200200
```
201-
$upload = File::name('avatar')->save();
201+
$upload = File::name('avatar')
202+
->save();
202203
203204
$upload->first('url);
204-
$upload->first('name);
205205
```
206206

207207
### Get
@@ -221,7 +221,6 @@ $upload->first('name);
221221
$upload = File::name('avatar')
222222
->save();
223223
224-
$upload->first();
225224
$upload->get('name);
226225
```
227226

@@ -322,7 +321,6 @@ File::name('avatar')
322321

323322
### Folder
324323
- Takes one param `string` as `folder_path` to save file
325-
- Remember the system already have your `baseDirectory`
326324

327325
```
328326
File::name('avatar')
@@ -361,9 +359,9 @@ File::name('avatar')
361359
| Key | Description |
362360
|-----------|-----------------------------------------------------------------------|
363361
| default | Files will be uploaded in defaut folder path |
364-
| year | Files will be uploaded in `default/year` path: `folder/2023` |
365-
| month | Files will be uploaded in `default/year` path: `folder/2023/02` |
366-
| day | Files will be uploaded in `default/year` path: `folder/2023/02/30` |
362+
| year | Files will be uploaded in `default/year` path: `folder/yyyy` |
363+
| month | Files will be uploaded in `default/year` path: `folder/yyyy/mm` |
364+
| day | Files will be uploaded in `default/year` path: `folder/yyyy/mm/dd` |
367365

368366

369367
1. Default

src/Dummy/dummyHtaccess.dum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Options -Indexes
2+
Order deny,allow
3+
Deny from all
4+
<Files *>
5+
Allow from all
6+
</Files>

src/Dummy/dummyHtml.dum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restricted Access

src/File.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function save($closure = null)
100100

101101
if($this->success){
102102
if(empty($this->folder)){
103-
$this->folder = 'images';
103+
$this->folder = '';
104104
}
105105

106106
$this->proceedToSave()
@@ -256,7 +256,7 @@ public function folder($folder)
256256
}
257257

258258
/**
259-
* Set Folder Structure Creation
259+
* Set Structure Creation
260260
*
261261
* @param string $structure
262262
* - Keys [default, year, month, day]

src/Traits/FileStorageTrait.php

Lines changed: 99 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Tamedevelopers\File\Traits;
66

7+
use Tamedevelopers\Support\Tame;
78
use 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

Comments
 (0)