1414namespace Sylius \ImportExport \Controller ;
1515
1616use Sylius \ImportExport \Messenger \Command \CreateImportProcess ;
17+ use Sylius \ImportExport \Uploader \ImportFileUploader ;
1718use Sylius \Resource \Metadata \RegistryInterface ;
1819use Symfony \Component \Form \FormFactoryInterface ;
1920use Symfony \Component \HttpFoundation \File \UploadedFile ;
@@ -29,7 +30,7 @@ public function __construct(
2930 private FormFactoryInterface $ formFactory ,
3031 private MessageBusInterface $ commandBus ,
3132 private string $ importForm ,
32- private string $ importFilesDirectory ,
33+ private ImportFileUploader $ importFileUploader ,
3334 private RegistryInterface $ metadataRegistry ,
3435 ) {
3536 }
@@ -59,23 +60,14 @@ public function __invoke(Request $request, string $grid): Response
5960
6061 $ data = $ form ->getData ();
6162 $ resourceClass = $ data ['resourceClass ' ];
62-
63+
6364 /** @var UploadedFile $file */
6465 $ file = $ data ['file ' ];
6566
6667 try {
67- if (!is_dir ($ this ->importFilesDirectory )) {
68- mkdir ($ this ->importFilesDirectory , 0755 , true );
69- }
70-
71- $ fileName = uniqid () . '_ ' . $ file ->getClientOriginalName ();
72- $ filePath = $ this ->importFilesDirectory . '/ ' . $ fileName ;
73-
74- $ file ->move ($ this ->importFilesDirectory , $ fileName );
75-
76- $ format = $ this ->getFormatFromMimeType ($ file ->getMimeType ());
68+ $ format = $ this ->importFileUploader ->getFormatFromMimeType ($ file ->getMimeType ());
69+ $ filePath = $ this ->importFileUploader ->upload ($ file );
7770
78- // Convert FQCN to resource alias (like sylius.product)
7971 $ metadata = $ this ->metadataRegistry ->getByClass ($ resourceClass );
8072 $ resourceAlias = $ metadata ->getAlias ();
8173
@@ -87,19 +79,10 @@ public function __invoke(Request $request, string $grid): Response
8779 ));
8880
8981 $ session ->getFlashBag ()->add ('success ' , 'sylius_import_export.import_started ' );
90- } catch (\Throwable ) {
91- $ session ->getFlashBag ()->add ('error ' , 'sylius_import_export.import_failed ' );
82+ } catch (\Throwable $ e ) {
83+ $ session ->getFlashBag ()->add ('error ' , 'sylius_import_export.upload_failed ' );
9284 }
9385
9486 return new RedirectResponse ($ request ->headers ->get ('referer ' ) ?? '/ ' );
9587 }
96-
97- private function getFormatFromMimeType (?string $ mimeType ): string
98- {
99- return match ($ mimeType ) {
100- 'application/json ' => 'json ' ,
101- 'text/csv ' , 'text/plain ' => 'csv ' ,
102- default => 'json ' , // Default fallback
103- };
104- }
10588}
0 commit comments