From 9d7455f022d9f31fd80123687ff25be2ae615661 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 7 Nov 2017 11:28:13 -0800 Subject: [PATCH] Fixed handling deleting old images better --- .../Controllers/AssetModelsController.php | 40 ++++++++++--------- app/Http/Controllers/CategoriesController.php | 3 -- app/Http/Controllers/CompaniesController.php | 34 ++++++++++++---- .../Controllers/DepartmentsController.php | 39 +++++++++++++----- app/Http/Controllers/LocationsController.php | 34 ++++++++++++---- .../Controllers/ManufacturersController.php | 35 ++++++++++------ app/Http/Controllers/SuppliersController.php | 39 +++++++++++++----- app/Providers/SettingsServiceProvider.php | 10 +++++ 8 files changed, 162 insertions(+), 72 deletions(-) diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index adc0236562..3fe707c344 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -91,7 +91,7 @@ class AssetModelsController extends Controller $image = Input::file('image'); $file_name = str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); - $path = public_path('uploads/models/'); + $path = app('models_upload_path'); if ($image->getClientOriginalExtension()!='svg') { Image::make($image->getRealPath())->resize(500, null, function ($constraint) { @@ -191,15 +191,14 @@ class AssetModelsController extends Controller return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist')); } - $model->depreciation_id = $request->input('depreciation_id'); - $model->eol = $request->input('eol'); + $model->depreciation_id = $request->input('depreciation_id'); + $model->eol = $request->input('eol'); $model->name = $request->input('name'); $model->model_number = $request->input('model_number'); $model->manufacturer_id = $request->input('manufacturer_id'); $model->category_id = $request->input('category_id'); $model->notes = $request->input('notes'); - - $model->requestable = Input::has('requestable'); + $model->requestable = $request->input('requestable', '0'); if ($request->input('custom_fieldset')=='') { $model->fieldset_id = null; @@ -207,35 +206,38 @@ class AssetModelsController extends Controller $model->fieldset_id = $request->input('custom_fieldset'); } - if (Input::file('image')) { + $old_image = $model->image; - $image = Input::file('image'); - $file_name = str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); - $path = public_path('uploads/models/'); - $old_image = $path.$model->image; + // Set the model's image property to null if the image is being deleted + if ($request->input('image_delete') == 1) { + $model->image = null; + } - try { - unlink($old_image); - } catch (\Exception $e) { - \Log::error($e); - } + if ($request->file('image')) { + $image = $request->file('image'); + $file_name = $model->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); if ($image->getClientOriginalExtension()!='svg') { Image::make($image->getRealPath())->resize(500, null, function ($constraint) { $constraint->aspectRatio(); $constraint->upsize(); - })->save($path.'/'.$file_name); + })->save(app('models_upload_path').$file_name); } else { - $image->move($path, $file_name); + $image->move(app('models_upload_path'), $file_name); } $model->image = $file_name; } - if ($request->input('image_delete') == 1 && Input::file('image') == "") { - $model->image = null; + if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { + try { + unlink(app('models_upload_path').$old_image); + } catch (\Exception $e) { + \Log::error($e); + } } + if ($model->save()) { return redirect()->route("models.index")->with('success', trans('admin/models/message.update.success')); } diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index 0646ecc9e2..4c3f9d68d5 100755 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -150,13 +150,11 @@ class CategoriesController extends Controller $old_image = $category->image; - // Set the model's image property to null if the image is being deleted if ($request->input('image_delete') == 1) { $category->image = null; } - // If a new file is being uploaded, resize it and save it if ($request->file('image')) { $image = $request->file('image'); $file_name = $category->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); @@ -173,7 +171,6 @@ class CategoriesController extends Controller } - // If there is a new file being uploaded if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { try { unlink(app('categories_upload_path').$old_image); diff --git a/app/Http/Controllers/CompaniesController.php b/app/Http/Controllers/CompaniesController.php index c6dbea0fad..c3a1df0ff9 100644 --- a/app/Http/Controllers/CompaniesController.php +++ b/app/Http/Controllers/CompaniesController.php @@ -110,17 +110,35 @@ final class CompaniesController extends Controller $company->name = $request->input('name'); + $old_image = $company->image; + + // Set the model's image property to null if the image is being deleted + if ($request->input('image_delete') == 1) { + $company->image = null; + } + if ($request->file('image')) { $image = $request->file('image'); - $file_name = str_random(25).".".$image->getClientOriginalExtension(); - $path = public_path('uploads/companies/'.$file_name); - Image::make($image->getRealPath())->resize(200, null, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - })->save($path); + $file_name = $company->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); + + if ($image->getClientOriginalExtension()!='svg') { + Image::make($image->getRealPath())->resize(500, null, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + })->save(app('companies_upload_path').$file_name); + } else { + $image->move(app('companies_upload_path'), $file_name); + } $company->image = $file_name; - } elseif ($request->input('image_delete')=='1') { - $company->image = null; + + } + + if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { + try { + unlink(app('companies_upload_path').$old_image); + } catch (\Exception $e) { + \Log::error($e); + } } diff --git a/app/Http/Controllers/DepartmentsController.php b/app/Http/Controllers/DepartmentsController.php index 5b3452793c..31b92673e9 100644 --- a/app/Http/Controllers/DepartmentsController.php +++ b/app/Http/Controllers/DepartmentsController.php @@ -152,21 +152,38 @@ class DepartmentsController extends Controller } $department->fill($request->all()); + $department->manager_id = ($request->has('manager_id' ) ? $request->input('manager_id') : null); - if ($request->file('image')) { - $image = $request->file('image'); - $file_name = str_random(25).".".$image->getClientOriginalExtension(); - $path = public_path('uploads/departments/'.$file_name); - Image::make($image->getRealPath())->resize(200, null, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - })->save($path); - $department->image = $file_name; - } elseif ($request->input('image_delete')=='1') { + $old_image = $department->image; + + // Set the model's image property to null if the image is being deleted + if ($request->input('image_delete') == 1) { $department->image = null; } - $department->manager_id = ($request->has('manager_id' ) ? $request->input('manager_id') : null); + if ($request->file('image')) { + $image = $request->file('image'); + $file_name = $department->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); + + if ($image->getClientOriginalExtension()!='svg') { + Image::make($image->getRealPath())->resize(500, null, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + })->save(app('departments_upload_path').$file_name); + } else { + $image->move(app('departments_upload_path'), $file_name); + } + $department->image = $file_name; + + } + + if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { + try { + unlink(app('departments_upload_path').$old_image); + } catch (\Exception $e) { + \Log::error($e); + } + } if ($department->save()) { return redirect()->route("departments.index")->with('success', trans('admin/departments/message.update.success')); diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index 340965866b..368e5b9ee6 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -204,17 +204,35 @@ class LocationsController extends Controller $location->ldap_ou = $request->input('ldap_ou'); $location->manager_id = $request->input('manager_id'); + $old_image = $location->image; + + // Set the model's image property to null if the image is being deleted + if ($request->input('image_delete') == 1) { + $location->image = null; + } + if ($request->file('image')) { $image = $request->file('image'); - $file_name = str_random(25).".".$image->getClientOriginalExtension(); - $path = public_path('uploads/locations/'.$file_name); - Image::make($image->getRealPath())->resize(200, null, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - })->save($path); + $file_name = $location->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); + + if ($image->getClientOriginalExtension()!='svg') { + Image::make($image->getRealPath())->resize(500, null, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + })->save(app('locations_upload_path').$file_name); + } else { + $image->move(app('locations_upload_path'), $file_name); + } $location->image = $file_name; - } elseif ($request->input('image_delete')=='1') { - $location->image = null; + + } + + if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { + try { + unlink(app('locations_upload_path').$old_image); + } catch (\Exception $e) { + \Log::error($e); + } } diff --git a/app/Http/Controllers/ManufacturersController.php b/app/Http/Controllers/ManufacturersController.php index 7bd6858b11..cd23d12cd4 100755 --- a/app/Http/Controllers/ManufacturersController.php +++ b/app/Http/Controllers/ManufacturersController.php @@ -138,26 +138,35 @@ class ManufacturersController extends Controller $manufacturer->support_phone = $request->input('support_phone'); $manufacturer->support_email = $request->input('support_email'); + $old_image = $manufacturer->image; + + // Set the model's image property to null if the image is being deleted + if ($request->input('image_delete') == 1) { + $manufacturer->image = null; + } + if ($request->file('image')) { $image = $request->file('image'); - $file_name = str_slug($image->getClientOriginalName()).".".$image->getClientOriginalExtension(); - $path = public_path('uploads/manufacturers/'.$file_name); - $old_image = $path.$manufacturer->image; + $file_name = $manufacturer->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); + if ($image->getClientOriginalExtension()!='svg') { + Image::make($image->getRealPath())->resize(500, null, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + })->save(app('manufacturers_upload_path').$file_name); + } else { + $image->move(app('manufacturers_upload_path'), $file_name); + } + $manufacturer->image = $file_name; + + } + + if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { try { - unlink($old_image); + unlink(app('manufacturers_upload_path').$old_image); } catch (\Exception $e) { \Log::error($e); } - - - Image::make($image->getRealPath())->resize(200, null, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - })->save($path); - $manufacturer->image = $file_name; - } elseif ($request->input('image_delete')=='1') { - $manufacturer->image = null; } diff --git a/app/Http/Controllers/SuppliersController.php b/app/Http/Controllers/SuppliersController.php index 8d6c4926c2..fdd399a1fb 100755 --- a/app/Http/Controllers/SuppliersController.php +++ b/app/Http/Controllers/SuppliersController.php @@ -159,19 +159,38 @@ class SuppliersController extends Controller $supplier->notes = request('notes'); - if ($request->file('image')) { - $image = $request->file('image'); - $file_name = str_random(25).".".$image->getClientOriginalExtension(); - $path = public_path('uploads/suppliers/'.$file_name); - Image::make($image->getRealPath())->resize(200, null, function ($constraint) { - $constraint->aspectRatio(); - $constraint->upsize(); - })->save($path); - $supplier->image = $file_name; - } elseif ($request->input('image_delete')=='1') { + $old_image = $supplier->image; + + // Set the model's image property to null if the image is being deleted + if ($request->input('image_delete') == 1) { $supplier->image = null; } + if ($request->file('image')) { + $image = $request->file('image'); + $file_name = $supplier->id.'-'.str_slug($image->getClientOriginalName()) . "." . $image->getClientOriginalExtension(); + + if ($image->getClientOriginalExtension()!='svg') { + Image::make($image->getRealPath())->resize(500, null, function ($constraint) { + $constraint->aspectRatio(); + $constraint->upsize(); + })->save(app('suppliers_upload_path').$file_name); + } else { + $image->move(app('suppliers_upload_path'), $file_name); + } + $supplier->image = $file_name; + + } + + if ((($request->file('image')) && (isset($old_image)) && ($old_image!='')) || ($request->input('image_delete') == 1)) { + try { + unlink(app('suppliers_upload_path').$old_image); + } catch (\Exception $e) { + \Log::error($e); + } + } + + if ($supplier->save()) { return redirect()->route('suppliers.index')->with('success', trans('admin/suppliers/message.update.success')); } diff --git a/app/Providers/SettingsServiceProvider.php b/app/Providers/SettingsServiceProvider.php index 9b79f3c6b5..ed965bf9c7 100644 --- a/app/Providers/SettingsServiceProvider.php +++ b/app/Providers/SettingsServiceProvider.php @@ -102,6 +102,16 @@ class SettingsServiceProvider extends ServiceProvider return url('/').'/uploads/departments/'; }); + // Company paths and URLs + \App::singleton('companies_upload_path', function(){ + return public_path('/uploads/companies/'); + }); + + \App::singleton('companies_upload_url', function(){ + return url('/').'/uploads/companies/'; + }); + + // Set the monetary locale to the configured locale to make helper::parseFloat work. setlocale(LC_MONETARY, config('app.locale'));