Fixed: HeroImage upload bug fixed
This commit is contained in:
parent
4566a3d39c
commit
e680dcaad1
BIN
backend/Screenshot from 2025-01-23 10-50-04.png
Normal file
BIN
backend/Screenshot from 2025-01-23 10-50-04.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.8 KiB |
|
@ -1,4 +1,5 @@
|
||||||
import { PrismaClient } from "@prisma/client";
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
import CloudinaryUploadHandler from "../utils/CloudinaryClient.js";
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
@ -15,30 +16,49 @@ export const uploadHeroImages = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const files = req.files;
|
const files = req.files;
|
||||||
const heroImages = files.map((file) => file.filename);
|
const heroImages = files.map((file) => file.filename);
|
||||||
|
const CloudinaryURLs = await heroImages.map((item) =>
|
||||||
|
CloudinaryUploadHandler(`./uploads/${item}`)
|
||||||
|
);
|
||||||
|
Promise.all(CloudinaryURLs)
|
||||||
|
.then(async (results) => {
|
||||||
|
console.log("RTesults: ", results);
|
||||||
const newHeroImages = await prisma.heroImages.create({
|
const newHeroImages = await prisma.heroImages.create({
|
||||||
data: {
|
data: {
|
||||||
urls: heroImages,
|
urls: results,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
res.status(201).json({ status: 201, data: newHeroImages });
|
return res
|
||||||
|
.status(201)
|
||||||
|
.json({ status: 201, data: newHeroImages });
|
||||||
|
})
|
||||||
|
.catch((error) =>
|
||||||
|
res.status(500).json({ status: 500, message: error.message })
|
||||||
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ status: 500, message: error.message });
|
res.status(500).json({ status: 500, message: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateHeroImage = async (req, res) => {
|
export const updateHeroImage = async (req, res) => {
|
||||||
|
console.log(`req--->`, req);
|
||||||
const { id, index } = req.params;
|
const { id, index } = req.params;
|
||||||
try {
|
try {
|
||||||
const data = await prisma.heroImages.findUnique({where:{id: id}})
|
const data = await prisma.heroImages.findUnique({ where: { id: id } });
|
||||||
let oldArray = data.urls
|
console.log(`data---->`, data);
|
||||||
oldArray[index] = req.file.filename
|
let dataArray = data.urls;
|
||||||
const updatedEvent = await prisma.heroImages.update({
|
const fileName = req?.file?.filename;
|
||||||
where: { id: id },
|
const CloudinaryURL = await CloudinaryUploadHandler(
|
||||||
|
`./uploads/${fileName}`
|
||||||
|
);
|
||||||
|
console.log("Cloud", CloudinaryURL);
|
||||||
|
dataArray[index] = CloudinaryURL;
|
||||||
|
const newHeroImages = await prisma.heroImages.update({
|
||||||
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
urls: oldArray
|
urls: dataArray,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
res.status(200).json({ status: 200, data: updatedEvent });
|
return res.status(200).json({ status: 200, data: newHeroImages });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ status: 500, message: error.message });
|
res.status(500).json({ status: 500, message: error.message });
|
||||||
}
|
}
|
||||||
|
|
31
backend/package-lock.json
generated
31
backend/package-lock.json
generated
|
@ -9,6 +9,7 @@
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "^6.2.1",
|
"@prisma/client": "^6.2.1",
|
||||||
|
"cloudinary": "^2.5.1",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"express": "^4.21.2",
|
"express": "^4.21.2",
|
||||||
|
@ -183,6 +184,19 @@
|
||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/cloudinary": {
|
||||||
|
"version": "2.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/cloudinary/-/cloudinary-2.5.1.tgz",
|
||||||
|
"integrity": "sha512-CNg6uU53Hl4FEVynkTGpt5bQEAQWDHi3H+Sm62FzKf5uQHipSN2v7qVqS8GRVqeb0T1WNV+22+75DOJeRXYeSQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"lodash": "^4.17.21",
|
||||||
|
"q": "^1.5.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=9"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/concat-stream": {
|
"node_modules/concat-stream": {
|
||||||
"version": "1.6.2",
|
"version": "1.6.2",
|
||||||
"resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz",
|
||||||
|
@ -594,6 +608,12 @@
|
||||||
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",
|
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/lodash": {
|
||||||
|
"version": "4.17.21",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||||
|
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/math-intrinsics": {
|
"node_modules/math-intrinsics": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
|
||||||
|
@ -803,6 +823,17 @@
|
||||||
"node": ">= 0.10"
|
"node": ">= 0.10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/q": {
|
||||||
|
"version": "1.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz",
|
||||||
|
"integrity": "sha512-kV/CThkXo6xyFEZUugw/+pIOywXcDbFYgSct5cT3gqlbkBE1SJdwy6UQoZvodiWF/ckQLZyDE/Bu1M6gVu5lVw==",
|
||||||
|
"deprecated": "You or someone you depend on is using Q, the JavaScript Promise library that gave JavaScript developers strong feelings about promises. They can almost certainly migrate to the native JavaScript promise now. Thank you literally everyone for joining me in this bet against the odds. Be excellent to each other.\n\n(For a CapTP with native promises, see @endo/eventual-send and @endo/captp)",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.6.0",
|
||||||
|
"teleport": ">=0.2.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/qs": {
|
"node_modules/qs": {
|
||||||
"version": "6.13.0",
|
"version": "6.13.0",
|
||||||
"resolved": "https://registry.npmjs.org/qs/-/qs-6.13.0.tgz",
|
"resolved": "https://registry.npmjs.org/qs/-/qs-6.13.0.tgz",
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "^6.2.1",
|
"@prisma/client": "^6.2.1",
|
||||||
|
"cloudinary": "^2.5.1",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
"express": "^4.21.2",
|
"express": "^4.21.2",
|
||||||
|
|
38
backend/utils/CloudinaryClient.js
Normal file
38
backend/utils/CloudinaryClient.js
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import { v2 as cloudinary } from "cloudinary";
|
||||||
|
|
||||||
|
cloudinary.config({
|
||||||
|
cloud_name: "ds6sqynn2",
|
||||||
|
api_key: "814617551846369",
|
||||||
|
api_secret: "Gx3rvuiGhNDRIu9qBnBvyzM6eew", // Click 'View API Keys' above to copy your API secret
|
||||||
|
});
|
||||||
|
|
||||||
|
// // Upload an image
|
||||||
|
// const uploadResult = await cloudinary.uploader
|
||||||
|
// .upload(
|
||||||
|
// "https://res.cloudinary.com/demo/image/upload/getting-started/shoes.jpg",
|
||||||
|
// {
|
||||||
|
// public_id: "shoesByRIshav",
|
||||||
|
// }
|
||||||
|
// )
|
||||||
|
// .catch((error) => {
|
||||||
|
// console.log(error);
|
||||||
|
// });
|
||||||
|
|
||||||
|
// console.log(uploadResult);
|
||||||
|
// console.log(optimizeUrl);
|
||||||
|
|
||||||
|
export default async function CloudinaryUploadHandler(imagePath) {
|
||||||
|
const options = {
|
||||||
|
use_filename: true,
|
||||||
|
unique_filename: false,
|
||||||
|
overwrite: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await cloudinary.uploader.upload(imagePath, options);
|
||||||
|
console.log(result.secure_url)
|
||||||
|
return result.secure_url;
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue