From d0579032291ad262d19f571d049ef8259599d45b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 3 Jun 2019 15:31:20 +1000 Subject: [PATCH] Logging --- app/Http/Middleware/QueryLogging.php | 4 ++-- app/Jobs/Util/UploadFile.php | 29 +++++++++++++++++++++------- app/Models/Client.php | 6 ++++-- app/Models/Document.php | 9 +++++++++ database/seeds/RandomDataSeeder.php | 8 +++++--- tests/Integration/UploadFileTest.php | 3 --- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/Http/Middleware/QueryLogging.php b/app/Http/Middleware/QueryLogging.php index cc5d9ed9db..cfd820c9cb 100644 --- a/app/Http/Middleware/QueryLogging.php +++ b/app/Http/Middleware/QueryLogging.php @@ -49,8 +49,8 @@ class QueryLogging $time = $timeEnd - $timeStart; Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time); - // if($count > 20) - // Log::info($queries); + if($count > 20) + Log::info($queries); } } diff --git a/app/Jobs/Util/UploadFile.php b/app/Jobs/Util/UploadFile.php index ef517904ab..0c5fea99cb 100644 --- a/app/Jobs/Util/UploadFile.php +++ b/app/Jobs/Util/UploadFile.php @@ -58,22 +58,25 @@ class UploadFile implements ShouldQueue public function handle() : ?Document { - //$path = $this->encodePrimaryKey($this->company->id) . '/' . sha1(time()) . '_' . str_replace(" ", "", $this->file->getClientOriginalName()); $path = $this->encodePrimaryKey($this->company->id); - $file_path = $path . '/' . $this->file->hashName(); -// Storage::makeDirectory($path); + $file_path = $path . '/' . $this->file->hashName(); Storage::put($path, $this->file); $width = 0; + $height = 0; if (in_array($this->file->getClientOriginalExtension(),['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) { + $imageSize = getimagesize($this->file); + $width = $imageSize[0]; + $height = $imageSize[1]; + } $document = new Document(); @@ -88,21 +91,33 @@ class UploadFile implements ShouldQueue $document->width = $width; $document->height = $height; - $preview_path = $this->encodePrimaryKey($this->company->id) . '/' . sha1(time()) . '_preview_' . str_replace(" ", "", $this->file->getClientOriginalName()); + $preview_path = $this->encodePrimaryKey($this->company->id); $document->preview = $this->generatePreview($preview_path); $this->entity->documents()->save($document); - return $document; + return $document; } private function generatePreview($preview_path) : string { + $extension = $this->file->getClientOriginalExtension(); + + if (empty(Document::$types[$extension]) && ! empty(Document::$extraExtensions[$extension])) { + $documentType = Document::$extraExtensions[$extension]; + } else { + $documentType = $extension; + } + + if (empty(Document::$types[$documentType])) { + return 'Unsupported file type'; + } + $preview = ''; - if (in_array($this->file->getClientOriginalExtension(), ['jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) + if (in_array($this->file->getClientOriginalExtension(),['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'psd'])) { $makePreview = false; $imageSize = getimagesize($this->file); @@ -135,7 +150,7 @@ class UploadFile implements ShouldQueue // We haven't created a preview yet $imgManager = new ImageManager($imgManagerConfig); - $img = $imgManager->make($filePath); + $img = $imgManager->make($preview_path); if ($width <= Document::DOCUMENT_PREVIEW_SIZE && $height <= Document::DOCUMENT_PREVIEW_SIZE) { $previewWidth = $width; diff --git a/app/Models/Client.php b/app/Models/Client.php index fc91370018..dd13688b0b 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -18,6 +18,7 @@ use App\Models\Company; use App\Models\Country; use App\Models\Filterable; use App\Models\Timezone; +use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesHash; use Hashids\Hashids; @@ -32,6 +33,7 @@ class Client extends BaseModel use MakesDates; use SoftDeletes; use Filterable; + use GeneratesCounter; protected $presenter = 'App\Models\Presenters\ClientPresenter'; @@ -50,7 +52,7 @@ class Client extends BaseModel 'country', 'shipping_country' ]; - + /* protected $with = [ 'contacts', 'primary_contact', @@ -58,7 +60,7 @@ class Client extends BaseModel 'shipping_country', 'company' ]; - + */ protected $casts = [ 'settings' => 'object' ]; diff --git a/app/Models/Document.php b/app/Models/Document.php index 9086189cba..63de91a987 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -76,6 +76,15 @@ class Document extends BaseModel ], ]; + /** + * @var array + */ + public static $extraExtensions = [ + 'jpg' => 'jpeg', + 'tif' => 'tiff', + ]; + + public function documentable() { return $this->morphTo(); diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index 9752f46019..745889bba6 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -71,7 +71,7 @@ class RandomDataSeeder extends Seeder ]); - factory(\App\Models\Client::class, 5)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){ + factory(\App\Models\Client::class, 6)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){ factory(\App\Models\ClientContact::class,1)->create([ 'user_id' => $user->id, @@ -92,14 +92,16 @@ class RandomDataSeeder extends Seeder factory(\App\Models\Product::class,50)->create(['user_id' => $user->id, 'company_id' => $company->id]); /** Invoice Factory */ - factory(\App\Models\Invoice::class,50)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + factory(\App\Models\Invoice::class,500)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); $clients = Client::all(); foreach($clients as $client) { - $client->getNextNumber($client); + $client->getNextClientNumber($client); $client->save(); } + } + } diff --git a/tests/Integration/UploadFileTest.php b/tests/Integration/UploadFileTest.php index a9fbdb12d3..3e059e3fb4 100644 --- a/tests/Integration/UploadFileTest.php +++ b/tests/Integration/UploadFileTest.php @@ -44,9 +44,6 @@ class UploadFileTest extends TestCase $document = UploadFile::dispatchNow(UploadedFile::fake()->image('avatar.jpg'), $this->invoice->user, $this->invoice->company, $this->invoice); $this->assertNotNull($document); - -Log::error($document); -// $this->assertEquals($invoice->amount, $payment); }