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

Commit

Permalink
add delete method
Browse files Browse the repository at this point in the history
  • Loading branch information
AliGhaleyan committed Mar 5, 2020
1 parent e2a704a commit 79feba6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ $fileName = $upload->getName();
|`type($type = null)` |change type for upload if is null so use of default type|
|`getFile($name = null)` |get file by name and return a `\AliGhale\FileManager\Models\File`|
| `setPath($path)` |set file upload path |
| `delete($filename)` |delete the file help by this provider type |
| `getUploadPath()` |get upload path |
| `dateTimePrefix($value = true)` |if is `true` so upload file with `/{year}/{month}/{day}` prefix|
| `setName(string $name)` |set file name |
Expand Down
19 changes: 19 additions & 0 deletions src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ public function setFile(File $file)
}


public function delete($filename = null)
{
/** @var File $file */
$file = $this->getFile($filename);
$flag = $this->handleDelete($file);
$file->delete();
return $flag;
}


/**
* handling upload file
*
Expand All @@ -244,6 +254,15 @@ public function setFile(File $file)
abstract protected function handle($file);


/**
* handle delete file
*
* @param File $file
* @return mixed
*/
abstract protected function handleDelete(File $file);


/********** Getters & Setters **********/

/**
Expand Down
1 change: 1 addition & 0 deletions src/Facades/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @method static getFileName()
* @method static BaseType fetchProperties(array $config = null)
* @method static BaseType type($type = null)
* @method static boolean delete($filename = null)
*/
class File extends Facade
{
Expand Down
15 changes: 15 additions & 0 deletions src/Types/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace AliGhale\FileManager\Types;

use AliGhale\FileManager\BaseType;
use AliGhale\FileManager\Models\File as FileModel;
use Illuminate\Support\Facades\File as FileFacade;

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

return $this;
}


protected function handleDelete(FileModel $file)
{
$path = $file->base_path . $file->file_name;
if ($file->private) {
$path = storage_path($path);
} else {
$path = public_path($path);
}

return FileFacade::delete($path);
}
}
38 changes: 38 additions & 0 deletions src/Types/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace AliGhale\FileManager\Types;

use AliGhale\FileManager\BaseType;
use AliGhale\FileManager\Models\File;
use Illuminate\Support\Facades\File as FileFacade;

class Image extends BaseType
{
Expand All @@ -31,6 +33,42 @@ protected function handle($file)
}


protected function handleDelete(File $file)
{
if (is_null($this->getSizes())) {
if ($sizes = $this->getConfig("sizes"))
$this->setSizes($sizes);
else
$this->setSizes(["16", "24", "32", "64", "128"]);
}

if (is_null($this->getThumbSize())) {
if (!$thumb = $this->getConfig("thumb"))
$this->setThumbSize($thumb);
else
$this->setThumbSize("128");
}

$sizes = $this->getSizes();
foreach ($sizes as $size) {
$sizePath = $file->base_path . "{$size}/";
$sizePath = $sizePath . $file->file_name;
if ($file->private) {
$sizePath = storage_path($sizePath);
} else {
$sizePath = public_path($sizePath);
}

FileFacade::delete($sizePath);
}

FileFacade::delete($file->base_path . "thumb/" . $file->file_name);
FileFacade::delete($file->base_path . "original/" . $file->file_name);

return true;
}


/**
* resize image and return specific array of images
*
Expand Down

0 comments on commit 79feba6

Please sign in to comment.