From 7756ca7436f6ff434c5385fc6fbed33e911d83f5 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 14 May 2021 21:15:11 +1000 Subject: [PATCH 1/2] Don't store PDFs --- app/Http/Controllers/InvoiceController.php | 15 ++++++++---- app/Jobs/Entity/CreateEntityPdf.php | 22 +++++++++-------- app/Jobs/Util/UnlinkFile.php | 2 -- app/Services/Invoice/GenerateDeliveryNote.php | 14 ++++------- app/Services/Invoice/GetInvoicePdf.php | 24 ++++++++++--------- 5 files changed, 40 insertions(+), 37 deletions(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index bf887607c1..78dab90a5a 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -795,7 +795,11 @@ class InvoiceController extends BaseController $file_path = $invoice->service()->getInvoicePdf($contact); - return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache']); + return response()->streamDownload(function () use ($file_path) { + echo file_get_contents($file_path); + }, basename($file_path), ['Cache-Control:' => 'no-cache']); + + //return response()->download(Storage::url($file_path), basename($file_path), ['Cache-Control:' => 'no-cache']); } /** @@ -844,12 +848,15 @@ class InvoiceController extends BaseController */ public function deliveryNote(ShowInvoiceRequest $request, Invoice $invoice) { - $file_path = $invoice->service()->getInvoiceDeliveryNote($invoice, $invoice->invitations->first()->contact); + $file = $invoice->service()->getInvoiceDeliveryNote($invoice, $invoice->invitations->first()->contact); try { - $file = public_path("storage/{$file_path}"); - return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache']); + return response()->streamDownload(function () use ($file) { + echo file_get_contents($file); + }, basename($file), ['Cache-Control:' => 'no-cache']); + + // return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache']); } catch (\Exception $e) { return response(['message' => 'Oops, something went wrong. Make sure you have symlink to storage/ in public/ directory.'], 500); } diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index 6fd777da27..a4d7a5fcec 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -30,6 +30,7 @@ use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HtmlEngine; use App\Utils\Ninja; use App\Utils\PhantomJS\Phantom; +use App\Utils\TempFile; use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesInvoiceHtml; use App\Utils\Traits\NumberFormatter; @@ -187,22 +188,23 @@ class CreateEntityPdf implements ShouldQueue } - if ($pdf) { + // if ($pdf) { - try{ + // try{ - Storage::disk($this->disk)->put($file_path, $pdf); + // // Storage::disk($this->disk)->put($file_path, $pdf); - } - catch(\Exception $e) - { + // } + // catch(\Exception $e) + // { - throw new FilePermissionsFailure('Could not write the PDF, permission issues!'); + // throw new FilePermissionsFailure('Could not write the PDF, permission issues!'); - } - } + // } + // } + // // nlog("entity says path = {$file_path}"); + return TempFile::filePath($pdf, $this->entity->numberFormatter().'.pdf'); - return $file_path; } public function failed($e) diff --git a/app/Jobs/Util/UnlinkFile.php b/app/Jobs/Util/UnlinkFile.php index 70461add3b..a7f5f02cb4 100644 --- a/app/Jobs/Util/UnlinkFile.php +++ b/app/Jobs/Util/UnlinkFile.php @@ -39,8 +39,6 @@ class UnlinkFile implements ShouldQueue */ public function handle() { - // nlog("deleting"); - // nlog($this->file_path); Storage::disk($this->disk)->delete($this->file_path); } } diff --git a/app/Services/Invoice/GenerateDeliveryNote.php b/app/Services/Invoice/GenerateDeliveryNote.php index 3974071217..157a49ca49 100644 --- a/app/Services/Invoice/GenerateDeliveryNote.php +++ b/app/Services/Invoice/GenerateDeliveryNote.php @@ -59,7 +59,7 @@ class GenerateDeliveryNote ? $this->invoice->design_id : $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id')); - $file_path = sprintf('%s%s_delivery_note.pdf', $this->invoice->client->invoice_filepath(), $this->invoice->number); + $filename = sprintf('%s_delivery_note.pdf', $this->invoice->number); if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { return (new Phantom)->generate($this->invoice->invitations->first()); @@ -91,8 +91,6 @@ class GenerateDeliveryNote ->design($template) ->build(); - // Storage::makeDirectory($this->invoice->client->invoice_filepath(), 0775); - if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){ $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true)); } @@ -104,12 +102,8 @@ class GenerateDeliveryNote info($maker->getCompiledHTML()); } - Storage::disk($this->disk)->put($file_path, $pdf); - - /* Copy from remote disk to local when using cloud file storage. */ - if(config('filesystems.default') == 's3') - return TempFile::path(Storage::disk($this->disk)->url($file_path)); - - return $file_path; + return TempFile::filePath($pdf, $filename); + // Storage::disk($this->disk)->put($file_path, $pdf); + // return $file_path; } } diff --git a/app/Services/Invoice/GetInvoicePdf.php b/app/Services/Invoice/GetInvoicePdf.php index 96917d16f2..60d48d25e5 100644 --- a/app/Services/Invoice/GetInvoicePdf.php +++ b/app/Services/Invoice/GetInvoicePdf.php @@ -35,24 +35,26 @@ class GetInvoicePdf extends AbstractService $invitation = $this->invoice->invitations->where('client_contact_id', $this->contact->id)->first(); - $path = $this->invoice->client->invoice_filepath(); + // $path = $this->invoice->client->invoice_filepath(); - $file_path = $path.$this->invoice->numberFormatter().'.pdf'; + // $file_path = $path.$this->invoice->numberFormatter().'.pdf'; - $disk = config('filesystems.default'); + // $disk = config('filesystems.default'); - $file = Storage::disk($disk)->exists($file_path); + // $file = Storage::disk($disk)->exists($file_path); - if (! $file) { - $file_path = CreateEntityPdf::dispatchNow($invitation); - } + // if (! $file) { + return CreateEntityPdf::dispatchNow($invitation); + // } + // return TempFile::filePath($pdf, $file_path); + // return $file_path; /* Copy from remote disk to local when using cloud file storage. */ - if(config('filesystems.default') == 's3') - return TempFile::path(Storage::disk($disk)->url($file_path)); + // if(config('filesystems.default') == 's3') + // return TempFile::path(Storage::disk($disk)->url($file_path)); - // return Storage::disk($disk)->url($file_path); - return Storage::disk($disk)->path($file_path); + // // return Storage::disk($disk)->url($file_path); + // return Storage::disk($disk)->path($file_path); } } From c52311bcde78e01902fdfb761321b91d376b1e6d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Sat, 15 May 2021 12:19:36 +1000 Subject: [PATCH 2/2] Fixes for PDF file paths --- .../ClientPortal/InvoiceController.php | 8 ++--- .../ClientPortal/QuoteController.php | 7 ++--- app/Http/Controllers/CreditController.php | 10 +++---- app/Http/Controllers/InvoiceController.php | 22 +++++--------- app/Http/Controllers/QuoteController.php | 10 +++---- .../RecurringInvoiceController.php | 2 +- app/Jobs/Entity/CreateEntityPdf.php | 26 ++++++++-------- app/Jobs/Invoice/ZipInvoices.php | 9 +++--- app/Models/BaseModel.php | 3 ++ app/Models/Credit.php | 30 +++++++++++-------- app/Models/Expense.php | 5 ++++ app/Models/Invoice.php | 15 ++-------- app/Models/Quote.php | 23 +++++++------- app/Services/Invoice/GenerateDeliveryNote.php | 16 ++++++---- app/Services/Invoice/GetInvoicePdf.php | 23 +++++++------- tests/Feature/PdfCreatorTest.php | 10 +++---- 16 files changed, 109 insertions(+), 110 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index 213eef5360..0472578597 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -164,10 +164,10 @@ class InvoiceController extends Controller //if only 1 pdf, output to buffer for download if ($invoices->count() == 1) { - return response()->streamDownload(function () use ($invoices) { - echo file_get_contents($invoices->first()->pdf_file_path()); - }, basename($invoices->first()->pdf_file_path()), ['Cache-Control:' => 'no-cache']); - //return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($invoices->first()->pdf_file_path())); + + $file = $invoices->first()->pdf_file_path(); + return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);; + } // enable output of HTTP headers diff --git a/app/Http/Controllers/ClientPortal/QuoteController.php b/app/Http/Controllers/ClientPortal/QuoteController.php index a7d6354eee..8e180cdebd 100644 --- a/app/Http/Controllers/ClientPortal/QuoteController.php +++ b/app/Http/Controllers/ClientPortal/QuoteController.php @@ -76,10 +76,9 @@ class QuoteController extends Controller } if ($quotes->count() == 1) { - return response()->streamDownload(function () use ($quotes) { - echo file_get_contents($quotes->first()->pdf_file_path()); - }, basename($quotes->first()->pdf_file_path()), ['Cache-Control:' => 'no-cache']); - //return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($quotes->first()->pdf_file_path())); + + $file = $quotes->first()->pdf_file_path(); + return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); } // enable output of HTTP headers diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index 459e701169..9feb62c7e4 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -535,11 +535,9 @@ class CreditController extends BaseController return $this->itemResponse($credit); } break; - case 'download': - return response()->streamDownload(function () use ($credit) { - echo file_get_contents($credit->pdf_file_path()); - }, basename($credit->pdf_file_path()), ['Cache-Control:' => 'no-cache']); - //return response()->download(TempFile::path($credit->pdf_file_path()), basename($credit->pdf_file_path())); + case 'download': + $file = $credit->pdf_file_path(); + return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); break; case 'archive': $this->credit_repository->archive($credit); @@ -589,7 +587,7 @@ class CreditController extends BaseController $file_path = $credit->service()->getCreditPdf($invitation); - return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache']); + return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); } /** diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 78dab90a5a..b2ea9da7ca 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -671,10 +671,10 @@ class InvoiceController extends BaseController } break; case 'download': - return response()->streamDownload(function () use ($invoice) { - echo file_get_contents($invoice->pdf_file_path()); - }, basename($invoice->pdf_file_path()), ['Cache-Control:' => 'no-cache']); - //return response()->download(TempFile::path($invoice->pdf_file_path()), basename($invoice->pdf_file_path())); + + $file = $invoice->pdf_file_path(); + return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); + break; case 'restore': $this->invoice_repo->restore($invoice); @@ -793,13 +793,10 @@ class InvoiceController extends BaseController $contact = $invitation->contact; $invoice = $invitation->invoice; - $file_path = $invoice->service()->getInvoicePdf($contact); + $file = $invoice->service()->getInvoicePdf($contact); - return response()->streamDownload(function () use ($file_path) { - echo file_get_contents($file_path); - }, basename($file_path), ['Cache-Control:' => 'no-cache']); + return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);; - //return response()->download(Storage::url($file_path), basename($file_path), ['Cache-Control:' => 'no-cache']); } /** @@ -851,12 +848,7 @@ class InvoiceController extends BaseController $file = $invoice->service()->getInvoiceDeliveryNote($invoice, $invoice->invitations->first()->contact); try { - - return response()->streamDownload(function () use ($file) { - echo file_get_contents($file); - }, basename($file), ['Cache-Control:' => 'no-cache']); - - // return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache']); + return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); } catch (\Exception $e) { return response(['message' => 'Oops, something went wrong. Make sure you have symlink to storage/ in public/ directory.'], 500); } diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index ff7c212eac..927de58d26 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -675,10 +675,10 @@ class QuoteController extends BaseController // code... break; case 'download': - return response()->streamDownload(function () use ($quote) { - echo file_get_contents($quote->pdf_file_path()); - }, basename($quote->pdf_file_path()), ['Cache-Control:' => 'no-cache']); - //return response()->download(TempFile::path($quote->pdf_file_path()), basename($quote->pdf_file_path())); + + $file = $quote->pdf_file_path(); + return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); + break; case 'restore': $this->quote_repo->restore($quote); @@ -730,7 +730,7 @@ class QuoteController extends BaseController $file_path = $quote->service()->getQuotePdf($contact); - return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache']); + return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); } /** diff --git a/app/Http/Controllers/RecurringInvoiceController.php b/app/Http/Controllers/RecurringInvoiceController.php index cbe08ba890..93bf651560 100644 --- a/app/Http/Controllers/RecurringInvoiceController.php +++ b/app/Http/Controllers/RecurringInvoiceController.php @@ -497,7 +497,7 @@ class RecurringInvoiceController extends BaseController $file_path = $recurring_invoice->service()->getInvoicePdf($contact); - return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache']); + return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true); } /** diff --git a/app/Jobs/Entity/CreateEntityPdf.php b/app/Jobs/Entity/CreateEntityPdf.php index a4d7a5fcec..f76711a8e0 100644 --- a/app/Jobs/Entity/CreateEntityPdf.php +++ b/app/Jobs/Entity/CreateEntityPdf.php @@ -30,7 +30,6 @@ use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HtmlEngine; use App\Utils\Ninja; use App\Utils\PhantomJS\Phantom; -use App\Utils\TempFile; use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesInvoiceHtml; use App\Utils\Traits\NumberFormatter; @@ -87,7 +86,9 @@ class CreateEntityPdf implements ShouldQueue $this->contact = $invitation->contact; - $this->disk = $disk ?? config('filesystems.default'); + $this->disk = 'public'; + + // $this->disk = $disk ?? config('filesystems.default'); } public function handle() @@ -188,23 +189,22 @@ class CreateEntityPdf implements ShouldQueue } - // if ($pdf) { + if ($pdf) { - // try{ + try{ - // // Storage::disk($this->disk)->put($file_path, $pdf); + Storage::disk($this->disk)->put($file_path, $pdf); - // } - // catch(\Exception $e) - // { + } + catch(\Exception $e) + { - // throw new FilePermissionsFailure('Could not write the PDF, permission issues!'); + throw new FilePermissionsFailure('Could not write the PDF, permission issues!'); - // } - // } - // // nlog("entity says path = {$file_path}"); - return TempFile::filePath($pdf, $this->entity->numberFormatter().'.pdf'); + } + } + return $file_path; } public function failed($e) diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index 4d1035db93..07a0544b63 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -83,23 +83,24 @@ class ZipInvoices implements ShouldQueue $zip = new ZipStream($file_name, $options); foreach ($this->invoices as $invoice) { - $zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path())); + //$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path())); + $zip->addFileFromPath(basename($invoice->pdf_file_path()), $invoice->pdf_file_path()); } $zip->finish(); - Storage::disk(config('filesystems.default'))->put($path.$file_name, $tempStream); + Storage::disk('public')->put($path.$file_name, $tempStream); fclose($tempStream); $nmo = new NinjaMailerObject; - $nmo->mailable = new DownloadInvoices(Storage::disk(config('filesystems.default'))->url($path.$file_name), $this->company); + $nmo->mailable = new DownloadInvoices(Storage::disk('public')->url($path.$file_name), $this->company); $nmo->to_user = $this->user; $nmo->settings = $this->settings; $nmo->company = $this->company; NinjaMailerJob::dispatch($nmo); - UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1)); + UnlinkFile::dispatch('public', $path.$file_name)->delay(now()->addHours(1)); } } diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index ab0e0ee4b8..6a6c59de62 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -13,12 +13,14 @@ namespace App\Models; use App\DataMapper\ClientSettings; use App\DataMapper\CompanySettings; +use App\Jobs\Entity\CreateEntityPdf; use App\Utils\Traits\MakesHash; use App\Utils\Traits\UserSessionAttributes; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Storage; /** @@ -199,4 +201,5 @@ class BaseModel extends Model return $formatted_number; } + } diff --git a/app/Models/Credit.php b/app/Models/Credit.php index 6578a22788..0f50a65a4f 100644 --- a/app/Models/Credit.php +++ b/app/Models/Credit.php @@ -251,25 +251,29 @@ class Credit extends BaseModel $this->save(); } - public function pdf_file_path($invitation = null) + public function pdf_file_path($invitation = null, string $type = 'url') { - $storage_path = Storage::url($this->client->credit_filepath().$this->numberFormatter().'.pdf'); - - if (Storage::exists($this->client->credit_filepath().$this->numberFormatter().'.pdf')) { - return $storage_path; - } - if (! $invitation) { - event(new CreditWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - CreateEntityPdf::dispatchNow($this->invitations->first()); - } else { - event(new CreditWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - CreateEntityPdf::dispatchNow($invitation); + + if($this->invitations()->exists()) + $invitation = $this->invitations()->first(); + else{ + $this->service()->createInvitations(); + $invitation = $this->invitations()->first(); + } + } - return $storage_path; + if(!$invitation) + throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); + + + $file_path = CreateEntityPdf::dispatchNow($invitation); + + return Storage::disk('public')->path($file_path); } + public function markInvitationsSent() { $this->invitations->each(function ($invitation) { diff --git a/app/Models/Expense.php b/app/Models/Expense.php index c50a3f59f7..8a6c12935e 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -96,4 +96,9 @@ class Expense extends BaseModel { return $this->belongsTo(Vendor::class); } + + public function client() + { + return $this->belongsTo(Client::class); + } } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 7acfb32588..d46a36ca9f 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -408,21 +408,12 @@ class Invoice extends BaseModel if(!$invitation) throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); - $storage_path = Storage::$type($this->client->invoice_filepath().$this->numberFormatter().'.pdf'); + + $file_path = CreateEntityPdf::dispatchNow($invitation); - if (! Storage::exists($this->client->invoice_filepath().$this->numberFormatter().'.pdf')) { - event(new InvoiceWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - CreateEntityPdf::dispatchNow($invitation); - } - -nlog($storage_path); - - return $storage_path; + return Storage::disk('public')->path($file_path); } - /** - * Updates Invites to SENT. - */ public function markInvitationsSent() { $this->invitations->each(function ($invitation) { diff --git a/app/Models/Quote.php b/app/Models/Quote.php index 7eba2847be..18df2d3b5d 100644 --- a/app/Models/Quote.php +++ b/app/Models/Quote.php @@ -210,23 +210,26 @@ class Quote extends BaseModel public function pdf_file_path($invitation = null, string $type = 'url') { if (! $invitation) { - $invitation = $this->invitations->first(); + + if($this->invitations()->exists()) + $invitation = $this->invitations()->first(); + else{ + $this->service()->createInvitations(); + $invitation = $this->invitations()->first(); + } + } - $storage_path = Storage::$type($this->client->quote_filepath().$this->numberFormatter().'.pdf'); + if(!$invitation) + throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); - nlog($storage_path); + + $file_path = CreateEntityPdf::dispatchNow($invitation); - if (! Storage::exists($this->client->quote_filepath().$this->numberFormatter().'.pdf')) { - event(new QuoteWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - CreateEntityPdf::dispatchNow($invitation); - } - - return $storage_path; + return Storage::disk('public')->path($file_path); } - /** * @param int $status * @return string diff --git a/app/Services/Invoice/GenerateDeliveryNote.php b/app/Services/Invoice/GenerateDeliveryNote.php index 157a49ca49..4c8d681a73 100644 --- a/app/Services/Invoice/GenerateDeliveryNote.php +++ b/app/Services/Invoice/GenerateDeliveryNote.php @@ -20,7 +20,6 @@ use App\Services\PdfMaker\PdfMaker as PdfMakerService; use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HtmlEngine; use App\Utils\PhantomJS\Phantom; -use App\Utils\TempFile; use App\Utils\Traits\MakesHash; use App\Utils\Traits\Pdf\PdfMaker; use Illuminate\Support\Facades\Storage; @@ -50,7 +49,9 @@ class GenerateDeliveryNote $this->contact = $contact; - $this->disk = $disk ?? config('filesystems.default'); + $this->disk = 'public'; + + // $this->disk = $disk ?? config('filesystems.default'); } public function run() @@ -59,7 +60,7 @@ class GenerateDeliveryNote ? $this->invoice->design_id : $this->decodePrimaryKey($this->invoice->client->getSetting('invoice_design_id')); - $filename = sprintf('%s_delivery_note.pdf', $this->invoice->number); + $file_path = sprintf('%s%s_delivery_note.pdf', $this->invoice->client->invoice_filepath(), $this->invoice->number); if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') { return (new Phantom)->generate($this->invoice->invitations->first()); @@ -91,6 +92,8 @@ class GenerateDeliveryNote ->design($template) ->build(); + // Storage::makeDirectory($this->invoice->client->invoice_filepath(), 0775); + if(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){ $pdf = (new NinjaPdf())->build($maker->getCompiledHTML(true)); } @@ -102,8 +105,9 @@ class GenerateDeliveryNote info($maker->getCompiledHTML()); } - return TempFile::filePath($pdf, $filename); - // Storage::disk($this->disk)->put($file_path, $pdf); - // return $file_path; + Storage::disk($this->disk)->put($file_path, $pdf); + + return Storage::disk($this->disk)->path($file_path); + } } diff --git a/app/Services/Invoice/GetInvoicePdf.php b/app/Services/Invoice/GetInvoicePdf.php index 60d48d25e5..e142f024e4 100644 --- a/app/Services/Invoice/GetInvoicePdf.php +++ b/app/Services/Invoice/GetInvoicePdf.php @@ -35,26 +35,25 @@ class GetInvoicePdf extends AbstractService $invitation = $this->invoice->invitations->where('client_contact_id', $this->contact->id)->first(); - // $path = $this->invoice->client->invoice_filepath(); + $path = $this->invoice->client->invoice_filepath(); - // $file_path = $path.$this->invoice->numberFormatter().'.pdf'; + $file_path = $path.$this->invoice->numberFormatter().'.pdf'; + + $disk = 'public'; // $disk = config('filesystems.default'); - // $file = Storage::disk($disk)->exists($file_path); + $file = Storage::disk($disk)->exists($file_path); - // if (! $file) { - return CreateEntityPdf::dispatchNow($invitation); - // } - // return TempFile::filePath($pdf, $file_path); + if (! $file) { + $file_path = CreateEntityPdf::dispatchNow($invitation); + } - // return $file_path; - - /* Copy from remote disk to local when using cloud file storage. */ + // /* Copy from remote disk to local when using cloud file storage. */ // if(config('filesystems.default') == 's3') // return TempFile::path(Storage::disk($disk)->url($file_path)); - // // return Storage::disk($disk)->url($file_path); - // return Storage::disk($disk)->path($file_path); + // return Storage::disk($disk)->url($file_path); + return Storage::disk($disk)->path($file_path); } } diff --git a/tests/Feature/PdfCreatorTest.php b/tests/Feature/PdfCreatorTest.php index a08fb0cb4c..1660270abd 100644 --- a/tests/Feature/PdfCreatorTest.php +++ b/tests/Feature/PdfCreatorTest.php @@ -40,20 +40,20 @@ class PdfCreatorTest extends TestCase { $credit_path = CreateEntityPdf::dispatchNow($this->credit->invitations->first()); - $this->assertTrue(Storage::exists($this->client->credit_filepath().$this->credit->number.'.pdf')); + $this->assertTrue(Storage::disk('public')->exists($credit_path)); } public function testInvoicePdfCreated() { $invoice_path = CreateEntityPdf::dispatchNow($this->invoice->invitations->first()); - - $this->assertTrue(Storage::exists($this->client->invoice_filepath().$this->invoice->number.'.pdf')); - } + + $this->assertTrue(Storage::disk('public')->exists($invoice_path)); + } public function testQuotePdfCreated() { $quote_path = CreateEntityPdf::dispatchNow($this->quote->invitations->first()); - $this->assertTrue(Storage::exists($this->client->quote_filepath().$this->quote->number.'.pdf')); + $this->assertTrue(Storage::disk('public')->exists($quote_path)); } }