Merge pull request #5706 from turbo124/v5-develop

Don't store PDFs
This commit is contained in:
David Bomba 2021-05-15 12:19:59 +10:00 committed by GitHub
commit c7dfa8547f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 90 additions and 88 deletions

View File

@ -164,10 +164,10 @@ class InvoiceController extends Controller
//if only 1 pdf, output to buffer for download //if only 1 pdf, output to buffer for download
if ($invoices->count() == 1) { if ($invoices->count() == 1) {
return response()->streamDownload(function () use ($invoices) {
echo file_get_contents($invoices->first()->pdf_file_path()); $file = $invoices->first()->pdf_file_path();
}, basename($invoices->first()->pdf_file_path()), ['Cache-Control:' => 'no-cache']); return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);;
//return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($invoices->first()->pdf_file_path()));
} }
// enable output of HTTP headers // enable output of HTTP headers

View File

@ -76,10 +76,9 @@ class QuoteController extends Controller
} }
if ($quotes->count() == 1) { if ($quotes->count() == 1) {
return response()->streamDownload(function () use ($quotes) {
echo file_get_contents($quotes->first()->pdf_file_path()); $file = $quotes->first()->pdf_file_path();
}, basename($quotes->first()->pdf_file_path()), ['Cache-Control:' => 'no-cache']); return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
//return response()->download(TempFile::path($invoices->first()->pdf_file_path()), basename($quotes->first()->pdf_file_path()));
} }
// enable output of HTTP headers // enable output of HTTP headers

View File

@ -536,10 +536,8 @@ class CreditController extends BaseController
} }
break; break;
case 'download': case 'download':
return response()->streamDownload(function () use ($credit) { $file = $credit->pdf_file_path();
echo file_get_contents($credit->pdf_file_path()); return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
}, basename($credit->pdf_file_path()), ['Cache-Control:' => 'no-cache']);
//return response()->download(TempFile::path($credit->pdf_file_path()), basename($credit->pdf_file_path()));
break; break;
case 'archive': case 'archive':
$this->credit_repository->archive($credit); $this->credit_repository->archive($credit);
@ -589,7 +587,7 @@ class CreditController extends BaseController
$file_path = $credit->service()->getCreditPdf($invitation); $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);
} }
/** /**

View File

@ -671,10 +671,10 @@ class InvoiceController extends BaseController
} }
break; break;
case 'download': case 'download':
return response()->streamDownload(function () use ($invoice) {
echo file_get_contents($invoice->pdf_file_path()); $file = $invoice->pdf_file_path();
}, basename($invoice->pdf_file_path()), ['Cache-Control:' => 'no-cache']); return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
//return response()->download(TempFile::path($invoice->pdf_file_path()), basename($invoice->pdf_file_path()));
break; break;
case 'restore': case 'restore':
$this->invoice_repo->restore($invoice); $this->invoice_repo->restore($invoice);
@ -793,9 +793,10 @@ class InvoiceController extends BaseController
$contact = $invitation->contact; $contact = $invitation->contact;
$invoice = $invitation->invoice; $invoice = $invitation->invoice;
$file_path = $invoice->service()->getInvoicePdf($contact); $file = $invoice->service()->getInvoicePdf($contact);
return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);;
return response()->download($file_path, basename($file_path), ['Cache-Control:' => 'no-cache']);
} }
/** /**
@ -844,12 +845,10 @@ class InvoiceController extends BaseController
*/ */
public function deliveryNote(ShowInvoiceRequest $request, Invoice $invoice) 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 { try {
$file = public_path("storage/{$file_path}"); return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache']);
} catch (\Exception $e) { } catch (\Exception $e) {
return response(['message' => 'Oops, something went wrong. Make sure you have symlink to storage/ in public/ directory.'], 500); return response(['message' => 'Oops, something went wrong. Make sure you have symlink to storage/ in public/ directory.'], 500);
} }

View File

@ -675,10 +675,10 @@ class QuoteController extends BaseController
// code... // code...
break; break;
case 'download': case 'download':
return response()->streamDownload(function () use ($quote) {
echo file_get_contents($quote->pdf_file_path()); $file = $quote->pdf_file_path();
}, basename($quote->pdf_file_path()), ['Cache-Control:' => 'no-cache']); return response()->download($file, basename($file), ['Cache-Control:' => 'no-cache'])->deleteFileAfterSend(true);
//return response()->download(TempFile::path($quote->pdf_file_path()), basename($quote->pdf_file_path()));
break; break;
case 'restore': case 'restore':
$this->quote_repo->restore($quote); $this->quote_repo->restore($quote);
@ -730,7 +730,7 @@ class QuoteController extends BaseController
$file_path = $quote->service()->getQuotePdf($contact); $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);
} }
/** /**

View File

@ -497,7 +497,7 @@ class RecurringInvoiceController extends BaseController
$file_path = $recurring_invoice->service()->getInvoicePdf($contact); $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);
} }
/** /**

View File

@ -86,7 +86,9 @@ class CreateEntityPdf implements ShouldQueue
$this->contact = $invitation->contact; $this->contact = $invitation->contact;
$this->disk = $disk ?? config('filesystems.default'); $this->disk = 'public';
// $this->disk = $disk ?? config('filesystems.default');
} }
public function handle() public function handle()

View File

@ -83,23 +83,24 @@ class ZipInvoices implements ShouldQueue
$zip = new ZipStream($file_name, $options); $zip = new ZipStream($file_name, $options);
foreach ($this->invoices as $invoice) { 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(); $zip->finish();
Storage::disk(config('filesystems.default'))->put($path.$file_name, $tempStream); Storage::disk('public')->put($path.$file_name, $tempStream);
fclose($tempStream); fclose($tempStream);
$nmo = new NinjaMailerObject; $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->to_user = $this->user;
$nmo->settings = $this->settings; $nmo->settings = $this->settings;
$nmo->company = $this->company; $nmo->company = $this->company;
NinjaMailerJob::dispatch($nmo); 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));
} }
} }

View File

@ -39,8 +39,6 @@ class UnlinkFile implements ShouldQueue
*/ */
public function handle() public function handle()
{ {
// nlog("deleting");
// nlog($this->file_path);
Storage::disk($this->disk)->delete($this->file_path); Storage::disk($this->disk)->delete($this->file_path);
} }
} }

View File

@ -13,12 +13,14 @@ namespace App\Models;
use App\DataMapper\ClientSettings; use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
use App\Jobs\Entity\CreateEntityPdf;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes; use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
/** /**
@ -199,4 +201,5 @@ class BaseModel extends Model
return $formatted_number; return $formatted_number;
} }
} }

View File

@ -251,25 +251,29 @@ class Credit extends BaseModel
$this->save(); $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) { if (! $invitation) {
event(new CreditWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
CreateEntityPdf::dispatchNow($this->invitations->first()); if($this->invitations()->exists())
} else { $invitation = $this->invitations()->first();
event(new CreditWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); else{
CreateEntityPdf::dispatchNow($invitation); $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() public function markInvitationsSent()
{ {
$this->invitations->each(function ($invitation) { $this->invitations->each(function ($invitation) {

View File

@ -96,4 +96,9 @@ class Expense extends BaseModel
{ {
return $this->belongsTo(Vendor::class); return $this->belongsTo(Vendor::class);
} }
public function client()
{
return $this->belongsTo(Client::class);
}
} }

View File

@ -408,21 +408,12 @@ class Invoice extends BaseModel
if(!$invitation) if(!$invitation)
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?'); 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');
if (! Storage::exists($this->client->invoice_filepath().$this->numberFormatter().'.pdf')) { $file_path = CreateEntityPdf::dispatchNow($invitation);
event(new InvoiceWasUpdated($this, $this->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
CreateEntityPdf::dispatchNow($invitation); return Storage::disk('public')->path($file_path);
} }
nlog($storage_path);
return $storage_path;
}
/**
* Updates Invites to SENT.
*/
public function markInvitationsSent() public function markInvitationsSent()
{ {
$this->invitations->each(function ($invitation) { $this->invitations->each(function ($invitation) {

View File

@ -210,22 +210,25 @@ class Quote extends BaseModel
public function pdf_file_path($invitation = null, string $type = 'url') public function pdf_file_path($invitation = null, string $type = 'url')
{ {
if (! $invitation) { 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');
nlog($storage_path);
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; 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);
}
/** /**
* @param int $status * @param int $status

View File

@ -20,7 +20,6 @@ use App\Services\PdfMaker\PdfMaker as PdfMakerService;
use App\Utils\HostedPDF\NinjaPdf; use App\Utils\HostedPDF\NinjaPdf;
use App\Utils\HtmlEngine; use App\Utils\HtmlEngine;
use App\Utils\PhantomJS\Phantom; use App\Utils\PhantomJS\Phantom;
use App\Utils\TempFile;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Pdf\PdfMaker; use App\Utils\Traits\Pdf\PdfMaker;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -50,7 +49,9 @@ class GenerateDeliveryNote
$this->contact = $contact; $this->contact = $contact;
$this->disk = $disk ?? config('filesystems.default'); $this->disk = 'public';
// $this->disk = $disk ?? config('filesystems.default');
} }
public function run() public function run()
@ -106,10 +107,7 @@ class GenerateDeliveryNote
Storage::disk($this->disk)->put($file_path, $pdf); Storage::disk($this->disk)->put($file_path, $pdf);
/* Copy from remote disk to local when using cloud file storage. */ return Storage::disk($this->disk)->path($file_path);
if(config('filesystems.default') == 's3')
return TempFile::path(Storage::disk($this->disk)->url($file_path));
return $file_path;
} }
} }

View File

@ -39,7 +39,9 @@ class GetInvoicePdf extends AbstractService
$file_path = $path.$this->invoice->numberFormatter().'.pdf'; $file_path = $path.$this->invoice->numberFormatter().'.pdf';
$disk = config('filesystems.default'); $disk = 'public';
// $disk = config('filesystems.default');
$file = Storage::disk($disk)->exists($file_path); $file = Storage::disk($disk)->exists($file_path);
@ -47,10 +49,9 @@ class GetInvoicePdf extends AbstractService
$file_path = CreateEntityPdf::dispatchNow($invitation); $file_path = CreateEntityPdf::dispatchNow($invitation);
} }
// /* 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')
if(config('filesystems.default') == 's3') // return TempFile::path(Storage::disk($disk)->url($file_path));
return TempFile::path(Storage::disk($disk)->url($file_path));
// return Storage::disk($disk)->url($file_path); // return Storage::disk($disk)->url($file_path);
return Storage::disk($disk)->path($file_path); return Storage::disk($disk)->path($file_path);

View File

@ -40,20 +40,20 @@ class PdfCreatorTest extends TestCase
{ {
$credit_path = CreateEntityPdf::dispatchNow($this->credit->invitations->first()); $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() public function testInvoicePdfCreated()
{ {
$invoice_path = CreateEntityPdf::dispatchNow($this->invoice->invitations->first()); $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() public function testQuotePdfCreated()
{ {
$quote_path = CreateEntityPdf::dispatchNow($this->quote->invitations->first()); $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));
} }
} }