1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/Console/Commands/BackupUpdate.php

208 lines
6.4 KiB
PHP
Raw Normal View History

2021-10-20 05:05:46 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-10-20 05:05:46 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Console\Commands;
2023-10-26 04:57:44 +02:00
use App\Libraries\MultiDB;
2021-10-20 05:05:46 +02:00
use App\Models\Backup;
2023-03-05 03:52:31 +01:00
use App\Models\Client;
2022-11-06 22:20:14 +01:00
use App\Models\Company;
2022-11-11 04:52:50 +01:00
use App\Models\Document;
2023-03-05 03:52:31 +01:00
use App\Models\GroupSetting;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
2021-10-20 05:05:46 +02:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
class BackupUpdate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2022-11-06 22:20:14 +01:00
protected $signature = 'ninja:backup-files {--disk=}';
2021-10-20 05:05:46 +02:00
/**
* The console command description.
*
* @var string
*/
2022-11-06 22:20:14 +01:00
protected $description = 'Shift files between object storage locations';
2021-10-20 05:05:46 +02:00
/**
* 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
2023-10-26 04:57:44 +02:00
if(Ninja::isSelfHost()) {
2023-04-22 07:25:54 +02:00
return;
2023-10-26 04:57:44 +02:00
}
2023-04-22 07:25:54 +02:00
2021-10-20 05:05:46 +02:00
$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);
2021-10-20 05:05:46 +02:00
}
}
private function handleOnDb()
{
2022-02-19 06:11:30 +01:00
set_time_limit(0);
2022-03-05 04:11:16 +01:00
2022-11-06 22:20:14 +01:00
//logos
2022-11-11 04:52:50 +01:00
Company::cursor()
2023-02-16 02:36:09 +01:00
->each(function ($company) {
2023-03-05 03:52:31 +01:00
$company_logo_path = $company->settings->company_logo;
2022-11-11 04:52:50 +01:00
2023-11-24 00:23:40 +01:00
if ($company_logo_path == config('ninja.app_logo') || $company_logo_path == '') {
2023-02-16 02:36:09 +01:00
return;
}
2022-11-11 04:52:50 +01:00
2023-03-05 03:52:31 +01:00
$logo = @file_get_contents($company_logo_path);
$extension = @pathinfo($company->settings->company_logo, PATHINFO_EXTENSION);
if ($logo && $extension) {
$path = "{$company->company_key}/{$company->company_key}.{$extension}";
2022-11-06 22:20:14 +01:00
2023-02-16 02:36:09 +01:00
Storage::disk($this->option('disk'))->put($path, $logo);
2023-03-05 03:52:31 +01:00
$url = Storage::disk($this->option('disk'))->url($path);
nlog("Company - Moving {$company_logo_path} logo to {$this->option('disk')} final URL = {$url}}");
$settings = $company->settings;
$settings->company_logo = $url;
$company->settings = $settings;
;
$company->save();
2023-02-16 02:36:09 +01:00
}
2022-11-06 22:20:14 +01:00
});
2023-03-05 03:52:31 +01:00
Client::withTrashed()
->whereNotNull('settings->company_logo')
->cursor()
->each(function ($client) {
$company_logo_path = $client->settings->company_logo;
$logo = @file_get_contents($company_logo_path);
$extension = @pathinfo($company_logo_path, PATHINFO_EXTENSION);
if ($logo && $extension) {
$path = "{$client->company->company_key}/{$client->client_hash}.{$extension}";
Storage::disk($this->option('disk'))->put($path, $logo);
$url = Storage::disk($this->option('disk'))->url($path);
nlog("Client - Moving {$company_logo_path} logo to {$this->option('disk')} final URL = {$url}}");
$settings = $client->settings;
$settings->company_logo = $url;
$client->settings = $settings;
;
$client->saveQuietly();
}
});
GroupSetting::withTrashed()
->whereNotNull('settings->company_logo')
->orWhere('settings->company_logo', '!=', '')
->cursor()
->each(function ($group) {
$company_logo_path = $group->settings->company_logo;
if (!$company_logo_path) {
return;
}
$logo = @file_get_contents($company_logo_path);
$extension = @pathinfo($company_logo_path, PATHINFO_EXTENSION);
if ($logo && $extension) {
$path = "{$group->company->company_key}/{$group->hashed_id}.{$extension}";
Storage::disk($this->option('disk'))->put($path, $logo);
$url = Storage::disk($this->option('disk'))->url($path);
nlog("Group - Moving {$company_logo_path} logo to {$this->option('disk')} final URL = {$url}}");
$settings = $group->settings;
$settings->company_logo = $url;
$group->settings = $settings;
;
$group->saveQuietly();
}
});
2022-11-06 22:20:14 +01:00
//documents
2022-11-11 04:52:50 +01:00
Document::cursor()
2023-03-05 03:52:31 +01:00
->each(function (Document $document) {
$doc_bin = false;
try {
$doc_bin = $document->getFile();
} catch(\Exception $e) {
nlog($e->getMessage());
}
2022-11-11 04:52:50 +01:00
2023-02-16 02:36:09 +01:00
if ($doc_bin) {
2022-11-11 04:52:50 +01:00
Storage::disk($this->option('disk'))->put($document->url, $doc_bin);
2023-03-05 03:52:31 +01:00
$document->disk = $this->option('disk');
$document->saveQuietly();
nlog("Documents - Moving {$document->url} to {$this->option('disk')}");
2023-02-16 02:36:09 +01:00
}
2022-11-11 04:52:50 +01:00
});
2022-11-06 22:20:14 +01:00
2023-02-16 02:36:09 +01:00
//backups
2023-03-05 03:52:31 +01:00
Backup::whereNotNull('filename')
->where('filename', '!=', '')
->cursor()
2023-02-16 02:36:09 +01:00
->each(function ($backup) {
$backup_bin = Storage::disk('s3')->get($backup->filename);
2022-11-11 04:52:50 +01:00
2023-02-16 02:36:09 +01:00
if ($backup_bin) {
2022-11-11 04:52:50 +01:00
Storage::disk($this->option('disk'))->put($backup->filename, $backup_bin);
2023-03-05 03:52:31 +01:00
nlog("Backups - Moving {$backup->filename} to {$this->option('disk')}");
2023-02-16 02:36:09 +01:00
}
2022-11-11 04:52:50 +01:00
});
2021-10-20 05:05:46 +02:00
}
}