Skip to content

Commit 7774dd0

Browse files
committed
Merge remote-tracking branch 'origin/develop'
Signed-off-by: snipe <[email protected]> # Conflicts: # config/version.php
2 parents 03b0168 + 5e1d792 commit 7774dd0

File tree

27 files changed

+366
-1012
lines changed

27 files changed

+366
-1012
lines changed

app/Console/Commands/ObjectImportCommand.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Input\InputArgument;
77
use Symfony\Component\Console\Input\InputOption;
88
use Illuminate\Support\Facades\Log;
9+
use Symfony\Component\Console\Helper\ProgressIndicator;
910

1011
ini_set('max_execution_time', env('IMPORT_TIME_LIMIT', 600)); //600 seconds = 10 minutes
1112
ini_set('memory_limit', env('IMPORT_MEMORY_LIMIT', '500M'));
@@ -29,6 +30,11 @@ class ObjectImportCommand extends Command
2930
*/
3031
protected $description = 'Import Items from CSV';
3132

33+
/**
34+
* The progress indicator instance.
35+
*/
36+
protected ProgressIndicator $progressIndicator;
37+
3238
/**
3339
* Create a new command instance.
3440
*
@@ -39,15 +45,15 @@ public function __construct()
3945
parent::__construct();
4046
}
4147

42-
private $bar;
43-
4448
/**
4549
* Execute the console command.
4650
*
4751
* @return mixed
4852
*/
4953
public function handle()
5054
{
55+
$this->progressIndicator = new ProgressIndicator($this->output);
56+
5157
$filename = $this->argument('filename');
5258
$class = title_case($this->option('item-type'));
5359
$classString = "App\\Importer\\{$class}Importer";
@@ -61,46 +67,25 @@ public function handle()
6167
// This $logFile/useFiles() bit is currently broken, so commenting it out for now
6268
// $logFile = $this->option('logfile');
6369
// Log::useFiles($logFile);
64-
$this->comment('======= Importing Items from '.$filename.' =========');
65-
$importer->import();
70+
$this->progressIndicator->start('======= Importing Items from '.$filename.' =========');
6671

67-
$this->bar = null;
72+
$importer->import();
6873

69-
if (! empty($this->errors)) {
70-
$this->comment('The following Errors were encountered.');
71-
foreach ($this->errors as $asset => $error) {
72-
$this->comment('Error: Item: '.$asset.' failed validation: '.json_encode($error));
73-
}
74-
} else {
75-
$this->comment('All Items imported successfully!');
76-
}
77-
$this->comment('');
74+
$this->progressIndicator->finish('Import finished.');
7875
}
7976

80-
public function errorCallback($item, $field, $errorString)
77+
public function errorCallback($item, $field, $error)
8178
{
82-
$this->errors[$item->name][$field] = $errorString;
79+
$this->output->write("\x0D\x1B[2K");
80+
81+
$this->warn('Error: Item: '.$item->name.' failed validation: '.json_encode($error));
8382
}
8483

85-
public function progress($count)
84+
public function progress($importedItemsCount)
8685
{
87-
if (! $this->bar) {
88-
$this->bar = $this->output->createProgressBar($count);
89-
}
90-
static $index = 0;
91-
$index++;
92-
if ($index < $count) {
93-
$this->bar->advance();
94-
} else {
95-
$this->bar->finish();
96-
}
86+
$this->progressIndicator->advance();
9787
}
9888

99-
// Tracks the current item for error messages
100-
private $updating;
101-
// An array of errors encountered while parsing
102-
private $errors;
103-
10489
/**
10590
* Log a message to file, configurable by the --log-file parameter.
10691
* If a warning message is passed, we'll spit it to the console as well.

app/Helpers/Helper.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,7 @@ public static function filetype_icon($filename)
11231123
'png' => 'far fa-image',
11241124
'webp' => 'far fa-image',
11251125
'avif' => 'far fa-image',
1126+
'svg' => 'fas fa-vector-square',
11261127
// word
11271128
'doc' => 'far fa-file-word',
11281129
'docx' => 'far fa-file-word',
@@ -1135,7 +1136,7 @@ public static function filetype_icon($filename)
11351136
//Text
11361137
'txt' => 'far fa-file-alt',
11371138
'rtf' => 'far fa-file-alt',
1138-
'xml' => 'far fa-file-alt',
1139+
'xml' => 'fas fa-code',
11391140
// Misc
11401141
'pdf' => 'far fa-file-pdf',
11411142
'lic' => 'far fa-save',
@@ -1148,41 +1149,7 @@ public static function filetype_icon($filename)
11481149
return 'far fa-file';
11491150
}
11501151

1151-
public static function show_file_inline($filename)
1152-
{
1153-
$extension = substr(strrchr($filename, '.'), 1);
1154-
1155-
if ($extension) {
1156-
switch ($extension) {
1157-
case 'jpg':
1158-
case 'jpeg':
1159-
case 'gif':
1160-
case 'png':
1161-
case 'webp':
1162-
case 'avif':
1163-
return true;
1164-
break;
1165-
default:
1166-
return false;
1167-
}
1168-
}
11691152

1170-
return false;
1171-
}
1172-
1173-
/**
1174-
* Generate a random encrypted password.
1175-
*
1176-
* @author Wes Hulette <[email protected]>
1177-
*
1178-
* @since 5.0.0
1179-
*
1180-
* @return string
1181-
*/
1182-
public static function generateEncyrptedPassword(): string
1183-
{
1184-
return bcrypt(self::generateUnencryptedPassword());
1185-
}
11861153

11871154
/**
11881155
* Get a random unencrypted password.

app/Helpers/StorageHelper.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Http\RedirectResponse;
88
use Symfony\Component\HttpFoundation\BinaryFileResponse;
99
use Symfony\Component\HttpFoundation\StreamedResponse;
10+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
1011
class StorageHelper
1112
{
1213
public static function downloader($filename, $disk = 'default') : BinaryFileResponse | RedirectResponse | StreamedResponse
@@ -25,4 +26,64 @@ public static function downloader($filename, $disk = 'default') : BinaryFileResp
2526
return Storage::disk($disk)->download($filename);
2627
}
2728
}
29+
30+
31+
/**
32+
* This determines the file types that should be allowed inline and checks their fileinfo extension
33+
* to determine that they are safe to display inline.
34+
*
35+
* @author <A. Gianotto> [<[email protected]]>
36+
* @since v7.0.14
37+
* @param $file_with_path
38+
* @return bool
39+
*/
40+
public static function allowSafeInline($file_with_path) {
41+
42+
$allowed_inline = [
43+
'pdf',
44+
'svg',
45+
'jpg',
46+
'gif',
47+
'svg',
48+
'avif',
49+
'webp',
50+
'png',
51+
];
52+
53+
54+
// The file exists and is allowed to be displayed inline
55+
if (Storage::exists($file_with_path) && (in_array(pathinfo($file_with_path, PATHINFO_EXTENSION), $allowed_inline))) {
56+
return true;
57+
}
58+
return false;
59+
60+
}
61+
62+
/**
63+
* Decide whether to show the file inline or download it.
64+
*/
65+
public static function showOrDownloadFile($file, $filename) {
66+
67+
$headers = [];
68+
69+
if (request('inline') == 'true') {
70+
71+
$headers = [
72+
'Content-Disposition' => 'inline',
73+
];
74+
75+
// This is NOT allowed as inline - force it to be displayed as text in the browser
76+
if (self::allowSafeInline($file) != true) {
77+
$headers = array_merge($headers, ['Content-Type' => 'text/plain']);
78+
}
79+
}
80+
81+
// Everything else seems okay, but the file doesn't exist on the server.
82+
if (Storage::missing($file)) {
83+
throw new FileNotFoundException();
84+
}
85+
86+
return Storage::download($file, $filename, $headers);
87+
88+
}
2889
}

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+
return redirect()->route('accessories.show', ['accessory' => $accessory])->with('error', trans('general.log_record_not_found'));
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/Api/StatuslabelsController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public function store(Request $request) : JsonResponse
9595
$request->except('deployable', 'pending', 'archived');
9696

9797
if (! $request->filled('type')) {
98-
return response()->json(Helper::formatStandardApiResponse('error', null, ['type' => ['Status label type is required.']]), 500);
98+
99+
return response()->json(Helper::formatStandardApiResponse('error', null, ['type' => ['Status label type is required.']]));
99100
}
100101

101102
$statuslabel = new Statuslabel;

app/Http/Controllers/Assets/AssetFilesController.php

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,43 +61,30 @@ 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)) {
67-
$this->authorize('view', $asset);
68-
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-
}
73-
74-
$file = 'private_uploads/assets/'.$log->filename;
64+
if ($asset = Asset::find($assetId)) {
7565

76-
if ($log->action_type == 'audit') {
77-
$file = 'private_uploads/audits/'.$log->filename;
78-
}
66+
$this->authorize('view', $asset);
7967

80-
if (! Storage::exists($file)) {
81-
return response('File '.$file.' not found on server', 404)
82-
->header('Content-Type', 'text/plain');
83-
}
68+
if ($log = Actionlog::whereNotNull('filename')->where('item_id', $asset->id)->find($fileId)) {
69+
$file = 'private_uploads/assets/'.$log->filename;
8470

85-
if (request('inline') == 'true') {
71+
if ($log->action_type == 'audit') {
72+
$file = 'private_uploads/audits/'.$log->filename;
73+
}
8674

87-
$headers = [
88-
'Content-Disposition' => 'inline',
89-
];
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+
}
9080

91-
return Storage::download($file, $log->filename, $headers);
9281
}
9382

94-
return StorageHelper::downloader($file);
83+
return redirect()->route('hardware.show', ['hardware' => $asset])->with('error', trans('general.log_record_not_found'));
9584
}
96-
// Prepare the error message
97-
$error = trans('admin/hardware/message.does_not_exist', ['id' => $fileId]);
9885

99-
// Redirect to the hardware management page
100-
return redirect()->route('hardware.index')->with('error', $error);
86+
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
87+
10188
}
10289

10390
/**

app/Http/Controllers/Assets/AssetsController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use App\Models\Setting;
1818
use App\Models\Statuslabel;
1919
use App\Models\User;
20-
use Illuminate\Support\Facades\Auth;
2120
use App\View\Label;
2221
use Carbon\Carbon;
2322
use Illuminate\Support\Facades\DB;

0 commit comments

Comments
 (0)