Following on from my article about the use-cloudinary - useSearch hook, this time we'll be streamlining our code and diving into some of whats possible with when manipulating our cloudinary folders.
If we recount what we did last time:
This is a great, simple solution, but there is one problem. If we are stating that our user can only load their last 3 images but at the same time allowing them to save as many images as they like then we put ourselves and our cloudinary accounts credits at risk of being abused. This of course would be no fault of the users, we are the ones setting up the rules by which they should follow. No, alas it is our fault (well mine.... cough cough). Lets refactor a wee bit and see what we can do to make this process better.
Our search function will remain unchanged, our function will still search for an exact folder based on our users name. Our upload function however could do with some work.
The previous upload serverless function
The function takes and parses the passed in event, from which our file to upload is destructured and added to the cloudinary uploader function along with the rest of the image transformations. Cool.
The super duper upload serverless function
Brace yourselves.....here it comes....
We'll go through this step by step to understand whats going on.
Now we have ensured that our cloudinary credits are safe from those pesky image uploaders. Our folder is set to only accept 3 images. Of course we could choose any number we like. In an up coming tutorial series I'll walk you through creating an authenticated photo album library which will extensively use this hooks library and cloudinary.