Skip to content

Commit cd266a6

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents d5ef7f3 + 0f49456 commit cd266a6

File tree

7 files changed

+71
-13
lines changed

7 files changed

+71
-13
lines changed

app/Http/Controllers/Api/AssetsController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ public function index(Request $request, $action = null, $upcoming_status = null)
116116
'asset_eol_date',
117117
'requestable',
118118
'jobtitle',
119+
// These are *relationships* so we wouldn't normally include them in this array,
120+
// since they would normally create a `column not found` error,
121+
// BUT we account for them in the ordering switch down at the end of this method
122+
// DO NOT ADD ANYTHING TO THIS LIST WITHOUT CHECKING THE ORDERING SWITCH BELOW!
123+
'company',
124+
'model',
125+
'location',
126+
'rtd_location',
127+
'category',
128+
'status_label',
129+
'manufacturer',
130+
'supplier',
131+
'jobtitle',
132+
'assigned_to',
133+
'created_by',
134+
119135
];
120136

121137
$all_custom_fields = CustomField::all(); //used as a 'cache' of custom fields throughout this page load
@@ -132,6 +148,7 @@ public function index(Request $request, $action = null, $upcoming_status = null)
132148
$filter = array_filter($filter, function ($key) use ($allowed_columns) {
133149
return in_array($key, $allowed_columns);
134150
}, ARRAY_FILTER_USE_KEY);
151+
135152
}
136153

137154
$assets = Asset::select('assets.*')

app/Http/Requests/StoreAssetRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function authorize(): bool
2626

2727
public function prepareForValidation(): void
2828
{
29+
parent::prepareForValidation(); // call ImageUploadRequest thing
2930
// Guard against users passing in an array for company_id instead of an integer.
3031
// If the company_id is not an integer then we simply use what was
3132
// provided to be caught by model level validation later.

app/Http/Traits/ConvertsBase64ToFiles.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Traits;
44

5+
use Illuminate\Validation\ValidationException;
56
use Illuminate\Http\UploadedFile;
67
use Illuminate\Support\Arr;
78
use Illuminate\Support\Collection;
@@ -38,28 +39,22 @@ protected function prepareForValidation()
3839
if (!$base64Contents) {
3940
return;
4041
}
41-
42-
// autogenerate filenames
43-
if ($filename == 'auto'){
44-
$header = explode(';', $base64Contents, 2)[0];
45-
// Grab the image type from the header while we're at it.
46-
$filename = $key . '.' . substr($header, strpos($header, '/')+1);
47-
}
4842

4943
// Generate a temporary path to store the Base64 contents
5044
$tempFilePath = tempnam(sys_get_temp_dir(), $filename);
5145

52-
// Store the contents using a stream, or by decoding manually
46+
// Store the contents using a stream, or throw an Error (which doesn't do anything?)
5347
if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) {
54-
$source = fopen($base64Contents, 'r');
48+
$source = fopen($base64Contents, 'r'); // PHP has special processing for "data:" URL's
5549
$destination = fopen($tempFilePath, 'w');
5650

5751
stream_copy_to_stream($source, $destination);
5852

5953
fclose($source);
6054
fclose($destination);
6155
} else {
62-
file_put_contents($tempFilePath, base64_decode($base64Contents, true));
56+
// TODO - to get a better error message here, can we maybe do something with modifying the errorBag?
57+
throw new ValidationException("Need Base64 URL starting with 'data:'"); // This doesn't actually throw?
6358
}
6459

6560
$uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true);

app/Models/Actionlog.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,10 @@ public function uploads_file_path()
493493
return 'private_uploads/eula-pdfs/'.$this->filename;
494494
}
495495

496+
if ($this->action_type == 'audit') {
497+
return 'private_uploads/audits/'.$this->filename;
498+
}
499+
496500
switch ($this->item_type) {
497501
case Accessory::class:
498502
return 'private_uploads/accessories/'.$this->filename;

app/Models/Asset.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,16 +1881,32 @@ function ($query) use ($filter) {
18811881
);
18821882
}
18831883

1884-
if ($fieldname =='assigned_to') {
1884+
if ($fieldname == 'assigned_to') {
18851885
$query->whereHasMorph(
18861886
'assignedTo', [User::class], function ($query) use ($search_val) {
18871887
$query->where(
18881888
function ($query) use ($search_val) {
18891889
$query->where('users.first_name', 'LIKE', '%'.$search_val.'%')
1890-
->orWhere('users.last_name', 'LIKE', '%'.$search_val.'%');
1890+
->orWhere('users.last_name', 'LIKE', '%'.$search_val.'%')
1891+
->orWhere('users.username', 'LIKE', '%'.$search_val.'%');
18911892
}
18921893
);
18931894
}
1895+
)->orWhereHasMorph(
1896+
'assignedTo', [Location::class], function ($query) use ($search_val) {
1897+
$query->where('locations.name', 'LIKE', '%'.$search_val.'%');
1898+
}
1899+
)->orWhereHasMorph(
1900+
'assignedTo', [Asset::class], function ($query) use ($search_val) {
1901+
$query->where(
1902+
function ($query) use ($search_val) {
1903+
// Don't use the asset table prefix here because it will pull from the original asset,
1904+
// not the subselect we're doing here to get the assigned asset
1905+
$query->where('name', 'LIKE', '%'.$search_val.'%')
1906+
->orWhere('asset_tag', 'LIKE', '%'.$search_val.'%');
1907+
}
1908+
);
1909+
}
18941910
);
18951911
}
18961912

resources/views/hardware/view.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1391,7 +1391,7 @@ class="table table-striped snipe-table"
13911391
<th data-visible="true" data-field="icon" style="width: 40px;" class="hidden-xs" data-formatter="iconFormatter">{{ trans('admin/hardware/table.icon') }}</th>
13921392
<th data-visible="true" data-field="created_at" data-sortable="true" data-formatter="dateDisplayFormatter">{{ trans('general.date') }}</th>
13931393
<th data-visible="true" data-field="admin" data-formatter="usersLinkObjFormatter">{{ trans('general.created_by') }}</th>
1394-
<th class="col-sm-2" data-field="file" data-sortable="true" data-visible="false" data-formatter="fileUploadNameFormatter">{{ trans('general.file_name') }}</th>
1394+
<th class="col-sm-2" data-field="file" data-sortable="true" data-visible="false" data-formatter="fileNameFormatter">{{ trans('general.file_name') }}</th>
13951395
<th data-field="note">{{ trans('general.notes') }}</th>
13961396
<th data-visible="false" data-field="file" data-visible="false" data-formatter="fileDownloadButtonsFormatter">{{ trans('general.download') }}</th>
13971397
<th data-field="log_meta" data-visible="true" data-formatter="changeLogFormatter">{{ trans('admin/hardware/table.changed')}}</th>

tests/Feature/Assets/Api/StoreAssetTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Models\Supplier;
1212
use App\Models\User;
1313
use Illuminate\Support\Facades\Crypt;
14+
use Illuminate\Support\Facades\Storage;
1415
use Illuminate\Testing\Fluent\AssertableJson;
1516
use PHPUnit\Framework\Attributes\DataProvider;
1617
use Tests\TestCase;
@@ -831,4 +832,28 @@ public function testPermissionNeededToStoreEncryptedField()
831832
$asset = Asset::findOrFail($response['payload']['id']);
832833
$this->assertEquals('This is encrypted field', Crypt::decrypt($asset->{$field->db_column_name()}));
833834
}
835+
836+
public function testBase64AssetImages()
837+
{
838+
$status = Statuslabel::factory()->readyToDeploy()->create();
839+
$model = AssetModel::factory()->create();
840+
$superuser = User::factory()->superuser()->create();
841+
842+
$response = $this->actingAsForApi($superuser)
843+
->postJson(route('api.assets.store'), [
844+
'model_id' => $model->id,
845+
'status_id' => $status->id,
846+
'asset_tag' => '1234',
847+
'image' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAEsAQMAAADXeXeBAAAABlBMVEX+AAD///+KQee0AAAACXBIWXMAAAsSAAALEgHS3X78AAAAB3RJTUUH5QQbCAoNcoiTQAAAACZJREFUaN7twTEBAAAAwqD1T20JT6AAAAAAAAAAAAAAAAAAAICnATvEAAEnf54JAAAAAElFTkSuQmCC'
848+
])
849+
->assertStatusMessageIs('success')
850+
->assertOk()
851+
->json();
852+
853+
$asset = Asset::findOrFail($response['payload']['id']);
854+
$this->assertEquals($asset->asset_tag, '1234');
855+
$image_data = Storage::disk('public')->get(app('assets_upload_path') . e($asset->image));
856+
//$this->assertEquals('3d67fb99a0b6926e350f7b71397525d7a6b936c1', sha1($image_data)); //this doesn't work because the image gets resized - use the resized hash instead
857+
$this->assertEquals('db2e13ba04318c99058ca429d67777322f48566b', sha1($image_data));
858+
}
834859
}

0 commit comments

Comments
 (0)