A free open source IT asset/license management system
Code cleanup
snipeef9b6e3b072f
diff --git a/app/Http/Controllers/Accessories/AccessoriesFilesController.php b/app/Http/Controllers/Accessories/AccessoriesFilesController.phpindex b63c202d30..9ebdea9b83 100644--- a/app/Http/Controllers/Accessories/AccessoriesFilesController.php+++ b/app/Http/Controllers/Accessories/AccessoriesFilesController.php@@ -106,50 +106,29 @@ class AccessoriesFilesController extends Controller* @param int $accessoryId* @param int $fileId*/- public function show($accessoryId = null, $fileId = null, $download = true) : View | RedirectResponse | Response | BinaryFileResponse | StreamedResponse+ public function show($accessoryId = null, $fileId = null) : View | RedirectResponse | Response | BinaryFileResponse | StreamedResponse{- Log::debug('Private filesystem is: '.config('filesystems.default'));- $accessory = Accessory::find($accessoryId);--// the accessory is valid- if (isset($accessory->id)) {+ if ($accessory = Accessory::find($accessoryId)) {$this->authorize('view', $accessory);$this->authorize('accessories.files', $accessory);- if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $accessory->id)->find($fileId)) {- return redirect()->route('accessories.index')->with('error', trans('admin/users/message.log_record_not_found'));- }-- $file = 'private_uploads/accessories/'.$log->filename;-- if (Storage::missing($file)) {- Log::debug('FILE DOES NOT EXISTS for '.$file);- Log::debug('URL should be '.Storage::url($file));+ if ($log = Actionlog::whereNotNull('filename')->where('item_id', $accessory->id)->find($fileId)) {+ $file = 'private_uploads/accessories/'.$log->filename;- return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)- ->header('Content-Type', 'text/plain');- } else {-- // Display the file inline- if (request('inline') == 'true') {- $headers = [- 'Content-Disposition' => 'inline',- ];- return Storage::download($file, $log->filename, $headers);+ try {+ return StorageHelper::showOrDownloadFile($file, $log->filename);+ } catch (\Exception $e) {+ return redirect()->route('accessories.show', ['accessory' => $accessory])->with('error', trans('general.file_not_found'));}+ }+ // todo- // We have to override the URL stuff here, since local defaults in Laravel's Flysystem- // won't work, as they're not accessible via the web- if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?- return StorageHelper::downloader($file);- }- }}- return redirect()->route('accessories.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId]));+ return redirect()->route('accessories.index')->with('error', trans('general.file_not_found'));}}diff --git a/app/Http/Controllers/Assets/AssetFilesController.php b/app/Http/Controllers/Assets/AssetFilesController.phpindex b5a04759bb..b8ae3814da 100644--- a/app/Http/Controllers/Assets/AssetFilesController.php+++ b/app/Http/Controllers/Assets/AssetFilesController.php@@ -61,43 +61,29 @@ class AssetFilesController extends Controller*/public function show($assetId = null, $fileId = null) : View | RedirectResponse | Response | StreamedResponse | BinaryFileResponse{- $asset = Asset::find($assetId);- // the asset is valid- if (isset($asset->id)) {+ if ($asset = Asset::find($assetId)) {+$this->authorize('view', $asset);- if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $asset->id)->find($fileId)) {- return response('No matching record for that asset/file', 500)- ->header('Content-Type', 'text/plain');- }+ if ($log = Actionlog::whereNotNull('filename')->where('item_id', $asset->id)->find($fileId)) {+ $file = 'private_uploads/assets/'.$log->filename;- $file = 'private_uploads/assets/'.$log->filename;+ if ($log->action_type == 'audit') {+ $file = 'private_uploads/audits/'.$log->filename;+ }- if ($log->action_type == 'audit') {- $file = 'private_uploads/audits/'.$log->filename;- }+ try {+ return StorageHelper::showOrDownloadFile($file, $log->filename);+ } catch (\Exception $e) {+ return redirect()->route('hardware.show', ['hardware' => $asset])->with('error', trans('general.file_not_found'));+ }- if (! Storage::exists($file)) {- return response('File '.$file.' not found on server', 404)- ->header('Content-Type', 'text/plain');}- if (request('inline') == 'true') {-- $headers = [- 'Content-Disposition' => 'inline',- ];-- return Storage::download($file, $log->filename, $headers);- }+ return redirect()->back()->with('error', trans('general.file_not_found_redirect'));- return StorageHelper::downloader($file);}- // Prepare the error message- $error = trans('admin/hardware/message.does_not_exist', ['id' => $fileId]);- // Redirect to the hardware management page- return redirect()->route('hardware.index')->with('error', $error);}/**diff --git a/app/Http/Controllers/Components/ComponentsFilesController.php b/app/Http/Controllers/Components/ComponentsFilesController.phpindex a7d42bb072..4fa3366d32 100644--- a/app/Http/Controllers/Components/ComponentsFilesController.php+++ b/app/Http/Controllers/Components/ComponentsFilesController.php@@ -112,40 +112,25 @@ class ComponentsFilesController extends Controllerpublic function show($componentId = null, $fileId = null){Log::debug('Private filesystem is: '.config('filesystems.default'));- $component = Component::find($componentId);+// the component is valid- if (isset($component->id)) {+ if ($component = Component::find($componentId)) {$this->authorize('view', $component);$this->authorize('components.files', $component);- if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $component->id)->find($fileId)) {- return response('No matching record for that asset/file', 500)- ->header('Content-Type', 'text/plain');- }-- $file = 'private_uploads/components/'.$log->filename;+ if ($log = Actionlog::whereNotNull('filename')->where('item_id', $component->id)->find($fileId)) {- if (Storage::missing($file)) {- Log::debug('FILE DOES NOT EXISTS for '.$file);- Log::debug('URL should be '.Storage::url($file));+ $file = 'private_uploads/components/'.$log->filename;- return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)- ->header('Content-Type', 'text/plain');- } else {-- // Display the file inline- if (request('inline') == 'true') {- $headers = [- 'Content-Disposition' => 'inline',- ];- return Storage::download($file, $log->filename, $headers);+ try {+ return StorageHelper::showOrDownloadFile($file, $log->filename);+ } catch (\Exception $e) {+ return redirect()->route('components.show', ['component' => $component])->with('error', trans('general.file_not_found'));}-- if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?- return StorageHelper::downloader($file);- }}+ // todo+}return redirect()->route('components.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId]));diff --git a/app/Http/Controllers/Consumables/ConsumablesFilesController.php b/app/Http/Controllers/Consumables/ConsumablesFilesController.phpindex 35a4ae841e..6ada31f63f 100644--- a/app/Http/Controllers/Consumables/ConsumablesFilesController.php+++ b/app/Http/Controllers/Consumables/ConsumablesFilesController.php@@ -104,7 +104,6 @@ class ConsumablesFilesController extends Controller* @since [v1.4]* @param int $consumableId* @param int $fileId- * @return \Symfony\Consumable\HttpFoundation\Response* @throws \Illuminate\Auth\Access\AuthorizationException*/public function show($consumableId = null, $fileId = null)@@ -116,36 +115,17 @@ class ConsumablesFilesController extends Controller$this->authorize('view', $consumable);$this->authorize('consumables.files', $consumable);- if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $consumable->id)->find($fileId)) {- return response('No matching record for that asset/file', 500)- ->header('Content-Type', 'text/plain');- }-- $file = 'private_uploads/consumables/'.$log->filename;-- if (Storage::missing($file)) {- Log::debug('FILE DOES NOT EXISTS for '.$file);- Log::debug('URL should be '.Storage::url($file));-- return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)- ->header('Content-Type', 'text/plain');- } else {-- // Display the file inline- if (request('inline') == 'true') {- $headers = [- 'Content-Disposition' => 'inline',- ];- return Storage::download($file, $log->filename, $headers);- }-+ if ($log = Actionlog::whereNotNull('filename')->where('item_id', $consumable->id)->find($fileId)) {+ $file = 'private_uploads/consumables/'.$log->filename;- // We have to override the URL stuff here, since local defaults in Laravel's Flysystem- // won't work, as they're not accessible via the web- if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?- return StorageHelper::downloader($file);+ try {+ return StorageHelper::showOrDownloadFile($file, $log->filename);+ } catch (\Exception $e) {+ return redirect()->route('consumables.show', ['consumable' => $consumable])->with('error', trans('general.file_not_found'));}}+ // todo+}return redirect()->route('consumables.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId]));diff --git a/app/Http/Controllers/Licenses/LicenseFilesController.php b/app/Http/Controllers/Licenses/LicenseFilesController.phpindex fa18e8cf48..662a696c49 100644--- a/app/Http/Controllers/Licenses/LicenseFilesController.php+++ b/app/Http/Controllers/Licenses/LicenseFilesController.php@@ -112,37 +112,18 @@ class LicenseFilesController extends Controller$this->authorize('view', $license);$this->authorize('licenses.files', $license);- if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $license->id)->find($fileId)) {- return response('No matching record for that asset/file', 500)- ->header('Content-Type', 'text/plain');- }-- $file = 'private_uploads/licenses/'.$log->filename;-- if (Storage::missing($file)) {- Log::debug('NOT EXISTS for '.$file);- Log::debug('NOT EXISTS URL should be '.Storage::url($file));-- return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)- ->header('Content-Type', 'text/plain');- } else {-- if (request('inline') == 'true') {+ if ($log = Actionlog::whereNotNull('filename')->where('item_id', $license->id)->find($fileId)) {+ $file = 'private_uploads/licenses/'.$log->filename;- $headers = [- 'Content-Disposition' => 'inline',- ];-- return Storage::download($file, $log->filename, $headers);+ try {+ return StorageHelper::showOrDownloadFile($file, $log->filename);+ } catch (\Exception $e) {+ return redirect()->route('licenses.show', ['licenses' => $license])->with('error', trans('general.file_not_found'));}+ }- // We have to override the URL stuff here, since local defaults in Laravel's Flysystem- // won't work, as they're not accessible via the web- if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?- return StorageHelper::downloader($file);+ // todo- }- }}return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.does_not_exist', ['id' => $fileId]));diff --git a/app/Http/Controllers/Users/UserFilesController.php b/app/Http/Controllers/Users/UserFilesController.phpindex 15b8018b78..de3a37de32 100644--- a/app/Http/Controllers/Users/UserFilesController.php+++ b/app/Http/Controllers/Users/UserFilesController.php@@ -114,43 +114,27 @@ class UserFilesController extends Controller{--if (empty($fileId)) {return redirect()->route('users.show')->with('error', 'Invalid file request');}- $user = User::find($userId);-- // the license is valid- if (isset($user->id)) {+ if ($user = User::find($userId)) {$this->authorize('view', $user);if ($log = Actionlog::whereNotNull('filename')->where('item_id', $user->id)->find($fileId)) {-$file = 'private_uploads/users/'.$log->filename;--- if (request('inline') == 'true') {-- $headers = [- 'Content-Disposition' => 'inline',- ];-- // This is NOT allowed as inline - force it to be displayed as text- if (StorageHelper::allowSafeInline($file) === false) {- array_push($headers, ['Content-Type' => 'text/plain']);- }-- return Storage::download($file, $log->filename, $headers);+ try {+ return StorageHelper::showOrDownloadFile($file, $log->filename);+ } catch (\Exception $e) {+ return redirect()->route('users.show', ['user' => $user])->with('error', trans('general.file_not_found'));}-- return Storage::download($file);}- return redirect()->route('users.index')->with('error', trans('admin/users/message.log_record_not_found'));+ // todo++ return redirect()->back()->with('error', trans('general.file_not_found'));}// Redirect to the user management page if the user doesn't exist