Previews are not deleted

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Previews are not deleted

Post by MikaelNazarenko » 06 Dec 2019, 09:34

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..

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Previews are not deleted

Post by aimeos » 06 Dec 2019, 09:46

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.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: Previews are not deleted

Post by MikaelNazarenko » 06 Dec 2019, 10:58

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 ))

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: Previews are not deleted

Post by MikaelNazarenko » 06 Dec 2019, 11:06

it is surprise from PHP (bool) -1 = true ))

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Previews are not deleted

Post by aimeos » 06 Dec 2019, 11:51

I think, "!strcmp()" is wrong, it should be "strcmp()" only (without exclamation mark).
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: Previews are not deleted

Post by MikaelNazarenko » 06 Dec 2019, 12:15

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 ?

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Previews are not deleted

Post by aimeos » 06 Dec 2019, 13:38

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.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: Previews are not deleted

Post by MikaelNazarenko » 06 Dec 2019, 13:55

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 ?

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Previews are not deleted

Post by aimeos » 06 Dec 2019, 14:00

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.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply