Okay, now that we've got our Cloudinary configuration set up next we're going to create a service so
that we can upload an image via our API into Cloudinary.
So we'll need a package from Cloudinary to do this.
So let's open up a terminal and go to NuGet.
And we're going to look for Cloudinary.
And inside here we need the Cloudinary dotnet.
Just grab the latest version of this and we'll add it into our project.
And once we have this available let's go to our Solution Explorer.
And inside the API let's create or we've already got a folder for services.
So let's create a new service.
And it's going to be a class.
And I'm going to call it image service.
And inside here we need to initialize our Cloudinary settings effectively.
So I'm going to create a private read only variable called Cloudinary or using a type of cloudinary
which we get from Cloudinary.
Net.
So please make sure you bring this in.
And because it's a private variable, the convention is to start this with an underscore.
So we'll have that and then we'll create a constructor.
So we'll say ctor.
And in the parameters here we're going to use our configuration.
Now the way that we access it via our Cloudinary settings that we created over here and added as a configuration
inside our program class is we can use the I options interface.
So we're going to inject I options into this.
And then we specify the type as the Cloudinary settings that we created and we'll call it config.
And then inside our constructor we're going to create a new Cloudinary account effectively in here.
So we'll say var acc for account equals new.
And then we'll specify account.
And be careful here because we do have account in a few different places.
But we've already brought in cloudinary net.
So that's where this is coming from.
And then we need to give this some configuration values.
So inside here if we take a look at the different options we've got for accounts, we can specify no
parameters just the cloud name, the cloud name and a string of the OAuth token or the one that we're
going to use.
The fourth overload here where we specify the cloud name, the API key, and the API secret.
They have to be presented to this method in this order.
Any deviation from this, and you'll start getting errors from Cloudinary when you try and upload an
image.
So please pay attention to the ordering here because that is important.
And inside the parentheses I'll just move this down to the next line and we'll use config.
And then we use value.
And then from here we can get access to our Cloudinary settings values.
And the first one is the cloud name.
The second one is config dot value dot api key, and the third one is config dot value dot API secrets.
And close that with a semicolon, and then we can assign our new account or pass our new account to
the cloudinary variable we created above.
So I'm going to set Cloudinary equal to new cloudinary.
And we'll pass in the account that we have created.
So now we've got access to all of the Cloudinary functionality inside this variable here.
And let's go down and add a couple of methods inside here we're going to have a method to add an image
to Cloudinary.
And we're going to have a method to remove an image from Cloudinary.
So we'll use public async task.
And this is going to return an image upload result we get from Cloudinary.
Net actions and we'll call it add image async.
And we're going to pass to this as a parameter an eye form file type and call it file.
And then open curly brackets.
And if I hover over the eye form file then this represents a file sent with an HTTP request.
So that's how we're going to send the file up to our API server.
And then we'll create an upload result variable.
So we'll save our upload result equals new image upload result.
So that's what we'll store it in when we get it back from Cloudinary.
And then we'll need to check to make sure we've got a file being passed to us.
So I'm going to check to see if the file dot length is greater than zero.
And if it is, then we're going to effectively upload that file to cloudinary.
And we'll use a streamreader for this.
So a streamreader is something that we can use inside this method, but then dispose of as soon as we're
finished with it because it does consume memory.
So we'll use the using keyword so that it does get disposed of after we're finished with it.
And we'll say using var stream equals file.
And then we can use its open read stream method.
And then we need to create some upload params.
So I'll say var upload Params equals new image upload params which we again get from cloudinary and
inside here we can specify the file that we're going to send up, and we'll set this equal to a new
file description, and we'll pass in the file dot file name and the stream as the second parameter.
And we'll add a comma.
And we can specify a folder here and I will do.
And as I use cloudinary for several other projects.
So I want to make sure these go in a specific folder on Cloudinary.
So we can easily locate the images that have been uploaded for this specific project.
And I'll just say Rs dash course for this one, but you can use whatever you like or not even use a
folder.
It doesn't have to be contained inside a folder, you can just upload it without specifying a folder.
And then I'm going to set the upload results that we created above equal to and I'll say await and then
use Cloudinary and the upload a sync method and we can pass in our upload params.
And then below the if statement we can return the upload results.
So that takes care of the image upload.
And below this we'll also have a method for deleting an image.
So we'll use public async task that returns a deletion result.
Again another type that we get from Cloudinary.
And we'll use delete image async for this one.
And in here we'll pass in the string of the public id.
This is what Cloudinary uses as the unique ID for each image uploaded into Cloudinary.
And then we can create some deletion parameters.
So I'll say var delete params equals new deletion params and pass in the public id.
And then I can create a variable to store the result in.
So var results equals await and cloudinary, and we use the destroy async method from Cloudinary and
pass it the deletes params.
And then we can simply return the result, which is a type of deletion result.
So what we have now is a new service.
And like any other service, because we're going to inject this where we need to use it.
We need to add this to our program class.
So inside here let's find it neat and tidy place to put this.
And I'll just add it underneath the payment service.
In fact I'll even copy the payment service and just change this to image service.
And we'll just check our terminal.
And I think I'm just going to restart this because this looks like a mess.
And I doubt that hot reload would accommodate the changes that I've made inside there.
So now that we've got an image service and the ability to upload an image, we need to think about how
we're going to deal with this on our API.
And we'll take a look at creating a photo entity next.