Page 1 of 1

Previews are not deleted

Posted: 06 Dec 2019, 09:34
by MikaelNazarenko
Hi community)

I use the next controller for my needs and when I call delete method previews files are not deleted, but main file is deleted.

Code: Select all

https://github.com/aimeos/aimeos-core/blob/master/controller/common/src/Controller/Common/Media/Standard.php#L147
take a look, there is !strcmp( $preview, $mimedir ) in the condition. As I know it will return true only if strings are the same.

But in my case $preview is 'preview/4/e/4ef58950ec3a7545446021f13fa419e4.jpg'

and $mimedir is 'packages/aimeos/shop/mimeicons'.

I don't understand why there is strcmp.

Please make this moment clear for me. Maybe I did something wrong, or I need overwrite delete method and remove strcmp from there..

Re: Previews are not deleted

Posted: 06 Dec 2019, 09:46
by aimeos
You mean here: https://github.com/aimeos/aimeos-core/b ... d.php#L147

strcmp() return 0 if both string are equal and -1/+1 if they are different, so "!strcmp()" returns true if strings are different. The reason why the previews aren't deleted must be somewhere else.

Re: Previews are not deleted

Posted: 06 Dec 2019, 10:58
by MikaelNazarenko
Hah.. strange thing.

I have added some code to debug:

Code: Select all

		    dump($preview);
		    dump($mimedir);
		    dump(!strcmp( $preview, $mimedir ));
		    dd($fs->has( $preview ));


			try
			{
				if( $preview !== '' && !strcmp( $preview, $mimedir ) && $fs->has( $preview ) ) {
					$fs->rm( $preview );
				}
			}
			catch( \Exception $e ) { ; } // continue if removing file fails
This is the output:

Code: Select all

"preview/4/e/4ef58950ec3a7545446021f13fa419e4.jpg"
"packages/aimeos/shop/mimeicons"
false
true
Strings are different! if you run in php console var_dump(!-1) - it returns false ))

Re: Previews are not deleted

Posted: 06 Dec 2019, 11:06
by MikaelNazarenko
it is surprise from PHP (bool) -1 = true ))

Re: Previews are not deleted

Posted: 06 Dec 2019, 11:51
by aimeos
I think, "!strcmp()" is wrong, it should be "strcmp()" only (without exclamation mark).

Re: Previews are not deleted

Posted: 06 Dec 2019, 12:15
by MikaelNazarenko
Yes, will you fix it in future ?) for now I will fix it for myself with extending class.

Also let me ask you something else. Files stored like this folder structure: files->3->6->file.img. Can we delete 3 and 6 folders also when we delete file ? I don't want to have a lot of empty folders. As I know it is not implemented in aimeos, is it good idea if I implement this folder deletion ?

Re: Previews are not deleted

Posted: 06 Dec 2019, 13:38
by aimeos
MikaelNazarenko wrote: 06 Dec 2019, 12:15 Yes, will you fix it in future ?) for now I will fix it for myself with extending class.
Yes, but I think the really correct form would be:

Code: Select all

strncmp( $preview, $mimedir, strlen($mimedir) )
MikaelNazarenko wrote: 06 Dec 2019, 12:15 Also let me ask you something else. Files stored like this folder structure: files->3->6->file.img. Can we delete 3 and 6 folders also when we delete file ? I don't want to have a lot of empty folders. As I know it is not implemented in aimeos, is it good idea if I implement this folder deletion ?
You would have to check if there are more files or folders in the current one. As storage operations are costly and not all storages support folders or retrieving entries of a folder (e.g. AWS S3), I think it's acceptable to have max 16x16=256 folders even if some are empty when you delete files.

Re: Previews are not deleted

Posted: 06 Dec 2019, 13:55
by MikaelNazarenko
Thank you for the clarification! But where did you get this calculation 16x16=256 ? Is this from the code and it means that files storage logic built such way that it will keep maximum 256 folders ?

Re: Previews are not deleted

Posted: 06 Dec 2019, 14:00
by aimeos
Folder names use the first two characters of the file name. As the file names are hex-encoded MD5 strings, you have 16 characters 0-9,a,b,c,d,e,f -> 16 folders x 16 subfolders each = 256 folders in total. More are not possible.