Kaynağa Gözat

prima nota s3

FabioFratini 8 ay önce
ebeveyn
işleme
cf54edff17
1 değiştirilmiş dosya ile 81 ekleme ve 7 silme
  1. 81 7
      app/Http/Livewire/Record.php

+ 81 - 7
app/Http/Livewire/Record.php

@@ -9,6 +9,8 @@ use DateTime;
 
 use PhpOffice\PhpSpreadsheet\Spreadsheet;
 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+use Illuminate\Support\Facades\Storage;
+use Illuminate\Support\Facades\Log;
 
 
 class Record extends Component
@@ -260,7 +262,6 @@ class Record extends Component
     public function export()
     {
         ini_set('memory_limit', '512M');
-
         gc_enable();
 
         $letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
@@ -453,13 +454,86 @@ class Record extends Component
             $activeWorksheet->getColumnDimension($l)->setWidth(20);
         }
 
-        $writer = new Xlsx($spreadsheet);
-        $path = storage_path('prima_nota_' . date("YmdHis") . '.xlsx');
-        $writer->save($path);
+        $filename = 'prima_nota_' . date("YmdHis") . '.xlsx';
+
+        try {
+            $currentClient = session('currentClient', 'default');
+
+            $tempPath = sys_get_temp_dir() . '/' . $filename;
+
+            $writer = new Xlsx($spreadsheet);
+            $writer->save($tempPath);
+
+            unset($spreadsheet, $activeWorksheet, $writer);
+            gc_collect_cycles();
+
+            $disk = Storage::disk('s3');
+
+            $s3Path = $currentClient . '/prima_nota/' . $filename;
+
+            $primaNotaFolderPath = $currentClient . '/prima_nota/.gitkeep';
+            if (!$disk->exists($primaNotaFolderPath)) {
+                $disk->put($primaNotaFolderPath, '');
+                Log::info("Created prima_nota folder for client: {$currentClient}");
+            }
+
+            $fileContent = file_get_contents($tempPath);
+            $uploaded = $disk->put($s3Path, $fileContent, 'private');
+
+            if (!$uploaded) {
+                throw new \Exception('Failed to upload file to Wasabi S3');
+            }
+
+            Log::info("Prima Nota exported to Wasabi", [
+                'client' => $currentClient,
+                'path' => $s3Path,
+                'size' => filesize($tempPath),
+                'records_processed' => $recordsProcessed
+            ]);
+
+            if (file_exists($tempPath)) {
+                unlink($tempPath);
+            }
 
-        unset($spreadsheet, $activeWorksheet, $writer);
-        gc_collect_cycles();
+            $downloadUrl = $disk->temporaryUrl($s3Path, now()->addHour());
+            return redirect($downloadUrl);
+        } catch (\Exception $e) {
+            Log::error('Error exporting Prima Nota to Wasabi S3', [
+                'error' => $e->getMessage(),
+                'client' => session('currentClient', 'unknown'),
+                'filename' => $filename,
+                'records_processed' => $recordsProcessed ?? 0
+            ]);
+
+            $currentClient = session('currentClient', 'default');
+            $clientFolder = storage_path('app/prima_nota/' . $currentClient);
+
+            if (!is_dir($clientFolder)) {
+                mkdir($clientFolder, 0755, true);
+                Log::info("Created local client prima_nota folder: {$clientFolder}");
+            }
 
-        return response()->download($path)->deleteFileAfterSend();
+            $localPath = $clientFolder . '/' . $filename;
+
+            if (isset($tempPath) && file_exists($tempPath)) {
+                rename($tempPath, $localPath);
+            } else {
+                $writer = new Xlsx($spreadsheet);
+                $writer->save($localPath);
+                unset($spreadsheet, $activeWorksheet, $writer);
+            }
+
+            gc_collect_cycles();
+
+            Log::warning("Prima Nota saved locally due to S3 error", [
+                'client' => $currentClient,
+                'local_path' => $localPath,
+                'error' => $e->getMessage()
+            ]);
+
+            session()->flash('warning', 'File salvato localmente a causa di un errore del cloud storage.');
+
+            return response()->download($localPath)->deleteFileAfterSend();
+        }
     }
 }