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); } }