Skip to content

Commit a878434

Browse files
author
Fredrick Peter
committed
update
1 parent 232d6a8 commit a878434

File tree

8 files changed

+88
-39
lines changed

8 files changed

+88
-39
lines changed

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* [INPUT HTML STRUCTURE](#input-html-structure)
2626
* [Driver](#driver)
2727
* [Name](#name)
28+
* [BaseDir](#baseDir)
2829
* [Folder](#folder)
2930
* [Filter](#filter)
3031
* [Structure](#structure)
@@ -133,7 +134,7 @@ config_file(
133134
],
134135
config: [
135136
'limit' => 1,
136-
'mime' => 'images', // video|audio|files|images|general_image|general_media|general_file
137+
'mime' => 'image', // video|audio|file|image|general_image|general_media|general_file
137138
'size' => 2097152, // 2mb
138139
'baseDir' => 'public',
139140
'driver' => 'local',
@@ -185,8 +186,7 @@ $file->getClass();
185186

186187
### First
187188
- This will get the first uploaded data
188-
- You can pass and [optional] param as string `name` \| `url`
189-
- Returns an array `[name, path, url]`
189+
- [optional] You can pass the mode as string `name` \| `path`\|`url`
190190

191191
```
192192
->save(function($response){
@@ -206,6 +206,7 @@ $upload->first('name);
206206
### Get
207207
- This will get all uploaded data
208208
- Returns an index array of all uploaded data, `[name, path, url]`
209+
- [optional] You can pass the mode as string `name` \| `path`\|`url`
209210

210211
```
211212
->save(function($response){
@@ -220,7 +221,7 @@ $upload = File::name('avatar')
220221
->save();
221222
222223
$upload->first();
223-
$upload->get();
224+
$upload->get('name);
224225
```
225226

226227
### Form
@@ -301,6 +302,15 @@ File::name('avatar')
301302
File::name('html_input_name');
302303
```
303304

305+
### BaseDir
306+
- Takes one param `string` as base directory name
307+
- This will override the global configuration settings (Domain and Server Path will be set)
308+
309+
```
310+
File::name('avatar')
311+
->baseDir('newBaseDirectory');
312+
```
313+
304314
### Folder
305315
- Takes one param `string` as `folder_path` to save file
306316
- Remember the system already have your `baseDirectory`
@@ -586,17 +596,17 @@ if($file->isCompleted()){
586596
```
587597
'video' => ['.mp4', '.mpeg', '.mov', '.avi', '.wmv'],
588598
'audio' => ['.mp3', '.wav'],
589-
'files' => ['.docx', '.pdf', '.txt'],
590-
'images' => ['.jpg', '.jpeg', '.png'],
599+
'file' => ['.docx', '.pdf', '.txt'],
600+
'image' => ['.jpg', '.jpeg', '.png'],
591601
'general_file' => ['.docx', '.pdf', '.txt', '.zip', '.rar', '.xlsx', '.xls'],
592602
'general_image' => ['.jpg', '.jpeg', '.png', '.webp'],
593603
'general_media' => ['.mp3', '.wav', '.mp4', '.mpeg', '.mov', '.avi', '.wmv']
594604
```
595605
- video
596606
- audio
597-
- files
607+
- file
608+
- image
598609
- general_file
599-
- images
600610
- general_image
601611
- general_media
602612

src/File.php

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Exception;
88
use Tamedevelopers\Support\Str;
99
use Tamedevelopers\Support\Tame;
10+
use Tamedevelopers\Support\Server;
1011
use Tamedevelopers\File\ImageCompress;
1112
use Tamedevelopers\File\ImageWatermark;
1213
use Tamedevelopers\Validator\Validator;
@@ -115,7 +116,7 @@ public function save($closure = null)
115116
* @param string $watermarkSource The path to the watermark image.
116117
*
117118
* @param string $position The position of the watermark. - Default is 'bottom-right'
118-
* - ['center', 'bottom-right', 'bottom-left', 'top-right', 'top-left']
119+
* ['center', 'bottom-right', 'bottom-left', 'top-right', 'top-left']
119120
*
120121
* @param int $padding Padding in pixels (applied to all positions except 'center')
121122
*
@@ -185,6 +186,30 @@ public function compress()
185186
return $this;
186187
}
187188

189+
/**
190+
* Change base directory
191+
*
192+
* @param string $directory
193+
* @return $this
194+
*/
195+
public function baseDir($directory)
196+
{
197+
// clean the path given
198+
$baseDirName = trim((string) $directory, '\/');
199+
200+
// base domain path
201+
$this->config['baseUrl'] = Server::cleanServerPath(
202+
domain($baseDirName)
203+
);
204+
205+
// trim string
206+
$this->config['baseDir'] = Server::cleanServerPath(
207+
base_path($baseDirName)
208+
);
209+
210+
return $this;
211+
}
212+
188213
/**
189214
* Change driver type
190215
*
@@ -235,13 +260,12 @@ public function structure($structure)
235260
/**
236261
* Upload size in mb
237262
*
238-
* @param int|string|null $size
263+
* @param int|string $size
239264
* [optional] Default is (2mb)
240265
*
241-
*
242266
* @return $this
243267
*/
244-
public function size($size = null)
268+
public function size($size)
245269
{
246270
$this->config['size'] = Tame::sizeToBytes(
247271
!empty($size) && (int) $size >= 1024
@@ -255,10 +279,10 @@ public function size($size = null)
255279
/**
256280
* Upload limit
257281
*
258-
* @param int|string|null $limit
282+
* @param int|string $limit
259283
* @return $this
260284
*/
261-
public function limit($limit = null)
285+
public function limit($limit)
262286
{
263287
$this->config['limit'] = self::numbericToInt($limit) ?: 1;
264288

@@ -268,13 +292,13 @@ public function limit($limit = null)
268292
/**
269293
* Set width
270294
*
271-
* @param int|string|null $width
295+
* @param int|string $width
272296
* @param bool $width - Default is `true`
273297
* [optional] Set to false will make size equal to or greather than
274298
*
275299
* @return $this
276300
*/
277-
public function width($width = null, ?bool $actual = true)
301+
public function width($width, ?bool $actual = true)
278302
{
279303
$this->config['width'] = [
280304
'size' => self::numbericToInt($width),
@@ -287,13 +311,13 @@ public function width($width = null, ?bool $actual = true)
287311
/**
288312
* Set Height
289313
*
290-
* @param int|string|null $height
314+
* @param int|string $height
291315
* @param bool $width - Default is `true`
292316
* [optional] Set to false will make size equal to or greather than
293317
*
294318
* @return $this
295319
*/
296-
public function height($height = null, ?bool $actual = true)
320+
public function height($height, ?bool $actual = true)
297321
{
298322
$this->config['height'] = [
299323
'size' => self::numbericToInt($height),
@@ -306,10 +330,13 @@ public function height($height = null, ?bool $actual = true)
306330
/**
307331
* Set Mime Type
308332
*
309-
* @param string|null $mime
333+
* @param string $mime
334+
* [available keys]
335+
* - video|audio|file|image|general_image|general_media|general_file
336+
*
310337
* @return $this
311338
*/
312-
public function mime($mime = null)
339+
public function mime($mime)
313340
{
314341
$this->config['mime'] = $mime;
315342

@@ -390,6 +417,7 @@ public function isCompleted()
390417
* Get First Element of Uploads
391418
*
392419
* @param string|null $mode
420+
* - [name, path, url]
393421
*
394422
* @return array|string|null
395423
*/
@@ -403,11 +431,19 @@ public function first($mode = null)
403431
/**
404432
* Get all Element of Uploads
405433
*
434+
* @param string|null $mode
435+
* - [name, path, url]
436+
*
406437
* @return array|null
407438
*/
408-
public function get()
439+
public function get($mode = null)
409440
{
410-
return self::getUploads($this->uploads);
441+
$data = self::getUploads($this->uploads);
442+
if(is_null($mode)){
443+
return $data;
444+
}
445+
446+
return array_column($data, $mode);
411447
}
412448

413449
/**

src/FileHelper.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function extension()
4242
/**
4343
* Get Temporary Path
4444
*
45-
* @return void
45+
* @return string
4646
*/
4747
public function tmp()
4848
{
@@ -98,10 +98,11 @@ public function imageSize()
9898
'height' => $imagePath[1] ?? null
9999
];
100100
}
101-
101+
102102
/**
103103
* Generate new file
104-
*
104+
*
105+
* @param bool $allow
105106
* @return string
106107
* - Generated Filename
107108
*/

src/Methods/FileMethod.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static public function has($name)
7878
*
7979
* @param mixed $uploads
8080
* @param bool $first
81-
* @return void
81+
* @return array
8282
*/
8383
static protected function getUploads($uploads, $first = false)
8484
{
@@ -103,6 +103,8 @@ static protected function getUploads($uploads, $first = false)
103103

104104
return $files;
105105
}
106+
107+
return [];
106108
}
107109

108110
/**
@@ -209,7 +211,7 @@ static protected function numbericToInt($value = null)
209211
static protected function getStructureType($mode = null)
210212
{
211213
return match ($mode) {
212-
'default', 'year', 'month', 'day' => $mode,
214+
'year', 'month', 'day' => $mode,
213215
default => 'default',
214216
};
215217
}
@@ -225,8 +227,8 @@ static protected function allowedMimeType()
225227
$mimeType = [
226228
'video' => ['video/mp4','video/mpeg','video/quicktime','video/x-msvideo','video/x-ms-wmv'],
227229
'audio' => ['audio/mpeg','audio/x-wav'],
228-
'files' => ['application/msword','application/pdf','text/plain'],
229-
'images' => ['image/jpeg', 'image/png', 'image/gif'],
230+
'file' => ['application/msword','application/pdf','text/plain'],
231+
'image' => ['image/jpeg', 'image/png', 'image/gif'],
230232
'general_image' => ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/vnd.microsoft.icon'],
231233
'general_media' => ['audio/mpeg','audio/x-wav', 'video/mp4','video/mpeg','video/quicktime','video/x-msvideo','video/x-ms-wmv'],
232234
'general_file' => [
@@ -241,8 +243,8 @@ static protected function allowedMimeType()
241243
$extensionType = [
242244
'video' => ['.mp4', '.mpeg', '.mov', '.avi', '.wmv'],
243245
'audio' => ['.mp3', '.wav'],
244-
'files' => ['.docx', '.pdf', '.txt'],
245-
'images' => ['.jpg', '.jpeg', '.png', '.gif'],
246+
'file' => ['.docx', '.pdf', '.txt'],
247+
'image' => ['.jpg', '.jpeg', '.png', '.gif'],
246248
'general_image' => ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.ico'],
247249
'general_media' => ['.mp3', '.wav', '.mp4', '.mpeg', '.mov', '.avi', '.wmv'],
248250
'general_file' => ['.docx', '.pdf', '.txt', '.zip', '.rar', '.xlsx', '.xls'],

src/Traits/FilePropertyTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ trait FilePropertyTrait{
7979
'width' => null,
8080
'height' => null,
8181
'size' => null,
82-
'mime' => 'images',
82+
'mime' => 'image',
8383
'structure' => null,
8484
'generate' => true,
8585
];

src/Traits/FileTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function globalConfig($message = [], $config = [], $class = [])
5353
$config = array_merge([
5454
'limit' => 1,
5555
'size' => 2097152, // 2mb
56-
'mime' => 'images', // video|audio|files|images|general_image|general_media|general_file
56+
'mime' => 'image', // video|audio|file|image|general_image|general_media|general_file
5757
'baseDir' => $baseDirName,
5858
'driver' => 'local', // local|s3
5959
'structure' => 'default', // default|year|month|day

src/Traits/FileValidatorTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ private function proceedToValidate()
9999
foreach($this->fileItemsData() as $key => $file){
100100

101101
// Mime types
102-
$mimeTypes = $this->allowedMimeType()['mime'][$this->config['mime']] ?? $this->allowedMimeType()['mime']['images'];
102+
$mimeTypes = $this->allowedMimeType()['mime'][$this->config['mime']] ?? $this->allowedMimeType()['mime']['image'];
103103

104104
// mimeExtensions
105-
$mimeExtensions = $this->allowedMimeType()['extension'][$this->config['mime']] ?? $this->allowedMimeType()['extension']['images'];
105+
$mimeExtensions = $this->allowedMimeType()['extension'][$this->config['mime']] ?? $this->allowedMimeType()['extension']['image'];
106106

107107
// if image width and height is allowed
108108
$imageSizeAllowed = $this->isImageWithHeightAllowed($file->imageSize());

tests/mime.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
$mimeType = [
2424
'video' => ['video/mp4','video/mpeg','video/quicktime','video/x-msvideo','video/x-ms-wmv'],
2525
'audio' => ['audio/mpeg','audio/x-wav'],
26-
'files' => ['application/msword','application/pdf','text/plain'],
27-
'images' => ['image/jpeg', 'image/png', 'image/gif'],
26+
'file' => ['application/msword','application/pdf','text/plain'],
27+
'image' => ['image/jpeg', 'image/png', 'image/gif'],
2828
'general_image' => ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/vnd.microsoft.icon'],
2929
'general_media' => ['audio/mpeg','audio/x-wav', 'video/mp4','video/mpeg','video/quicktime','video/x-msvideo','video/x-ms-wmv'],
3030
'general_file' => [
@@ -40,8 +40,8 @@
4040
$extensionType = [
4141
'video' => ['.mp4', '.mpeg', '.mov', '.avi', '.wmv'],
4242
'audio' => ['.mp3', '.wav'],
43-
'files' => ['.docx', '.pdf', '.txt'],
44-
'images' => ['.jpg', '.jpeg', '.png', '.gif'],
43+
'file' => ['.docx', '.pdf', '.txt'],
44+
'image' => ['.jpg', '.jpeg', '.png', '.gif'],
4545
'general_image' => ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.ico'],
4646
'general_media' => ['.mp3', '.wav', '.mp4', '.mpeg', '.mov', '.avi', '.wmv'],
4747
'general_file' => ['.docx', '.pdf', '.txt', '.zip', '.rar', '.xlsx', '.xls'],

0 commit comments

Comments
 (0)