Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
159 righe
4.7 KiB
159 righe
4.7 KiB
<?php |
|
|
|
namespace App\Jobs\Preservation; |
|
|
|
use Exception; |
|
use ZipArchive; |
|
use Illuminate\Support\Str; |
|
use Illuminate\Bus\Queueable; |
|
use App\Classes\PreservationClass; |
|
use Illuminate\Support\Facades\DB; |
|
use Illuminate\Support\Facades\Log; |
|
use Illuminate\Contracts\Cache\Store; |
|
use Illuminate\Queue\SerializesModels; |
|
use Illuminate\Support\Facades\Storage; |
|
use Illuminate\Queue\InteractsWithQueue; |
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
use App\Interfaces\PreservationClassInterface; |
|
use Illuminate\Contracts\Queue\ShouldBeUnique; |
|
use App\Models\Tenant\Conservazione\Conservazione; |
|
|
|
$root = dirname(base_path()); |
|
include_once "{$root}/config.php"; |
|
class MakeZipArchiveJob implements ShouldQueue |
|
{ |
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
|
|
|
/** |
|
* The number of times the job may be attempted. |
|
* |
|
* @var int |
|
*/ |
|
public $tries = 5; |
|
|
|
/** |
|
* The maximum number of unhandled exceptions to allow before failing. |
|
* |
|
* @var int |
|
*/ |
|
public $maxExceptions = 5; |
|
|
|
/** |
|
* The number of seconds the job can run before timing out. |
|
* |
|
* @var int |
|
*/ |
|
public $timeout = 1800; |
|
|
|
/** |
|
* Indicate if the job should be marked as failed on timeout. |
|
* |
|
* @var bool |
|
*/ |
|
public $failOnTimeout = true; |
|
|
|
/** |
|
* Tenant's id |
|
* |
|
* @var Int |
|
*/ |
|
protected $tenant; |
|
|
|
/** |
|
* Package's id |
|
* |
|
* @var Int |
|
*/ |
|
protected $package; |
|
|
|
/** |
|
* Create a new job instance. |
|
* |
|
* @return void |
|
*/ |
|
public function __construct(Int $tenant, Int $package) |
|
{ |
|
$this->tenant = $tenant; |
|
$this->package = $package; |
|
} |
|
|
|
/** |
|
* Execute the job. |
|
* |
|
* @return void |
|
*/ |
|
public function handle() |
|
{ |
|
global $config; |
|
|
|
$limit = ini_get('memory_limit'); |
|
ini_set('memory_limit', 1024 * 5 . "M"); |
|
|
|
setup_db_connection($this->tenant); |
|
|
|
$preservation = new PreservationClass($this->tenant, $this->package); |
|
|
|
$path = "archivi-conservazione/{$this->tenant}/{$this->package}"; |
|
|
|
$zip = new ZipArchive(); |
|
$zip->open(storage_path("app/{$path}/ZIPARCH-{$this->package}.zip"), ZIPARCHIVE::CREATE); |
|
|
|
if(Storage::disk("local")->exists("{$path}/ZIPARCH-{$this->package}.zip")) { Storage::disk("local")->delete("{$path}/ZIPARCH-{$this->package}.zip"); } |
|
|
|
$package = Conservazione::findOrFail($this->package); |
|
$package->archivio = "W"; |
|
$package->save(); |
|
|
|
$package->items()->chunk(1000, function($files) use ($preservation, $path, $zip) { |
|
|
|
foreach($files as $file) { |
|
|
|
try { |
|
|
|
list($item, $meta, $content) = $preservation->getItem($file->codice); |
|
if(empty($meta["nomeFile"])) { |
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
$mime = finfo_buffer($finfo, $content); |
|
finfo_close($finfo); |
|
unset($finfo); |
|
$finfo = null; |
|
gc_collect_cycles(); |
|
|
|
$meta["nomeFile"] = $meta["titolo"] . "." . array_flip(PreservationClassInterface::EXTENSIONS)[$mime]; |
|
|
|
} |
|
|
|
Storage::disk("local")->put("{$path}/tmp/{$file->codice} - {$meta["nomeFile"]}", $content); |
|
$zip->addFile(storage_path("app/{$path}/tmp/{$file->codice} - {$meta["nomeFile"]}"), "ZIPARCH-{$this->package}/" . str_replace(["b_", "r_"], "", $item->tabella_riferimento) . "/{$file->codice} - {$meta["nomeFile"]}"); |
|
|
|
} catch (Exception $exception) { |
|
|
|
Log::error($exception); |
|
Log::error("Unable to get file id {$file->codice}"); |
|
echo "Unable to get file id {$file->codice}" . PHP_EOL; |
|
echo $exception->getMessage() . " on line " . $exception->getLine() . " in " . $exception->getFile() . PHP_EOL; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
$zip->close(); |
|
if(file_exists(storage_path("app/{$path}/ZIPARCH-{$this->package}.zip")) && filesize(storage_path("app/{$path}/ZIPARCH-{$this->package}.zip")) > 1) { |
|
|
|
if(! is_dir("{$config["mediaFolder"]}/{$this->tenant}/conservazione")) { mkdir("{$config["mediaFolder"]}/{$this->tenant}/conservazione", 0755, TRUE); } |
|
if(rename(storage_path("app/{$path}/ZIPARCH-{$this->package}.zip"), "{$config["mediaFolder"]}/{$this->tenant}/conservazione/ZIPARCH-{$this->package}.zip")) { |
|
$package->archivio = "S"; |
|
$package->save(); |
|
} |
|
|
|
} |
|
|
|
Storage::disk("local")->deleteDirectory("{$path}/tmp", 1); |
|
ini_set('memory_limit', $limit); |
|
|
|
} |
|
}
|
|
|