2024-05-10 12:05:36 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Jobs\Document;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Document;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
|
|
|
class CopyDocs implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 1;
|
|
|
|
|
|
|
|
/**
|
2024-06-14 09:09:44 +02:00
|
|
|
*
|
2024-05-10 12:05:36 +02:00
|
|
|
*/
|
|
|
|
public function __construct(private \Illuminate\Support\Collection $document_ids, private $entity, private string $db)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
MultiDB::setDb($this->db);
|
|
|
|
|
2024-07-14 13:28:54 +02:00
|
|
|
Document::query()
|
|
|
|
->whereIn('id', $this->document_ids)
|
2024-05-10 12:05:36 +02:00
|
|
|
->where('company_id', $this->entity->company_id)
|
2024-06-14 09:09:44 +02:00
|
|
|
->each(function ($document) {
|
2024-05-10 12:05:36 +02:00
|
|
|
|
2024-07-14 13:28:54 +02:00
|
|
|
/** @var \App\Models\Document $document */
|
2024-05-10 12:05:36 +02:00
|
|
|
$file = $document->getFile();
|
2024-06-14 09:09:44 +02:00
|
|
|
|
|
|
|
$extension = pathinfo($document->name, PATHINFO_EXTENSION);
|
2024-05-10 12:05:36 +02:00
|
|
|
|
|
|
|
$new_hash = \Illuminate\Support\Str::random(32) . "." . $extension;
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-05-10 12:05:36 +02:00
|
|
|
Storage::disk($document->disk)->put(
|
|
|
|
"{$this->entity->company->company_key}/documents/{$new_hash}",
|
|
|
|
$file,
|
|
|
|
);
|
|
|
|
|
|
|
|
$instance = Storage::disk($document->disk)->path("{$this->entity->company->company_key}/documents/{$new_hash}");
|
|
|
|
|
|
|
|
$new_doc = new Document();
|
|
|
|
$new_doc->user_id = $this->entity->user_id;
|
|
|
|
$new_doc->company_id = $this->entity->company_id;
|
|
|
|
$new_doc->url = $instance;
|
|
|
|
$new_doc->name = $document->name;
|
|
|
|
$new_doc->type = $extension;
|
|
|
|
$new_doc->disk = $document->disk;
|
|
|
|
$new_doc->hash = $new_hash;
|
|
|
|
$new_doc->size = $document->size;
|
|
|
|
$new_doc->width = $document->width;
|
|
|
|
$new_doc->height = $document->height;
|
|
|
|
$new_doc->is_public = $document->is_public;
|
2024-06-14 09:09:44 +02:00
|
|
|
|
2024-05-10 12:05:36 +02:00
|
|
|
$this->entity->documents()->save($new_doc);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|