1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Fixes for PDF file paths

This commit is contained in:
David Bomba 2021-05-15 12:19:36 +10:00
parent 7756ca7436
commit c52311bcde
16 changed files with 109 additions and 110 deletions

View File

@ -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

View File

@ -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

View File

@ -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);
}
/**

View File

@ -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);
}

View File

@ -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);
}
/**

View File

@ -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);
}
/**

View File

@ -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)

View File

@ -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));
}
}

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -96,4 +96,9 @@ class Expense extends BaseModel
{
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)
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) {

View File

@ -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

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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));
}
}