2019-04-21 14:24:26 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @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)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2019-04-21 14:24:26 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-10-20 05:05:46 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
2023-03-08 08:33:42 +01:00
|
|
|
/**
|
|
|
|
* App\Models\Backup
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property int $activity_id
|
|
|
|
* @property string|null $json_backup
|
|
|
|
* @property int|null $created_at
|
|
|
|
* @property int|null $updated_at
|
|
|
|
* @property string $amount
|
|
|
|
* @property string|null $filename
|
|
|
|
* @property string|null $disk
|
|
|
|
* @property-read \App\Models\Activity $activity
|
|
|
|
* @property-read mixed $hashed_id
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup newQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereActivityId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereAmount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereDisk($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereFilename($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereJsonBackup($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Backup whereUpdatedAt($value)
|
|
|
|
* @mixin \Eloquent
|
|
|
|
*/
|
2019-04-21 14:24:26 +02:00
|
|
|
class Backup extends BaseModel
|
|
|
|
{
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
public function activity()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Activity::class);
|
|
|
|
}
|
2021-10-20 05:05:46 +02:00
|
|
|
|
2022-02-19 06:11:30 +01:00
|
|
|
public function storeRemotely(?string $html, Client $client)
|
2021-10-20 05:05:46 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $html || strlen($html) == 0) {
|
2021-10-20 05:05:46 +02:00
|
|
|
return;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-10-20 05:05:46 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$path = $client->backup_path().'/';
|
|
|
|
$filename = now()->format('Y_m_d').'_'.md5(time()).'.html';
|
|
|
|
$file_path = $path.$filename;
|
2021-10-20 05:05:46 +02:00
|
|
|
|
|
|
|
Storage::disk(config('filesystems.default'))->put($file_path, $html);
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$this->filename = $file_path;
|
|
|
|
$this->save();
|
2021-10-20 05:05:46 +02:00
|
|
|
}
|
2022-05-20 01:46:21 +02:00
|
|
|
|
|
|
|
public function deleteFile()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('deleting => '.$this->filename);
|
2022-05-20 01:46:21 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$this->filename) {
|
2022-08-04 01:29:30 +02:00
|
|
|
return;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-08-04 01:29:30 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-05-20 01:46:21 +02:00
|
|
|
Storage::disk(config('filesystems.default'))->delete($this->filename);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
nlog('BACKUPEXCEPTION deleting backup file with error '.$e->getMessage());
|
2022-05-20 01:46:21 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|