88use App \Services \ResourcesUploadValidator ;
99use Illuminate \Http \RedirectResponse ;
1010use Illuminate \Http \Request ;
11+ use Illuminate \Support \Facades \Cache ;
1112use Illuminate \Support \Facades \Crypt ;
1213use Illuminate \Support \Facades \Storage ;
14+ use Illuminate \Support \Str ;
1315use Illuminate \View \View ;
1416use Maatwebsite \Excel \Facades \Excel ;
1517
@@ -111,9 +113,18 @@ function ($attribute, $value, $fail) {
111113 ->withInput ();
112114 }
113115
116+ $ focus = (bool ) $ request ->boolean ('focus ' );
114117 $ request ->session ()->put (self ::SESSION_FILE_PATH , $ path );
115118 $ request ->session ()->put (self ::SESSION_ROWS , $ rows );
116- $ request ->session ()->put (self ::SESSION_FOCUS , (bool ) $ request ->boolean ('focus ' ));
119+ $ request ->session ()->put (self ::SESSION_FOCUS , $ focus );
120+
121+ $ importToken = Str::random (64 );
122+ Cache::put ('resources_import_ ' . $ importToken , [
123+ 'path ' => $ path ,
124+ 'disk ' => $ tempDisk ,
125+ 'focus ' => $ focus ,
126+ ], now ()->addHours (1 ));
127+ $ request ->session ()->put ('resources_import_token ' , $ importToken );
117128
118129 return redirect ()->route ('admin.resources-import.preview ' );
119130 }
@@ -127,23 +138,28 @@ public function preview(Request $request): View|RedirectResponse
127138 $ focus = $ request ->session ()->get (self ::SESSION_FOCUS , false );
128139
129140 if (! is_array ($ rows ) || empty ($ rows )) {
130- $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS ]);
141+ $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS , ' resources_import_token ' ]);
131142
132143 return redirect ()->route ('admin.resources-import.index ' )
133144 ->with ('info ' , 'No preview data found. Please upload and verify a file first. ' );
134145 }
135146
147+ $ importToken = $ request ->session ()->get ('resources_import_token ' );
136148 $ path = $ request ->session ()->get (self ::SESSION_FILE_PATH );
137149 $ tempDisk = config ('filesystems.resources_import_temp_disk ' , 'local ' );
138- $ importPayload = $ path ? Crypt::encryptString (json_encode ([
139- 'path ' => $ path ,
140- 'focus ' => $ focus ,
141- 'disk ' => $ tempDisk ,
142- ])) : '' ;
150+ $ importPayload = '' ;
151+ if (empty ($ importToken ) && $ path ) {
152+ $ importPayload = Crypt::encryptString (json_encode ([
153+ 'path ' => $ path ,
154+ 'focus ' => $ focus ,
155+ 'disk ' => $ tempDisk ,
156+ ]));
157+ }
143158
144159 return view ('admin.resources-import.preview ' , [
145160 'rows ' => $ rows ,
146161 'focus ' => $ focus ,
162+ 'import_token ' => $ importToken ?? '' ,
147163 'import_payload ' => $ importPayload ,
148164 ]);
149165 }
@@ -158,32 +174,54 @@ public function import(Request $request): RedirectResponse
158174 $ focus = false ;
159175 $ tempDisk = config ('filesystems.resources_import_temp_disk ' , 'local ' );
160176
161- $ payload = $ request ->input ('import_payload ' );
162- if (is_string ($ payload ) && $ payload !== '' ) {
163- try {
164- $ decoded = json_decode (Crypt::decryptString ($ payload ), true );
165- if (is_array ($ decoded ) && ! empty ($ decoded ['path ' ])) {
166- $ path = $ decoded ['path ' ];
167- $ focus = (bool ) ($ decoded ['focus ' ] ?? false );
168- if (! empty ($ decoded ['disk ' ])) {
169- $ tempDisk = $ decoded ['disk ' ];
177+ $ token = $ request ->input ('import_token ' );
178+ if (is_string ($ token ) && $ token !== '' ) {
179+ $ cached = Cache::get ('resources_import_ ' . $ token );
180+ if (is_array ($ cached ) && ! empty ($ cached ['path ' ])) {
181+ $ path = $ cached ['path ' ];
182+ $ tempDisk = $ cached ['disk ' ] ?? $ tempDisk ;
183+ $ focus = (bool ) ($ cached ['focus ' ] ?? false );
184+ }
185+ }
186+
187+ if (! $ path ) {
188+ $ payload = $ request ->input ('import_payload ' );
189+ if (empty ($ payload ) || ! is_string ($ payload )) {
190+ $ payload = $ request ->session ()->get ('resources_import_payload ' );
191+ }
192+ if (is_string ($ payload ) && $ payload !== '' ) {
193+ try {
194+ $ decoded = json_decode (Crypt::decryptString ($ payload ), true );
195+ if (is_array ($ decoded ) && ! empty ($ decoded ['path ' ])) {
196+ $ path = $ decoded ['path ' ];
197+ $ focus = (bool ) ($ decoded ['focus ' ] ?? false );
198+ if (! empty ($ decoded ['disk ' ])) {
199+ $ tempDisk = $ decoded ['disk ' ];
200+ }
170201 }
202+ } catch (\Throwable $ e ) {
203+ // Fall back to session
171204 }
172- } catch (\Throwable $ e ) {
173- // Invalid or expired payload, fall back to session
174205 }
175206 }
176-
177207 if (! $ path ) {
178208 $ path = $ request ->session ()->get (self ::SESSION_FILE_PATH );
179209 $ focus = $ request ->session ()->get (self ::SESSION_FOCUS , false );
180210 }
181211
182212 if (! $ path || ! Storage::disk ($ tempDisk )->exists ($ path )) {
183- $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS ]);
213+ $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS , 'resources_import_token ' ]);
214+ if (is_string ($ token ?? null ) && $ token !== '' ) {
215+ Cache::forget ('resources_import_ ' . $ token );
216+ }
217+
218+ $ message = 'No verified file found. Please upload and verify a file first. ' ;
219+ if ($ path && ! Storage::disk ($ tempDisk )->exists ($ path )) {
220+ $ message .= ' If you use multiple servers, set RESOURCES_IMPORT_TEMP_DISK=s3 in .env so the file is stored in shared storage. ' ;
221+ }
184222
185223 return redirect ()->route ('admin.resources-import.index ' )
186- ->withErrors (['import ' => ' No verified file found. Please upload and verify a file first. ' ]);
224+ ->withErrors (['import ' => $ message ]);
187225 }
188226 $ edits = $ request ->input ('edits ' , []);
189227 if (! is_array ($ edits )) {
@@ -222,7 +260,10 @@ public function import(Request $request): RedirectResponse
222260 Excel::import ($ import , $ path , $ tempDisk );
223261
224262 Storage::disk ($ tempDisk )->delete ($ path );
225- $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS ]);
263+ $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS , 'resources_import_token ' ]);
264+ if (is_string ($ token = $ request ->input ('import_token ' )) && $ token !== '' ) {
265+ Cache::forget ('resources_import_ ' . $ token );
266+ }
226267
227268 $ request ->session ()->flash ('resources_import_report_created ' , $ result ->created );
228269 $ request ->session ()->flash ('resources_import_report_updated ' , $ result ->updated );
@@ -233,7 +274,10 @@ public function import(Request $request): RedirectResponse
233274 if (Storage::disk ($ tempDisk )->exists ($ path )) {
234275 Storage::disk ($ tempDisk )->delete ($ path );
235276 }
236- $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS ]);
277+ $ request ->session ()->forget ([self ::SESSION_FILE_PATH , self ::SESSION_ROWS , self ::SESSION_FOCUS , 'resources_import_token ' ]);
278+ if (is_string ($ token = $ request ->input ('import_token ' )) && $ token !== '' ) {
279+ Cache::forget ('resources_import_ ' . $ token );
280+ }
237281
238282 return redirect ()->route ('admin.resources-import.preview ' )
239283 ->withErrors (['import ' => 'Import failed: ' .$ e ->getMessage ()]);
0 commit comments