How to deal with publicID if photo inside a folder?

Options
rwahdan
rwahdan Member Posts: 51

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?

Tagged:

Best Answer

  • rwahdan
    rwahdan Member Posts: 51
    Answer ✓
    Options

    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.

Answers

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 53
    Options

    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

  • rwahdan
    rwahdan Member Posts: 51
    edited March 12
    Options

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

        });

      }

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 53
    Options

    Hi @rwahdan,

    Any chance you could share the URL associated with this asset? I can't seem to find anything in our logs so it would be great to have the URL so I can pinpoint it.

    Thanks in advance.

    Best,

    Loic

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 53
    Options

    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 the deleteParams on your side, do you get anything? It seems it is actually failing before proceeding with the destroy call.

    Thanks in advance.

    Best,

    Loic

  • rwahdan
    rwahdan Member Posts: 51
    Options

    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

  • rwahdan
    rwahdan Member Posts: 51
    Options

    if i remove the folder when uploading a photo then everything is fine!

  • rwahdan
    rwahdan Member Posts: 51
    edited March 13
    Options

    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?

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 53
    edited March 14
    Options

    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

  • rwahdan
    rwahdan Member Posts: 51
    Options

    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?

  • victor_cloudinary
    victor_cloudinary Member, Cloudinary Staff Posts: 9
    Options

    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.