From 491a788cd0b68bb47d58c000e1947eb6d4b944b4 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Wed, 23 Jun 2021 11:22:41 +0300 Subject: [PATCH 01/37] testing image upload via api --- app/Http/Controllers/Api/AccessoriesController.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index edbf90d15c..2224e4d08e 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -13,6 +13,7 @@ use Carbon\Carbon; use Auth; use DB; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; class AccessoriesController extends Controller { @@ -86,10 +87,10 @@ class AccessoriesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Accessory::class); $accessory = new Accessory; @@ -180,11 +181,11 @@ class AccessoriesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Accessory::class); $accessory = Accessory::findOrFail($id); From 695bf1e15f30f2174568ca8e01e1777453d9b2d2 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Mon, 28 Jun 2021 07:58:08 +0300 Subject: [PATCH 02/37] save images --- app/Http/Controllers/Api/AccessoriesController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index 2224e4d08e..5a5265c0cb 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -95,7 +95,8 @@ class AccessoriesController extends Controller $this->authorize('create', Accessory::class); $accessory = new Accessory; $accessory->fill($request->all()); - + $accessory = $request->handleImages($accessory); + if ($accessory->save()) { return response()->json(Helper::formatStandardApiResponse('success', $accessory, trans('admin/accessories/message.create.success'))); } From e0c5205e9b885b40fef4a593d547082dc057ddc2 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Mon, 28 Jun 2021 08:11:33 +0300 Subject: [PATCH 03/37] save images --- app/Http/Controllers/Api/AccessoriesController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index 5a5265c0cb..ab0b83bfa0 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -96,7 +96,7 @@ class AccessoriesController extends Controller $accessory = new Accessory; $accessory->fill($request->all()); $accessory = $request->handleImages($accessory); - + if ($accessory->save()) { return response()->json(Helper::formatStandardApiResponse('success', $accessory, trans('admin/accessories/message.create.success'))); } @@ -191,6 +191,8 @@ class AccessoriesController extends Controller $this->authorize('update', Accessory::class); $accessory = Accessory::findOrFail($id); $accessory->fill($request->all()); + + $accessory = $request->handleImages($accessory); if ($accessory->save()) { return response()->json(Helper::formatStandardApiResponse('success', $accessory, trans('admin/accessories/message.update.success'))); From 8d4219759ef75489feebcc5606b744047c053db3 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:25:47 +0300 Subject: [PATCH 04/37] Support images and multipart requests --- app/Http/Controllers/Api/AccessoriesController.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index ab0b83bfa0..29de7115f4 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -87,7 +87,7 @@ class AccessoriesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param App\Http\Requests\ImageUploadRequest $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ public function store(ImageUploadRequest $request) @@ -182,7 +182,7 @@ class AccessoriesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param App\Http\Requests\ImageUploadRequest $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ @@ -191,7 +191,6 @@ class AccessoriesController extends Controller $this->authorize('update', Accessory::class); $accessory = Accessory::findOrFail($id); $accessory->fill($request->all()); - $accessory = $request->handleImages($accessory); if ($accessory->save()) { From aa402bf8966f677346adde1480dfcf4c56f45b6c Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:26:15 +0300 Subject: [PATCH 05/37] Support images and multipart requests --- app/Http/Controllers/Api/AssetModelsController.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index abc1f65a5a..9bc3e46cfe 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -9,6 +9,7 @@ use App\Http\Transformers\SelectlistTransformer; use App\Models\Asset; use App\Models\AssetModel; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; /** @@ -107,14 +108,15 @@ class AssetModelsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', AssetModel::class); $assetmodel = new AssetModel; $assetmodel->fill($request->all()); + $assetmodel = $request->handleImages($assetmodel); if ($assetmodel->save()) { return response()->json(Helper::formatStandardApiResponse('success', $assetmodel, trans('admin/models/message.create.success'))); @@ -159,16 +161,17 @@ class AssetModelsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', AssetModel::class); $assetmodel = AssetModel::findOrFail($id); $assetmodel->fill($request->all()); - + $assetmodel = $request->handleImages($assetmodel); + /** * Allow custom_fieldset_id to override and populate fieldset_id. * This is stupid, but required for legacy API support. From e8a4059db97078736a9a9aedef43d050f2a3bb70 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:26:24 +0300 Subject: [PATCH 06/37] Support images and multipart requests --- app/Http/Controllers/Api/AssetsController.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 20f8db1f23..dbd092a525 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -21,6 +21,7 @@ use Auth; use Carbon\Carbon; use DB; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; use Input; use Paginator; use Slack; @@ -425,11 +426,11 @@ class AssetsController extends Controller * Accepts a POST request to create a new asset * * @author [A. Gianotto] [] - * @param Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @since [v4.0] * @return JsonResponse */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Asset::class); @@ -474,6 +475,8 @@ class AssetsController extends Controller $asset->image = $saved_image_path; } + $asset = $request->handleImages($asset); + // Update custom fields in the database. // Validation for these fields is handled through the AssetRequest form request $model = AssetModel::find($request->get('model_id')); @@ -540,11 +543,11 @@ class AssetsController extends Controller * Accepts a POST request to update an asset * * @author [A. Gianotto] [] - * @param Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @since [v4.0] * @return JsonResponse */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Asset::class); @@ -584,6 +587,8 @@ class AssetsController extends Controller } } + $asset = $request->handleImages($asset); + // Update custom fields if (($model = AssetModel::find($asset->model_id)) && (isset($model->fieldset))) { foreach ($model->fieldset->fields as $field) { From 4ed9788a0e71eda855d1dcce482696887bfc707e Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:26:45 +0300 Subject: [PATCH 07/37] Support images and multipart requests --- app/Http/Controllers/Api/CategoriesController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php index ae085c0f35..9010bf1500 100644 --- a/app/Http/Controllers/Api/CategoriesController.php +++ b/app/Http/Controllers/Api/CategoriesController.php @@ -8,6 +8,7 @@ use App\Http\Transformers\CategoriesTransformer; use App\Http\Transformers\SelectlistTransformer; use App\Models\Category; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class CategoriesController extends Controller @@ -54,14 +55,15 @@ class CategoriesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Category::class); $category = new Category; $category->fill($request->all()); + $category = $request->handleImages($category); if ($category->save()) { return response()->json(Helper::formatStandardApiResponse('success', $category, trans('admin/categories/message.create.success'))); @@ -92,15 +94,16 @@ class CategoriesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Category::class); $category = Category::findOrFail($id); $category->fill($request->all()); + $category = $request->handleImages($category); if ($category->save()) { return response()->json(Helper::formatStandardApiResponse('success', $category, trans('admin/categories/message.update.success'))); From 604a0b6df150f94794e2159f8f27d008519cd530 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:26:59 +0300 Subject: [PATCH 08/37] Support images and multipart requests --- app/Http/Controllers/Api/CompaniesController.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php index c56ed9f871..8b471f27b3 100644 --- a/app/Http/Controllers/Api/CompaniesController.php +++ b/app/Http/Controllers/Api/CompaniesController.php @@ -8,6 +8,7 @@ use App\Http\Transformers\CompaniesTransformer; use App\Http\Transformers\SelectlistTransformer; use App\Models\Company; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class CompaniesController extends Controller @@ -65,15 +66,16 @@ class CompaniesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Company::class); $company = new Company; $company->fill($request->all()); - + $company = $request->handleImages($company); + if ($company->save()) { return response()->json(Helper::formatStandardApiResponse('success', (new CompaniesTransformer)->transformCompany($company), trans('admin/companies/message.create.success'))); } @@ -104,15 +106,16 @@ class CompaniesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Company::class); $company = Company::findOrFail($id); $company->fill($request->all()); + $company = $request->handleImages($company); if ($company->save()) { return response() From a7b1e31776e24454a709dfd906ab11e2e0c4ce30 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:27:08 +0300 Subject: [PATCH 09/37] Support images and multipart requests --- app/Http/Controllers/Api/ComponentsController.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index d7af18b21c..d7654a920d 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -8,6 +8,7 @@ use App\Http\Transformers\ComponentsTransformer; use App\Models\Company; use App\Models\Component; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; class ComponentsController extends Controller { @@ -78,14 +79,15 @@ class ComponentsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Component::class); $component = new Component; $component->fill($request->all()); + $component = $request->handleImages($component); if ($component->save()) { return response()->json(Helper::formatStandardApiResponse('success', $component, trans('admin/components/message.create.success'))); @@ -116,15 +118,17 @@ class ComponentsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Component::class); $component = Component::findOrFail($id); $component->fill($request->all()); + $component = $request->handleImages($component); + if ($component->save()) { return response()->json(Helper::formatStandardApiResponse('success', $component, trans('admin/components/message.update.success'))); From 4ba75291e41fa9cc51e3c159b4ec8ef9b0bbbd8c Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:27:18 +0300 Subject: [PATCH 10/37] Support images and multipart requests --- app/Http/Controllers/Api/ConsumablesController.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index e67215a00d..146311811f 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -10,6 +10,7 @@ use App\Models\Company; use App\Models\Consumable; use App\Models\User; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; class ConsumablesController extends Controller { @@ -90,14 +91,15 @@ class ConsumablesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Consumable::class); $consumable = new Consumable; $consumable->fill($request->all()); + $consumable = $request->handleImages($consumable); if ($consumable->save()) { return response()->json(Helper::formatStandardApiResponse('success', $consumable, trans('admin/consumables/message.create.success'))); @@ -125,16 +127,17 @@ class ConsumablesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Consumable::class); $consumable = Consumable::findOrFail($id); $consumable->fill($request->all()); - + $consumable = $request->handleImages($consumable); + if ($consumable->save()) { return response()->json(Helper::formatStandardApiResponse('success', $consumable, trans('admin/consumables/message.update.success'))); } From fc8498972e44f5f241999ea7337bb51acf158f07 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:28:32 +0300 Subject: [PATCH 11/37] Support images and multipart requests --- app/Http/Controllers/Api/DepartmentsController.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php index b692d378b7..6c3da83539 100644 --- a/app/Http/Controllers/Api/DepartmentsController.php +++ b/app/Http/Controllers/Api/DepartmentsController.php @@ -9,6 +9,7 @@ use App\Http\Transformers\SelectlistTransformer; use App\Models\Department; use Auth; use Illuminate\Http\Request; +use app\http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class DepartmentsController extends Controller @@ -73,14 +74,16 @@ class DepartmentsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Department::class); $department = new Department; $department->fill($request->all()); + $department = $request->handleImages($department) + $department->user_id = Auth::user()->id; $department->manager_id = ($request->filled('manager_id' ) ? $request->input('manager_id') : null); @@ -111,15 +114,16 @@ class DepartmentsController extends Controller * * @author [A. Gianotto] [] * @since [v5.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Department::class); $department = Department::findOrFail($id); $department->fill($request->all()); + $department = $request->handleImages($department); if ($department->save()) { return response()->json(Helper::formatStandardApiResponse('success', $department, trans('admin/departments/message.update.success'))); From f9da83bc467e79a9ea8d6efd417eb29aeea4127b Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:28:42 +0300 Subject: [PATCH 12/37] Support images and multipart requests --- app/Http/Controllers/Api/LocationsController.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php index ed71685159..6d70e7aaf1 100644 --- a/app/Http/Controllers/Api/LocationsController.php +++ b/app/Http/Controllers/Api/LocationsController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; use App\Http\Controllers\Controller; use App\Helpers\Helper; use App\Models\Location; @@ -86,14 +87,15 @@ class LocationsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Location::class); $location = new Location; $location->fill($request->all()); + $location = $request->handleImages($location); if ($location->save()) { return response()->json(Helper::formatStandardApiResponse('success', (new LocationsTransformer)->transformLocation($location), trans('admin/locations/message.create.success'))); @@ -141,17 +143,17 @@ class LocationsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\JsonResponse */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Location::class); $location = Location::findOrFail($id); $location->fill($request->all()); - + $location = $request->handleImages($location); if ($location->isValid()) { From a936744e2e83c6a0ad5ed47a6528fb3dd96bdf89 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:28:52 +0300 Subject: [PATCH 13/37] Support images and multipart requests --- app/Http/Controllers/Api/ManufacturersController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index 6b9212137f..aa55b92b33 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -8,6 +8,7 @@ use App\Http\Transformers\ManufacturersTransformer; use App\Http\Transformers\SelectlistTransformer; use App\Models\Manufacturer; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class ManufacturersController extends Controller @@ -59,14 +60,15 @@ class ManufacturersController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Manufacturer::class); $manufacturer = new Manufacturer; $manufacturer->fill($request->all()); + $manufacturer = $request->handleImages($manufacturer); if ($manufacturer->save()) { return response()->json(Helper::formatStandardApiResponse('success', $manufacturer, trans('admin/manufacturers/message.create.success'))); @@ -96,15 +98,16 @@ class ManufacturersController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Manufacturer::class); $manufacturer = Manufacturer::findOrFail($id); $manufacturer->fill($request->all()); + $manufacturer = $request->handleImages($manufacturer) if ($manufacturer->save()) { return response()->json(Helper::formatStandardApiResponse('success', $manufacturer, trans('admin/manufacturers/message.update.success'))); From 8c0be3aa878ade8832a06a0632d795639e45da35 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:29:07 +0300 Subject: [PATCH 14/37] Support images and multipart requests --- app/Http/Controllers/Api/SuppliersController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Api/SuppliersController.php b/app/Http/Controllers/Api/SuppliersController.php index 5616913ff3..3985bac5b8 100644 --- a/app/Http/Controllers/Api/SuppliersController.php +++ b/app/Http/Controllers/Api/SuppliersController.php @@ -8,6 +8,7 @@ use App\Http\Transformers\SelectlistTransformer; use App\Http\Transformers\SuppliersTransformer; use App\Models\Supplier; use Illuminate\Http\Request; +use App\http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class SuppliersController extends Controller @@ -55,14 +56,15 @@ class SuppliersController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(ImageUploadRequest $request) { $this->authorize('create', Supplier::class); $supplier = new Supplier; $supplier->fill($request->all()); + $supplier = $request->handleImages($supplier); if ($supplier->save()) { return response()->json(Helper::formatStandardApiResponse('success', $supplier, trans('admin/suppliers/message.create.success'))); @@ -92,15 +94,16 @@ class SuppliersController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @param \Illuminate\Http\Request $request + * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(ImageUploadRequest $request, $id) { $this->authorize('update', Supplier::class); $supplier = Supplier::findOrFail($id); $supplier->fill($request->all()); + $supplier = $request->handleImages($supplier); if ($supplier->save()) { return response()->json(Helper::formatStandardApiResponse('success', $supplier, trans('admin/suppliers/message.update.success'))); From b49733832cc6ebe615abaf395263e29689d5587e Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 12:29:17 +0300 Subject: [PATCH 15/37] Support images and multipart requests --- app/Http/Controllers/Api/UsersController.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index b8bbf39d4d..5f6e0ae74c 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -16,6 +16,7 @@ use App\Models\License; use App\Models\User; use Auth; use Illuminate\Http\Request; +use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class UsersController extends Controller @@ -237,7 +238,8 @@ class UsersController extends Controller $tmp_pass = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20); $user->password = bcrypt($request->get('password', $tmp_pass)); - + app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar'); + if ($user->save()) { if ($request->filled('groups')) { $user->groups()->sync($request->input('groups')); @@ -290,7 +292,7 @@ class UsersController extends Controller $user->fill($request->all()); - + if ($user->id == $request->input('manager_id')) { return response()->json(Helper::formatStandardApiResponse('error', null, 'You cannot be your own manager')); } @@ -320,6 +322,9 @@ class UsersController extends Controller Asset::where('assigned_type', User::class) ->where('assigned_to', $user->id)->update(['location_id' => $request->input('location_id', null)]); + + app('App\Http\Requests\ImageUploadRequest')->handleImages($user, 600, 'image', 'avatars', 'avatar'); + if ($user->save()) { // Sync group memberships: From b0aa26e6cb18440832d6fe4d501eccb3835d58f6 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 29 Jun 2021 14:25:20 +0300 Subject: [PATCH 16/37] Support images and multipart requests --- app/Http/Controllers/Api/DepartmentsController.php | 2 +- app/Http/Controllers/Api/ManufacturersController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php index 6c3da83539..6b3691c4e2 100644 --- a/app/Http/Controllers/Api/DepartmentsController.php +++ b/app/Http/Controllers/Api/DepartmentsController.php @@ -82,7 +82,7 @@ class DepartmentsController extends Controller $this->authorize('create', Department::class); $department = new Department; $department->fill($request->all()); - $department = $request->handleImages($department) + $department = $request->handleImages($department); $department->user_id = Auth::user()->id; $department->manager_id = ($request->filled('manager_id' ) ? $request->input('manager_id') : null); diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index aa55b92b33..0301ae587c 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -107,7 +107,7 @@ class ManufacturersController extends Controller $this->authorize('update', Manufacturer::class); $manufacturer = Manufacturer::findOrFail($id); $manufacturer->fill($request->all()); - $manufacturer = $request->handleImages($manufacturer) + $manufacturer = $request->handleImages($manufacturer); if ($manufacturer->save()) { return response()->json(Helper::formatStandardApiResponse('success', $manufacturer, trans('admin/manufacturers/message.update.success'))); From 6048acc95d342b64030f5b7aecdaad6abb21436f Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Wed, 30 Jun 2021 06:36:12 +0300 Subject: [PATCH 17/37] fixed capitalization --- app/Http/Controllers/Api/DepartmentsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php index 6b3691c4e2..da8eeaf2f5 100644 --- a/app/Http/Controllers/Api/DepartmentsController.php +++ b/app/Http/Controllers/Api/DepartmentsController.php @@ -9,7 +9,7 @@ use App\Http\Transformers\SelectlistTransformer; use App\Models\Department; use Auth; use Illuminate\Http\Request; -use app\http\Requests\ImageUploadRequest; +use App\http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class DepartmentsController extends Controller From e15159b9c3e0342a330b16e5dcae4f5ba7b0fc4d Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Fri, 2 Jul 2021 10:17:29 +0300 Subject: [PATCH 18/37] trait base64 encoded files --- app/Http/Traits/ConvertsBase64toFiles.php | 111 ++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 app/Http/Traits/ConvertsBase64toFiles.php diff --git a/app/Http/Traits/ConvertsBase64toFiles.php b/app/Http/Traits/ConvertsBase64toFiles.php new file mode 100644 index 0000000000..5d4f20b5af --- /dev/null +++ b/app/Http/Traits/ConvertsBase64toFiles.php @@ -0,0 +1,111 @@ +request; + } + + /** + * Helper method to get the uploaded files bag. + * + * @return FileBag + */ + private function uploadFilesBag(): FileBag + { + return $this->files; + } + + /** + * Pulls the Base64 contents for each file key and creates + * an UploadedFile instance from it and sets it on the + * request. + * + * @return void + */ + protected function prepareForValidation() + { + $flattened = Arr::dot($this->base64FileKeys()); + + Collection::make($flattened)->each(function ($filename, $key) { + rescue(function () use ($key, $filename) { + // dont process plain files + if ( $this->file($key)){ + return; + } + + $base64Contents = $this->input($key); + + if (!$base64Contents) { + return; + } + + // autogenerate filenames + if ($filename == 'auto'){ + $header = explode(';', $base64Contents, 2)[0]; + // Grab the image type from the header while we're at it. + $filename = $key . '.' . substr($header, strpos($header, '/')+1); + } + + // Generate a temporary path to store the Base64 contents + $tempFilePath = tempnam(sys_get_temp_dir(), $filename); + + // Store the contents using a stream, or by decoding manually + if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) { + $source = fopen($base64Contents, 'r'); + $destination = fopen($tempFilePath, 'w'); + + stream_copy_to_stream($source, $destination); + + fclose($source); + fclose($destination); + } else { + file_put_contents($tempFilePath, base64_decode($base64Contents, true)); + } + + $uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true); + + $body = $this->bodyParametersBag()->all(); + Arr::forget($body, $key); + $this->bodyParametersBag()->replace($body); + + $files = $this->uploadFilesBag()->all(); + Arr::set($files, $key, $uploadedFile); + $this->uploadFilesBag()->replace($files); + }, null, false); + }); + } +} +/** + * This file based on https://github.com/protonemedia/laravel-mixins/tree/master/src/Request + * + * MIT License + * + * Copyright (c) Protone Media B.V. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ \ No newline at end of file From e846e6ac76c989dd00335bd0cf5fa45b8fd11615 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Fri, 2 Jul 2021 10:18:18 +0300 Subject: [PATCH 19/37] trait base64 encoded files --- app/Http/Requests/ImageUploadRequest.php | 19 +++++++++++++++---- app/Http/Traits/ConvertsBase64toFiles.php | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 3f4a7b64b0..73ffc8eed3 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -5,10 +5,13 @@ namespace App\Http\Requests; use App\Models\SnipeModel; use Intervention\Image\Facades\Image; use enshrined\svgSanitize\Sanitizer; +use App\Traits\ConvertsBase64ToFiles; use Storage; class ImageUploadRequest extends Request { + use ConvertsBase64ToFiles; + /** * Determine if the user is authorized to make this request. * @@ -26,10 +29,11 @@ class ImageUploadRequest extends Request */ public function rules() { - return [ - 'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp', - 'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp', - ]; + + return [ + 'image' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp', + 'avatar' => 'mimes:png,gif,jpg,jpeg,svg,bmp,svg+xml,webp', + ]; } public function response(array $errors) @@ -37,6 +41,13 @@ class ImageUploadRequest extends Request return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag); } + protected function base64FileKeys(): array + { + return [ + 'image' => 'auto', + ]; + } + /** * Handle and store any images attached to request * @param SnipeModel $item Item the image is associated with diff --git a/app/Http/Traits/ConvertsBase64toFiles.php b/app/Http/Traits/ConvertsBase64toFiles.php index 5d4f20b5af..71d316f848 100644 --- a/app/Http/Traits/ConvertsBase64toFiles.php +++ b/app/Http/Traits/ConvertsBase64toFiles.php @@ -1,6 +1,6 @@ Date: Fri, 2 Jul 2021 10:29:26 +0300 Subject: [PATCH 20/37] trait base64 encoded files --- app/Http/Requests/ImageUploadRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 73ffc8eed3..1bde3edf95 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -5,7 +5,7 @@ namespace App\Http\Requests; use App\Models\SnipeModel; use Intervention\Image\Facades\Image; use enshrined\svgSanitize\Sanitizer; -use App\Traits\ConvertsBase64ToFiles; +use App\Http\Traits\ConvertsBase64ToFiles; use Storage; class ImageUploadRequest extends Request From f59f3dbde435a720360a7eef85c66d9c296239f5 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Fri, 2 Jul 2021 10:43:15 +0300 Subject: [PATCH 21/37] case case again.. --- .../{ConvertsBase64toFiles.php => ConvertsBase64ToFiles.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/Http/Traits/{ConvertsBase64toFiles.php => ConvertsBase64ToFiles.php} (100%) diff --git a/app/Http/Traits/ConvertsBase64toFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.php similarity index 100% rename from app/Http/Traits/ConvertsBase64toFiles.php rename to app/Http/Traits/ConvertsBase64ToFiles.php From d48a9d549df244202a659a861a8644869b72c15a Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Sun, 4 Jul 2021 13:37:05 +0300 Subject: [PATCH 22/37] Try without trait --- app/Http/Requests/ImageUploadRequest.php | 90 +++++++++++++++++++++++- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 1bde3edf95..fe1a0897db 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -5,12 +5,11 @@ namespace App\Http\Requests; use App\Models\SnipeModel; use Intervention\Image\Facades\Image; use enshrined\svgSanitize\Sanitizer; -use App\Http\Traits\ConvertsBase64ToFiles; + use Storage; class ImageUploadRequest extends Request { - use ConvertsBase64ToFiles; /** * Determine if the user is authorized to make this request. @@ -40,7 +39,10 @@ class ImageUploadRequest extends Request { return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag); } - + + /** + * Fields that should be traited from base64 to files + */ protected function base64FileKeys(): array { return [ @@ -176,4 +178,86 @@ class ImageUploadRequest extends Request return $item; } + + + /** + * Helper method to get the body parameters bag. + * + * @return \Symfony\Component\HttpFoundation\ParameterBag + */ + private function bodyParametersBag(): ParameterBag + { + return $this->request; + } + + /** + * Helper method to get the uploaded files bag. + * + * @return FileBag + */ + private function uploadFilesBag(): FileBag + { + return $this->files; + } + + /** + * Pulls the Base64 contents for each file key and creates + * an UploadedFile instance from it and sets it on the + * request. + * + * @return void + */ + protected function prepareForValidation() + { + $flattened = Arr::dot($this->base64FileKeys()); + + Collection::make($flattened)->each(function ($filename, $key) { + rescue(function () use ($key, $filename) { + // dont process plain files + if ( $this->file($key)){ + return; + } + + $base64Contents = $this->input($key); + + if (!$base64Contents) { + return; + } + + // autogenerate filenames + if ($filename == 'auto'){ + $header = explode(';', $base64Contents, 2)[0]; + // Grab the image type from the header while we're at it. + $filename = $key . '.' . substr($header, strpos($header, '/')+1); + } + + // Generate a temporary path to store the Base64 contents + $tempFilePath = tempnam(sys_get_temp_dir(), $filename); + + // Store the contents using a stream, or by decoding manually + if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) { + $source = fopen($base64Contents, 'r'); + $destination = fopen($tempFilePath, 'w'); + + stream_copy_to_stream($source, $destination); + + fclose($source); + fclose($destination); + } else { + file_put_contents($tempFilePath, base64_decode($base64Contents, true)); + } + + $uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true); + + $body = $this->bodyParametersBag()->all(); + Arr::forget($body, $key); + $this->bodyParametersBag()->replace($body); + + $files = $this->uploadFilesBag()->all(); + Arr::set($files, $key, $uploadedFile); + $this->uploadFilesBag()->replace($files); + }, null, false); + }); + } + } From 02fb7ac03e591a9441e544d8e31b3379662f5842 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Sun, 4 Jul 2021 13:39:46 +0300 Subject: [PATCH 23/37] Try without trait --- app/Http/Requests/ImageUploadRequest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index fe1a0897db..39d8287d5b 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -5,7 +5,11 @@ namespace App\Http\Requests; use App\Models\SnipeModel; use Intervention\Image\Facades\Image; use enshrined\svgSanitize\Sanitizer; - +use Illuminate\Support\Arr; +use Illuminate\Support\Collection; +use Illuminate\Support\Str; +use Symfony\Component\HttpFoundation\FileBag; +use Symfony\Component\HttpFoundation\ParameterBag; use Storage; class ImageUploadRequest extends Request From 013df747d7aa77bf1f338eb998887fea8cea5d01 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Mon, 5 Jul 2021 09:59:19 +0300 Subject: [PATCH 24/37] debug trait --- app/Http/Requests/ImageUploadRequest.php | 88 +---------------------- app/Http/Traits/ConvertsBase64ToFiles.php | 4 ++ 2 files changed, 6 insertions(+), 86 deletions(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 39d8287d5b..a6da2e3e40 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -5,15 +5,12 @@ namespace App\Http\Requests; use App\Models\SnipeModel; use Intervention\Image\Facades\Image; use enshrined\svgSanitize\Sanitizer; -use Illuminate\Support\Arr; -use Illuminate\Support\Collection; -use Illuminate\Support\Str; -use Symfony\Component\HttpFoundation\FileBag; -use Symfony\Component\HttpFoundation\ParameterBag; +use App\Http\Traits\ConvertsBase64ToFiles; use Storage; class ImageUploadRequest extends Request { + use ConvertsBase64ToFiles; /** * Determine if the user is authorized to make this request. @@ -182,86 +179,5 @@ class ImageUploadRequest extends Request return $item; } - - /** - * Helper method to get the body parameters bag. - * - * @return \Symfony\Component\HttpFoundation\ParameterBag - */ - private function bodyParametersBag(): ParameterBag - { - return $this->request; - } - - /** - * Helper method to get the uploaded files bag. - * - * @return FileBag - */ - private function uploadFilesBag(): FileBag - { - return $this->files; - } - - /** - * Pulls the Base64 contents for each file key and creates - * an UploadedFile instance from it and sets it on the - * request. - * - * @return void - */ - protected function prepareForValidation() - { - $flattened = Arr::dot($this->base64FileKeys()); - - Collection::make($flattened)->each(function ($filename, $key) { - rescue(function () use ($key, $filename) { - // dont process plain files - if ( $this->file($key)){ - return; - } - - $base64Contents = $this->input($key); - - if (!$base64Contents) { - return; - } - - // autogenerate filenames - if ($filename == 'auto'){ - $header = explode(';', $base64Contents, 2)[0]; - // Grab the image type from the header while we're at it. - $filename = $key . '.' . substr($header, strpos($header, '/')+1); - } - - // Generate a temporary path to store the Base64 contents - $tempFilePath = tempnam(sys_get_temp_dir(), $filename); - - // Store the contents using a stream, or by decoding manually - if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) { - $source = fopen($base64Contents, 'r'); - $destination = fopen($tempFilePath, 'w'); - - stream_copy_to_stream($source, $destination); - - fclose($source); - fclose($destination); - } else { - file_put_contents($tempFilePath, base64_decode($base64Contents, true)); - } - - $uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true); - - $body = $this->bodyParametersBag()->all(); - Arr::forget($body, $key); - $this->bodyParametersBag()->replace($body); - - $files = $this->uploadFilesBag()->all(); - Arr::set($files, $key, $uploadedFile); - $this->uploadFilesBag()->replace($files); - }, null, false); - }); - } - } diff --git a/app/Http/Traits/ConvertsBase64ToFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.php index 71d316f848..13c939e4db 100644 --- a/app/Http/Traits/ConvertsBase64ToFiles.php +++ b/app/Http/Traits/ConvertsBase64ToFiles.php @@ -88,10 +88,14 @@ trait ConvertsBase64ToFiles $body = $this->bodyParametersBag()->all(); Arr::forget($body, $key); $this->bodyParametersBag()->replace($body); + \Log::debug("Trait: file field $key replaced:". $request->has($key)); + $files = $this->uploadFilesBag()->all(); Arr::set($files, $key, $uploadedFile); $this->uploadFilesBag()->replace($files); + \Log::debug("Trait: file field $key inserted:". $request->hasFile($key)); + }, null, false); }); } From d66ef233bfd46549f5cc1d8f8a49f994e53a9ad9 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 06:25:37 +0300 Subject: [PATCH 25/37] handle files via standard field --- app/Http/Requests/ImageUploadRequest.php | 16 +++++++++++++--- app/Http/Traits/ConvertsBase64ToFiles.php | 19 ++++++++----------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index a6da2e3e40..46f47e7480 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -6,6 +6,7 @@ use App\Models\SnipeModel; use Intervention\Image\Facades\Image; use enshrined\svgSanitize\Sanitizer; use App\Http\Traits\ConvertsBase64ToFiles; +use Illuminate\Http\UploadedFile; use Storage; class ImageUploadRequest extends Request @@ -91,14 +92,23 @@ class ImageUploadRequest extends Request \Log::debug('Form fieldname is: '.$form_fieldname); \Log::debug('DB fieldname is: '.$use_db_field); \Log::debug('Trying to upload to '. $path); + + // ConvertBase64ToFiles just changes object type, + // as it cannot currently insert files to $this->files + if ($this->offsetGet($form_fieldname) instanceof UploadedFile) { + $image=$this->offsetGet($form_fieldname); + } else { + if ($this->hasFile($form_fieldname)) { + $image = $this->file($form_fieldname); + } + } - \Log::debug($this->file()); + \Log::debug($image); - if ($this->hasFile($form_fieldname)) { + if (isset($image)) { if (!config('app.lock_passwords')) { - $image = $this->file($form_fieldname); $ext = $image->getClientOriginalExtension(); $file_name = $type.'-'.$form_fieldname.'-'.str_random(10).'.'.$ext; diff --git a/app/Http/Traits/ConvertsBase64ToFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.php index 13c939e4db..793746dbb6 100644 --- a/app/Http/Traits/ConvertsBase64ToFiles.php +++ b/app/Http/Traits/ConvertsBase64ToFiles.php @@ -85,17 +85,14 @@ trait ConvertsBase64ToFiles $uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true); - $body = $this->bodyParametersBag()->all(); - Arr::forget($body, $key); - $this->bodyParametersBag()->replace($body); - \Log::debug("Trait: file field $key replaced:". $request->has($key)); - - - $files = $this->uploadFilesBag()->all(); - Arr::set($files, $key, $uploadedFile); - $this->uploadFilesBag()->replace($files); - \Log::debug("Trait: file field $key inserted:". $request->hasFile($key)); - + \Log::debug("Trait: uploadedfile ". $tempFilePath); + $this->offsetUnset($key); + \Log::debug("Trait: encoded field \"$key\" removed" ); + + //Inserting new file to $this-files does not work so have to deal this after + $this->offsetSet($key,$uploadedFile); + \Log::debug("Trait: encoded field \"$key\" inserted" ); + }, null, false); }); } From e3166c2209669d5329c30eb99e8d74d1ffdd74bd Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 06:41:37 +0300 Subject: [PATCH 26/37] fix debug placement --- app/Http/Requests/ImageUploadRequest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 46f47e7480..6ad002f9bb 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -103,9 +103,8 @@ class ImageUploadRequest extends Request } } - \Log::debug($image); - if (isset($image)) { + \Log::debug($image); if (!config('app.lock_passwords')) { From c1bc2486ad2d1561844ff9992b76f49c195c146c Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 06:49:19 +0300 Subject: [PATCH 27/37] keep using request --- app/Http/Controllers/Api/ComponentsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 62618a29df..66b1bd4479 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller; use App\Http\Transformers\ComponentsTransformer; use App\Models\Company; use App\Models\Component; +use App\Http\Requests\Request; use App\Http\Requests\ImageUploadRequest; use App\Events\CheckoutableCheckedIn; use App\Events\ComponentCheckedIn; From c21b29148419e12520f88d1d6d56239198dd2d0d Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 06:51:07 +0300 Subject: [PATCH 28/37] keep using request --- app/Http/Controllers/Api/ComponentsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 66b1bd4479..4f08218985 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -7,7 +7,7 @@ use App\Http\Controllers\Controller; use App\Http\Transformers\ComponentsTransformer; use App\Models\Company; use App\Models\Component; -use App\Http\Requests\Request; +use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; use App\Events\CheckoutableCheckedIn; use App\Events\ComponentCheckedIn; From 3d8acd1bd83c1e43d2d1e64355ed9f7f60a15667 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 07:00:12 +0300 Subject: [PATCH 29/37] removed unused Bag-functions --- app/Http/Traits/ConvertsBase64ToFiles.php | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/app/Http/Traits/ConvertsBase64ToFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.php index 793746dbb6..537d2788c0 100644 --- a/app/Http/Traits/ConvertsBase64ToFiles.php +++ b/app/Http/Traits/ConvertsBase64ToFiles.php @@ -6,8 +6,6 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Str; -use Symfony\Component\HttpFoundation\FileBag; -use Symfony\Component\HttpFoundation\ParameterBag; trait ConvertsBase64ToFiles { @@ -16,26 +14,6 @@ trait ConvertsBase64ToFiles return []; } - /** - * Helper method to get the body parameters bag. - * - * @return \Symfony\Component\HttpFoundation\ParameterBag - */ - private function bodyParametersBag(): ParameterBag - { - return $this->request; - } - - /** - * Helper method to get the uploaded files bag. - * - * @return FileBag - */ - private function uploadFilesBag(): FileBag - { - return $this->files; - } - /** * Pulls the Base64 contents for each file key and creates * an UploadedFile instance from it and sets it on the From e275c9ee90e29a9b6ad9bd0a3aee03b109813d6b Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 07:10:03 +0300 Subject: [PATCH 30/37] Fixed case again --- app/Http/Controllers/Api/DepartmentsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php index da8eeaf2f5..04b806d406 100644 --- a/app/Http/Controllers/Api/DepartmentsController.php +++ b/app/Http/Controllers/Api/DepartmentsController.php @@ -9,7 +9,7 @@ use App\Http\Transformers\SelectlistTransformer; use App\Models\Department; use Auth; use Illuminate\Http\Request; -use App\http\Requests\ImageUploadRequest; +use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; class DepartmentsController extends Controller From f53cabee24c15e766544084ec33b8827b3110af2 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Tue, 6 Jul 2021 07:26:16 +0300 Subject: [PATCH 31/37] Better debug comment --- app/Http/Traits/ConvertsBase64ToFiles.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Traits/ConvertsBase64ToFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.php index 537d2788c0..685106a226 100644 --- a/app/Http/Traits/ConvertsBase64ToFiles.php +++ b/app/Http/Traits/ConvertsBase64ToFiles.php @@ -69,7 +69,7 @@ trait ConvertsBase64ToFiles //Inserting new file to $this-files does not work so have to deal this after $this->offsetSet($key,$uploadedFile); - \Log::debug("Trait: encoded field \"$key\" inserted" ); + \Log::debug("Trait: field \"$key\" inserted as UplodedFile" ); }, null, false); }); From 168d7f7004793cfd1c6def4bda51316c3e9416c1 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Wed, 7 Jul 2021 09:08:37 +0300 Subject: [PATCH 32/37] Handle image_source with ConvertBase64ToFiles --- app/Http/Controllers/Api/AssetsController.php | 39 ------------------- app/Http/Requests/ImageUploadRequest.php | 5 +++ 2 files changed, 5 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index dbd092a525..d3e881038f 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -459,22 +459,6 @@ class AssetsController extends Controller $asset->rtd_location_id = $request->get('rtd_location_id', null); $asset->location_id = $request->get('rtd_location_id', null); - if ($request->has('image_source') && $request->input('image_source') != "") { - $saved_image_path = Helper::processUploadedImage( - $request->input('image_source'), 'uploads/assets/' - ); - - if (!$saved_image_path) { - return response()->json(Helper::formatStandardApiResponse( - 'error', - null, - trans('admin/hardware/message.create.error') - ), 200); - } - - $asset->image = $saved_image_path; - } - $asset = $request->handleImages($asset); // Update custom fields in the database. @@ -561,31 +545,8 @@ class AssetsController extends Controller ($request->filled('company_id')) ? $asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : ''; -($request->filled('rtd_location_id')) ? - $asset->location_id = $request->get('rtd_location_id') : null; - - - if ($request->filled('image_source')) { - if ($request->input('image_source') == "") { ($request->filled('rtd_location_id')) ? $asset->location_id = $request->get('rtd_location_id') : null; - $asset->image = null; - } else { - $saved_image_path = Helper::processUploadedImage( - $request->input('image_source'), 'uploads/assets/' - ); - - if (!$saved_image_path) { - return response()->json(Helper::formatStandardApiResponse( - 'error', - null, - trans('admin/hardware/message.update.error') - ), 200); - } - - $asset->image = $saved_image_path; - } - } $asset = $request->handleImages($asset); diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 6ad002f9bb..0ed4561ae6 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -47,8 +47,13 @@ class ImageUploadRequest extends Request */ protected function base64FileKeys(): array { + /** + * image_source is here just legacy reasons. Api\AssetController + * had it once to allow encoded image uploads. + */ return [ 'image' => 'auto', + 'image_source' => 'auto' ]; } From b2d3ba74102f76428eefbcf8a01f9329bff4dfc7 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Wed, 7 Jul 2021 09:20:38 +0300 Subject: [PATCH 33/37] Revert "Handle image_source with ConvertBase64ToFiles" This reverts commit 168d7f7004793cfd1c6def4bda51316c3e9416c1. --- app/Http/Controllers/Api/AssetsController.php | 39 +++++++++++++++++++ app/Http/Requests/ImageUploadRequest.php | 5 --- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index d3e881038f..dbd092a525 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -459,6 +459,22 @@ class AssetsController extends Controller $asset->rtd_location_id = $request->get('rtd_location_id', null); $asset->location_id = $request->get('rtd_location_id', null); + if ($request->has('image_source') && $request->input('image_source') != "") { + $saved_image_path = Helper::processUploadedImage( + $request->input('image_source'), 'uploads/assets/' + ); + + if (!$saved_image_path) { + return response()->json(Helper::formatStandardApiResponse( + 'error', + null, + trans('admin/hardware/message.create.error') + ), 200); + } + + $asset->image = $saved_image_path; + } + $asset = $request->handleImages($asset); // Update custom fields in the database. @@ -545,8 +561,31 @@ class AssetsController extends Controller ($request->filled('company_id')) ? $asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : ''; +($request->filled('rtd_location_id')) ? + $asset->location_id = $request->get('rtd_location_id') : null; + + + if ($request->filled('image_source')) { + if ($request->input('image_source') == "") { ($request->filled('rtd_location_id')) ? $asset->location_id = $request->get('rtd_location_id') : null; + $asset->image = null; + } else { + $saved_image_path = Helper::processUploadedImage( + $request->input('image_source'), 'uploads/assets/' + ); + + if (!$saved_image_path) { + return response()->json(Helper::formatStandardApiResponse( + 'error', + null, + trans('admin/hardware/message.update.error') + ), 200); + } + + $asset->image = $saved_image_path; + } + } $asset = $request->handleImages($asset); diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 0ed4561ae6..6ad002f9bb 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -47,13 +47,8 @@ class ImageUploadRequest extends Request */ protected function base64FileKeys(): array { - /** - * image_source is here just legacy reasons. Api\AssetController - * had it once to allow encoded image uploads. - */ return [ 'image' => 'auto', - 'image_source' => 'auto' ]; } From 7a424649c83f68e30174d37ac61444ce89b94b38 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Wed, 7 Jul 2021 09:24:24 +0300 Subject: [PATCH 34/37] convert image_source field This reverts commit b2d3ba74102f76428eefbcf8a01f9329bff4dfc7. --- app/Http/Controllers/Api/AssetsController.php | 39 ------------------- app/Http/Requests/ImageUploadRequest.php | 5 +++ 2 files changed, 5 insertions(+), 39 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index dbd092a525..d3e881038f 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -459,22 +459,6 @@ class AssetsController extends Controller $asset->rtd_location_id = $request->get('rtd_location_id', null); $asset->location_id = $request->get('rtd_location_id', null); - if ($request->has('image_source') && $request->input('image_source') != "") { - $saved_image_path = Helper::processUploadedImage( - $request->input('image_source'), 'uploads/assets/' - ); - - if (!$saved_image_path) { - return response()->json(Helper::formatStandardApiResponse( - 'error', - null, - trans('admin/hardware/message.create.error') - ), 200); - } - - $asset->image = $saved_image_path; - } - $asset = $request->handleImages($asset); // Update custom fields in the database. @@ -561,31 +545,8 @@ class AssetsController extends Controller ($request->filled('company_id')) ? $asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : ''; -($request->filled('rtd_location_id')) ? - $asset->location_id = $request->get('rtd_location_id') : null; - - - if ($request->filled('image_source')) { - if ($request->input('image_source') == "") { ($request->filled('rtd_location_id')) ? $asset->location_id = $request->get('rtd_location_id') : null; - $asset->image = null; - } else { - $saved_image_path = Helper::processUploadedImage( - $request->input('image_source'), 'uploads/assets/' - ); - - if (!$saved_image_path) { - return response()->json(Helper::formatStandardApiResponse( - 'error', - null, - trans('admin/hardware/message.update.error') - ), 200); - } - - $asset->image = $saved_image_path; - } - } $asset = $request->handleImages($asset); diff --git a/app/Http/Requests/ImageUploadRequest.php b/app/Http/Requests/ImageUploadRequest.php index 6ad002f9bb..0ed4561ae6 100644 --- a/app/Http/Requests/ImageUploadRequest.php +++ b/app/Http/Requests/ImageUploadRequest.php @@ -47,8 +47,13 @@ class ImageUploadRequest extends Request */ protected function base64FileKeys(): array { + /** + * image_source is here just legacy reasons. Api\AssetController + * had it once to allow encoded image uploads. + */ return [ 'image' => 'auto', + 'image_source' => 'auto' ]; } From 41deabf99882aeb34d0c52da77e53a8332ac0f13 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Wed, 7 Jul 2021 09:33:48 +0300 Subject: [PATCH 35/37] hand legacy image_source field --- app/Http/Controllers/Api/AssetsController.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index d3e881038f..dac9ac4d50 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -548,6 +548,14 @@ class AssetsController extends Controller ($request->filled('rtd_location_id')) ? $asset->location_id = $request->get('rtd_location_id') : null; + /** + * this is here just legacy reasons. Api\AssetController + * used image_source once to allow encoded image uploads. + */ + if ($request->has('image_source')) { + $request->offsetSet('image', $request->offsetGet('image_source')); + } + $asset = $request->handleImages($asset); // Update custom fields From 4379ea61fa0032300406b8dfd51a4471fc04bda9 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Thu, 8 Jul 2021 19:50:33 +0300 Subject: [PATCH 36/37] Just mention source of idea as code rewriten --- app/Http/Traits/ConvertsBase64ToFiles.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/app/Http/Traits/ConvertsBase64ToFiles.php b/app/Http/Traits/ConvertsBase64ToFiles.php index 685106a226..aed8fc0dfb 100644 --- a/app/Http/Traits/ConvertsBase64ToFiles.php +++ b/app/Http/Traits/ConvertsBase64ToFiles.php @@ -76,15 +76,5 @@ trait ConvertsBase64ToFiles } } /** - * This file based on https://github.com/protonemedia/laravel-mixins/tree/master/src/Request - * - * MIT License - * - * Copyright (c) Protone Media B.V. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ \ No newline at end of file + * Loosely based on idea https://github.com/protonemedia/laravel-mixins/tree/master/src/Request + * */ \ No newline at end of file From febf1ec20f7f2f7423f3a5754d980e1b8ed97b1e Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Wed, 14 Jul 2021 13:09:50 +0300 Subject: [PATCH 37/37] Support legacy image_source property --- app/Http/Controllers/Api/AssetsController.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index dac9ac4d50..61e70adf86 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -459,6 +459,14 @@ class AssetsController extends Controller $asset->rtd_location_id = $request->get('rtd_location_id', null); $asset->location_id = $request->get('rtd_location_id', null); + /** + * this is here just legacy reasons. Api\AssetController + * used image_source once to allow encoded image uploads. + */ + if ($request->has('image_source')) { + $request->offsetSet('image', $request->offsetGet('image_source')); + } + $asset = $request->handleImages($asset); // Update custom fields in the database.