Skip to content

A free open source IT asset/license management system

File Uploads: first sweep replacing singletons with FileStorage enums

snipe3c0809da425e
diff --git a/app/Console/Commands/BulkDelete.php b/app/Console/Commands/BulkDelete.php
index b097d27bb7..1206fee089 100644
--- a/app/Console/Commands/BulkDelete.php
+++ b/app/Console/Commands/BulkDelete.php
@@ -2,6 +2,7 @@
namespace App\Console\Commands;
+use App\Enums\FileStorage;
use App\Events\CheckoutableCheckedIn;
use App\Mail\BulkDeleteReportMail;
use App\Models\Accessory;
@@ -548,10 +549,10 @@ class BulkDelete extends Command
if ($deleteFiles) {
if ($asset->image) {
- $this->deleteStorageFile('public', app('assets_upload_path').$asset->image);
+ $this->deleteStorageFile('public', FileStorage::Assets->publicPath().$asset->image);
}
foreach ($maintenanceImages as $img) {
- $this->deleteStorageFile('public', app('maintenances_upload_path').$img);
+ $this->deleteStorageFile('public', FileStorage::Maintenances->publicPath().$img);
}
foreach ($actionLogPaths as $path) {
$this->deleteStorageFile('local', $path);
@@ -740,7 +741,7 @@ class BulkDelete extends Command
if ($deleteFiles) {
if ($accessory->image) {
- $this->deleteStorageFile('public', app('accessories_upload_path').$accessory->image);
+ $this->deleteStorageFile('public', FileStorage::Accessories->publicPath().$accessory->image);
}
foreach ($actionLogPaths as $path) {
$this->deleteStorageFile('local', $path);
@@ -827,7 +828,7 @@ class BulkDelete extends Command
if ($deleteFiles) {
if ($component->image) {
- $this->deleteStorageFile('public', app('components_upload_path').$component->image);
+ $this->deleteStorageFile('public', FileStorage::Components->publicPath().$component->image);
}
foreach ($actionLogPaths as $path) {
$this->deleteStorageFile('local', $path);
@@ -897,7 +898,7 @@ class BulkDelete extends Command
if ($deleteFiles) {
if ($consumable->image) {
- $this->deleteStorageFile('public', app('consumables_upload_path').$consumable->image);
+ $this->deleteStorageFile('public', FileStorage::Consumables->publicPath().$consumable->image);
}
foreach ($actionLogPaths as $path) {
$this->deleteStorageFile('local', $path);
@@ -1018,7 +1019,7 @@ class BulkDelete extends Command
if ($deleteFiles) {
if ($user->avatar) {
- $this->deleteStorageFile('public', app('users_upload_path').$user->avatar);
+ $this->deleteStorageFile('public', FileStorage::Avatars->publicPath().$user->avatar);
}
$acceptancesToDelete->each(fn (CheckoutAcceptance $ca) => $this->deleteAcceptanceFiles($ca));
foreach ($actionLogPaths as $path) {
diff --git a/app/Console/Commands/MoveUploadsToNewDisk.php b/app/Console/Commands/MoveUploadsToNewDisk.php
index 25a49482f1..cebfe12205 100644
--- a/app/Console/Commands/MoveUploadsToNewDisk.php
+++ b/app/Console/Commands/MoveUploadsToNewDisk.php
@@ -2,6 +2,7 @@
namespace App\Console\Commands;
+use App\Enums\FileStorage;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
@@ -47,18 +48,17 @@ class MoveUploadsToNewDisk extends Command
}
$delete_local = $this->argument('delete_local');
- $public_uploads['accessories'] = glob('public/uploads/accessories'.'/*.*');
- $public_uploads['assets'] = glob('public/uploads/assets'.'/*.*');
- $public_uploads['avatars'] = glob('public/uploads/avatars'.'/*.*');
- $public_uploads['categories'] = glob('public/uploads/categories'.'/*.*');
- $public_uploads['companies'] = glob('public/uploads/companies'.'/*.*');
- $public_uploads['components'] = glob('public/uploads/components'.'/*.*');
- $public_uploads['consumables'] = glob('public/uploads/consumables'.'/*.*');
- $public_uploads['departments'] = glob('public/uploads/departments'.'/*.*');
- $public_uploads['locations'] = glob('public/uploads/locations'.'/*.*');
- $public_uploads['manufacturers'] = glob('public/uploads/manufacturers'.'/*.*');
- $public_uploads['suppliers'] = glob('public/uploads/suppliers'.'/*.*');
- $public_uploads['assetmodels'] = glob('public/uploads/models'.'/*.*');
+ // Public upload directories, resolved through FileStorage.
+ $public_uploads = [];
+ foreach (FileStorage::cases() as $case) {
+ if (!$case->hasPublicScope()) {
+ continue;
+ }
+ if ($case === FileStorage::Barcodes) {
+ continue;
+ }
+ $public_uploads[$case->value] = glob($case->publicDir() . '/*.*');
+ }
// iterate files
foreach ($public_uploads as $public_type => $public_upload) {
@@ -92,14 +92,14 @@ class MoveUploadsToNewDisk extends Command
$this->info($type_count.'. LOGO: '.$filename.' was copied to '.config('filesystems.disks.public_aws.url').'/uploads/'.$filename);
}
- $private_uploads['assets'] = glob('storage/private_uploads/assets'.'/*.*');
- $private_uploads['signatures'] = glob('storage/private_uploads/signatures'.'/*.*');
- $private_uploads['audits'] = glob('storage/private_uploads/audits'.'/*.*');
- $private_uploads['assetmodels'] = glob('storage/private_uploads/models'.'/*.*');
- $private_uploads['imports'] = glob('storage/private_uploads/imports'.'/*.*');
- $private_uploads['licenses'] = glob('storage/private_uploads/licenses'.'/*.*');
- $private_uploads['users'] = glob('storage/private_uploads/users'.'/*.*');
- $private_uploads['backups'] = glob('storage/private_uploads/backups'.'/*.*');
+ // Private upload directories, resolved through FileStorage.
+ $private_uploads = [];
+ foreach (FileStorage::cases() as $case) {
+ if (!$case->hasPrivateScope()) {
+ continue;
+ }
+ $private_uploads[$case->value] = glob($case->privateDir() . '/*.*');
+ }
foreach ($private_uploads as $private_type => $private_upload) {
diff --git a/app/Console/Commands/RestoreFromBackup.php b/app/Console/Commands/RestoreFromBackup.php
index d1861f7996..61bbd68e44 100644
--- a/app/Console/Commands/RestoreFromBackup.php
+++ b/app/Console/Commands/RestoreFromBackup.php
@@ -377,7 +377,7 @@ class RestoreFromBackup extends Command
// print("INTERESTING - last_pos is $last_pos when searching $raw_path for $dir - last_pos+strlen(\$dir) is: ".($last_pos+strlen($dir))." and strlen(\$rawpath) is: ".strlen($raw_path)."\n");
// print("We would copy $raw_path to $dir.\n"); //FIXME append to a path?
// the CSV bit, below, is because we store CSV files as "blahcsv" - without an extension
- if (! in_array($extension, $allowed_extensions) && ! ($dir == 'storage/private_uploads/imports' && substr($raw_path, -3) == 'csv' && $extension == '')) {
+ if (! in_array($extension, $allowed_extensions) && ! ($dir == FileStorage::Imports->privateDir() && substr($raw_path, -3) == 'csv' && $extension == '')) {
$unsafe_files[] = $raw_path;
Log::debug($raw_path.' from directory '.$dir.' is being skipped');
} else {
diff --git a/app/Http/Transformers/DepartmentsTransformer.php b/app/Http/Transformers/DepartmentsTransformer.php
index 2a6143104a..9a953f955b 100644
--- a/app/Http/Transformers/DepartmentsTransformer.php
+++ b/app/Http/Transformers/DepartmentsTransformer.php
@@ -2,6 +2,7 @@
namespace App\Http\Transformers;
+use App\Enums\FileStorage;
use App\Helpers\Helper;
use App\Models\Department;
use Illuminate\Database\Eloquent\Collection;
@@ -28,7 +29,7 @@ class DepartmentsTransformer
'name' => e($department->name),
'phone' => ($department->phone != '') ? e($department->phone) : null,
'fax' => ($department->fax != '') ? e($department->fax) : null,
- 'image' => ($department->image) ? Storage::disk('public')->url(app('departments_upload_url').e($department->image)) : null,
+ 'image' => ($department->image) ? Storage::disk('public')->url(FileStorage::Departments->publicPath().e($department->image)) : null,
'company' => ($department->company) ? [
'id' => (int) $department->company->id,
'name' => e($department->company->name),
diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php
index 564c8dc397..d77dbf9689 100755
--- a/app/Models/Accessory.php
+++ b/app/Models/Accessory.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;
@@ -295,7 +296,7 @@ class Accessory extends SnipeModel
public function getImageUrl($path = null)
{
if ($this->image) {
- return Storage::disk('public')->url(app('accessories_upload_path').$this->image);
+ return Storage::disk('public')->url(FileStorage::Accessories->publicPath().$this->image);
}
return false;
diff --git a/app/Models/Asset.php b/app/Models/Asset.php
index d2b26d6a5b..8704267429 100644
--- a/app/Models/Asset.php
+++ b/app/Models/Asset.php
@@ -1031,11 +1031,11 @@ class Asset extends Depreciable
public function getImageUrl($path = null)
{
if ($this->image && ! empty($this->image)) {
- return Storage::disk('public')->url(app('assets_upload_path').e($this->image));
+ return Storage::disk('public')->url(FileStorage::Assets->publicPath().e($this->image));
} elseif ($this->model && ! empty($this->model->image)) {
- return Storage::disk('public')->url(app('models_upload_path').e($this->model->image));
+ return Storage::disk('public')->url(FileStorage::Models->publicPath().e($this->model->image));
} elseif ($this->model?->category && ! empty($this->model->category->image)) {
- return Storage::disk('public')->url(app('categories_upload_path').e($this->model->category->image));
+ return Storage::disk('public')->url(FileStorage::Categories->publicPath().e($this->model->category->image));
}
return false;
diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php
index 7c05480916..a5c5dfcbc7 100755
--- a/app/Models/AssetModel.php
+++ b/app/Models/AssetModel.php
@@ -326,7 +326,7 @@ class AssetModel extends SnipeModel
public function getImageUrl($path = null)
{
if ($this->image) {
- return Storage::disk('public')->url(app('models_upload_path').$this->image);
+ return Storage::disk('public')->url(FileStorage::Models->publicPath().$this->image);
}
return false;
diff --git a/app/Models/Consumable.php b/app/Models/Consumable.php
index 204379d1b4..6cac021642 100644
--- a/app/Models/Consumable.php
+++ b/app/Models/Consumable.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;
@@ -275,11 +276,11 @@ class Consumable extends SnipeModel
{
// If there is a consumable image, use that
if ($this->image) {
- return Storage::disk('public')->url(app('consumables_upload_path').$this->image);
+ return Storage::disk('public')->url(FileStorage::Consumables->publicPath().$this->image);
// Otherwise check for a category image
} elseif (($this->category) && ($this->category->image)) {
- return Storage::disk('public')->url(app('categories_upload_path').e($this->category->image));
+ return Storage::disk('public')->url(FileStorage::Categories->publicPath().e($this->category->image));
}
return false;
diff --git a/app/Presenters/AssetModelPresenter.php b/app/Presenters/AssetModelPresenter.php
index 5ce02f9ff2..ec950f4d6a 100644
--- a/app/Presenters/AssetModelPresenter.php
+++ b/app/Presenters/AssetModelPresenter.php
@@ -2,6 +2,7 @@
namespace App\Presenters;
+use App\Enums\FileStorage;
use App\Helpers\Helper;
use Illuminate\Support\Facades\Storage;
@@ -314,7 +315,7 @@ class AssetModelPresenter extends Presenter
public function imageUrl()
{
if (! empty($this->image)) {
- $url = Storage::disk('public')->url(app('models_upload_path').e($this->image));
+ $url = Storage::disk('public')->url(FileStorage::Models->publicPath().e($this->image));
return '<img src="'.$url.'" alt="'.e($this->name).'" height="50" width="50">';
}
@@ -330,7 +331,7 @@ class AssetModelPresenter extends Presenter
public function imageSrc()
{
if (! empty($this->image)) {
- return Storage::disk('public')->url(app('models_upload_path').e($this->image));
+ return Storage::disk('public')->url(FileStorage::Models->publicPath().e($this->image));
}
return '';
diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php
index 6c2257da30..cb307bb04f 100644
--- a/app/Presenters/AssetPresenter.php
+++ b/app/Presenters/AssetPresenter.php
@@ -2,6 +2,7 @@
namespace App\Presenters;
+use App\Enums\FileStorage;
use App\Models\CustomField;
use Carbon\CarbonImmutable;
use DateTime;
@@ -633,7 +634,7 @@ class AssetPresenter extends Presenter
$imageAlt = $this->model->name;
}
if (! empty($imagePath)) {
- $url = Storage::disk('public')->url(app('assets_upload_path').e($imagePath));
+ $url = Storage::disk('public')->url(FileStorage::Assets->publicPath().e($imagePath));
$imagePath = '<img src="'.$url.'" height="50" width="50" alt="'.e($imageAlt).'">';
}
@@ -654,7 +655,7 @@ class AssetPresenter extends Presenter
$imagePath = $this->model->image;
}
if (! empty($imagePath)) {
- return Storage::disk('public')->url(app('assets_upload_path').e($imagePath));
+ return Storage::disk('public')->url(FileStorage::Assets->publicPath().e($imagePath));
}
return $imagePath;
diff --git a/app/Presenters/DepreciationReportPresenter.php b/app/Presenters/DepreciationReportPresenter.php
index 46f641b7c1..827a1d9f74 100644
--- a/app/Presenters/DepreciationReportPresenter.php
+++ b/app/Presenters/DepreciationReportPresenter.php
@@ -2,6 +2,7 @@
namespace App\Presenters;
+use App\Enums\FileStorage;
use DateTime;
use Illuminate\Support\Facades\Storage;
@@ -258,7 +259,7 @@ class DepreciationReportPresenter extends Presenter
$imagePath = $this->model->image;
}
if (! empty($imagePath)) {
- return Storage::disk('public')->url(app('assets_upload_path').e($imagePath));
+ return Storage::disk('public')->url(FileStorage::Assets->publicPath().e($imagePath));
}
return $imagePath;
diff --git a/app/SyncAdapters/AppleBusinessManager/AppleDBImageFetcher.php b/app/SyncAdapters/AppleBusinessManager/AppleDBImageFetcher.php
index f79e7aaefd..66812d5729 100644
--- a/app/SyncAdapters/AppleBusinessManager/AppleDBImageFetcher.php
+++ b/app/SyncAdapters/AppleBusinessManager/AppleDBImageFetcher.php
@@ -2,6 +2,7 @@
namespace App\SyncAdapters\AppleBusinessManager;
+use App\Enums\FileStorage;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
@@ -155,7 +156,7 @@ class AppleDBImageFetcher
private static function downloadAndStore(string $productType, string $imageKey, string $colorKey): ?string
{
$filename = self::filenameFor($productType, $colorKey);
- $relativePath = app('models_upload_path').$filename;
+ $relativePath = FileStorage::Models->publicPath().$filename;
// Idempotent: if a prior pull already stored this image,
// return the cached filename without re-downloading.