Init
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const moment = require("moment");
|
||||
const date = moment();
|
||||
|
||||
const uploadImage = (image, res, type) => {
|
||||
const maxSize = 15 * 1024 * 1024; // 15MB (example limit)
|
||||
|
||||
if (image.size > maxSize) {
|
||||
res.status(500)
|
||||
throw new Error("Image file size exceeds the limit")
|
||||
}
|
||||
let imageName =
|
||||
date.format("YYYY-MM-DD") +
|
||||
"-Img-" +
|
||||
Math.floor(Math.random() * 900) +
|
||||
100 +
|
||||
image.name;
|
||||
|
||||
let saveToPath
|
||||
let preFile
|
||||
|
||||
switch (type) {
|
||||
case "1":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/products", imageName);
|
||||
preFile = "/uploads/products/"
|
||||
break;
|
||||
|
||||
case "2":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/blogs", imageName);
|
||||
preFile = "/uploads/blogs/"
|
||||
break;
|
||||
|
||||
case "3":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/personal", imageName);
|
||||
preFile = "/uploads/personal/"
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/none", imageName);
|
||||
preFile = "/uploads/none/"
|
||||
break;
|
||||
}
|
||||
|
||||
fs.writeFile(saveToPath, image.data, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ msg: "Error saving the image" });
|
||||
} else {
|
||||
res.status(201).json({ url: `${preFile}${imageName}` });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
const uploadImages = (images, res, type) => {
|
||||
const maxSize = 15 * 1024 * 1024; // 15MB (example limit)
|
||||
const imageUrls = [];
|
||||
|
||||
if (!Array.isArray(images)) {
|
||||
|
||||
if (images.size > maxSize) {
|
||||
res.status(500)
|
||||
throw new Error("Image file size exceeds the limit")
|
||||
}
|
||||
|
||||
let imageName =
|
||||
date.format("YYYY-MM-DD") +
|
||||
"-Img-" +
|
||||
Math.floor(Math.random() * 900) +
|
||||
100 +
|
||||
images.name;
|
||||
|
||||
let saveToPath
|
||||
let preFile
|
||||
|
||||
switch (type) {
|
||||
case "1":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/products", imageName);
|
||||
preFile = "/uploads/products/"
|
||||
break;
|
||||
|
||||
case "2":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/blogs", imageName);
|
||||
preFile = "/uploads/blogs/"
|
||||
break;
|
||||
|
||||
case "3":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/personal", imageName);
|
||||
preFile = "/uploads/personal/"
|
||||
break;
|
||||
|
||||
case "4":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/slider", imageName);
|
||||
preFile = "/uploads/slider/"
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/none", imageName);
|
||||
preFile = "/uploads/none/"
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
fs.writeFile(saveToPath, images.data, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({ msg: "Error saving the image" });
|
||||
} else {
|
||||
imageUrls.push(`${preFile}${imageName}`)
|
||||
res.status(201).json({ urls: imageUrls });
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
images.forEach(async (image) => {
|
||||
if (image.size > maxSize) {
|
||||
res.status(400)
|
||||
throw new Error("Image file size exceeds the limit")
|
||||
}
|
||||
|
||||
let imageName =
|
||||
date.format("YYYY-MM-DD") +
|
||||
"-Img-" +
|
||||
Math.floor(Math.random() * 900) +
|
||||
100 +
|
||||
image.name;
|
||||
|
||||
let saveToPath
|
||||
let preFile
|
||||
|
||||
|
||||
switch (type) {
|
||||
case "1":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/products", imageName);
|
||||
preFile = "/uploads/products/"
|
||||
break;
|
||||
|
||||
case "2":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/blogs", imageName);
|
||||
preFile = "/uploads/blogs/"
|
||||
break;
|
||||
|
||||
case "3":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/personal", imageName);
|
||||
preFile = "/uploads/personal/"
|
||||
break;
|
||||
|
||||
case "4":
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/slider", imageName);
|
||||
preFile = "/uploads/slider/"
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
// Specify the save location
|
||||
saveToPath = path.join(__dirname, "..", "uploads/none", imageName);
|
||||
preFile = "/uploads/none/"
|
||||
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
imageUrls.push(`${preFile}${imageName}`);
|
||||
|
||||
|
||||
fs.writeFile(saveToPath, image.data, (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
res.status(500)
|
||||
throw new Error("Can't save images")
|
||||
okd = false
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
res.status(201).json({ urls: imageUrls });
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
const deleteImage = (address, res) => {
|
||||
const addressOf = path.join(__dirname, "..", address);
|
||||
|
||||
fs.unlink(addressOf, (err) => {
|
||||
if (err) {
|
||||
console.error("Error removing image:", err);
|
||||
return;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
deleteImage,
|
||||
uploadImage,
|
||||
uploadImages,
|
||||
};
|
||||
Reference in New Issue
Block a user