Skip to content

Commit ef9b6e3

Browse files
committed
Code cleanup
Signed-off-by: snipe <[email protected]>
1 parent 06c599c commit ef9b6e3

File tree

6 files changed

+58
-163
lines changed

6 files changed

+58
-163
lines changed

app/Http/Controllers/Accessories/AccessoriesFilesController.php

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -106,50 +106,29 @@ public function destroy($accessoryId = null, $fileId = null) : RedirectResponse
106106
* @param int $accessoryId
107107
* @param int $fileId
108108
*/
109-
public function show($accessoryId = null, $fileId = null, $download = true) : View | RedirectResponse | Response | BinaryFileResponse | StreamedResponse
109+
public function show($accessoryId = null, $fileId = null) : View | RedirectResponse | Response | BinaryFileResponse | StreamedResponse
110110
{
111111

112-
Log::debug('Private filesystem is: '.config('filesystems.default'));
113-
$accessory = Accessory::find($accessoryId);
114-
115-
116112

117113
// the accessory is valid
118-
if (isset($accessory->id)) {
114+
if ($accessory = Accessory::find($accessoryId)) {
119115
$this->authorize('view', $accessory);
120116
$this->authorize('accessories.files', $accessory);
121117

122-
if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $accessory->id)->find($fileId)) {
123-
return redirect()->route('accessories.index')->with('error', trans('admin/users/message.log_record_not_found'));
124-
}
125-
126-
$file = 'private_uploads/accessories/'.$log->filename;
127-
128-
if (Storage::missing($file)) {
129-
Log::debug('FILE DOES NOT EXISTS for '.$file);
130-
Log::debug('URL should be '.Storage::url($file));
118+
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $accessory->id)->find($fileId)) {
119+
$file = 'private_uploads/accessories/'.$log->filename;
131120

132-
return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)
133-
->header('Content-Type', 'text/plain');
134-
} else {
135-
136-
// Display the file inline
137-
if (request('inline') == 'true') {
138-
$headers = [
139-
'Content-Disposition' => 'inline',
140-
];
141-
return Storage::download($file, $log->filename, $headers);
121+
try {
122+
return StorageHelper::showOrDownloadFile($file, $log->filename);
123+
} catch (\Exception $e) {
124+
return redirect()->route('accessories.show', ['accessory' => $accessory])->with('error', trans('general.file_not_found'));
142125
}
126+
}
143127

128+
// todo
144129

145-
// We have to override the URL stuff here, since local defaults in Laravel's Flysystem
146-
// won't work, as they're not accessible via the web
147-
if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?
148-
return StorageHelper::downloader($file);
149-
}
150-
}
151130
}
152131

153-
return redirect()->route('accessories.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId]));
132+
return redirect()->route('accessories.index')->with('error', trans('general.file_not_found'));
154133
}
155134
}

app/Http/Controllers/Assets/AssetFilesController.php

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,43 +61,29 @@ public function store(UploadFileRequest $request, $assetId = null) : RedirectRes
6161
*/
6262
public function show($assetId = null, $fileId = null) : View | RedirectResponse | Response | StreamedResponse | BinaryFileResponse
6363
{
64-
$asset = Asset::find($assetId);
65-
// the asset is valid
66-
if (isset($asset->id)) {
64+
if ($asset = Asset::find($assetId)) {
65+
6766
$this->authorize('view', $asset);
6867

69-
if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $asset->id)->find($fileId)) {
70-
return response('No matching record for that asset/file', 500)
71-
->header('Content-Type', 'text/plain');
72-
}
68+
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $asset->id)->find($fileId)) {
69+
$file = 'private_uploads/assets/'.$log->filename;
7370

74-
$file = 'private_uploads/assets/'.$log->filename;
71+
if ($log->action_type == 'audit') {
72+
$file = 'private_uploads/audits/'.$log->filename;
73+
}
7574

76-
if ($log->action_type == 'audit') {
77-
$file = 'private_uploads/audits/'.$log->filename;
78-
}
75+
try {
76+
return StorageHelper::showOrDownloadFile($file, $log->filename);
77+
} catch (\Exception $e) {
78+
return redirect()->route('hardware.show', ['hardware' => $asset])->with('error', trans('general.file_not_found'));
79+
}
7980

80-
if (! Storage::exists($file)) {
81-
return response('File '.$file.' not found on server', 404)
82-
->header('Content-Type', 'text/plain');
8381
}
8482

85-
if (request('inline') == 'true') {
86-
87-
$headers = [
88-
'Content-Disposition' => 'inline',
89-
];
90-
91-
return Storage::download($file, $log->filename, $headers);
92-
}
83+
return redirect()->back()->with('error', trans('general.file_not_found_redirect'));
9384

94-
return StorageHelper::downloader($file);
9585
}
96-
// Prepare the error message
97-
$error = trans('admin/hardware/message.does_not_exist', ['id' => $fileId]);
9886

99-
// Redirect to the hardware management page
100-
return redirect()->route('hardware.index')->with('error', $error);
10187
}
10288

10389
/**

app/Http/Controllers/Components/ComponentsFilesController.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,40 +112,25 @@ public function destroy($componentId = null, $fileId = null)
112112
public function show($componentId = null, $fileId = null)
113113
{
114114
Log::debug('Private filesystem is: '.config('filesystems.default'));
115-
$component = Component::find($componentId);
115+
116116

117117
// the component is valid
118-
if (isset($component->id)) {
118+
if ($component = Component::find($componentId)) {
119119
$this->authorize('view', $component);
120120
$this->authorize('components.files', $component);
121121

122-
if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $component->id)->find($fileId)) {
123-
return response('No matching record for that asset/file', 500)
124-
->header('Content-Type', 'text/plain');
125-
}
126-
127-
$file = 'private_uploads/components/'.$log->filename;
122+
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $component->id)->find($fileId)) {
128123

129-
if (Storage::missing($file)) {
130-
Log::debug('FILE DOES NOT EXISTS for '.$file);
131-
Log::debug('URL should be '.Storage::url($file));
124+
$file = 'private_uploads/components/'.$log->filename;
132125

133-
return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)
134-
->header('Content-Type', 'text/plain');
135-
} else {
136-
137-
// Display the file inline
138-
if (request('inline') == 'true') {
139-
$headers = [
140-
'Content-Disposition' => 'inline',
141-
];
142-
return Storage::download($file, $log->filename, $headers);
126+
try {
127+
return StorageHelper::showOrDownloadFile($file, $log->filename);
128+
} catch (\Exception $e) {
129+
return redirect()->route('components.show', ['component' => $component])->with('error', trans('general.file_not_found'));
143130
}
144-
145-
if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?
146-
return StorageHelper::downloader($file);
147-
}
148131
}
132+
// todo
133+
149134
}
150135

151136
return redirect()->route('components.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId]));

app/Http/Controllers/Consumables/ConsumablesFilesController.php

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ public function destroy($consumableId = null, $fileId = null)
104104
* @since [v1.4]
105105
* @param int $consumableId
106106
* @param int $fileId
107-
* @return \Symfony\Consumable\HttpFoundation\Response
108107
* @throws \Illuminate\Auth\Access\AuthorizationException
109108
*/
110109
public function show($consumableId = null, $fileId = null)
@@ -116,36 +115,17 @@ public function show($consumableId = null, $fileId = null)
116115
$this->authorize('view', $consumable);
117116
$this->authorize('consumables.files', $consumable);
118117

119-
if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $consumable->id)->find($fileId)) {
120-
return response('No matching record for that asset/file', 500)
121-
->header('Content-Type', 'text/plain');
122-
}
123-
124-
$file = 'private_uploads/consumables/'.$log->filename;
125-
126-
if (Storage::missing($file)) {
127-
Log::debug('FILE DOES NOT EXISTS for '.$file);
128-
Log::debug('URL should be '.Storage::url($file));
129-
130-
return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)
131-
->header('Content-Type', 'text/plain');
132-
} else {
133-
134-
// Display the file inline
135-
if (request('inline') == 'true') {
136-
$headers = [
137-
'Content-Disposition' => 'inline',
138-
];
139-
return Storage::download($file, $log->filename, $headers);
140-
}
141-
118+
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $consumable->id)->find($fileId)) {
119+
$file = 'private_uploads/consumables/'.$log->filename;
142120

143-
// We have to override the URL stuff here, since local defaults in Laravel's Flysystem
144-
// won't work, as they're not accessible via the web
145-
if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?
146-
return StorageHelper::downloader($file);
121+
try {
122+
return StorageHelper::showOrDownloadFile($file, $log->filename);
123+
} catch (\Exception $e) {
124+
return redirect()->route('consumables.show', ['consumable' => $consumable])->with('error', trans('general.file_not_found'));
147125
}
148126
}
127+
// todo
128+
149129
}
150130

151131
return redirect()->route('consumables.index')->with('error', trans('general.file_does_not_exist', ['id' => $fileId]));

app/Http/Controllers/Licenses/LicenseFilesController.php

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -112,37 +112,18 @@ public function show($licenseId = null, $fileId = null, $download = true)
112112
$this->authorize('view', $license);
113113
$this->authorize('licenses.files', $license);
114114

115-
if (! $log = Actionlog::whereNotNull('filename')->where('item_id', $license->id)->find($fileId)) {
116-
return response('No matching record for that asset/file', 500)
117-
->header('Content-Type', 'text/plain');
118-
}
119-
120-
$file = 'private_uploads/licenses/'.$log->filename;
121-
122-
if (Storage::missing($file)) {
123-
Log::debug('NOT EXISTS for '.$file);
124-
Log::debug('NOT EXISTS URL should be '.Storage::url($file));
125-
126-
return response('File '.$file.' ('.Storage::url($file).') not found on server', 404)
127-
->header('Content-Type', 'text/plain');
128-
} else {
129-
130-
if (request('inline') == 'true') {
115+
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $license->id)->find($fileId)) {
116+
$file = 'private_uploads/licenses/'.$log->filename;
131117

132-
$headers = [
133-
'Content-Disposition' => 'inline',
134-
];
135-
136-
return Storage::download($file, $log->filename, $headers);
118+
try {
119+
return StorageHelper::showOrDownloadFile($file, $log->filename);
120+
} catch (\Exception $e) {
121+
return redirect()->route('licenses.show', ['licenses' => $license])->with('error', trans('general.file_not_found'));
137122
}
123+
}
138124

139-
// We have to override the URL stuff here, since local defaults in Laravel's Flysystem
140-
// won't work, as they're not accessible via the web
141-
if (config('filesystems.default') == 'local') { // TODO - is there any way to fix this at the StorageHelper layer?
142-
return StorageHelper::downloader($file);
125+
// todo
143126

144-
}
145-
}
146127
}
147128

148129
return redirect()->route('licenses.index')->with('error', trans('admin/licenses/message.does_not_exist', ['id' => $fileId]));

app/Http/Controllers/Users/UserFilesController.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,43 +114,27 @@ public function show($userId = null, $fileId = null)
114114
{
115115

116116

117-
118-
119117
if (empty($fileId)) {
120118
return redirect()->route('users.show')->with('error', 'Invalid file request');
121119
}
122120

123-
$user = User::find($userId);
124-
125-
// the license is valid
126-
if (isset($user->id)) {
121+
if ($user = User::find($userId)) {
127122

128123
$this->authorize('view', $user);
129124

130125
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $user->id)->find($fileId)) {
131-
132126
$file = 'private_uploads/users/'.$log->filename;
133127

134-
135-
136-
if (request('inline') == 'true') {
137-
138-
$headers = [
139-
'Content-Disposition' => 'inline',
140-
];
141-
142-
// This is NOT allowed as inline - force it to be displayed as text
143-
if (StorageHelper::allowSafeInline($file) === false) {
144-
array_push($headers, ['Content-Type' => 'text/plain']);
145-
}
146-
147-
return Storage::download($file, $log->filename, $headers);
128+
try {
129+
return StorageHelper::showOrDownloadFile($file, $log->filename);
130+
} catch (\Exception $e) {
131+
return redirect()->route('users.show', ['user' => $user])->with('error', trans('general.file_not_found'));
148132
}
149-
150-
return Storage::download($file);
151133
}
152134

153-
return redirect()->route('users.index')->with('error', trans('admin/users/message.log_record_not_found'));
135+
// todo
136+
137+
return redirect()->back()->with('error', trans('general.file_not_found'));
154138
}
155139

156140
// Redirect to the user management page if the user doesn't exist

0 commit comments

Comments
 (0)