mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Refactor for backup storage location
This commit is contained in:
parent
4b728c3dd9
commit
ae3edef16c
91
app/Console/Commands/BackupUpdate.php
Normal file
91
app/Console/Commands/BackupUpdate.php
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Libraries\MultiDB;
|
||||||
|
use App\Models\Backup;
|
||||||
|
use App\Models\Design;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
|
class BackupUpdate extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'ninja:backup-update';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Shift backups from DB to storage';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
//always return state to first DB
|
||||||
|
|
||||||
|
$current_db = config('database.default');
|
||||||
|
|
||||||
|
if (! config('ninja.db.multi_db_enabled')) {
|
||||||
|
$this->handleOnDb();
|
||||||
|
} else {
|
||||||
|
|
||||||
|
//multiDB environment, need to
|
||||||
|
foreach (MultiDB::$dbs as $db) {
|
||||||
|
MultiDB::setDB($db);
|
||||||
|
|
||||||
|
$this->handleOnDb();
|
||||||
|
}
|
||||||
|
|
||||||
|
MultiDB::setDB($current_db);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function handleOnDb()
|
||||||
|
{
|
||||||
|
|
||||||
|
Backup::whereHas('activity')->whereNotNull('html_backup')->cursor()->each(function($backup){
|
||||||
|
|
||||||
|
if($backup->activity->client()->exists()){
|
||||||
|
|
||||||
|
$client = $backup->activity->client;
|
||||||
|
$backup->storeRemotely($backup->html_backup, $client);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,7 @@ use App\Utils\Traits\Pdf\PdfMaker;
|
|||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Response;
|
use Illuminate\Http\Response;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
@ -136,19 +137,33 @@ class ActivityController extends BaseController
|
|||||||
public function downloadHistoricalEntity(DownloadHistoricalEntityRequest $request, Activity $activity)
|
public function downloadHistoricalEntity(DownloadHistoricalEntityRequest $request, Activity $activity)
|
||||||
{
|
{
|
||||||
$backup = $activity->backup;
|
$backup = $activity->backup;
|
||||||
|
$html_backup = '';
|
||||||
|
|
||||||
if (! $backup || ! $backup->html_backup) {
|
/* Refactor 20-10-2021
|
||||||
|
*
|
||||||
|
* We have moved the backups out of the database and into object storage.
|
||||||
|
* In order to handle edge cases, we still check for the database backup
|
||||||
|
* in case the file no longer exists
|
||||||
|
*/
|
||||||
|
|
||||||
|
if($backup && $backup->filename && Storage::disk(config('filesystems.default'))->exists($backup->filename)){ //disk
|
||||||
|
$html_backup = file_get_contents(Storage::disk(config('filesystems.default'))->path($backup->filename));
|
||||||
|
}
|
||||||
|
elseif($backup && $backup->html_backup){ //db
|
||||||
|
$html_backup = $backup->html_backup;
|
||||||
|
}
|
||||||
|
elseif (! $backup || ! $backup->html_backup) { //failed
|
||||||
return response()->json(['message'=> ctrans('texts.no_backup_exists'), 'errors' => new stdClass], 404);
|
return response()->json(['message'=> ctrans('texts.no_backup_exists'), 'errors' => new stdClass], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
||||||
$pdf = (new Phantom)->convertHtmlToPdf($backup->html_backup);
|
$pdf = (new Phantom)->convertHtmlToPdf($html_backup);
|
||||||
}
|
}
|
||||||
elseif(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){
|
elseif(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){
|
||||||
$pdf = (new NinjaPdf())->build($backup->html_backup);
|
$pdf = (new NinjaPdf())->build($html_backup);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$pdf = $this->makePdf(null, null, $backup->html_backup);
|
$pdf = $this->makePdf(null, null, $html_backup);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($activity->invoice_id)) {
|
if (isset($activity->invoice_id)) {
|
||||||
|
@ -70,6 +70,13 @@ class StoreInvoiceRequest extends Request
|
|||||||
$input['amount'] = 0;
|
$input['amount'] = 0;
|
||||||
$input['balance'] = 0;
|
$input['balance'] = 0;
|
||||||
|
|
||||||
|
if(array_key_exists('tax_rate1', $input) && is_null($input['tax_rate1']))
|
||||||
|
$input['tax_rate1'] = 0;
|
||||||
|
if(array_key_exists('tax_rate2', $input) && is_null($input['tax_rate2']))
|
||||||
|
$input['tax_rate2'] = 0;
|
||||||
|
if(array_key_exists('tax_rate3', $input) && is_null($input['tax_rate3']))
|
||||||
|
$input['tax_rate3'] = 0;
|
||||||
|
|
||||||
$this->replace($input);
|
$this->replace($input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,9 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Models\Client;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
class Backup extends BaseModel
|
class Backup extends BaseModel
|
||||||
{
|
{
|
||||||
public function getEntityType()
|
public function getEntityType()
|
||||||
@ -22,4 +25,26 @@ class Backup extends BaseModel
|
|||||||
{
|
{
|
||||||
return $this->belongsTo(Activity::class);
|
return $this->belongsTo(Activity::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function storeRemotely(string $html, Client $client)
|
||||||
|
{
|
||||||
|
|
||||||
|
if(strlen($html) == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
$path = $client->backup_path() . "/";
|
||||||
|
$filename = now()->format('Y_m_d'). "_" . md5(time()) . ".html";
|
||||||
|
$file_path = $path . $filename;
|
||||||
|
|
||||||
|
Storage::disk(config('filesystems.default'))->makeDirectory($path, 0775);
|
||||||
|
|
||||||
|
Storage::disk(config('filesystems.default'))->put($file_path, $html);
|
||||||
|
|
||||||
|
if(Storage::disk(config('filesystems.default'))->exists($file_path)){
|
||||||
|
$this->html_backup = '';
|
||||||
|
$this->filename = $file_path;
|
||||||
|
$this->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -717,6 +717,12 @@ class Client extends BaseModel implements HasLocalePreference
|
|||||||
})->first()->locale;
|
})->first()->locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function backup_path()
|
||||||
|
{
|
||||||
|
return $this->company->company_key.'/'.$this->client_hash.'/backups';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function invoice_filepath($invitation)
|
public function invoice_filepath($invitation)
|
||||||
{
|
{
|
||||||
$contact_key = $invitation->contact->contact_key;
|
$contact_key = $invitation->contact->contact_key;
|
||||||
|
@ -73,24 +73,25 @@ class ActivityRepository extends BaseRepository
|
|||||||
if ($entity instanceof User || $entity->company->is_disabled)
|
if ($entity instanceof User || $entity->company->is_disabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
$backup = new Backup();
|
|
||||||
|
|
||||||
if (get_class($entity) == Invoice::class
|
if (get_class($entity) == Invoice::class
|
||||||
|| get_class($entity) == Quote::class
|
|| get_class($entity) == Quote::class
|
||||||
|| get_class($entity) == Credit::class
|
|| get_class($entity) == Credit::class
|
||||||
|| get_class($entity) == RecurringInvoice::class
|
|| get_class($entity) == RecurringInvoice::class
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
$backup = new Backup();
|
||||||
$entity->load('client');
|
$entity->load('client');
|
||||||
$contact = $entity->client->primary_contact()->first();
|
$contact = $entity->client->primary_contact()->first();
|
||||||
$backup->html_backup = $this->generateHtml($entity);
|
$backup->html_backup = $this->generateHtml($entity);
|
||||||
$backup->amount = $entity->amount;
|
$backup->amount = $entity->amount;
|
||||||
}
|
|
||||||
|
|
||||||
$backup->activity_id = $activity->id;
|
$backup->activity_id = $activity->id;
|
||||||
$backup->json_backup = '';
|
$backup->json_backup = '';
|
||||||
$backup->save();
|
$backup->save();
|
||||||
|
|
||||||
|
$backup->storeRemotely($this->generateHtml($entity), $entity->client);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTokenId(array $event_vars)
|
public function getTokenId(array $event_vars)
|
||||||
@ -126,7 +127,7 @@ class ActivityRepository extends BaseRepository
|
|||||||
|
|
||||||
if(!$entity->invitations()->exists() || !$design){
|
if(!$entity->invitations()->exists() || !$design){
|
||||||
nlog("No invitations for entity {$entity->id} - {$entity->number}");
|
nlog("No invitations for entity {$entity->id} - {$entity->number}");
|
||||||
return;
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$entity->load('client.company', 'invitations');
|
$entity->load('client.company', 'invitations');
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AddFilenameToBackupsTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('backups', function (Blueprint $table) {
|
||||||
|
$table->text('filename')->nullable();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user