Skip to content

Commit 777b0d0

Browse files
author
Fredrick Peter
committed
General Upgrade and isCompleted() method added.
1 parent 5de9a80 commit 777b0d0

File tree

5 files changed

+103
-71
lines changed

5 files changed

+103
-71
lines changed

README.md

Lines changed: 63 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* [Not Empty](#not-empty)
4141
* [Is Empty](#is-empty)
4242
* [Has Error](#has-error)
43+
* [Is Completed](#is-completed)
4344
* [Mime Types](#mime-types)
4445
* [Useful links](#useful-links)
4546

@@ -101,7 +102,7 @@ $file = TameFile();
101102
- Define as `named argument`
102103

103104

104-
| Keys | Types | Description |
105+
| Key | Types | Description |
105106
|---------------|-------------------|---------------------------------------------------|
106107
| message | Assoc `array` | Create all error message in different language |
107108
| config | Assoc `array` | Create all needed config data |
@@ -144,7 +145,7 @@ FileConfig(
144145
### Get Message
145146
- This will return error message
146147

147-
```usage
148+
```
148149
$file = File::name('html_input_name');
149150
150151
$file->getMessage();
@@ -153,7 +154,7 @@ $file->getMessage();
153154
### Get Status
154155
- This will return error status code
155156

156-
```usage
157+
```
157158
$file = File::name('html_input_name');
158159
159160
$file->getStatus();
@@ -164,14 +165,15 @@ $file->getStatus();
164165
- You can pass and [optional] param as string `name` \| `url`
165166
- Returns an array `[name, url]`
166167

167-
```usage
168+
```
168169
->save(function($response){
169170
170171
$response->first();
171172
});
172173
```
173174

174-
```or
175+
- `or`
176+
```
175177
$upload = File::name('avatar')
176178
->validate()
177179
->save();
@@ -184,14 +186,15 @@ $upload->first('name);
184186
- This will get all uploaded data
185187
- Returns an index array of all uploaded data
186188

187-
```usage
189+
```
188190
->save(function($response){
189191
190192
$response->get();
191193
});
192194
```
193195

194-
```or
196+
- `or`
197+
```
195198
$upload = File::name('avatar')
196199
->validate()
197200
->save();
@@ -207,8 +210,8 @@ $upload->get();
207210
```
208211
<input type="file" name="avatar">
209212
```
210-
- or -- `For Multiple Data`
211213

214+
- or -- `For Multiple Data`
212215
```
213216
<input type="file" name="avatar[]" multiple>
214217
```
@@ -217,18 +220,18 @@ $upload->get();
217220
- More drivers are to be added in the future
218221
- By default driver is set to `local`
219222

220-
| Type | Description |
223+
| Key | Description |
221224
|-----------|-------------------------------------------------------------------------------|
222225
| s3 | Amazon `s3` driver. The package loaded `[Ec2 and CloudWatch]` needed by s3 |
223226
| local | Local `host server` driver. |
224227

225-
```usage
226-
228+
```
227229
File::name('avatar')
228230
->driver('s3');
229231
```
230232

231-
```using driver if project not in Laravel
233+
- `using driver if project is not in any Frameworks`
234+
```
232235
use Tamedevelopers\Support\Env;
233236
234237
// if your project is not on on core php, then you'll need to load env.
@@ -239,7 +242,6 @@ use Tamedevelopers\Support\Env;
239242
Env::createOrIgnore();
240243
Env::load();
241244
242-
243245
File::name('avatar')
244246
->driver('s3');
245247
```
@@ -248,15 +250,15 @@ File::name('avatar')
248250
- Takes one param `string` as input name
249251
- Static method by default
250252

251-
```usage
253+
```
252254
File::name('html_input_name');
253255
```
254256

255257
### Folder
256258
- Takes one param `string` as `folder_path` to save file
257259
- Remember the system already have your `baseDirectory`
258260

259-
```usage
261+
```
260262
File::name('avatar')
261263
->folder('upload/user');
262264
```
@@ -275,12 +277,13 @@ File::name('avatar')
275277
| 405 | Image dimension allowed is |
276278
| 200 | File uploaded successfully |
277279

278-
```usage
280+
```
279281
File::name('avatar')
280282
->filter(401, 402);
281283
```
282284

283-
```or
285+
- `or`
286+
```
284287
File::name('avatar')
285288
->filter([401, 402, 405]);
286289
```
@@ -289,7 +292,7 @@ File::name('avatar')
289292
- Takes one param `string` as `structure type`
290293
- Best used for `Media\|Blog\|Newsletter` Websites.
291294

292-
| Type | Description |
295+
| Key | Description |
293296
|-----------|-----------------------------------------------------------------------|
294297
| default | Files will be uploaded in defaut folder path |
295298
| year | Files will be uploaded in `default/year` path: `folder/2023` |
@@ -302,7 +305,7 @@ File::name('avatar')
302305
- Month
303306
- Day
304307

305-
```usage
308+
```
306309
File::name('avatar')
307310
->structure('month');
308311
```
@@ -311,12 +314,13 @@ File::name('avatar')
311314
- Takes one param `string` \| `int`
312315
- size in `int` \| `kb` \| `mb` \| `gb`
313316

314-
```usage
317+
```
315318
File::name('avatar')
316319
->size('1.5mb'); // will be converted to: 1.5 * (1024 * 1024) = 1572864
317320
```
318321

319-
```or
322+
- `or`
323+
```
320324
File::name('avatar')
321325
->size(2097152); // = 2097152|2mb
322326
```
@@ -325,7 +329,7 @@ File::name('avatar')
325329
- Takes one param `string` \| `int`
326330
- Default limit is set to `1` upload
327331

328-
```usage
332+
```
329333
File::name('avatar')
330334
->limit(2);
331335
```
@@ -345,7 +349,7 @@ File::name('avatar')
345349
| general_file | `['application/msword','application/pdf','text/plain','application/zip', 'application/x-zip-compressed', 'multipart/x-zip','application/x-zip-compressed' 'application/x-rar-compressed', 'application/octet-stream', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']`|
346350

347351

348-
```usage
352+
```
349353
File::name('avatar')
350354
->mime('images');
351355
```
@@ -355,7 +359,7 @@ File::name('avatar')
355359
- 1st param is `string` \| `int`. width size
356360
- 2nd param is `bool`. This allow to check if size should be === or >= size of uploaded image. Default is `true`
357361

358-
```usage
362+
```
359363
$file = File::name('avatar')
360364
->width(700, false);
361365
@@ -367,7 +371,7 @@ dd(
367371
### Height
368372
- Same as `width` method
369373

370-
```usage
374+
```
371375
File::name('avatar')
372376
->width(700)
373377
->height(400);
@@ -377,15 +381,16 @@ File::name('avatar')
377381
- Takes an [optional] param as a `callable\|closure` function.
378382
- The method needs to be called to validate upload errors before saving
379383

380-
```usage
384+
```
381385
File::name('banner')
382386
->folder('upload/banner')
383387
->width(700)
384388
->height(400)
385389
->validate();
386390
```
387391

388-
```or
392+
- `or`
393+
```
389394
File::name('banner')
390395
->folder('upload/banner')
391396
->validate(function($response){
@@ -398,7 +403,7 @@ File::name('banner')
398403
- Takes an [optional] param as a `callable\|closure` function.
399404
- Calling this [method] will automatically save uploaded data on `success`.
400405

401-
```usage
406+
```
402407
File::name('banner')
403408
->folder('upload/banner')
404409
->validate()
@@ -408,7 +413,8 @@ File::name('banner')
408413
});
409414
```
410415

411-
```or
416+
- `or`
417+
```
412418
$file = File::name('banner')
413419
->folder('upload/banner')
414420
->validate()
@@ -424,7 +430,7 @@ dd(
424430
- Takes two param as `size` `int` width and height
425431
- Returns an instance of self
426432

427-
```usage
433+
```
428434
File::name()
429435
->folder('upload/banner')
430436
->validate('avatar')
@@ -437,23 +443,34 @@ File::name()
437443
```
438444

439445
### WaterMark
440-
- Returns an instance of self
446+
- Takes three param `watermarkSource` \| `position` \| `padding`
447+
- [mandatory] $watermarkSource
448+
- Returns an instance of self
449+
- Padding is applied evenly on all position apart from `center`
441450

442-
```usage
451+
| Position key | Description |
452+
|-------------------|-----------------------------------------------|
453+
| bottom-right | Default is `bottom-right` |
454+
| bottom-left | apply watermart to possition `bottom-left` |
455+
| top-right | apply watermart to possition `top-right` |
456+
| top-left | apply watermart to possition `top-left` |
457+
| center | apply watermart to possition `center` |
458+
459+
```
443460
File::name('avatar')
444461
->folder('upload/banner')
445462
->validate()
446463
->save(function($response){
447464
448465
// perform watermark
449-
$response->watermark('watermark.png', 'center');
466+
$response->watermark('watermark.png', 'center', 50);
450467
});
451468
```
452469

453470
### Compress
454471
- Returns an instance of self
455472

456-
```usage
473+
```
457474
File::name('avatar')
458475
->folder('upload/banner')
459476
->validate()
@@ -474,7 +491,7 @@ File::name('avatar')
474491
- Takes one param as `string`
475492
- Return an `array` \| `null`
476493

477-
```usage
494+
```
478495
File::getImageSize('full_source_path')
479496
480497
[
@@ -519,6 +536,17 @@ if($file->hasError()){
519536
}
520537
```
521538

539+
### Is Completed
540+
- Returns true or false. Check if upload has been completed
541+
542+
```
543+
$file = File::name('avatar')
544+
545+
if($file->isCompleted()){
546+
547+
}
548+
```
549+
522550
### Mime Types
523551
```
524552
'video' => ['.mp4', '.mpeg', '.mov', '.avi', '.wmv'],

src/File.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function __construct($name = null, $files = null)
4848
/**
4949
* Initiliaze File Upload Using `name`
5050
*
51-
* @param string $name
51+
* @param string|null $name
5252
* @return $this
5353
*/
54-
static public function name($name)
54+
static public function name($name = null)
5555
{
5656
return new static(
5757
$name,
@@ -107,7 +107,7 @@ public function save(callable $function = null)
107107
*
108108
* @return $this
109109
*/
110-
public function watermark($watermarkSource, $position = 'bottom-right', $padding = 10)
110+
public function watermark($watermarkSource, $position = 'bottom-right', $padding = 15)
111111
{
112112
$watermark = new ImageWatermark(
113113
object: $this,
@@ -313,28 +313,38 @@ public function getMessage()
313313
/**
314314
* Get Status Code
315315
*
316-
* @return int|null
316+
* @return int
317317
*/
318318
public function getStatus()
319319
{
320-
return $this->data['status'];
320+
return (int) $this->data['status'];
321321
}
322322

323323
/**
324324
* Check if upload has an error
325325
*
326-
* @return int|null
326+
* @return bool
327327
*/
328328
public function hasError()
329329
{
330330
$status = $this->getStatus();
331-
if(!empty($status) && !$status == 0)
331+
if(!empty($status) && !in_array($status, [0, 200]))
332332
{
333333
return true;
334334
}
335335

336336
return false;
337337
}
338+
339+
/**
340+
* Check if file has been uploaded
341+
*
342+
* @return bool
343+
*/
344+
public function isCompleted()
345+
{
346+
return $this->getStatus() === 200;
347+
}
338348

339349
/**
340350
* Get First Element of Uploads

0 commit comments

Comments
 (0)