cloudinary / cloudinary_php

PHP extension for Cloudinary
https://cloudinary.com/documentation/php_integration
MIT License
389 stars 151 forks source link

Missing a way to easily request every asset type #353

Closed vasilvestre closed 2 years ago

vasilvestre commented 2 years ago

Feature request for Cloudinary PHP SDK

Explain your use case

I'm creating an adapter for cloudinary that could handle every sort of files.

Describe the problem you’re trying to solve

I wish to search an asset by name, without knowing if it's a video, raw or image type.

Do you have a proposed solution?

Allowing 'all' value as 'resource_type' would be fine.

Code :

$this->adminApi->asset($path, ['resource_type' => AssetType::ALL])

Error : Cloudinary\Api\Exception\BadRequest: Invalid value all for parameter resource_type

tommyg-cld commented 2 years ago

Hi there,

Currently, the asset type is image by default so for any other type you need to specify it. But you can use our Search API which searches all resource types by default unless specified.

Let me know if you have any other questions or queries.

vasilvestre commented 2 years ago

Hi there,

Currently, the asset type is image by default so for any other type you need to specify it. But you can use our Search API which searches all resource types by default unless specified.

Let me know if you have any other questions or queries.

I will rework my package with search. Thank you for the information :)

patrick-tolosa commented 2 years ago

@vasilvestre I'm closing this issue as it seems resolved, please reach out in case you still need assistance.

vasilvestre commented 2 years ago

@vasilvestre I'm closing this issue as it seems resolved, please reach out in case you still need assistance.

I got the same problem with Destroy method, should I open another issue ?

tommyg-cld commented 2 years ago

destroy method has resource_type of an image by default as well so you'd need to specify video or raw for other asset types. Can you use Search API and destroy together or is there something specific you're trying to achieve?

vasilvestre commented 2 years ago

destroy method has resource_type of an image by default as well so you'd need to specify video or raw for other asset types. Can you use Search API and destroy together or is there something specific you're trying to achieve?

I'm actually using search API to search then destroy, I now hit the rate limit more often but I guess I have no choice. Than you :)

tommyg-cld commented 2 years ago

No problem, you can always check https://cloudinary.com/roadmap to see the latest updates we have or you can submit this idea as well :).

vasilvestre commented 2 years ago

Nice, I would really love a folder deletion in API even if it's not empty :) It's pretty unconvenient at the moment to loop through folder to delete resources..

tommyg-cld commented 2 years ago

You could delete resources by prefix for up to 1000 original resources per https://support.cloudinary.com/hc/en-us/articles/202521092-How-to-delete-all-images-within-a-folder- , hope it helps.

vasilvestre commented 2 years ago

You could delete resources by prefix for up to 1000 original resources per https://support.cloudinary.com/hc/en-us/articles/202521092-How-to-delete-all-images-within-a-folder- , hope it helps.

This is perfect ! :) I need to do 3 calls to specify resource type but.. it works

vasilvestre commented 2 years ago

You could delete resources by prefix for up to 1000 original resources per https://support.cloudinary.com/hc/en-us/articles/202521092-How-to-delete-all-images-within-a-folder- , hope it helps.

I think this is the final question, how may I handle public_id extension removal on files except raw type ? I use "auto" to let cloudinary decide which file type it is.

aleksandar-cloudinary commented 2 years ago

Hi @vasilvestre - For assets with resource_type of "image" or "video" the public_id will not contain the extension so there shouldn't be any need to modify the public_id. However, for assets with resource_type "raw" the extension needs to be part of the public_id. If you're not setting the public_id in your upload code and using "auto" resource_type then Cloudinary will automatically handle that for you when generating the public_id. You will receive that public_id of the uploaded asset, regardless of resource_type, back in the Upload API Response and you should record that on your side (such as in a database) and if you need to interact with that asset via the API (like get details of a single resource() or destroy() you'd need to use the relevant public_id (it just happens that for "raw" files it'll contain the extension). For "raw" assets you shouldn't modify or edit the public_id to remove the extension and just use the public_id returned by the API.

Lastly, if you use the Search API with an empty expression then we will return all assets (regardless of type/resource_type) and the response will contain the relevant public_ids of each so you could list resources this way without needing to specify the type/resource_type explicitly.

vasilvestre commented 2 years ago

Hi @vasilvestre - For assets with resource_type of "image" or "video" the public_id will not contain the extension so there shouldn't be any need to modify the public_id. However, for assets with resource_type "raw" the extension needs to be part of the public_id. If you're not setting the public_id in your upload code and using "auto" resource_type then Cloudinary will automatically handle that for you when generating the public_id. You will receive that public_id of the uploaded asset, regardless of resource_type, back in the Upload API Response and you should record that on your side (such as in a database) and if you need to interact with that asset via the API (like get details of a single resource() or destroy() you'd need to use the relevant public_id (it just happens that for "raw" files it'll contain the extension). For "raw" assets you shouldn't modify or edit the public_id to remove the extension and just use the public_id returned by the API.

Lastly, if you use the Search API with an empty expression then we will return all assets (regardless of type/resource_type) and the response will contain the relevant public_ids of each so you could list resources this way without needing to specify the type/resource_type explicitly.

I set the public id in every uploads, I just need to stop so ? Code related : https://github.com/vasilvestre/flysystem-cloudinary-adapter/blob/main/src/CloudinaryAdapter.php#L82

aleksandar-cloudinary commented 2 years ago

@vasilvestre If you're setting the public_id in your code then you don't need to stop, however, the only requirement is that for "image" / "video" resource_type the extension should not be passed as part of the public_id but for "raw" assets the extension does need to be part of the public_id. So to know if you need to set the extension in the public_id or not you'd have to look at the MIME type of the file and determine what the "auto" resource_type in Cloudinary will determine the asset as. At that point, if you're checking the MIME type you can set the resource_type explicitly too as you'll know whether it's an image/video/audio file or any other asset in which case it'll be raw and should have the extension as part of the public_id.

vasilvestre commented 2 years ago

@vasilvestre If you're setting the public_id in your code then you don't need to stop, however, the only requirement is that for "image" / "video" resource_type the extension should not be passed as part of the public_id but for "raw" assets the extension does need to be part of the public_id. So to know if you need to set the extension in the public_id or not you'd have to look at the MIME type of the file and determine what the "auto" resource_type in Cloudinary will determine the asset as. At that point, if you're checking the MIME type you can set the resource_type explicitly too as you'll know whether it's an image/video/audio file or any other asset in which case it'll be raw and should have the extension as part of the public_id.

There are really a lot of existing MIME types to handle (exhaustive list), could this be part of the SDK as a helper? I don't know what you think of this.

edit : I searched a bit and all mime types start with video/ or image/ so .. it will be easy and fast :) Thank you a lot !

aleksandar-cloudinary commented 2 years ago

@vasilvestre - Yeah, that's a way and/or you could use something like mime_content_type (https://www.php.net/manual/en/function.mime-content-type.php) to pass a file and you'll get back the MIME Type. Note that since audio files are considered part of "video" resource_type any audio/ MIME Type can be sent as "video" resource_type.

vasilvestre commented 2 years ago

@vasilvestre - Yeah, that's a way and/or you could use something like mime_content_type (https://www.php.net/manual/en/function.mime-content-type.php) to pass a file and you'll get back the MIME Type. Note that since audio files are considered part of "video" resource_type any audio/ MIME Type can be sent as "video" resource_type.

Right thank you :) FYI league/mime-type-detection is doing exactly what we are talking about :D

aleksandar-cloudinary commented 2 years ago

You're welcome!