1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Remove illegal characters from pdf file names

This commit is contained in:
David Bomba 2021-03-17 22:29:20 +11:00
parent f10f7dca42
commit f52fb31ced
18 changed files with 38 additions and 29 deletions

View File

@ -142,11 +142,11 @@ class ActivityController extends BaseController
$pdf = $this->makePdf(null, null, $backup->html_backup);
if (isset($activity->invoice_id)) {
$filename = $activity->invoice->number.'.pdf';
$filename = $activity->invoice->numberFormatter().'.pdf';
} elseif (isset($activity->quote_id)) {
$filename = $activity->quote->number.'.pdf';
$filename = $activity->quote->numberFormatter().'.pdf';
} elseif (isset($activity->credit_id)) {
$filename = $activity->credit->number.'.pdf';
$filename = $activity->credit->numberFormatter().'.pdf';
} else {
$filename = 'backup.pdf';
}

View File

@ -115,7 +115,7 @@ class CreateEntityPdf implements ShouldQueue
$entity_design_id = 'invoice_design_id';
}
$file_path = $path.$this->entity->number.'.pdf';
$file_path = $path.$this->entity->numberFormatter().'.pdf';
$entity_design_id = $this->entity->design_id ? $this->entity->design_id : $this->decodePrimaryKey($this->entity->client->getSetting($entity_design_id));

View File

@ -186,6 +186,15 @@ class BaseModel extends Model
*/
public function getFileName($extension = 'pdf')
{
return $this->number.'.'.$extension;
return $this->numberFormatter().'.'.$extension;
}
public function numberFormatter()
{
$formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $this->number);
// Remove any runs of periods (thanks falstro!)
$formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number);
return $formatted_number;
}
}

View File

@ -248,9 +248,9 @@ class Credit extends BaseModel
public function pdf_file_path($invitation = null)
{
$storage_path = Storage::url($this->client->credit_filepath().$this->number.'.pdf');
$storage_path = Storage::url($this->client->credit_filepath().$this->numberFormatter().'.pdf');
if (Storage::exists($this->client->credit_filepath().$this->number.'.pdf')) {
if (Storage::exists($this->client->credit_filepath().$this->numberFormatter().'.pdf')) {
return $storage_path;
}

View File

@ -126,9 +126,9 @@ class CreditInvitation extends BaseModel
public function pdf_file_path()
{
$storage_path = Storage::url($this->credit->client->quote_filepath().$this->credit->number.'.pdf');
$storage_path = Storage::url($this->credit->client->quote_filepath().$this->credit->numberFormatter().'.pdf');
if (! Storage::exists($this->credit->client->credit_filepath().$this->credit->number.'.pdf')) {
if (! Storage::exists($this->credit->client->credit_filepath().$this->credit->numberFormatter().'.pdf')) {
event(new CreditWasUpdated($this, $this->company, Ninja::eventVars()));
CreateEntityPdf::dispatchNow($this);
}

View File

@ -388,9 +388,9 @@ class Invoice extends BaseModel
$invitation = $this->invitations->first();
}
$storage_path = Storage::$type($this->client->invoice_filepath().$this->number.'.pdf');
$storage_path = Storage::$type($this->client->invoice_filepath().$this->numberFormatter().'.pdf');
if (! Storage::exists($this->client->invoice_filepath().$this->number.'.pdf')) {
if (! Storage::exists($this->client->invoice_filepath().$this->numberFormatter().'.pdf')) {
event(new InvoiceWasUpdated($this, $this->company, Ninja::eventVars()));
CreateEntityPdf::dispatchNow($invitation);
}

View File

@ -140,9 +140,9 @@ class InvoiceInvitation extends BaseModel
public function pdf_file_path()
{
$storage_path = Storage::url($this->invoice->client->invoice_filepath().$this->invoice->number.'.pdf');
$storage_path = Storage::url($this->invoice->client->invoice_filepath().$this->invoice->numberFormatter().'.pdf');
if (! Storage::exists($this->invoice->client->invoice_filepath().$this->invoice->number.'.pdf')) {
if (! Storage::exists($this->invoice->client->invoice_filepath().$this->invoice->numberFormatter().'.pdf')) {
event(new InvoiceWasUpdated($this->invoice, $this->company, Ninja::eventVars()));
CreateEntityPdf::dispatchNow($this);
}

View File

@ -208,11 +208,11 @@ class Quote extends BaseModel
$invitation = $this->invitations->first();
}
$storage_path = Storage::$type($this->client->quote_filepath().$this->number.'.pdf');
$storage_path = Storage::$type($this->client->quote_filepath().$this->numberFormatter().'.pdf');
nlog($storage_path);
if (! Storage::exists($this->client->quote_filepath().$this->number.'.pdf')) {
if (! Storage::exists($this->client->quote_filepath().$this->numberFormatter().'.pdf')) {
event(new QuoteWasUpdated($this, $this->company, Ninja::eventVars()));
CreateEntityPdf::dispatchNow($invitation);
}

View File

@ -130,9 +130,9 @@ class QuoteInvitation extends BaseModel
public function pdf_file_path()
{
$storage_path = Storage::url($this->quote->client->quote_filepath().$this->quote->number.'.pdf');
$storage_path = Storage::url($this->quote->client->quote_filepath().$this->quote->numberFormatter().'.pdf');
if (! Storage::exists($this->quote->client->quote_filepath().$this->quote->number.'.pdf')) {
if (! Storage::exists($this->quote->client->quote_filepath().$this->quote->numberFormatter().'.pdf')) {
event(new QuoteWasUpdated($this->quote, $this->company, Ninja::eventVars()));
CreateEntityPdf::dispatchNow($this);
}

View File

@ -52,7 +52,7 @@ class InvoiceObserver
WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company);
}
// UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->number.'.pdf');
// UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->numberFormatter().'.pdf');
}

View File

@ -140,7 +140,7 @@ class CreditService
public function deletePdf()
{
UnlinkFile::dispatchNow(config('filesystems.default'), $this->credit->client->credit_filepath() . $this->credit->number.'.pdf');
UnlinkFile::dispatchNow(config('filesystems.default'), $this->credit->client->credit_filepath() . $this->credit->numberFormatter().'.pdf');
return $this;
}

View File

@ -38,7 +38,7 @@ class GetCreditPdf extends AbstractService
$path = $this->credit->client->credit_filepath();
$file_path = $path.$this->credit->number.'.pdf';
$file_path = $path.$this->credit->numberFormatter().'.pdf';
$disk = config('filesystems.default');

View File

@ -37,7 +37,7 @@ class GetInvoicePdf extends AbstractService
$path = $this->invoice->client->invoice_filepath();
$file_path = $path.$this->invoice->number.'.pdf';
$file_path = $path.$this->invoice->numberFormatter().'.pdf';
$disk = config('filesystems.default');

View File

@ -274,8 +274,8 @@ class InvoiceService
public function deletePdf()
{
//UnlinkFile::dispatchNow(config('filesystems.default'), $this->invoice->client->invoice_filepath() . $this->invoice->number.'.pdf');
Storage::disk(config('filesystems.default'))->delete($this->invoice->client->invoice_filepath() . $this->invoice->number.'.pdf');
//UnlinkFile::dispatchNow(config('filesystems.default'), $this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');
Storage::disk(config('filesystems.default'))->delete($this->invoice->client->invoice_filepath() . $this->invoice->numberFormatter().'.pdf');
return $this;
}

View File

@ -36,7 +36,7 @@ class GetQuotePdf extends AbstractService
$path = $this->quote->client->quote_filepath();
$file_path = $path.$this->quote->number.'.pdf';
$file_path = $path.$this->quote->numberFormatter().'.pdf';
$disk = config('filesystems.default');

View File

@ -178,7 +178,7 @@ class QuoteService
public function deletePdf()
{
UnlinkFile::dispatchNow(config('filesystems.default'), $this->quote->client->quote_filepath() . $this->quote->number.'.pdf');
UnlinkFile::dispatchNow(config('filesystems.default'), $this->quote->client->quote_filepath() . $this->quote->numberFormatter().'.pdf');
return $this;
}

View File

@ -87,7 +87,7 @@ class RecurringService
public function deletePdf()
{
UnlinkFile::dispatchNow(config('filesystems.default'), $this->recurring_entity->client->recurring_invoice_filepath() . $this->recurring_entity->number.'.pdf');
UnlinkFile::dispatchNow(config('filesystems.default'), $this->recurring_entity->client->recurring_invoice_filepath() . $this->recurring_entity->numberFormatter().'.pdf');
return $this;
}

View File

@ -76,7 +76,7 @@ class Phantom
$path = $entity_obj->client->recurring_invoice_filepath();
}
$file_path = $path.$entity_obj->number.'.pdf';
$file_path = $path.$entity_obj->numberFormatter().'.pdf';
$url = config('ninja.app_url').'/phantom/'.$entity.'/'.$invitation->key.'?phantomjs_secret='.config('ninja.phantomjs_secret');
info($url);
@ -91,8 +91,8 @@ class Phantom
$instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf);
nlog($instance);
nlog($file_path);
// nlog($instance);
// nlog($file_path);
return $file_path;
}