From 8ed382a0b7403c5b93e99976b954fbf221e79d1e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 24 Jun 2020 18:59:56 +1000 Subject: [PATCH 1/2] Change to streaming downloads --- .../ClientPortal/InvoiceController.php | 5 +++- .../ClientPortal/QuoteController.php | 5 +++- app/Http/Controllers/CreditController.php | 5 +++- app/Http/Controllers/DocumentController.php | 23 +++++++++++++------ app/Http/Controllers/InvoiceController.php | 5 +++- app/Http/Controllers/QuoteController.php | 5 +++- app/Models/Company.php | 5 ++++ app/Models/Document.php | 7 ++++++ app/Transformers/DocumentTransformer.php | 2 +- 9 files changed, 49 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index ab632694febc..407c346bb8fd 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -137,7 +137,10 @@ class InvoiceController extends Controller //if only 1 pdf, output to buffer for download if ($invoices->count() == 1) { - return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($invoices->first()->pdf_file_path())); + return response()->streamDownload(function () use($invoices) { + echo file_get_contents($invoices->first()->pdf_file_path()); + }, basename($invoices->first()->pdf_file_path())); + //return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($invoices->first()->pdf_file_path())); } # enable output of HTTP headers diff --git a/app/Http/Controllers/ClientPortal/QuoteController.php b/app/Http/Controllers/ClientPortal/QuoteController.php index 8421ea3310b0..56b7e3963851 100644 --- a/app/Http/Controllers/ClientPortal/QuoteController.php +++ b/app/Http/Controllers/ClientPortal/QuoteController.php @@ -74,7 +74,10 @@ class QuoteController extends Controller } if ($quotes->count() == 1) { - return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($quotes->first()->pdf_file_path())); + return response()->streamDownload(function () use($invoices) { + echo file_get_contents($invoices->first()->pdf_file_path()); + }, basename($invoices->first()->pdf_file_path())); + //return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($quotes->first()->pdf_file_path())); } # enable output of HTTP headers diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index 8b56a5685c2f..6e2d4e214512 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -527,7 +527,10 @@ class CreditController extends BaseController } break; case 'download': - return response()->download(TempFile::path($credit->pdf_file_path()), basename($credit->pdf_file_path())); + return response()->streamDownload(function () use($credit) { + echo file_get_contents($credit->pdf_file_path()); + }, basename($credit->pdf_file_path())); + //return response()->download(TempFile::path($credit->pdf_file_path()), basename($credit->pdf_file_path())); break; case 'archive': $this->credit_repository->archive($credit); diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php index 3d71de850326..9c20cd2acdaf 100644 --- a/app/Http/Controllers/DocumentController.php +++ b/app/Http/Controllers/DocumentController.php @@ -2,9 +2,14 @@ namespace App\Http\Controllers; +use App\Http\Requests\Document\EditDocumentRequest; +use App\Http\Requests\Document\ShowDocumentRequest; +use App\Http\Requests\Document\StoreDocumentRequest; +use App\Http\Requests\Document\UpdateDocumentRequest; +use App\Models\Document; use Illuminate\Http\Request; -class DocumentController extends Controller +class DocumentController extends BaseController { /** * Display a listing of the resource. @@ -32,7 +37,7 @@ class DocumentController extends Controller * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(StoreDocumentRequest $request) { // } @@ -43,9 +48,13 @@ class DocumentController extends Controller * @param int $id * @return \Illuminate\Http\Response */ - public function show($id) + public function show(ShowDocumentRequest $request, Document $document) { - // + return response()->streamDownload(function () use($document) { + echo file_get_contents($document->generateUrl()); + }, basename($document->generateUrl())); + + //return response()->download($document->generateUrl()); } /** @@ -54,7 +63,7 @@ class DocumentController extends Controller * @param int $id * @return \Illuminate\Http\Response */ - public function edit($id) + public function edit(EditDocumentRegquest $request, Document $document) { // } @@ -66,7 +75,7 @@ class DocumentController extends Controller * @param int $id * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(UpdateDocumentRequest $request, Document $document) { // } @@ -77,7 +86,7 @@ class DocumentController extends Controller * @param int $id * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy(DestroyDocumentRequest $request, Document $document) { // } diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 48251ead318d..fa22c7ac82da 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -660,7 +660,10 @@ class InvoiceController extends BaseController } break; case 'download': - return response()->download(TempFile::path($invoice->pdf_file_path()), basename($invoice->pdf_file_path())); + return response()->streamDownload(function () use($invoice) { + echo file_get_contents($invoice->pdf_file_path()); + }, basename($invoice->pdf_file_path())); + //return response()->download(TempFile::path($invoice->pdf_file_path()), basename($invoice->pdf_file_path())); break; case 'restore': $this->invoice_repo->restore($invoice); diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 855c58c2012e..ee18a597d046 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -661,7 +661,10 @@ class QuoteController extends BaseController # code... break; case 'download': - return response()->download(TempFile::path($quote->pdf_file_path()), basename($quote->pdf_file_path())); + return response()->streamDownload(function () use($quote) { + echo file_get_contents($quote->pdf_file_path()); + }, basename($quote->pdf_file_path())); + //return response()->download(TempFile::path($quote->pdf_file_path()), basename($quote->pdf_file_path())); break; case 'archive': $this->invoice_repo->archive($quote); diff --git a/app/Models/Company.php b/app/Models/Company.php index 8185da8693a2..89f3afaacb97 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -134,6 +134,11 @@ class Company extends BaseModel self::ENTITY_RECURRING_QUOTE => 2048, ]; + public function documents() + { + return $this->morphMany(Document::class, 'documentable'); + } + public function getEntityType() { return Company::class; diff --git a/app/Models/Document.php b/app/Models/Document.php index f46e8276e139..b977c547e85c 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -112,4 +112,11 @@ class Document extends BaseModel return null; } + + public function generateRoute($absolute = false) + { + return route('api.documents.show', ['document' => $this->hashed_id]); + + //return route('document.show', ['document' => $this->hashed_id]); + } } diff --git a/app/Transformers/DocumentTransformer.php b/app/Transformers/DocumentTransformer.php index 6f34407b08e4..a1f9c8ca51e3 100644 --- a/app/Transformers/DocumentTransformer.php +++ b/app/Transformers/DocumentTransformer.php @@ -37,7 +37,7 @@ class DocumentTransformer extends EntityTransformer 'assigned_user_id' => $this->encodePrimaryKey($document->assigned_user_id), 'project_id' => $this->encodePrimaryKey($document->project_id), 'vendor_id' => $this->encodePrimaryKey($document->vendor_id), - 'url' => (string) $document->generateUrl() ?: '', + 'url' => (string) $document->generateRoute() ?: '', 'preview' => (string) $document->preview ?: '', 'name' => (string) $document->name, 'type' => (string) $document->type, From 049e9032cba927275f049697e85f9c3f6a075bff Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 24 Jun 2020 20:17:42 +1000 Subject: [PATCH 2/2] Documents --- app/Http/Controllers/DocumentController.php | 64 +++++++++++++++++-- app/Models/Document.php | 5 ++ app/Transformers/PaymentTransformer.php | 4 +- database/seeds/RandomDataSeeder.php | 68 ++++++++++----------- 4 files changed, 100 insertions(+), 41 deletions(-) diff --git a/app/Http/Controllers/DocumentController.php b/app/Http/Controllers/DocumentController.php index 9c20cd2acdaf..317b5c436c1c 100644 --- a/app/Http/Controllers/DocumentController.php +++ b/app/Http/Controllers/DocumentController.php @@ -7,10 +7,35 @@ use App\Http\Requests\Document\ShowDocumentRequest; use App\Http\Requests\Document\StoreDocumentRequest; use App\Http\Requests\Document\UpdateDocumentRequest; use App\Models\Document; +use App\Repositories\DocumentRepository; +use App\Transformers\DocumentTransformer; +use App\Utils\Traits\MakesHash; use Illuminate\Http\Request; class DocumentController extends BaseController { + use MakesHash; + + protected $entity_type = Document::class; + + protected $entity_transformer = DocumentTransformer::class; + + /** + * @var DocumentRepository + */ + protected $document_repo; + + /** + * DocumentController constructor. + * @param DocumentRepository $document_repo + */ + public function __construct(DocumentRepository $document_repo) + { + parent::__construct(); + + $this->document_repo = $document_repo; + } + /** * Display a listing of the resource. * @@ -50,11 +75,14 @@ class DocumentController extends BaseController */ public function show(ShowDocumentRequest $request, Document $document) { - return response()->streamDownload(function () use($document) { - echo file_get_contents($document->generateUrl()); - }, basename($document->generateUrl())); + return $this->itemResponse($document); + } - //return response()->download($document->generateUrl()); + public function download(DownloadDocumentRequest $request, Document $document) + { + return response()->streamDownload(function () use($document) { + echo file_get_contents($document->generateUrl()); + }, basename($document->generateUrl())); } /** @@ -88,11 +116,35 @@ class DocumentController extends BaseController */ public function destroy(DestroyDocumentRequest $request, Document $document) { - // + $this->document_repo->delete($document); + + return response()->json(['message'=>'success']); } public function bulk() { - + + $action = request()->input('action'); + + $ids = request()->input('ids'); + + $documents = Document::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()->get(); + + if (!$invoices) { + return response()->json(['message' => 'No Documents Found']); + } + + /* + * Send the other actions to the switch + */ + $documents->each(function ($document, $key) use ($action) { + if (auth()->user()->can('edit', $document)) { + $this->{$action}($document); + } + }); + + /* Need to understand which permission are required for the given bulk action ie. view / edit */ + + return $this->listResponse(Document::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()); } } diff --git a/app/Models/Document.php b/app/Models/Document.php index b977c547e85c..63c736dfdfce 100644 --- a/app/Models/Document.php +++ b/app/Models/Document.php @@ -119,4 +119,9 @@ class Document extends BaseModel //return route('document.show', ['document' => $this->hashed_id]); } + + public function deleteFile() + { + Storage::disk($this->disk)->delete($this->url); + } } diff --git a/app/Transformers/PaymentTransformer.php b/app/Transformers/PaymentTransformer.php index a2f574dabf10..35f87ff45a0b 100644 --- a/app/Transformers/PaymentTransformer.php +++ b/app/Transformers/PaymentTransformer.php @@ -25,7 +25,9 @@ class PaymentTransformer extends EntityTransformer protected $serializer; - protected $defaultIncludes = ['invoices']; + protected $defaultIncludes = [ + // 'invoices' + ]; protected $availableIncludes = [ 'client', diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index c4eed11060dc..855dfecf51dc 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -265,29 +265,29 @@ class RandomDataSeeder extends Seeder ]); - // if (config('ninja.testvars.stripe')) { - // $cg = new CompanyGateway; - // $cg->company_id = $company->id; - // $cg->user_id = $user->id; - // $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; - // $cg->require_cvv = true; - // $cg->show_billing_address = true; - // $cg->show_shipping_address = true; - // $cg->update_details = true; - // $cg->config = encrypt(config('ninja.testvars.stripe')); - // $cg->save(); + if (config('ninja.testvars.stripe')) { + $cg = new CompanyGateway; + $cg->company_id = $company->id; + $cg->user_id = $user->id; + $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $cg->require_cvv = true; + $cg->show_billing_address = true; + $cg->show_shipping_address = true; + $cg->update_details = true; + $cg->config = encrypt(config('ninja.testvars.stripe')); + $cg->save(); - // $cg = new CompanyGateway; - // $cg->company_id = $company->id; - // $cg->user_id = $user->id; - // $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; - // $cg->require_cvv = true; - // $cg->show_billing_address = true; - // $cg->show_shipping_address = true; - // $cg->update_details = true; - // $cg->config = encrypt(config('ninja.testvars.stripe')); - // $cg->save(); - // } + $cg = new CompanyGateway; + $cg->company_id = $company->id; + $cg->user_id = $user->id; + $cg->gateway_key = 'd14dd26a37cecc30fdd65700bfb55b23'; + $cg->require_cvv = true; + $cg->show_billing_address = true; + $cg->show_shipping_address = true; + $cg->update_details = true; + $cg->config = encrypt(config('ninja.testvars.stripe')); + $cg->save(); + } // if (config('ninja.testvars.paypal')) { // $cg = new CompanyGateway; @@ -315,18 +315,18 @@ class RandomDataSeeder extends Seeder // $cg->save(); // } - if(config('ninja.testvars.authorize')) { - $cg = new CompanyGateway; - $cg->company_id = $company->id; - $cg->user_id = $user->id; - $cg->gateway_key = '3b6621f970ab18887c4f6dca78d3f8bb'; - $cg->require_cvv = true; - $cg->show_billing_address = true; - $cg->show_shipping_address = true; - $cg->update_details = true; - $cg->config = encrypt(config('ninja.testvars.authorize')); - $cg->save(); - } + // if(config('ninja.testvars.authorize')) { + // $cg = new CompanyGateway; + // $cg->company_id = $company->id; + // $cg->user_id = $user->id; + // $cg->gateway_key = '3b6621f970ab18887c4f6dca78d3f8bb'; + // $cg->require_cvv = true; + // $cg->show_billing_address = true; + // $cg->show_shipping_address = true; + // $cg->update_details = true; + // $cg->config = encrypt(config('ninja.testvars.authorize')); + // $cg->save(); + // } } }