Skip to content

A free open source IT asset/license management system

Updated more locations from hard-coded paths and singletons

snipe2608b77ee7c4
diff --git a/app/Console/Commands/BulkDelete.php b/app/Console/Commands/BulkDelete.php
index 1206fee089..e3d200c2b1 100644
--- a/app/Console/Commands/BulkDelete.php
+++ b/app/Console/Commands/BulkDelete.php
@@ -1055,10 +1055,10 @@ class BulkDelete extends Command
private function deleteAcceptanceFiles(CheckoutAcceptance $acceptance): void
{
if ($acceptance->signature_filename) {
- $this->deleteStorageFile('local', 'private_uploads/signatures/'.$acceptance->signature_filename);
+ $this->deleteStorageFile('local', FileStorage::Signatures->privateStorageKey().$acceptance->signature_filename);
}
if ($acceptance->stored_eula_file) {
- $this->deleteStorageFile('local', 'private_uploads/eula-pdfs/'.$acceptance->stored_eula_file);
+ $this->deleteStorageFile('local', FileStorage::EulaPdfs->privateStorageKey().$acceptance->stored_eula_file);
}
}
diff --git a/app/Console/Commands/CheckOrphanUploads.php b/app/Console/Commands/CheckOrphanUploads.php
index d83f79e892..1622c66b2c 100644
--- a/app/Console/Commands/CheckOrphanUploads.php
+++ b/app/Console/Commands/CheckOrphanUploads.php
@@ -2,6 +2,7 @@
namespace App\Console\Commands;
+use App\Enums\FileStorage;
use App\Models\Accessory;
use App\Models\Actionlog;
use App\Models\Asset;
@@ -294,7 +295,7 @@ class CheckOrphanUploads extends Command
->chunkById($chunk, function ($rows) use (&$missing, &$count): void {
foreach ($rows as $import) {
$count++;
- $path = 'private_uploads/imports/'.$import->file_path;
+ $path = FileStorage::Imports->privateStorageKey().$import->file_path;
if (! Storage::exists($path)) {
$missing[] = [
diff --git a/app/Console/Commands/ImportLocations.php b/app/Console/Commands/ImportLocations.php
index d757f0813c..8bfa730542 100644
--- a/app/Console/Commands/ImportLocations.php
+++ b/app/Console/Commands/ImportLocations.php
@@ -2,6 +2,7 @@
namespace App\Console\Commands;
+use App\Enums\FileStorage;
use App\Models\Location;
use Illuminate\Console\Command;
use League\Csv\Reader;
@@ -44,8 +45,8 @@ class ImportLocations extends Command
}
$filename = $this->argument('filename');
- $csv = Reader::createFromPath(storage_path('private_uploads/imports/').$filename, 'r');
- $this->info('Attempting to process: '.storage_path('private_uploads/imports/').$filename);
+ $csv = Reader::createFromPath(storage_path(FileStorage::Imports->privateStorageKey()).$filename, 'r');
+ $this->info('Attempting to process: '.storage_path(FileStorage::Imports->privateStorageKey()).$filename);
$csv->setHeaderOffset(0); // because we don't want to insert the header
$results = $csv->getRecords();
diff --git a/app/Console/Commands/Purge.php b/app/Console/Commands/Purge.php
index e57ea68870..1ab36b0852 100644
--- a/app/Console/Commands/Purge.php
+++ b/app/Console/Commands/Purge.php
@@ -3,19 +3,18 @@
namespace App\Console\Commands;
use App\Enums\ActionType;
+use App\Enums\FileStorage;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Category;
use App\Models\CheckoutAcceptance;
-use App\Models\CheckoutRequest;
use App\Models\Company;
use App\Models\Component;
use App\Models\Consumable;
use App\Models\Department;
use App\Models\License;
use App\Models\Location;
-use App\Models\Maintenance;
use App\Models\Manufacturer;
use App\Models\Supplier;
use App\Models\User;
@@ -301,29 +300,29 @@ class Purge extends Command
private function deleteImageFiles(string $modelClass, string $table, Collection $ids): void
{
// Image/avatar files stored on the public disk, keyed by parent
- // model. Value is `column_name => public-disk subpath`. Purge
+ // model. Value is `column_name => FileStorage case`. Purge
// reads the filename off each trashed parent row and unlinks
- // `{subpath}/{column_value}` from the public disk.
+ // `{case->publicPath()}{column_value}` from the public disk.
$imageFiles = [
- User::class => ['avatar' => 'avatars'],
- Asset::class => ['image' => 'assets'],
- AssetModel::class => ['image' => 'models'],
- Accessory::class => ['image' => 'accessories'],
- Category::class => ['image' => 'categories'],
- Company::class => ['image' => 'companies'],
- Component::class => ['image' => 'components'],
- Consumable::class => ['image' => 'consumables'],
- Department::class => ['image' => 'departments'],
- Location::class => ['image' => 'locations'],
- Manufacturer::class => ['image' => 'manufacturers'],
- Supplier::class => ['image' => 'suppliers'],
+ User::class => ['avatar' => FileStorage::Avatars],
+ Asset::class => ['image' => FileStorage::Assets],
+ AssetModel::class => ['image' => FileStorage::Models],
+ Accessory::class => ['image' => FileStorage::Accessories],
+ Category::class => ['image' => FileStorage::Categories],
+ Company::class => ['image' => FileStorage::Companies],
+ Component::class => ['image' => FileStorage::Components],
+ Consumable::class => ['image' => FileStorage::Consumables],
+ Department::class => ['image' => FileStorage::Departments],
+ Location::class => ['image' => FileStorage::Locations],
+ Manufacturer::class => ['image' => FileStorage::Manufacturers],
+ Supplier::class => ['image' => FileStorage::Suppliers],
];
if (! array_key_exists($modelClass, $imageFiles)) {
return;
}
- foreach ($imageFiles[$modelClass] as $column => $subpath) {
+ foreach ($imageFiles[$modelClass] as $column => $case) {
$filenames = DB::table($table)
->whereIn('id', $ids)
->pluck($column)
@@ -332,7 +331,7 @@ class Purge extends Command
foreach ($filenames as $filename) {
try {
- $key = trim($subpath, '/').'/'.basename($filename);
+ $key = $case->publicPath().basename($filename);
if (Storage::disk('public')->exists($key)) {
Storage::disk('public')->delete($key);
}
@@ -372,21 +371,17 @@ class Purge extends Command
// the parent model of the file. These are the contracts, receipts,
// photos, etc. tracked in action_logs with action_type = 'uploaded'.
// Not done at soft-delete time so restoring a soft-deleted row
- // brings the files back with it.
- $uploadRoots = [
- Accessory::class => 'private_uploads/accessories',
- Asset::class => 'private_uploads/assets',
- AssetModel::class => 'private_uploads/models',
- Company::class => 'private_uploads/companies',
- Component::class => 'private_uploads/components',
- Consumable::class => 'private_uploads/consumables',
- Department::class => 'private_uploads/departments',
- License::class => 'private_uploads/licenses',
- Location::class => 'private_uploads/locations',
- Maintenance::class => 'private_uploads/maintenances',
- Supplier::class => 'private_uploads/suppliers',
- User::class => 'private_uploads/users',
- ];
+ // brings the files back with it. Built by iterating FileStorage
+ // so adding a new case there wires it up here automatically.
+ // Only private-scope cases that resolve to a single Eloquent
+ // model show up (Audits / Signatures / EulaPdfs / Imports /
+ // Backups return null from modelClass() and are skipped).
+ $uploadRoots = [];
+ foreach (FileStorage::cases() as $case) {
+ if ($case->hasPrivateScope() && ($model = $case->modelClass()) !== null) {
+ $uploadRoots[$model] = $case->privateStorageKey();
+ }
+ }
$logs = DB::table('action_logs')
->select('action_type', 'item_type', 'filename', 'accept_signature')
@@ -407,15 +402,15 @@ class Purge extends Command
// query-builder rows (no Eloquent).
if (! empty($log->filename)) {
if ($log->action_type === ActionType::Accepted->value || $log->action_type === ActionType::Declined->value) {
- $paths[] = 'private_uploads/eula-pdfs/'.$log->filename;
+ $paths[] = FileStorage::EulaPdfs->privateStorageKey().$log->filename;
} elseif ($log->action_type === ActionType::Audit->value) {
- $paths[] = 'private_uploads/audits/'.$log->filename;
+ $paths[] = FileStorage::Audits->privateStorageKey().$log->filename;
} elseif ($log->item_type && isset($uploadRoots[$log->item_type])) {
- $paths[] = rtrim($uploadRoots[$log->item_type], '/').'/'.$log->filename;
+ $paths[] = $uploadRoots[$log->item_type].$log->filename;
}
}
if (! empty($log->accept_signature)) {
- $paths[] = 'private_uploads/signatures/'.$log->accept_signature;
+ $paths[] = FileStorage::Signatures->privateStorageKey().$log->accept_signature;
}
}
@@ -434,8 +429,8 @@ class Purge extends Command
{
$privateFileColumns = [
CheckoutAcceptance::class => [
- 'signature_filename' => 'private_uploads/signatures',
- 'stored_eula_file' => 'private_uploads/eula-pdfs',
+ 'signature_filename' => FileStorage::Signatures->privateStorageKey(),
+ 'stored_eula_file' => FileStorage::EulaPdfs->privateStorageKey(),
],
];
diff --git a/app/Console/Commands/PurgeEulaPDFs.php b/app/Console/Commands/PurgeEulaPDFs.php
index 7760cf79f7..c71a88c420 100644
--- a/app/Console/Commands/PurgeEulaPDFs.php
+++ b/app/Console/Commands/PurgeEulaPDFs.php
@@ -2,6 +2,7 @@
namespace App\Console\Commands;
+use App\Enums\FileStorage;
use App\Models\CheckoutAcceptance;
use Carbon\Carbon;
use Illuminate\Console\Command;
@@ -42,8 +43,8 @@ class PurgeEulaPDFs extends Command
}
$interval_date = Carbon::now()->subDays($before);
- $signature_path = 'private_uploads/signatures/';
- $eula_path = 'private_uploads/eula-pdfs/';
+ $signature_path = FileStorage::Signatures->privateStorageKey();
+ $eula_path = FileStorage::EulaPdfs->privateStorageKey();
if (! Storage::exists($eula_path)) {
$this->fail('The storage directory "'.$eula_path.'" does not exist. No EULA files will be deleted.');
diff --git a/app/Enums/FileStorage.php b/app/Enums/FileStorage.php
index 286840c37f..f246c13270 100644
--- a/app/Enums/FileStorage.php
+++ b/app/Enums/FileStorage.php
@@ -147,13 +147,71 @@ enum FileStorage: string
*/
public function privateDir(): ?string
{
- if (!$this->hasPrivateScope()) {
+ if (! $this->hasPrivateScope()) {
return null;
}
return match ($this) {
self::Backups => 'storage/app/backups',
- default => 'storage/private_uploads/' . $this->value,
+ default => 'storage/private_uploads/'.$this->value,
+ };
+ }
+
+ /**
+ * Storage facade key for the default disk (local root is storage/app),
+ * with trailing slash. Suitable for appending a filename for
+ * `Storage::put(...)`, `Storage::delete(...)`, etc. Backups return
+ * `backups/` because they live directly under storage/app, every other
+ * private case returns `private_uploads/<name>/`.
+ */
+ public function privateStorageKey(): ?string
+ {
+ if (! $this->hasPrivateScope()) {
+ return null;
+ }
+
+ return match ($this) {
+ self::Backups => 'backups/',
+ default => 'private_uploads/'.$this->value.'/',
+ };
+ }
+
+ /**
+ * The Eloquent model class that owns files stored under this case, or
+ * null when no single parent model does. Audits, Signatures, EulaPdfs,
+ * Imports, and Backups return null because their files are not keyed
+ * to a single parent (audits are tied to action_logs across all
+ * assets, signatures / EULAs ride on acceptance records, imports are
+ * admin-queued uploads not owned by a model, Backups is Spatie
+ * output). Barcodes also returns null because that directory stores
+ * generated label PNGs rather than user uploads.
+ *
+ * Closes the loop with `HasImageUpload`, which declares the inverse
+ * (model -> FileStorage case) via `static::fileStorage()`.
+ *
+ * Returns a class string rather than a class-string<Model> union to
+ * avoid pulling the Eloquent namespace into the enum layer.
+ *
+ * @return class-string|null
+ */
+ public function modelClass(): ?string
+ {
+ return match ($this) {
+ self::Accessories => \App\Models\Accessory::class,
+ self::Assets => \App\Models\Asset::class,
+ self::Avatars, self::Users => \App\Models\User::class,
+ self::Categories => \App\Models\Category::class,
+ self::Companies => \App\Models\Company::class,
+ self::Components => \App\Models\Component::class,
+ self::Consumables => \App\Models\Consumable::class,
+ self::Departments => \App\Models\Department::class,
+ self::Licenses => \App\Models\License::class,
+ self::Locations => \App\Models\Location::class,
+ self::Maintenances => \App\Models\Maintenance::class,
+ self::Manufacturers => \App\Models\Manufacturer::class,
+ self::Models => \App\Models\AssetModel::class,
+ self::Suppliers => \App\Models\Supplier::class,
+ self::Audits, self::Backups, self::Barcodes, self::EulaPdfs, self::Imports, self::Signatures => null,
};
}
@@ -213,7 +271,7 @@ enum FileStorage: string
{
return array_values(array_filter(
self::publicDirs(),
- fn(string $dir) => !in_array(basename($dir), self::SKIP_IN_RESTORE_PRUNE, true),
+ fn (string $dir) => ! in_array(basename($dir), self::SKIP_IN_RESTORE_PRUNE, true),
));
}
@@ -229,7 +287,7 @@ enum FileStorage: string
{
return array_filter(
self::privateDirs(),
- fn(string $dir, int|string $key) => !in_array(
+ fn (string $dir, int|string $key) => ! in_array(
basename(is_string($key) ? $key : $dir),
self::SKIP_IN_RESTORE_PRUNE,
true,
diff --git a/app/Http/Controllers/Account/AcceptanceController.php b/app/Http/Controllers/Account/AcceptanceController.php
index ae05665a23..fc5c610322 100644
--- a/app/Http/Controllers/Account/AcceptanceController.php
+++ b/app/Http/Controllers/Account/AcceptanceController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Account;
+use App\Enums\FileStorage;
use App\Events\CheckoutAccepted;
use App\Events\CheckoutDeclined;
use App\Helpers\Helper;
@@ -151,15 +152,15 @@ class AcceptanceController extends Controller
/**
* Check for the signature directory
*/
- if (! Storage::exists('private_uploads/signatures')) {
- Storage::makeDirectory('private_uploads/signatures', 775);
+ if (! Storage::exists(rtrim(FileStorage::Signatures->privateStorageKey(), '/'))) {
+ Storage::makeDirectory(rtrim(FileStorage::Signatures->privateStorageKey(), '/'), 775);
}
/**
* Check for the eula-pdfs directory
*/
- if (! Storage::exists('private_uploads/eula-pdfs')) {
- Storage::makeDirectory('private_uploads/eula-pdfs', 775);
+ if (! Storage::exists(rtrim(FileStorage::EulaPdfs->privateStorageKey(), '/'))) {
+ Storage::makeDirectory(rtrim(FileStorage::EulaPdfs->privateStorageKey(), '/'), 775);
}
$item = $acceptance->checkoutable_type::find($acceptance->checkoutable_id);
@@ -213,7 +214,7 @@ class AcceptanceController extends Controller
// evidence file did not exist. Refuse to advance when the
// write did not land. Reported by Christopher Finks
// (christopherfi-dev) on 2026-08-02.
- if (! Storage::put('private_uploads/signatures/'.$sig_filename, (string) $decoded_image)) {
+ if (! Storage::put(FileStorage::Signatures->privateStorageKey().$sig_filename, (string) $decoded_image)) {
Log::warning('Acceptance signature write failed for '.$sig_filename);
return redirect()->back()->with('error', trans('admin/users/message.accept_signature_write_failed'));
@@ -295,7 +296,7 @@ class AcceptanceController extends Controller
// evidence file did not exist. Refuse to advance when the
// write did not land. Reported by Christopher Finks
// (christopherfi-dev) on 2026-08-02.
- if (! Storage::put('private_uploads/eula-pdfs/'.$pdf_filename, $pdf_content)) {
+ if (! Storage::put(FileStorage::EulaPdfs->privateStorageKey().$pdf_filename, $pdf_content)) {
Log::warning('Acceptance PDF write failed for '.$pdf_filename);
return redirect()->back()->with('error', trans('admin/users/message.accept_pdf_write_failed'));
diff --git a/app/Http/Controllers/ActionlogController.php b/app/Http/Controllers/ActionlogController.php
index 059275e6cc..d17fd6714b 100644
--- a/app/Http/Controllers/ActionlogController.php
+++ b/app/Http/Controllers/ActionlogController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
+use App\Enums\FileStorage;
use App\Helpers\Helper;
use App\Models\Actionlog;
use Illuminate\Http\RedirectResponse;
@@ -27,7 +28,7 @@ class ActionlogController extends Controller
switch (config("filesystems.disks.$disk.driver")) {
case 's3':
- $file = 'private_uploads/signatures/'.$filename;
+ $file = FileStorage::Signatures->privateStorageKey().$filename;
return redirect()->away(Storage::disk($disk)->temporaryUrl($file, now()->addMinutes(5)));
default:
@@ -55,10 +56,10 @@ class ActionlogController extends Controller
$this->authorize('view', $actionlog->user);
if (config('filesystems.default') == 's3_private') {
- return redirect()->away(Storage::disk('s3_private')->temporaryUrl('private_uploads/eula-pdfs/'.$filename, now()->addMinutes(5)));
+ return redirect()->away(Storage::disk('s3_private')->temporaryUrl(FileStorage::EulaPdfs->privateStorageKey().$filename, now()->addMinutes(5)));
}
- if (Storage::exists('private_uploads/eula-pdfs/'.$filename)) {
+ if (Storage::exists(FileStorage::EulaPdfs->privateStorageKey().$filename)) {
if (request()->input('inline') == 'true') {
return response()->file(config('app.private_uploads').'/eula-pdfs/'.$filename);
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index 1798646c6b..9821475973 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
+use App\Enums\FileStorage;
use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
@@ -1849,7 +1850,7 @@ class AssetsController extends Controller
$file_name = null;
// Legacy `image` posts are aliased to `file[0]` in UploadFileRequest::prepareForValidation.
if ($request->hasFile('file.0')) {
- $file_name = $request->handleFile('private_uploads/audits/', 'audit-'.$asset->id, $request->file('file.0'));
+ $file_name = $request->handleFile(FileStorage::Audits->privateStorageKey(), 'audit-'.$asset->id, $request->file('file.0'));
$payload['image'] = $file_name;
}
diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.php
index 5cfc8d8047..c61e31bb92 100644
--- a/app/Http/Controllers/Api/ImportController.php
+++ b/app/Http/Controllers/Api/ImportController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
+use App\Enums\FileStorage;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\ItemImportRequest;
@@ -53,7 +54,7 @@ class ImportController extends Controller
$files = Request::file('files');
// Path inside the private disk. Under PRIVATE_FILESYSTEM_DISK=local this resolves to
// storage/private_uploads/imports, and under s3_private it lands at <bucket>/private_uploads/imports
- $diskPath = 'private_uploads/imports';
+ $diskPath = rtrim(FileStorage::Imports->privateStorageKey(), '/');
$results = [];
$detector = new EncodingDetector;
@@ -478,7 +479,7 @@ class ImportController extends Controller
// the default (private) disk. The pre-fix path 'imports/'
// missed the 'private_uploads/' prefix and silently
// no-op'd on both drivers.
- Storage::delete('private_uploads/imports/'.$import->file_path);
+ Storage::delete(FileStorage::Imports->privateStorageKey().$import->file_path);
$import->delete();
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.import.file_delete_success')));
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php
index 660cc877e2..168ad3b46a 100755
--- a/app/Http/Controllers/Assets/AssetsController.php
+++ b/app/Http/Controllers/Assets/AssetsController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Assets;
+use App\Enums\FileStorage;
use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
@@ -506,7 +507,7 @@ class AssetsController extends Controller
if ($request->filled('image_delete')) {
try {
- unlink(public_path().'/uploads/assets/'.basename($asset->image));
+ Storage::disk('public')->delete(FileStorage::Assets->publicPath().basename($asset->image));
$asset->image = '';
} catch (\Exception $e) {
Log::info($e);
@@ -684,20 +685,16 @@ class AssetsController extends Controller
if ($asset) {
$size = Helper::barcodeDimensions($settings->label2_2d_type);
- $qr_file = public_path().'/uploads/barcodes/qr-'.str_slug($asset->asset_tag).'-'.str_slug($asset->id).'.png';
+ $qr_key = FileStorage::Barcodes->publicPath().'qr-'.str_slug($asset->asset_tag).'-'.str_slug($asset->id).'.png';
if (isset($asset->id, $asset->asset_tag)) {
- if (file_exists($qr_file)) {
- $header = ['Content-type' => 'image/png'];
-
- return response()->file($qr_file, $header);
- } else {
+ if (! Storage::disk('public')->exists($qr_key)) {
$barcode = new Barcode;
$barcode_obj = $barcode->getBarcodeObj($settings->label2_2d_type, route('hardware.show', $asset->id), $size['height'], $size['width'], 'black', [-2, -2, -2, -2]);
- file_put_contents($qr_file, $barcode_obj->getPngData());
-
- return response($barcode_obj->getPngData())->header('Content-type', 'image/png');
+ Storage::disk('public')->put($qr_key, $barcode_obj->getPngData());
}
+
+ return Storage::disk('public')->response($qr_key, basename($qr_key), ['Content-type' => 'image/png']);
}
}
@@ -730,29 +727,25 @@ class AssetsController extends Controller
// tags.
$this->authorize('view', $asset);
- $barcode_file = public_path().'/uploads/barcodes/'.str_slug($settings->label2_1d_type).'-'.str_slug($asset->asset_tag).'.png';
+ $barcode_key = FileStorage::Barcodes->publicPath().str_slug($settings->label2_1d_type).'-'.str_slug($asset->asset_tag).'.png';
if (isset($asset->id, $asset->asset_tag)) {
- if (file_exists($barcode_file)) {
- $header = ['Content-type' => 'image/png'];
-
- return response()->file($barcode_file, $header);
- } else {
+ if (! Storage::disk('public')->exists($barcode_key)) {
// Calculate barcode width in pixel based on label width (inch)
$barcode_width = ($settings->labels_width - $settings->labels_display_sgutter) * 200.000000000001;
$barcode = new Barcode;
try {
$barcode_obj = $barcode->getBarcodeObj($settings->label2_1d_type, $asset->asset_tag, ($barcode_width < 300 ? $barcode_width : 300), 50);
- file_put_contents($barcode_file, $barcode_obj->getPngData());
-
- return response($barcode_obj->getPngData())->header('Content-type', 'image/png');
+ Storage::disk('public')->put($barcode_key, $barcode_obj->getPngData());
} catch (\Exception|TypeError $e) {
Log::debug('The barcode format is invalid.');
- return response(file_get_contents(public_path('uploads/barcodes/invalid_barcode.gif')))->header('Content-type', 'image/gif');
+ return response(Storage::disk('public')->get(FileStorage::Barcodes->publicPath().'invalid_barcode.gif'))->header('Content-type', 'image/gif');
}
}
+
+ return Storage::disk('public')->response($barcode_key, basename($barcode_key), ['Content-type' => 'image/png']);
}
}
@@ -988,7 +981,7 @@ class AssetsController extends Controller
$file_name = null;
// Field name changed from `image` to the `file[]` shape UploadFileRequest actually validates.
if ($request->hasFile('file.0')) {
- $file_name = $request->handleFile('private_uploads/audits/', 'audit-'.$asset->id, $request->file('file.0'));
+ $file_name = $request->handleFile(FileStorage::Audits->privateStorageKey(), 'audit-'.$asset->id, $request->file('file.0'));
}
$asset->logAudit($request->input('note'), $request->input('location_id'), $file_name, $originalValues);
diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php
index e8566967f1..fb6b87fa2f 100644
--- a/app/Http/Controllers/Assets/BulkAssetsController.php
+++ b/app/Http/Controllers/Assets/BulkAssetsController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Assets;
+use App\Enums\FileStorage;
use App\Events\CheckoutableCheckedIn;
use App\Events\CheckoutablesCheckedOutInBulk;
use App\Helpers\Helper;
@@ -1168,7 +1169,7 @@ class BulkAssetsController extends Controller
$file_name = null;
if ($request->hasFile('file.0')) {
- $file_name = $request->handleFile('private_uploads/audits/', 'audit-'.$asset->id, $request->file('file.0'));
+ $file_name = $request->handleFile(FileStorage::Audits->privateStorageKey(), 'audit-'.$asset->id, $request->file('file.0'));
}
$asset->logAudit(
diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php
index dfb64b25fd..d938b6a75e 100644
--- a/app/Http/Controllers/Controller.php
+++ b/app/Http/Controllers/Controller.php
@@ -23,6 +23,7 @@
namespace App\Http\Controllers;
+use App\Enums\FileStorage;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\AssetModel;
@@ -66,23 +67,6 @@ abstract class Controller extends BaseController
'users' => User::class,
];
- public static $map_storage_path = [
- 'accessories' => 'private_uploads/accessories/',
- 'maintenances' => 'private_uploads/maintenances/',
- 'assets' => 'private_uploads/assets/',
- 'audits' => 'private_uploads/audits/',
- 'departments' => 'private_uploads/departments/',
- 'companies' => 'private_uploads/companies/',
- 'components' => 'private_uploads/components/',
- 'consumables' => 'private_uploads/consumables/',
- 'hardware' => 'private_uploads/assets/',
- 'licenses' => 'private_uploads/licenses/',
- 'locations' => 'private_uploads/locations/',
- 'models' => 'private_uploads/models/',
- 'suppliers' => 'private_uploads/suppliers/',
- 'users' => 'private_uploads/users/',
- ];
-
public static $map_file_prefix = [
'accessories' => 'accessory',
'maintenances' => 'maintenance',
@@ -173,7 +157,22 @@ abstract class Controller extends BaseController
*/
public static function getMapStoragePath(): array
{
- return static::$map_storage_path;
+ return [
+ 'accessories' => FileStorage::Accessories->privateStorageKey(),
+ 'maintenances' => FileStorage::Maintenances->privateStorageKey(),
+ 'assets' => FileStorage::Assets->privateStorageKey(),
+ 'audits' => FileStorage::Audits->privateStorageKey(),
+ 'departments' => FileStorage::Departments->privateStorageKey(),
+ 'companies' => FileStorage::Companies->privateStorageKey(),
+ 'components' => FileStorage::Components->privateStorageKey(),
+ 'consumables' => FileStorage::Consumables->privateStorageKey(),
+ 'hardware' => FileStorage::Assets->privateStorageKey(),
+ 'licenses' => FileStorage::Licenses->privateStorageKey(),
+ 'locations' => FileStorage::Locations->privateStorageKey(),
+ 'models' => FileStorage::Models->privateStorageKey(),
+ 'suppliers' => FileStorage::Suppliers->privateStorageKey(),
+ 'users' => FileStorage::Users->privateStorageKey(),
+ ];
}
/**
diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php
index 239d12df5a..52a578ea02 100755
--- a/app/Http/Controllers/ProfileController.php
+++ b/app/Http/Controllers/ProfileController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
+use App\Enums\FileStorage;
use App\Http\Requests\ImageUploadRequest;
use App\Models\Actionlog;
use App\Models\Asset;
@@ -292,10 +293,10 @@ class ProfileController extends Controller
}
if (config('filesystems.default') == 's3_private') {
- return redirect()->away(Storage::disk('s3_private')->temporaryUrl('private_uploads/eula-pdfs/'.$filename, now()->addMinutes(5)));
+ return redirect()->away(Storage::disk('s3_private')->temporaryUrl(FileStorage::EulaPdfs->privateStorageKey().$filename, now()->addMinutes(5)));
}
- if (Storage::exists('private_uploads/eula-pdfs/'.$filename)) {
+ if (Storage::exists(FileStorage::EulaPdfs->privateStorageKey().$filename)) {
return response()->download(config('app.private_uploads').'/eula-pdfs/'.$filename);
}
diff --git a/app/Http/Controllers/QrCodeController.php b/app/Http/Controllers/QrCodeController.php
index fd096dc069..dab2e1fba1 100644
--- a/app/Http/Controllers/QrCodeController.php
+++ b/app/Http/Controllers/QrCodeController.php
@@ -2,11 +2,13 @@
namespace App\Http\Controllers;
+use App\Enums\FileStorage;
use App\Helpers\Helper;
use App\Models\Setting;
use Com\Tecnick\Barcode\Barcode;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class QrCodeController extends Controller
@@ -31,8 +33,8 @@ class QrCodeController extends Controller
if ($settings->label2_2d_type === 'none') {
return false;
}
-
- $unavailable = fn() => abort(404, trans('general.generic_model_not_found', ['model' => trans('general.item')]));
+
+ $unavailable = fn () => abort(404, trans('general.generic_model_not_found', ['model' => trans('general.item')]));
if (! array_key_exists($object_type, self::$map_show_route)) {
$unavailable();
@@ -40,28 +42,26 @@ class QrCodeController extends Controller
$object = parent::getMapObjectType()[$object_type]::withTrashed()->find($id);
- if (!$object || !Gate::allows('view', $object)) {
+ if (! $object || ! Gate::allows('view', $object)) {
$unavailable();
}
$size = Helper::barcodeDimensions($settings->label2_2d_type);
- $qr_file = public_path().'/uploads/barcodes/qr-'.str_slug($object_type).'-'.str_slug($id).'.png';
+ $qr_key = FileStorage::Barcodes->publicPath().'qr-'.str_slug($object_type).'-'.str_slug($id).'.png';
- if (file_exists($qr_file)) {
- return response()->file($qr_file, ['Content-type' => 'image/png']);
+ if (! Storage::disk('public')->exists($qr_key)) {
+ $barcode = new Barcode;
+ $barcode_obj = $barcode->getBarcodeObj(
+ $settings->label2_2d_type,
+ route(self::$map_show_route[$object_type], $id),
+ $size['height'],
+ $size['width'],
+ 'black',
+ [-2, -2, -2, -2]
+ );
+ Storage::disk('public')->put($qr_key, $barcode_obj->getPngData());
}
- $barcode = new Barcode;
- $barcode_obj = $barcode->getBarcodeObj(
- $settings->label2_2d_type,
- route(self::$map_show_route[$object_type], $id),
- $size['height'],
- $size['width'],
- 'black',
- [-2, -2, -2, -2]
- );
- file_put_contents($qr_file, $barcode_obj->getPngData());
-
- return response($barcode_obj->getPngData())->header('Content-type', 'image/png');
+ return Storage::disk('public')->response($qr_key, basename($qr_key), ['Content-type' => 'image/png']);
}
}
diff --git a/app/Http/Controllers/UploadedFilesController.php b/app/Http/Controllers/UploadedFilesController.php
index dc549588b3..230dd37633 100644
--- a/app/Http/Controllers/UploadedFilesController.php
+++ b/app/Http/Controllers/UploadedFilesController.php
@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
+use App\Enums\FileStorage;
use App\Helpers\StorageHelper;
use App\Http\Requests\UploadFileRequest;
use App\Models\Actionlog;
@@ -188,7 +189,7 @@ class UploadedFilesController extends Controller
return redirect()->back()->with('error', trans('general.file_upload_status.file_not_found'));
}
- $storedPath = 'private_uploads/imports/'.$import->file_path;
+ $storedPath = FileStorage::Imports->privateStorageKey().$import->file_path;
if (! Storage::exists($storedPath)) {
return redirect()->back()->with('error', trans('general.file_upload_status.file_not_found'));
}
diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php
index 8f7d81495c..399535b2d7 100644
--- a/app/Http/Transformers/ActionlogsTransformer.php
+++ b/app/Http/Transformers/ActionlogsTransformer.php
@@ -3,6 +3,7 @@
namespace App\Http\Transformers;
use App\Enums\ActionType;
+use App\Enums\FileStorage;
use App\Helpers\Helper;
use App\Helpers\StorageHelper;
use App\Models\Actionlog;
@@ -265,7 +266,7 @@ class ActionlogsTransformer
? e($actionlog->orderItem->order->order_number)
: null,
'note' => ($actionlog->note) ? Helper::parseEscapedMarkedownInline($actionlog->note) : null,
- 'signature_file' => (($actionlog->accept_signature) && Storage::exists('private_uploads/signatures/'.$actionlog->accept_signature)) ? route('log.signature.view', ['filename' => $actionlog->accept_signature]) : null,
+ 'signature_file' => (($actionlog->accept_signature) && Storage::exists(FileStorage::Signatures->privateStorageKey().$actionlog->accept_signature)) ? route('log.signature.view', ['filename' => $actionlog->accept_signature]) : null,
'log_meta' => $clean_meta ?? null,
'remote_ip' => e($actionlog->remote_ip) ?? null,
'user_agent' => e($actionlog->user_agent) ?? null,
diff --git a/app/Livewire/Importer.php b/app/Livewire/Importer.php
index 4992a665dd..aab20a6b05 100644
--- a/app/Livewire/Importer.php
+++ b/app/Livewire/Importer.php
@@ -2,6 +2,7 @@
namespace App\Livewire;
+use App\Enums\FileStorage;
use App\Models\Accessory;
use App\Models\Asset;
use App\Models\AssetModel;
@@ -869,7 +870,7 @@ class Importer extends Component
// Existence via Storage so the check routes to whichever
// driver PRIVATE_FILESYSTEM_DISK resolves to, instead of only
// looking at the local filesystem.
- $storedPath = 'private_uploads/imports/'.$this->activeFile->file_path;
+ $storedPath = FileStorage::Imports->privateStorageKey().$this->activeFile->file_path;
if (! Storage::exists($storedPath)) {
$this->message = trans('admin/hardware/message.import.file_missing_on_disk');
$this->message_type = 'danger';
@@ -1128,7 +1129,7 @@ class Importer extends Component
// as the old createFromPath. On s3_private the stream pulls
// bytes directly from S3 via the SDK so we never need the
// file to touch local disk.
- $storedPath = 'private_uploads/imports/'.$this->activeFile->file_path;
+ $storedPath = FileStorage::Imports->privateStorageKey().$this->activeFile->file_path;
$stream = Storage::readStream($storedPath);
if ($stream === null) {
return [];
@@ -1173,7 +1174,7 @@ class Importer extends Component
}
// Same disk-aware read path as loadPreviewRows.
- $storedPath = 'private_uploads/imports/'.$this->activeFile->file_path;
+ $storedPath = FileStorage::Imports->privateStorageKey().$this->activeFile->file_path;
$stream = Storage::readStream($storedPath);
if ($stream === null) {
return 0;
@@ -1248,7 +1249,7 @@ class Importer extends Component
return;
}
- if (Storage::delete('private_uploads/imports/'.$import->file_path)) {
+ if (Storage::delete(FileStorage::Imports->privateStorageKey().$import->file_path)) {
$import->delete();
$this->message = trans('admin/hardware/message.import.file_delete_success');
$this->message_type = 'success';
@@ -1457,7 +1458,7 @@ class Importer extends Component
public function refreshExistingImportFiles(): void
{
$set = [];
- foreach (Storage::files('private_uploads/imports') as $path) {
+ foreach (Storage::files(rtrim(FileStorage::Imports->privateStorageKey(), '/')) as $path) {
$set[basename($path)] = true;
}
$this->existingImportFiles = $set;
@@ -1535,7 +1536,7 @@ class Importer extends Component
continue;
}
- Storage::delete('private_uploads/imports/'.$import->file_path);
+ Storage::delete(FileStorage::Imports->privateStorageKey().$import->file_path);
$import->delete();
unset($this->existingImportFiles[$import->file_path]);
$deleted++;
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php
index d77dbf9689..c0e81fa823 100755
--- a/app/Models/Accessory.php
+++ b/app/Models/Accessory.php
@@ -152,6 +152,37 @@ class Accessory extends SnipeModel
// recent acquisition context, falling back to the parent's
// default_* template fields on items with no order history yet).
+ /**
+ * On hard-delete, wipe the image file and Files-tab attachments.
+ * Soft-delete leaves everything alone so a restore comes back with
+ * the image + files intact. The attachment action_log rows get
+ * soft-deleted (not hard-deleted) so the audit trail of what was
+ * attached-and-when survives even after the parent is gone.
+ */
+ protected static function booted(): void
+ {
+ static::forceDeleted(function (self $accessory) {
+ if ($accessory->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Accessories->publicPath().$accessory->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+
+ foreach ($accessory->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
+ });
+ }
+
/**
* Parent-level "typical supplier" template. Distinct from
* per-acquisition supplier (which lives on Order.supplier_id).
diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php
index ac563d43c9..9ac00f0489 100755
--- a/app/Models/Actionlog.php
+++ b/app/Models/Actionlog.php
@@ -3,6 +3,7 @@
namespace App\Models;
use App\Enums\ActionType;
+use App\Enums\FileStorage;
use App\Models\Traits\CompanyableTrait;
use App\Models\Traits\Searchable;
use App\Presenters\ActionlogPresenter;
@@ -699,41 +700,30 @@ class Actionlog extends SnipeModel
{
if (($this->action_type == 'accepted') || ($this->action_type == 'declined')) {
- return 'private_uploads/eula-pdfs/'.$this->filename;
+ return FileStorage::EulaPdfs->privateStorageKey().$this->filename;
}
if ($this->action_type == 'audit') {
- return 'private_uploads/audits/'.$this->filename;
+ return FileStorage::Audits->privateStorageKey().$this->filename;
}
- switch ($this->item_type) {
- case Accessory::class:
- return 'private_uploads/accessories/'.$this->filename;
- case Asset::class:
- return 'private_uploads/assets/'.$this->filename;
- case AssetModel::class:
- return 'private_uploads/models/'.$this->filename;
- case Company::class:
- return 'private_uploads/companies/'.$this->filename;
- case Consumable::class:
- return 'private_uploads/consumables/'.$this->filename;
- case Department::class:
- return 'private_uploads/departments/'.$this->filename;
- case Component::class:
- return 'private_uploads/components/'.$this->filename;
- case License::class:
- return 'private_uploads/licenses/'.$this->filename;
- case Location::class:
- return 'private_uploads/locations/'.$this->filename;
- case Maintenance::class:
- return 'private_uploads/maintenances/'.$this->filename;
- case Supplier::class:
- return 'private_uploads/suppliers/'.$this->filename;
- case User::class:
- return 'private_uploads/users/'.$this->filename;
- default:
- return null;
- }
+ $case = match ($this->item_type) {
+ Accessory::class => FileStorage::Accessories,
+ Asset::class => FileStorage::Assets,
+ AssetModel::class => FileStorage::Models,
+ Company::class => FileStorage::Companies,
+ Consumable::class => FileStorage::Consumables,
+ Department::class => FileStorage::Departments,
+ Component::class => FileStorage::Components,
+ License::class => FileStorage::Licenses,
+ Location::class => FileStorage::Locations,
+ Maintenance::class => FileStorage::Maintenances,
+ Supplier::class => FileStorage::Suppliers,
+ User::class => FileStorage::Users,
+ default => null,
+ };
+
+ return $case ? $case->privateStorageKey().$this->filename : null;
}
// Manually sets $this->source for determineActionSource()
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index 8704267429..594b254553 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -26,6 +26,7 @@ use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Watson\Validating\ValidatingTrait;
@@ -285,6 +286,30 @@ class Asset extends Depreciable
{
static::forceDeleted(function (Asset $asset) {
$asset->requests()->forceDelete();
+
+ // Image + Files-tab attachments wipe on hard-delete only, so
+ // a restored soft-deleted asset keeps its image and files.
+ // Attachment action_log rows get soft-deleted (not
+ // hard-deleted) so the audit trail of what was attached-
+ // and-when survives even after the parent is gone.
+ if ($asset->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Assets->publicPath().$asset->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+
+ foreach ($asset->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
});
static::softDeleted(function (Asset $asset) {
diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php
index a5c5dfcbc7..c721c7a179 100755
--- a/app/Models/AssetModel.php
+++ b/app/Models/AssetModel.php
@@ -16,6 +16,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;
@@ -130,6 +131,30 @@ class AssetModel extends SnipeModel
{
static::forceDeleted(function (AssetModel $assetModel) {
$assetModel->requests()->forceDelete();
+
+ // Image + Files-tab attachments wipe on hard-delete only, so
+ // a restored soft-deleted model keeps its image and files.
+ // Attachment action_log rows get soft-deleted (not
+ // hard-deleted) so the audit trail of what was attached-
+ // and-when survives even after the parent is gone.
+ if ($assetModel->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Models->publicPath().$assetModel->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+
+ foreach ($assetModel->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
});
static::softDeleted(function (AssetModel $assetModel) {
diff --git a/app/Models/Company.php b/app/Models/Company.php
index f3a40ccef6..ef926845ab 100644
--- a/app/Models/Company.php
+++ b/app/Models/Company.php
@@ -475,6 +475,17 @@ final class Company extends SnipeModel
Log::info($e->getMessage());
}
}
+
+ foreach ($company->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
});
}
diff --git a/app/Models/Component.php b/app/Models/Component.php
index 6e89ccb1ed..9fbb37ae3b 100644
--- a/app/Models/Component.php
+++ b/app/Models/Component.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Enums\FileStorage;
use App\Models\Traits\Acceptable;
use App\Models\Traits\AdjustsQuantity;
use App\Models\Traits\CompanyableTrait;
@@ -20,6 +21,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;
/**
@@ -148,6 +151,32 @@ class Component extends SnipeModel
// "invalidating the 'cache'" seems like a fair choice here.
unset($model->sum_unconstrained_assets);
});
+
+ // On hard-delete, wipe the image file and Files-tab attachments.
+ // Soft-delete leaves everything alone so a restore comes back
+ // with the image + files intact. The attachment action_log rows
+ // get soft-deleted (not hard-deleted) so the audit trail of what
+ // was attached-and-when survives even after the parent is gone.
+ static::forceDeleted(function (self $component) {
+ if ($component->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Components->publicPath().$component->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+
+ foreach ($component->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
+ });
}
public function isDeletable()
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 69bfc39d13..7cbc1712c7 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.php
@@ -136,8 +136,11 @@ class Consumable extends SnipeModel
];
/**
- * On hard-delete, wipe the image file. Soft-delete leaves it alone so a
- * subsequent restore comes back with its image intact.
+ * On hard-delete, wipe the image file and Files-tab attachments.
+ * Soft-delete leaves everything alone so a restore comes back with
+ * the image + files intact. The attachment action_log rows get
+ * soft-deleted (not hard-deleted) so the audit trail of what was
+ * attached-and-when survives even after the parent is gone.
*/
protected static function booted(): void
{
@@ -149,6 +152,17 @@ class Consumable extends SnipeModel
Log::info($e->getMessage());
}
}
+
+ foreach ($consumable->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
});
}
diff --git a/app/Models/Department.php b/app/Models/Department.php
index d768a3799e..8c1b01ffb6 100644
--- a/app/Models/Department.php
+++ b/app/Models/Department.php
@@ -100,8 +100,11 @@ class Department extends SnipeModel
];
/**
- * On hard-delete, wipe the image file. Soft-delete leaves it alone so a
- * subsequent restore comes back with its image intact.
+ * On hard-delete, wipe the image file and Files-tab attachments.
+ * Soft-delete leaves everything alone so a restore comes back with
+ * the image + files intact. The attachment action_log rows get
+ * soft-deleted (not hard-deleted) so the audit trail of what was
+ * attached-and-when survives even after the parent is gone.
*/
protected static function booted(): void
{
@@ -113,6 +116,17 @@ class Department extends SnipeModel
Log::info($e->getMessage());
}
}
+
+ foreach ($department->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
});
}
diff --git a/app/Models/Location.php b/app/Models/Location.php
index b357c5f40d..009926639a 100755
--- a/app/Models/Location.php
+++ b/app/Models/Location.php
@@ -15,6 +15,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;
@@ -120,6 +121,37 @@ class Location extends SnipeModel
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
+ /**
+ * On hard-delete, wipe the image file and Files-tab attachments.
+ * Soft-delete leaves everything alone so a restore comes back with
+ * the image + files intact. The attachment action_log rows get
+ * soft-deleted (not hard-deleted) so the audit trail of what was
+ * attached-and-when survives even after the parent is gone.
+ */
+ protected static function booted(): void
+ {
+ static::forceDeleted(function (self $location) {
+ if ($location->image) {
+ try {
+ Storage::disk('public')->delete(FileStorage::Locations->publicPath().$location->image);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+
+ foreach ($location->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
+ });
+ }
+
/**
* Determine whether or not this location can be deleted.
*
diff --git a/app/Models/Supplier.php b/app/Models/Supplier.php
index c208cbfbf6..45e49de942 100755
--- a/app/Models/Supplier.php
+++ b/app/Models/Supplier.php
@@ -110,8 +110,11 @@ class Supplier extends SnipeModel
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.
+ // On hard-delete, wipe the image file and Files-tab attachments.
+ // Soft-delete leaves everything alone so a restore comes back
+ // with the image + files intact. The attachment action_log rows
+ // get soft-deleted (not hard-deleted) so the audit trail of what
+ // was attached-and-when survives even after the parent is gone.
static::forceDeleted(function (self $supplier) {
if ($supplier->image) {
try {
@@ -120,6 +123,17 @@ class Supplier extends SnipeModel
Log::info($e->getMessage());
}
}
+
+ foreach ($supplier->uploads as $upload) {
+ if (($path = $upload->uploads_file_path()) !== null) {
+ try {
+ Storage::delete($path);
+ } catch (\Exception $e) {
+ Log::info($e->getMessage());
+ }
+ }
+ $upload->delete();
+ }
});
}
diff --git a/app/Notifications/AcceptanceItemAcceptedToUserNotification.php b/app/Notifications/AcceptanceItemAcceptedToUserNotification.php
index 4a03fc6938..4951e97870 100644
--- a/app/Notifications/AcceptanceItemAcceptedToUserNotification.php
+++ b/app/Notifications/AcceptanceItemAcceptedToUserNotification.php
@@ -2,6 +2,7 @@
namespace App\Notifications;
+use App\Enums\FileStorage;
use App\Models\Setting;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@@ -84,7 +85,7 @@ class AcceptanceItemAcceptedToUserNotification extends Notification implements S
*/
public function toMail()
{
- $pdf_path = storage_path('private_uploads/eula-pdfs/'.$this->file);
+ $pdf_path = storage_path(FileStorage::EulaPdfs->privateStorageKey().$this->file);
$message = (new MailMessage)->markdown('notifications.markdown.asset-acceptance',
[
'item_tag' => $this->item_tag,
@@ -101,7 +102,7 @@ class AcceptanceItemAcceptedToUserNotification extends Notification implements S
'intro_text' => trans_choice('mail.acceptance_asset_accepted_to_user', $this->qty, ['qty' => $this->qty, 'site_name' => Setting::getSettings()->site_name]),
])
->attach($pdf_path)
- ->subject('✅ ' . trans_choice('mail.acceptance_asset_accepted_to_user', $this->qty, ['qty' => $this->qty, 'site_name' => Setting::getSettings()->site_name]))
+ ->subject('✅ '.trans_choice('mail.acceptance_asset_accepted_to_user', $this->qty, ['qty' => $this->qty, 'site_name' => Setting::getSettings()->site_name]))
->withSymfonyMessage(function (Email $message) {
$message->getHeaders()->addTextHeader(
'X-System-Sender', 'Snipe-IT'
diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.php
index 7b9a392627..60bc0caca5 100644
--- a/app/Observers/ConsumableObserver.php
+++ b/app/Observers/ConsumableObserver.php
@@ -2,6 +2,7 @@
namespace App\Observers;
+use App\Enums\FileStorage;
use App\Models\Actionlog;
use App\Models\CheckoutAcceptance;
use App\Models\Consumable;
@@ -69,7 +70,7 @@ class ConsumableObserver
foreach ($uploads as $file) {
try {
- Storage::delete('private_uploads/consumables/'.$file->filename);
+ Storage::delete(FileStorage::Consumables->privateStorageKey().$file->filename);
$file->delete();
} catch (\Exception $e) {
Log::info($e);