@@ -23,6 +23,9 @@ class ImageStorage
2323 /** @var string */
2424 private $ data_dir ;
2525
26+ /** @var string */
27+ private $ orig_path ;
28+
2629 /** @var callable(string): string */
2730 private $ algorithm_file ;
2831
@@ -60,6 +63,7 @@ class ImageStorage
6063 public function __construct (
6164 string $ data_path ,
6265 string $ data_dir ,
66+ string $ orig_path ,
6367 callable $ algorithm_file ,
6468 callable $ algorithm_content ,
6569 int $ quality ,
@@ -70,6 +74,7 @@ public function __construct(
7074 {
7175 $ this ->data_path = $ data_path ;
7276 $ this ->data_dir = $ data_dir ;
77+ $ this ->orig_path = $ orig_path ;
7378 $ this ->algorithm_file = $ algorithm_file ;
7479 $ this ->algorithm_content = $ algorithm_content ;
7580 $ this ->quality = $ quality ;
@@ -91,20 +96,39 @@ public function delete($arg, bool $onlyChangedImages = false): void
9196 $ dir = implode ('/ ' , [$ this ->data_path , $ script ->namespace , $ script ->prefix ]);
9297 $ origFile = $ script ->name . '. ' . $ script ->extension ;
9398
94- if (!file_exists ($ dir )) {
95- return ;
96- }
99+ if ($ this ->orig_path === $ this ->data_path ) {
100+ if (!file_exists ($ dir )) {
101+ return ;
102+ }
97103
98- foreach (new DirectoryIterator ($ dir ) as $ file_info ) {
99- if (
100- !preg_match ($ pattern , $ file_info ->getFilename ())
101- || !(!$ onlyChangedImages || $ origFile !== $ file_info ->getFilename ()
102- )
103- ) {
104- continue ;
104+ foreach (new DirectoryIterator ($ dir ) as $ file_info ) {
105+ if (
106+ !preg_match ($ pattern , $ file_info ->getFilename ())
107+ || !(!$ onlyChangedImages || $ origFile !== $ file_info ->getFilename ()
108+ )
109+ ) {
110+ continue ;
111+ }
112+
113+ unlink ($ file_info ->getPathname ());
105114 }
115+ } else {
116+ $ rmdir = function (string $ path ) use (&$ rmdir ) {
117+ if (is_file ($ path )) {
118+ unlink ($ path );
119+ } elseif (is_dir ($ path )) {
120+ $ scan = glob (rtrim ($ path , '/ ' ) . '/* ' );
121+ foreach ($ scan as $ sub_path ) {
122+ $ rmdir ($ sub_path );
123+ }
124+ rmdir ($ path );
125+ }
126+ };
106127
107- unlink ($ file_info ->getPathname ());
128+ if (!$ onlyChangedImages ){
129+ unlink (implode ('/ ' , [$ this ->orig_path , $ script ->namespace , $ script ->prefix , $ origFile ]));
130+ }
131+ $ rmdir ($ dir );
108132 }
109133 }
110134
@@ -133,7 +157,6 @@ private static function fixName(string $name): string
133157 return Strings::webalize ($ name , '._ ' );
134158 }
135159
136-
137160 /**
138161 * @param mixed $content
139162 */
@@ -167,13 +190,21 @@ public function fromIdentifier($args): Image
167190 }
168191
169192 $ identifier = $ args [0 ];
193+ $ quality = $ args [3 ] ?? $ this ->quality ;
194+ $ flag = $ args [2 ] ?? $ this ->default_transform ;
170195
196+ $ orig_file = implode ('/ ' , [$ this ->orig_path , $ identifier ]);
197+ $ data_file = implode ('/ ' , [$ this ->data_path , $ identifier ]);
171198 $ isNoImage = false ;
172199
173200 if (count ($ args ) === 1 ) {
174- if (!file_exists (implode ( ' / ' , [ $ this -> data_path , $ identifier ]) ) || !$ identifier ) {
201+ if (!file_exists ($ orig_file ) || !$ identifier ) {
175202 return $ this ->getNoImage (true );
176203 }
204+ if (!file_exists ($ data_file )) {
205+ @mkdir (dirname ($ data_file ), $ this ->mask , true );
206+ @copy ($ orig_file , $ data_file );
207+ }
177208
178209 return new Image ($ this ->friendly_url , $ this ->data_dir , $ this ->data_path , $ identifier );
179210 }
@@ -190,16 +221,13 @@ public function fromIdentifier($args): Image
190221 $ crop = [(int ) $ matches [4 ], (int ) $ matches [5 ], (int ) $ matches [6 ], (int ) $ matches [7 ]];
191222 }
192223
193- $ flag = $ args [2 ] ?? $ this ->default_transform ;
194- $ quality = $ args [3 ] ?? $ this ->quality ;
195-
196224 if (!$ identifier ) {
197225 $ isNoImage = false ;
198226 [$ script , $ file ] = $ this ->getNoImage (false );
199227 } else {
200228 $ script = ImageNameScript::fromIdentifier ($ identifier );
201229
202- $ file = implode ( ' / ' , [ $ this -> data_path , $ script -> original ]) ;
230+ $ file = $ orig_file ;
203231
204232 if (!file_exists ($ file )) {
205233 $ isNoImage = true ;
@@ -213,8 +241,9 @@ public function fromIdentifier($args): Image
213241 $ script ->setQuality ($ quality );
214242
215243 $ identifier = $ script ->getIdentifier ();
244+ $ data_file = implode ('/ ' , [$ this ->data_path , $ identifier ]);
216245
217- if (!file_exists (implode ( ' / ' , [ $ this -> data_path , $ identifier ]) )) {
246+ if (!file_exists ($ data_file )) {
218247 if (!file_exists ($ file )) {
219248 return new Image (false , '# ' , '# ' , 'Can not find image ' );
220249 }
@@ -243,8 +272,9 @@ public function fromIdentifier($args): Image
243272
244273 $ _image ->resize ($ size [0 ], $ size [1 ], $ flag );
245274
275+ @mkdir (dirname ($ data_file ), $ this ->mask , true );
246276 $ _image ->sharpen ()->save (
247- implode ( ' / ' , [ $ this -> data_path , $ identifier ]) ,
277+ $ data_file ,
248278 $ quality
249279 );
250280 }
@@ -306,7 +336,7 @@ public function getNoImage(bool $return_image = false)
306336 private function getSavePath (string $ name , string $ namespace , string $ checksum ): array
307337 {
308338 $ prefix = substr ($ checksum , 0 , 2 );
309- $ dir = implode ('/ ' , [$ this ->data_path , $ namespace , $ prefix ]);
339+ $ dir = implode ('/ ' , [$ this ->orig_path , $ namespace , $ prefix ]);
310340
311341 @mkdir ($ dir , $ this ->mask , true ); // Directory may exist
312342
0 commit comments