Skip to content

Commit fd08965

Browse files
Merge pull request #103 from CodeChefVIT/cloudinaryRoundRobin
Cloudinary round robin
2 parents 079040c + 099652d commit fd08965

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/app/api/upload/route.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,34 @@ import { campuses, exams, semesters, slots, years } from "@/components/select_op
44
import { connectToDatabase } from "@/lib/mongoose";
55
import cloudinary from "cloudinary";
66
import { type ICourses, type CloudinaryUploadResult } from "@/interface";
7-
import { PaperAdmin } from "@/db/papers";
7+
import Paper, { PaperAdmin } from "@/db/papers";
88
import axios from "axios";
99
// TODO: REMOVE THUMBNAIL FROM admin-buffer DB
10-
cloudinary.v2.config({
11-
cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME,
12-
api_key: process.env.CLOUDINARY_API_KEY,
13-
api_secret: process.env.CLOUDINARY_SECRET,
10+
11+
const cloudinaryConfig1 = cloudinary.v2;
12+
cloudinaryConfig1.config({
13+
cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME_1,
14+
api_key: process.env.CLOUDINARY_API_KEY_1,
15+
api_secret: process.env.CLOUDINARY_SECRET_1,
16+
});
17+
18+
const cloudinaryConfig2 = cloudinary.v2;
19+
cloudinaryConfig2.config({
20+
cloud_name: process.env.NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME_2,
21+
api_key: process.env.CLOUDINARY_API_KEY_2,
22+
api_secret: process.env.CLOUDINARY_SECRET_2,
1423
});
24+
const cloudinaryConfigs = [cloudinaryConfig1, cloudinaryConfig2];
1525

1626
export async function POST(req: Request) {
1727
try {
1828
if (!process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET) {
1929
return NextResponse.json({ message: "ServerMisconfig" }, { status: 500 });
2030
}
31+
const count: number = await Paper.countDocuments();
32+
33+
const configIndex = cloudinaryConfigs[count % cloudinaryConfigs.length];
34+
cloudinary.v2.config(configIndex);
2135
const uploadPreset = process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET;
2236
const formData = await req.formData();
2337
const files: File[] = formData.getAll("files") as File[];
@@ -90,6 +104,7 @@ export async function POST(req: Request) {
90104
.replace(/<img src='|'\s*\/>/g, "");
91105
const paper = new PaperAdmin({
92106
public_id_cloudinary,
107+
cloudinary_index: configIndex,
93108
finalUrl,
94109
thumbnailUrl,
95110
subject,

src/db/papers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { IPaper, type IAdminPaper, type ICourses } from "@/interface";
33

44
const adminSchema = new Schema<IAdminPaper>({
55
public_id_cloudinary: { type: String, required: true },
6+
cloudinary_index: { type: Number, required: true },
67
finalUrl: { type: String, required: true },
78
thumbnailUrl: { type: String, required: true },
89
subject: { type: String, required: true, index: true },

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface IAdminPaper {
7171
year: string;
7272
exam: "CAT-1" | "CAT-2" | "FAT" | "Model";
7373
semester: "Fall Semester" | "Winter Semester" | "Summer Semester" | "Weekend Semester";
74+
cloudinary_index: number;
7475
campus:
7576
| "Vellore"
7677
| "Chennai"

0 commit comments

Comments
 (0)