How to deal with publicID if photo inside a folder?
I am able to save photos with only pulicID such as "docs_filenamedotextention" and it is saved in the home directory of cloudinary since no folder is assigned. If I want to add photos to a folder called docs then the publicID will become "docs/filenamedotextention" and here is where I have a problem because it will through an error as it will consider looking for a path docs then filenamedotextension rather than looking for the publicID itself. How to solve this issue?
Best Answer
-
I found the problem. I used imageid to get the photo from db and from there i passed the publicid as string to the cloudinary and then i was able to delete the photo from bothe the cloudinary and the db.
I was trying to access cloudinary publicid without specifying wich photo so it was taking array of publicid and not the specific id of that image in my code.
0
Answers
-
Hi @rwahdan,
That's right, if you upload a photo to a folder then the path should be
folder/filename
. Do you mind giving an example of your code that consider looking for a path docs then filenamedotextension rather than looking for the publicID itself?Thanks in advance.
Best,
Loic
0 -
Saving the photo is fine but if I want to delete it through API and here is what i get
my api is
http://localhost:5180/api/familydocuments/delete-photo/{photoPublicId} so the highlighted part is the public id but I think it is looking for a path and not a variable... 404 not found.
my controller code:
[HttpDelete("delete-photo/{photoPublicId}")]
[Authorize]
public async Task<IActionResult> DeletePhoto(string photoPublicId)
{
var photo = await uow.familyRepository.GetPhotoByIdAsync(photoPublicId);
if (photo == null)
return BadRequest("No such photo exists");
var result = await photoService.DeletePhotoAsync(photoPublicId);
if (result.Error != null)
return BadRequest(result.Error.Message);
var delPhoto = uow.familyRepository.DeletePhoto(photo.PublicId);
if (await uow.SaveAsync()) return Ok();
return Ok();
}
}
my photoService.DeletePhotoAsync(photoPublicId)
public async Task<DeletionResult> DeletePhotoAsync(string publicId)
{
var deleteParams = new DeletionParams(publicId);
var result = await cloudinary.DestroyAsync(deleteParams);
return result;
}
for the frontend:
the service:
deleteFamilyPhoto(photoPublicId: string)
{
const httpOptions = {
headers: new HttpHeaders({
Authorization: 'Bearer ' + localStorage.getItem('token')
})
};
return this.http.delete(this.baseUrl + '/familydocuments/delete-photo/' + photoPublicId, httpOptions)
}
the code:
deletePhoto(photoPublicId: string)
{
this.housingService.deleteFamilyPhoto(photoPublicId).subscribe(()=> {
this.familyDocs = this.familyDocs.filter(p=>
p.publicId != photoPublicId);
setTimeout(()=>
{
window.location.reload();
this.router.navigate(["familydocuments"])
}, 500);
});
}
0 -
Hi @rwahdan,
Ok this is the one I found earlier but I am not able to see any deletion requests for the public_id
test/FamilyDocs_12_03_2024__01_13_52
. If you log thedeleteParams
on your side, do you get anything? It seems it is actually failing before proceeding with thedestroy
call.Thanks in advance.
Best,
Loic
0 -
yes that is because it includes a folder name and my api is teating that as a path. If i remove the folder from the public id it is ok
0 -
if i remove the folder when uploading a photo then everything is fine!
0 -
here is what I tried in postman for new image:
why when i add a folder to the public id i have this issue? When I went to see the public id in cloudinary I get this:
the folder is not included! why is that?
0 -
Hi @rwahdan,
The 400 is from your own server, not Cloudinary so it means that:
var photo = await uow.familyRepository.GetPhotoByIdAsync(photoPublicId);
is null. What does this method do?
Regarding the
public_id
I see you deleted it manually via the UI. Any chance you can upload a new one and share it with me without deleting it so I can have a look at it?Thanks,
Loic
0 -
I don't know why my problem is complecated. while uploading a photo if I don't include a folder, everything is fine and no error and photo deleted. The problem is when I upload a photo to specific folder, it will look at the deleting part as a path and not as a variable!
I managed to find the way with postman to create an environment variable:
how to have the same from your side?
0 -
Hi @rwahdan , I'm glad you were able to resolve the issue! Do please let us know if you have any other questions or needs and we'd be happy to work with you on them.
0