About image retrieval
I am building Ai image saas platform where I am using cloudinary when I transformed the image it is not shown in home section named as recent images
Answers
-
Hi @Abhinav1593 ,
Can you please share the URL that is not showing?
When Cloudinary returns an error for an image URL that failed to load due to a problem with the image or URL, we return an HTTP header containing more information about why the request failed.
This information is sent in thex-cld-error
header, and you can see the value by using your browser's web developer tools to examine the request and response for the image that didn't load correctly.Thanks,
Wissam0 -
"use server";
import { revalidatePath } from "next/cache";import { connectToDatabase } from "../database/mongoose";import { handleError } from "../utils";import User from "../database/models/user.model";import Image from "../database/models/image.model";import { redirect } from "next/navigation";
import { v2 as cloudinary } from 'cloudinary'
const populateUser = (query: any) => query.populate({ path: 'author', model: User, select: '_id firstName lastName clerkId'})
// ADD IMAGEexport async function addImage({ image, userId, path }: AddImageParams) { try { await connectToDatabase();
const author = await User.findById(userId);
if (!author) { throw new Error("User not found"); }
const newImage = await Image.create({ ...image, author: author._id, })
revalidatePath(path);
return JSON.parse(JSON.stringify(newImage)); } catch (error) { handleError(error) }}
// UPDATE IMAGEexport async function updateImage({ image, userId, path }: UpdateImageParams) { try { await connectToDatabase();
const imageToUpdate = await Image.findById(image._id);
if (!imageToUpdate || imageToUpdate.author.toHexString() !== userId) { throw new Error("Unauthorized or image not found"); }
const updatedImage = await Image.findByIdAndUpdate( imageToUpdate._id, image, { new: true } )
revalidatePath(path);
return JSON.parse(JSON.stringify(updatedImage)); } catch (error) { handleError(error) }}
// DELETE IMAGEexport async function deleteImage(imageId: string) { try { await connectToDatabase();
await Image.findByIdAndDelete(imageId); } catch (error) { handleError(error) } finally{ redirect('/') }}
// GET IMAGEexport async function getImageById(imageId: string) { try { await connectToDatabase();
const image = await populateUser(Image.findById(imageId));
if(!image) throw new Error("Image not found");
return JSON.parse(JSON.stringify(image)); } catch (error) { handleError(error) }}
// GET IMAGESexport async function getAllImages({ limit = 9, page = 1, searchQuery = '' }: { limit?: number; page: number; searchQuery?: string;}) { try { await connectToDatabase();
cloudinary.config({ cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME, api_key: process.env.CLOUDINARY_API_KEY, api_secret: process.env.CLOUDINARY_API_SECRET, secure: true, })
let expression = 'folder=imaginify';
if (searchQuery) { expression += ` AND ${searchQuery}` }
const { resources } = await cloudinary.search .expression(expression) .execute();
const resourceIds = resources.map((resource: any) => resource.public_id);
let query = {};
if(searchQuery) { query = { publicId: { $in: resourceIds } } }
const skipAmount = (Number(page) -1) * limit;
const images = await populateUser(Image.find(query)) .sort({ updatedAt: -1 }) .skip(skipAmount) .limit(limit); const totalImages = await Image.find(query).countDocuments(); const savedImages = await Image.find().countDocuments();
return { data: JSON.parse(JSON.stringify(images)), totalPage: Math.ceil(totalImages / limit), savedImages, } } catch (error) { handleError(error) }}
// GET IMAGES BY USERexport async function getUserImages({ limit = 9, page = 1, userId,}: { limit?: number; page: number; userId: string;}) { try { await connectToDatabase();
const skipAmount = (Number(page) - 1) * limit;
const images = await populateUser(Image.find({ author: userId })) .sort({ updatedAt: -1 }) .skip(skipAmount) .limit(limit);
const totalImages = await Image.find({ author: userId }).countDocuments();
return { data: JSON.parse(JSON.stringify(images)), totalPages: Math.ceil(totalImages / limit), }; } catch (error) { handleError(error); }}
this is my image.actions code my problem is that i am using google auto tagging and ai background removal. in my project i have build 5 tools which are image restoration, generative fill, object removal, object color and background change so suppose if i transformed 1 image using any tool it is shown in home page under recent images but in my case cloudinary did not retrieved back the transformed images and shown in home page0 -
Hi @Abhinav1593 . I think it is better we continue this conversation over a support ticket, where you can provide us with the response that your code returns when an upload is complete.
Could you please create a ticket via https://support.cloudinary.com?
Kind regards,
-Danny0