Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 79feba6

Browse files
committed
add delete method
1 parent e2a704a commit 79feba6

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ $fileName = $upload->getName();
9090
|`type($type = null)` |change type for upload if is null so use of default type|
9191
|`getFile($name = null)` |get file by name and return a `\AliGhale\FileManager\Models\File`|
9292
| `setPath($path)` |set file upload path |
93+
| `delete($filename)` |delete the file help by this provider type |
9394
| `getUploadPath()` |get upload path |
9495
| `dateTimePrefix($value = true)` |if is `true` so upload file with `/{year}/{month}/{day}` prefix|
9596
| `setName(string $name)` |set file name |

src/BaseType.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ public function setFile(File $file)
235235
}
236236

237237

238+
public function delete($filename = null)
239+
{
240+
/** @var File $file */
241+
$file = $this->getFile($filename);
242+
$flag = $this->handleDelete($file);
243+
$file->delete();
244+
return $flag;
245+
}
246+
247+
238248
/**
239249
* handling upload file
240250
*
@@ -244,6 +254,15 @@ public function setFile(File $file)
244254
abstract protected function handle($file);
245255

246256

257+
/**
258+
* handle delete file
259+
*
260+
* @param File $file
261+
* @return mixed
262+
*/
263+
abstract protected function handleDelete(File $file);
264+
265+
247266
/********** Getters & Setters **********/
248267

249268
/**

src/Facades/File.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @method static getFileName()
3939
* @method static BaseType fetchProperties(array $config = null)
4040
* @method static BaseType type($type = null)
41+
* @method static boolean delete($filename = null)
4142
*/
4243
class File extends Facade
4344
{

src/Types/File.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace AliGhale\FileManager\Types;
77

88
use AliGhale\FileManager\BaseType;
9+
use AliGhale\FileManager\Models\File as FileModel;
10+
use Illuminate\Support\Facades\File as FileFacade;
911

1012
class File extends BaseType
1113
{
@@ -21,4 +23,17 @@ protected function handle($file): BaseType
2123

2224
return $this;
2325
}
26+
27+
28+
protected function handleDelete(FileModel $file)
29+
{
30+
$path = $file->base_path . $file->file_name;
31+
if ($file->private) {
32+
$path = storage_path($path);
33+
} else {
34+
$path = public_path($path);
35+
}
36+
37+
return FileFacade::delete($path);
38+
}
2439
}

src/Types/Image.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace AliGhale\FileManager\Types;
77

88
use AliGhale\FileManager\BaseType;
9+
use AliGhale\FileManager\Models\File;
10+
use Illuminate\Support\Facades\File as FileFacade;
911

1012
class Image extends BaseType
1113
{
@@ -31,6 +33,42 @@ protected function handle($file)
3133
}
3234

3335

36+
protected function handleDelete(File $file)
37+
{
38+
if (is_null($this->getSizes())) {
39+
if ($sizes = $this->getConfig("sizes"))
40+
$this->setSizes($sizes);
41+
else
42+
$this->setSizes(["16", "24", "32", "64", "128"]);
43+
}
44+
45+
if (is_null($this->getThumbSize())) {
46+
if (!$thumb = $this->getConfig("thumb"))
47+
$this->setThumbSize($thumb);
48+
else
49+
$this->setThumbSize("128");
50+
}
51+
52+
$sizes = $this->getSizes();
53+
foreach ($sizes as $size) {
54+
$sizePath = $file->base_path . "{$size}/";
55+
$sizePath = $sizePath . $file->file_name;
56+
if ($file->private) {
57+
$sizePath = storage_path($sizePath);
58+
} else {
59+
$sizePath = public_path($sizePath);
60+
}
61+
62+
FileFacade::delete($sizePath);
63+
}
64+
65+
FileFacade::delete($file->base_path . "thumb/" . $file->file_name);
66+
FileFacade::delete($file->base_path . "original/" . $file->file_name);
67+
68+
return true;
69+
}
70+
71+
3472
/**
3573
* resize image and return specific array of images
3674
*

0 commit comments

Comments
 (0)