55namespace Shopsys \FrameworkBundle \Component \FileUpload ;
66
77use InvalidArgumentException ;
8+ use League \Flysystem \Config ;
89use League \Flysystem \FilesystemException ;
910use League \Flysystem \FilesystemOperator ;
1011use League \Flysystem \MountManager ;
1112use League \Flysystem \StorageAttributes ;
13+ use League \Flysystem \Visibility ;
1214use Shopsys \FrameworkBundle \Component \Cache \InMemoryCache ;
1315use Shopsys \FrameworkBundle \Component \CustomerUploadedFile \CustomerUploadedFile ;
1416use Shopsys \FrameworkBundle \Component \CustomerUploadedFile \CustomerUploadedFileRepository ;
1517use Shopsys \FrameworkBundle \Component \Doctrine \Exception \UnexpectedTypeException ;
16- use Shopsys \FrameworkBundle \Component \FileUpload \Exception \MissingFileClassDirectoryMappingException ;
18+ use Shopsys \FrameworkBundle \Component \FileUpload \Exception \MissingFileConfigByFileClassException ;
1719use Shopsys \FrameworkBundle \Component \FileUpload \Exception \MoveToEntityFailedException ;
1820use Shopsys \FrameworkBundle \Component \FileUpload \Exception \UploadFailedException ;
1921use Shopsys \FrameworkBundle \Component \Image \Image ;
@@ -30,9 +32,15 @@ class FileUpload
3032 protected const int DELETE_OLD_FILES_SECONDS = 86400 ;
3133 protected const string POSITION_BY_ENTITY_AND_TYPE_CACHE_NAMESPACE = 'positionByEntityAndType ' ;
3234
35+ /**
36+ * @param array<class-string<\Shopsys\FrameworkBundle\Component\FileUpload\EntityFileUploadInterface>, array{
37+ * directory: non-empty-string,
38+ * visibility: 'public'|'private',
39+ * }> $filesConfigByFileClass
40+ */
3341 public function __construct (
3442 protected readonly string $ temporaryDir ,
35- protected array $ directoriesByFileClass ,
43+ protected array $ filesConfigByFileClass ,
3644 protected readonly FileNamingConvention $ fileNamingConvention ,
3745 protected readonly MountManager $ mountManager ,
3846 protected readonly FilesystemOperator $ filesystem ,
@@ -54,6 +62,9 @@ public function upload(UploadedFile $file): string
5462 $ this ->mountManager ->move (
5563 'local:// ' . $ file ->getRealPath (),
5664 'main:// ' . $ this ->getTemporaryDirectory () . '/ ' . $ temporaryFilename ,
65+ [
66+ Config::OPTION_VISIBILITY => Visibility::PRIVATE ,
67+ ],
5768 );
5869
5970 return $ temporaryFilename ;
@@ -174,7 +185,13 @@ public function postFlushEntity(EntityFileUploadInterface $entity): void
174185 $ this ->filesystem ->delete ($ targetFilename );
175186 }
176187
177- $ this ->mountManager ->move ('main:// ' . $ sourceFilepath , 'main:// ' . $ targetFilename );
188+ $ this ->mountManager ->move (
189+ 'main:// ' . $ sourceFilepath ,
190+ 'main:// ' . $ targetFilename ,
191+ [
192+ Config::OPTION_VISIBILITY => $ this ->getVisibilityByFileClass ($ fileForUpload ->getFileClass ()),
193+ ],
194+ );
178195 $ entity ->setFileKeyAsUploaded ($ key );
179196 } catch (IOException $ ex ) {
180197 $ message = 'Failed to rename file from temporary directory to entity ' ;
@@ -266,20 +283,36 @@ protected function getUploadEntityType(EntityFileUploadInterface $entity): strin
266283 return $ uploadEntityType ;
267284 }
268285
269- protected function getDirectoryByFileClass (string $ fileClass ): string
286+ /**
287+ * @return array{
288+ * directory: non-empty-string,
289+ * visibility: 'public'|'private',
290+ * }
291+ */
292+ protected function getFileConfigByFileClass (string $ fileClass ): array
270293 {
271- if (array_key_exists ($ fileClass , $ this ->directoriesByFileClass )) {
272- return $ this ->directoriesByFileClass [$ fileClass ];
294+ if (array_key_exists ($ fileClass , $ this ->filesConfigByFileClass )) {
295+ return $ this ->filesConfigByFileClass [$ fileClass ];
273296 }
274297
275- foreach ($ this ->directoriesByFileClass as $ class => $ dir ) {
298+ foreach ($ this ->filesConfigByFileClass as $ class => $ config ) {
276299 if (is_subclass_of ($ fileClass , $ class )) {
277- return $ dir ;
300+ return $ config ;
278301 }
279302 }
280303
281- throw new MissingFileClassDirectoryMappingException (
282- sprintf ('Missing directory mapping for file class "%s" ' , $ fileClass ),
304+ throw new MissingFileConfigByFileClassException (
305+ sprintf ('Missing file config mapping for file class "%s" ' , $ fileClass ),
283306 );
284307 }
308+
309+ protected function getDirectoryByFileClass (string $ fileClass ): string
310+ {
311+ return $ this ->getFileConfigByFileClass ($ fileClass )['directory ' ];
312+ }
313+
314+ protected function getVisibilityByFileClass (string $ fileClass ): string
315+ {
316+ return $ this ->getFileConfigByFileClass ($ fileClass )['visibility ' ];
317+ }
285318}
0 commit comments