Skip to content

A free open source IT asset/license management system

Move file deletes out of the Actions and into boot methods

snipe1617d2c9669f
diff --git a/app/Actions/Categories/DestroyCategoryAction.php b/app/Actions/Categories/DestroyCategoryAction.php
index 469fe60387..bc582e1649 100644
--- a/app/Actions/Categories/DestroyCategoryAction.php
+++ b/app/Actions/Categories/DestroyCategoryAction.php
@@ -9,7 +9,6 @@ use App\Exceptions\ItemStillHasComponents;
use App\Exceptions\ItemStillHasConsumables;
use App\Exceptions\ItemStillHasLicenses;
use App\Models\Category;
-use Illuminate\Support\Facades\Storage;
class DestroyCategoryAction
{
@@ -51,7 +50,8 @@ class DestroyCategoryAction
throw new ItemStillHasAssetModels($category);
}
- Storage::disk('public')->delete('categories'.'/'.$category->image);
+ // Image file cleanup lives on the model's forceDeleted hook so
+ // soft-delete + restore preserves the image reference.
$category->delete();
return true;
diff --git a/app/Actions/Companies/DestroyCompanyAction.php b/app/Actions/Companies/DestroyCompanyAction.php
index 595f739ff8..b46305e049 100644
--- a/app/Actions/Companies/DestroyCompanyAction.php
+++ b/app/Actions/Companies/DestroyCompanyAction.php
@@ -2,7 +2,6 @@
namespace App\Actions\Companies;
-use App\Enums\FileStorage;
use App\Exceptions\ItemStillHasAccessories;
use App\Exceptions\ItemStillHasAssets;
use App\Exceptions\ItemStillHasChildCompanies;
@@ -11,8 +10,6 @@ use App\Exceptions\ItemStillHasConsumables;
use App\Exceptions\ItemStillHasLicenses;
use App\Exceptions\ItemStillHasUsers;
use App\Models\Company;
-use Illuminate\Support\Facades\Log;
-use Illuminate\Support\Facades\Storage;
class DestroyCompanyAction
{
@@ -68,14 +65,8 @@ class DestroyCompanyAction
throw new ItemStillHasChildCompanies($company);
}
- if ($company->image) {
- try {
- Storage::disk('public')->delete(FileStorage::Companies->publicPath().$company->image);
- } catch (\Exception $e) {
- Log::info($e->getMessage());
- }
- }
-
+ // Image file cleanup lives on the model's forceDeleted hook so
+ // soft-delete + restore preserves the image reference.
$company->delete();
return true;
diff --git a/app/Actions/Departments/DestroyDepartmentAction.php b/app/Actions/Departments/DestroyDepartmentAction.php
index d9bb955e3a..7ba0c6cd0b 100644
--- a/app/Actions/Departments/DestroyDepartmentAction.php
+++ b/app/Actions/Departments/DestroyDepartmentAction.php
@@ -2,11 +2,8 @@
namespace App\Actions\Departments;
-use App\Enums\FileStorage;
use App\Exceptions\ItemStillHasUsers;
use App\Models\Department;
-use Illuminate\Support\Facades\Log;
-use Illuminate\Support\Facades\Storage;
class DestroyDepartmentAction
{
@@ -21,14 +18,8 @@ class DestroyDepartmentAction
throw new ItemStillHasUsers($department);
}
- if ($department->image) {
- try {
- Storage::disk('public')->delete(FileStorage::Departments->publicPath().$department->image);
- } catch (\Exception $e) {
- Log::info($e->getMessage());
- }
- }
-
+ // Image file cleanup lives on the model's forceDeleted hook so
+ // soft-delete + restore preserves the image reference.
$department->delete();
return true;
diff --git a/app/Actions/Manufacturers/DestroyManufacturerAction.php b/app/Actions/Manufacturers/DestroyManufacturerAction.php
index 6e9f2246b7..ee43ed4804 100644
--- a/app/Actions/Manufacturers/DestroyManufacturerAction.php
+++ b/app/Actions/Manufacturers/DestroyManufacturerAction.php
@@ -2,15 +2,12 @@
namespace App\Actions\Manufacturers;
-use App\Enums\FileStorage;
use App\Exceptions\ItemStillHasAccessories;
use App\Exceptions\ItemStillHasAssets;
use App\Exceptions\ItemStillHasComponents;
use App\Exceptions\ItemStillHasConsumables;
use App\Exceptions\ItemStillHasLicenses;
use App\Models\Manufacturer;
-use Illuminate\Support\Facades\Log;
-use Illuminate\Support\Facades\Storage;
class DestroyManufacturerAction
{
@@ -47,16 +44,9 @@ class DestroyManufacturerAction
throw new ItemStillHasLicenses($manufacturer);
}
- if ($manufacturer->image) {
- try {
- Storage::disk('public')->delete(FileStorage::Manufacturers->publicPath().$manufacturer->image);
- } catch (\Exception $e) {
- Log::info($e);
- }
- }
-
+ // Image file cleanup lives on the model's forceDeleted hook so
+ // soft-delete + restore preserves the image reference.
$manufacturer->delete();
- // dd($manufacturer);
return true;
}
diff --git a/app/Actions/Suppliers/DestroySupplierAction.php b/app/Actions/Suppliers/DestroySupplierAction.php
index f58d3ef171..b72701fe77 100644
--- a/app/Actions/Suppliers/DestroySupplierAction.php
+++ b/app/Actions/Suppliers/DestroySupplierAction.php
@@ -2,7 +2,6 @@
namespace App\Actions\Suppliers;
-use App\Enums\FileStorage;
use App\Exceptions\ItemStillHasAccessories;
use App\Exceptions\ItemStillHasAssets;
use App\Exceptions\ItemStillHasComponents;
@@ -10,8 +9,6 @@ use App\Exceptions\ItemStillHasConsumables;
use App\Exceptions\ItemStillHasLicenses;
use App\Exceptions\ItemStillHasMaintenances;
use App\Models\Supplier;
-use Illuminate\Support\Facades\Log;
-use Illuminate\Support\Facades\Storage;
class DestroySupplierAction
{
@@ -57,14 +54,8 @@ class DestroySupplierAction
throw new ItemStillHasComponents($supplier);
}
- if ($supplier->image) {
- try {
- Storage::disk('public')->delete(FileStorage::Suppliers->publicPath().$supplier->image);
- } catch (\Exception $e) {
- Log::info($e->getMessage());
- }
- }
-
+ // Image file cleanup lives on the model's forceDeleted hook so
+ // soft-delete + restore preserves the image reference.
$supplier->delete();
return true;
diff --git a/app/Models/Category.php b/app/Models/Category.php
index 511a630b66..0d16c13771 100755
--- a/app/Models/Category.php
+++ b/app/Models/Category.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Enums\FileStorage;
use App\Http\Traits\TwoColumnUniqueUndeletedTrait;
use App\Models\Traits\Searchable;
use App\Presenters\CategoryPresenter;
@@ -11,6 +12,8 @@ use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Watson\Validating\ValidatingTrait;
@@ -112,6 +115,23 @@ class Category extends SnipeModel
'models_count',
];
+ /**
+ * On hard-delete, wipe the image file. Soft-delete leaves it alone so a
+ * subsequent restore comes back with its image intact.
+ */
+ protected static function booted(): void
+ {
+ static::forceDeleted(function (self $category) {
+ if ($category->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Categories->publicPath().$category->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ });
+ }
+
/**
* Checks if category can be deleted
*
diff --git a/app/Models/Company.php b/app/Models/Company.php
index b685c31444..f3a40ccef6 100644
--- a/app/Models/Company.php
+++ b/app/Models/Company.php
@@ -461,6 +461,23 @@ final class Company extends SnipeModel
|| ! empty(self::getCurrentUserCompanyIds());
}
+ /**
+ * On hard-delete, wipe the image file. Soft-delete leaves it alone so a
+ * subsequent restore comes back with its image intact.
+ */
+ protected static function booted(): void
+ {
+ self::forceDeleted(function (self $company) {
+ if ($company->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Companies->publicPath().$company->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ });
+ }
+
/**
* Checks if company can be deleted
*
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 6cac021642..69bfc39d13 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -19,6 +19,7 @@ use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
@@ -134,6 +135,23 @@ class Consumable extends SnipeModel
'orders' => ['order_number'],
];
+ /**
+ * On hard-delete, wipe the image file. Soft-delete leaves it alone so a
+ * subsequent restore comes back with its image intact.
+ */
+ protected static function booted(): void
+ {
+ static::forceDeleted(function (self $consumable) {
+ if ($consumable->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Consumables->publicPath().$consumable->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ });
+ }
+
/**
* Normalize the requestable form input so an empty string from an
* unchecked checkbox lands as false rather than a truthy "0" cast
diff --git a/app/Models/Department.php b/app/Models/Department.php
index 48b62bd5f1..d768a3799e 100644
--- a/app/Models/Department.php
+++ b/app/Models/Department.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Enums\FileStorage;
use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\HasUploads;
@@ -14,6 +15,8 @@ use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
class Department extends SnipeModel
@@ -96,6 +99,23 @@ class Department extends SnipeModel
'manager' => ['first_name', 'last_name', 'display_name'],
];
+ /**
+ * On hard-delete, wipe the image file. Soft-delete leaves it alone so a
+ * subsequent restore comes back with its image intact.
+ */
+ protected static function booted(): void
+ {
+ static::forceDeleted(function (self $department) {
+ if ($department->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Departments->publicPath().$department->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ });
+ }
+
public function isDeletable()
{
return Gate::allows('delete', $this) && (($this->users_count ?? $this->users()->count()) === 0);
diff --git a/app/Models/Manufacturer.php b/app/Models/Manufacturer.php
index 8570723fbc..cac36fd3f7 100755
--- a/app/Models/Manufacturer.php
+++ b/app/Models/Manufacturer.php
@@ -2,12 +2,15 @@
namespace App\Models;
+use App\Enums\FileStorage;
use App\Models\Traits\Searchable;
use App\Presenters\ManufacturerPresenter;
use App\Presenters\Presentable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
class Manufacturer extends SnipeModel
@@ -82,6 +85,23 @@ class Manufacturer extends SnipeModel
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
+ /**
+ * On hard-delete, wipe the image file. Soft-delete leaves it alone so a
+ * subsequent restore comes back with its image intact.
+ */
+ protected static function booted(): void
+ {
+ static::forceDeleted(function (self $manufacturer) {
+ if ($manufacturer->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Manufacturers->publicPath().$manufacturer->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ });
+ }
+
public function isDeletable()
{
return Gate::allows('delete', $this)
diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php
index 7506706479..c208cbfbf6 100755
--- a/app/Models/Supplier.php
+++ b/app/Models/Supplier.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Enums\FileStorage;
use App\Http\Traits\UniqueUndeletedTrait;
use App\Models\Traits\HasUploads;
use App\Models\Traits\Loggable;
@@ -12,6 +13,8 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
class Supplier extends SnipeModel
@@ -106,6 +109,18 @@ class Supplier extends SnipeModel
Consumable::where('default_supplier_id', $supplier->id)->update(['default_supplier_id' => null]);
Component::where('default_supplier_id', $supplier->id)->update(['default_supplier_id' => null]);
});
+
+ // On hard-delete, wipe the image file. Soft-delete leaves it
+ // alone so a subsequent restore comes back with its image intact.
+ static::forceDeleted(function (self $supplier) {
+ if ($supplier->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Suppliers->publicPath().$supplier->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ });
}
/**
diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php
index c9c80b037f..7b9a392627 100644
--- a/app/Observers/ConsumableObserver.php
+++ b/app/Observers/ConsumableObserver.php
@@ -2,7 +2,6 @@
namespace App\Observers;
-use App\Enums\FileStorage;
use App\Models\Actionlog;
use App\Models\CheckoutAcceptance;
use App\Models\Consumable;
@@ -77,14 +76,8 @@ class ConsumableObserver
}
}
- try {
- Storage::disk('public')->delete(FileStorage::Consumables->publicPath().$consumable->image);
- } catch (\Exception $e) {
- Log::info($e);
- }
-
- $consumable->image = null;
- $consumable->save();
+ // Image file cleanup lives on the Consumable model's forceDeleted
+ // hook so soft-delete + restore preserves the image reference.
$logAction = new Actionlog;
$logAction->item_type = Consumable::class;