Untitled
Anis
plain_text
2 years ago
3.8 kB
16
Indexable
const multer = require("multer");
const cloudinary = require("cloudinary").v2;
const CloudinaryStorage =
require("multer-storage-cloudinary").CloudinaryStorage;
const storage = new CloudinaryStorage({
cloudinary: cloudinary,
params: {
folder: "asia-sports/live-match", // optional: specify a folder in Cloudinary
allowed_formats: ["jpg", "png"],
// public_id: (req, file) => `${file.fieldname.replace(fileExt, "").toLowerCase().split(" ").join("-") + Date.now()}`
},
});
const storage = multer.diskStorage({
filename: function (req, file, cb) {
cb(null, file.originalname);
},
});
const upload = multer({
storage,
limits: {
fileSize: 5000000, // 5MB
},
fileFilter: (req, file, cb) => {
if (file.fieldname === "team_one_image") {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg"
) {
cb(null, true);
} else {
cb(new Error("Only .jpg, .png or .jpeg format allowed!"));
}
}
if (file.fieldname === "team_two_image") {
if (
file.mimetype === "image/png" ||
file.mimetype === "image/jpg" ||
file.mimetype === "image/jpeg"
) {
cb(null, true);
} else {
cb(new Error("Only .jpg, .png or .jpeg format allowed!"));
}
}
},
});
async function createMatch(req, res, next) {
try {
upload.fields([
{
name: "team_one_image",
maxCount: 1,
},
{
name: "team_two_image",
maxCount: 1,
},
])(req, res, async (err) => {
if (err instanceof multer.MulterError) {
console.error(err);
return res.status(500).json({ error: "Multer error!" });
} else if (err) {
console.error(err);
return res.status(500).json({ error: "Error uploading file!" });
}
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ status: false, errors: errors.array() });
}
// return res.status(200).json({
// status: true,
// message: "Match created test successful!",
// });
const matchData = req.body;
const streamingData = createStreaming(
JSON.parse(matchData.streaming_sources)
);
if (matchData?.team_one_image_type === "image") {
matchData.team_one_image = req.files?.team_one_image[0].path || null;
} else {
matchData.team_one_image = matchData?.team_one_image_url;
}
if (matchData?.team_two_image_type === "image") {
matchData.team_two_image = req.files?.team_two_image[0].path || null;
} else {
matchData.team_two_image = matchData?.team_two_image_url;
}
const newMatch = new LiveMatch({
...matchData,
streaming_sources: [],
});
const createdStreams = await Promise.all(
streamingData.map(async (streamData) => {
const newStream = new Stream({
id: generateRandomId(15),
matchId: newMatch._id,
match_id: newMatch.id,
...streamData,
});
newMatch.streaming_sources.push(newStream._id);
await newStream.save();
return newStream;
})
);
await newMatch.save();
return res.status(200).json({
status: true,
message: "Match created successfully!",
data: { match: newMatch, streams: createdStreams },
});
});
} catch (error) {
console.error(error);
return res.status(500).json({
status: false,
message: "An error occurred while creating the match!",
});
}
}Editor is loading...
Leave a Comment