From 1d6945368524c8a1e13de159fcce876cdb6a57c1 Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Thu, 5 Feb 2026 09:58:20 +0000 Subject: [PATCH] tweaks --- .../Controllers/BulkEventUploadController.php | 69 ++++++++++++++----- config/filesystems.php | 3 + .../views/admin/bulk-upload/index.blade.php | 56 ++++++++++++--- 3 files changed, 103 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/BulkEventUploadController.php b/app/Http/Controllers/BulkEventUploadController.php index aa673010a..bd5a9fe45 100644 --- a/app/Http/Controllers/BulkEventUploadController.php +++ b/app/Http/Controllers/BulkEventUploadController.php @@ -8,6 +8,7 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Storage; use Illuminate\View\View; use Maatwebsite\Excel\Facades\Excel; @@ -26,11 +27,23 @@ public function index(Request $request): View { $validationMissing = $request->session()->get('validation_missing') ?? $request->session()->get(self::SESSION_VALIDATION_MISSING, []); + $validationPassed = $request->session()->get(self::SESSION_VALIDATION_PASSED, false); + $filePath = $request->session()->get(self::SESSION_FILE_PATH); + $tempDisk = config('filesystems.bulk_upload_temp_disk', 'local'); + $importPayload = ''; + if ($validationPassed && $filePath) { + $importPayload = Crypt::encryptString(json_encode([ + 'path' => $filePath, + 'default_creator_email' => $request->session()->get(self::SESSION_DEFAULT_CREATOR), + 'disk' => $tempDisk, + ])); + } return view('admin.bulk-upload.index', [ - 'validationPassed' => $request->session()->get(self::SESSION_VALIDATION_PASSED, false), + 'validationPassed' => $validationPassed, 'validationMissing' => $validationMissing, 'storedDefaultCreatorEmail' => $request->session()->get(self::SESSION_DEFAULT_CREATOR), + 'import_payload' => $importPayload, ]); } @@ -81,25 +94,25 @@ function ($attribute, $value, $fail) { ->withInput(); } - // Remove any previously stored file + $tempDisk = config('filesystems.bulk_upload_temp_disk', 'local'); $oldPath = $request->session()->get(self::SESSION_FILE_PATH); - if ($oldPath && Storage::exists($oldPath)) { - Storage::delete($oldPath); + if ($oldPath && Storage::disk($tempDisk)->exists($oldPath)) { + Storage::disk($tempDisk)->delete($oldPath); } $request->session()->forget([self::SESSION_FILE_PATH, self::SESSION_VALIDATION_PASSED, self::SESSION_VALIDATION_MISSING, self::SESSION_DEFAULT_CREATOR]); - $path = $file->storeAs('temp', 'bulk_events_'.time().'.'.$extension); + $path = $file->storeAs('temp', 'bulk_events_'.time().'.'.$extension, $tempDisk); - $headerCheck = BulkEventUploadValidator::validateRequiredColumns($path, 'local'); + $headerCheck = BulkEventUploadValidator::validateRequiredColumns($path, $tempDisk); if (isset($headerCheck['error'])) { - Storage::delete($path); + Storage::disk($tempDisk)->delete($path); return redirect()->route('admin.bulk-upload.index') ->withErrors(['file' => 'Could not read file: '.$headerCheck['error']]) ->withInput(); } if (! $headerCheck['valid']) { - Storage::delete($path); + Storage::disk($tempDisk)->delete($path); return redirect()->route('admin.bulk-upload.index') ->with('validation_missing', $headerCheck['missing']) @@ -117,26 +130,48 @@ function ($attribute, $value, $fail) { } /** - * Run the import using the file stored in session (after validate). + * Run the import using file path from encrypted form payload or session. + * Payload avoids "No validated file found" when session is not shared (e.g. load balancer). */ public function import(Request $request): View|RedirectResponse { - $path = $request->session()->get(self::SESSION_FILE_PATH); - if (! $path || ! Storage::exists($path)) { + $path = null; + $defaultCreatorEmail = null; + $tempDisk = config('filesystems.bulk_upload_temp_disk', 'local'); + + $payload = $request->input('import_payload'); + if (is_string($payload) && $payload !== '') { + try { + $decoded = json_decode(Crypt::decryptString($payload), true); + if (is_array($decoded) && ! empty($decoded['path'])) { + $path = $decoded['path']; + $defaultCreatorEmail = $decoded['default_creator_email'] ?? null; + if (! empty($decoded['disk'])) { + $tempDisk = $decoded['disk']; + } + } + } catch (\Throwable $e) { + // Fall back to session + } + } + if (! $path) { + $path = $request->session()->get(self::SESSION_FILE_PATH); + $defaultCreatorEmail = $request->session()->get(self::SESSION_DEFAULT_CREATOR); + } + + if (! $path || ! Storage::disk($tempDisk)->exists($path)) { $request->session()->forget([self::SESSION_FILE_PATH, self::SESSION_DEFAULT_CREATOR, self::SESSION_VALIDATION_PASSED, self::SESSION_VALIDATION_MISSING]); return redirect()->route('admin.bulk-upload.index') ->withErrors(['import' => 'No validated file found. Please upload and validate a file first.']); } - $defaultCreatorEmail = $request->session()->get(self::SESSION_DEFAULT_CREATOR); - try { $result = new BulkEventImportResult; $import = new GenericEventsImport($defaultCreatorEmail, $result); - Excel::import($import, $path, 'local'); + Excel::import($import, $path, $tempDisk); - Storage::delete($path); + Storage::disk($tempDisk)->delete($path); $request->session()->forget([self::SESSION_FILE_PATH, self::SESSION_DEFAULT_CREATOR, self::SESSION_VALIDATION_PASSED, self::SESSION_VALIDATION_MISSING]); $this->clearMapCache(); @@ -146,8 +181,8 @@ public function import(Request $request): View|RedirectResponse return redirect()->route('admin.bulk-upload.report'); } catch (\Throwable $e) { - if (Storage::exists($path)) { - Storage::delete($path); + if (Storage::disk($tempDisk)->exists($path)) { + Storage::disk($tempDisk)->delete($path); } $request->session()->forget([self::SESSION_FILE_PATH, self::SESSION_DEFAULT_CREATOR, self::SESSION_VALIDATION_PASSED, self::SESSION_VALIDATION_MISSING]); diff --git a/config/filesystems.php b/config/filesystems.php index 1e6c8f8d5..756e57d00 100644 --- a/config/filesystems.php +++ b/config/filesystems.php @@ -4,6 +4,9 @@ 'cloud' => env('FILESYSTEM_CLOUD', 's3'), + 'resources_import_temp_disk' => env('RESOURCES_IMPORT_TEMP_DISK', 'local'), + 'bulk_upload_temp_disk' => env('BULK_UPLOAD_TEMP_DISK', 'local'), + 'disks' => [ 'latex' => [ 'driver' => 'local', diff --git a/resources/views/admin/bulk-upload/index.blade.php b/resources/views/admin/bulk-upload/index.blade.php index 483444fb2..6c0f1bcd2 100644 --- a/resources/views/admin/bulk-upload/index.blade.php +++ b/resources/views/admin/bulk-upload/index.blade.php @@ -35,7 +35,7 @@ {{-- Step 1: Upload & validate --}}

Step 1: Upload & validate

-
+ @csrf
@@ -49,19 +49,52 @@ class="w-full max-w-md px-3 py-2 border rounded"
- - -

Select a file above (required), then click the button below. Max 10 MB. Required columns: activity_title, name_of_organisation, type_of_organisation, activity_type, description, address, country, start_date, end_date, longitude, latitude, contact_email, organiser_website, participants_count, males_count, females_count, other_count.

+
+ + + No file chosen + +
+

Max 10 MB. Required columns: activity_title, name_of_organisation, type_of_organisation, activity_type, description, address, country, start_date, end_date, longitude, latitude, contact_email, organiser_website, participants_count, males_count, females_count, other_count.

- +
+ {{-- Validation result: missing columns --}} @@ -83,10 +116,17 @@ class="block w-full max-w-md">

Step 2: Import

All required columns are present. Click the button below to run the import.

-
+ @csrf - + +
+
@endif