8
8
namespace Magento \MediaStorage \Model \File \Validator ;
9
9
10
10
use Laminas \Validator \AbstractValidator ;
11
+ use Magento \Framework \App \ObjectManager ;
11
12
use Magento \Framework \File \Mime ;
12
13
use Magento \Framework \Filesystem \Driver \File ;
14
+ use Magento \Framework \Image \Adapter \ConfigInterface ;
13
15
use Magento \Framework \Image \Factory ;
16
+ use Psr \Log \LoggerInterface ;
14
17
15
18
/**
16
19
* Image validator
@@ -45,19 +48,35 @@ class Image extends AbstractValidator
45
48
*/
46
49
private $ file ;
47
50
51
+ /**
52
+ * @var ConfigInterface
53
+ */
54
+ private $ config ;
55
+
56
+ /**
57
+ * @var LoggerInterface
58
+ */
59
+ private $ logger ;
60
+
48
61
/**
49
62
* @param Mime $fileMime
50
63
* @param Factory $imageFactory
51
64
* @param File $file
65
+ * @param ConfigInterface|null $config
66
+ * @param LoggerInterface|null $logger
52
67
*/
53
68
public function __construct (
54
69
Mime $ fileMime ,
55
70
Factory $ imageFactory ,
56
- File $ file
71
+ File $ file ,
72
+ ConfigInterface $ config = null ,
73
+ LoggerInterface $ logger = null
57
74
) {
58
75
$ this ->fileMime = $ fileMime ;
59
76
$ this ->imageFactory = $ imageFactory ;
60
77
$ this ->file = $ file ;
78
+ $ this ->config = $ config ?? ObjectManager::getInstance ()->get (ConfigInterface::class);
79
+ $ this ->logger = $ logger ?? ObjectManager::getInstance ()->get (LoggerInterface::class);
61
80
62
81
parent ::__construct ();
63
82
}
@@ -71,12 +90,20 @@ public function isValid($filePath): bool
71
90
$ isValid = false ;
72
91
73
92
if (stripos (serialize ($ this ->imageMimeTypes ), $ fileMimeType ) !== false ) {
93
+ $ defaultAdapter = $ this ->config ->getAdapterAlias ();
74
94
try {
75
- $ image = $ this ->imageFactory ->create ($ filePath );
95
+ $ image = $ this ->imageFactory ->create ($ filePath , $ defaultAdapter );
96
+ $ image ->open ();
97
+ $ isValid = true ;
98
+ } catch (\InvalidArgumentException $ e ) {
99
+ $ adapters = $ this ->config ->getAdapters ();
100
+ unset($ adapters [$ defaultAdapter ]);
101
+ $ image = $ this ->imageFactory ->create ($ filePath , array_key_first ($ adapters ) ?? null );
76
102
$ image ->open ();
77
103
$ isValid = true ;
78
104
} catch (\Exception $ e ) {
79
105
$ isValid = false ;
106
+ $ this ->logger ->critical ($ e , ['exception ' => $ e ]);
80
107
}
81
108
}
82
109
0 commit comments