@@ -4,20 +4,34 @@ import { campuses, exams, semesters, slots, years } from "@/components/select_op
4
4
import { connectToDatabase } from "@/lib/mongoose" ;
5
5
import cloudinary from "cloudinary" ;
6
6
import { type ICourses , type CloudinaryUploadResult } from "@/interface" ;
7
- import { PaperAdmin } from "@/db/papers" ;
7
+ import Paper , { PaperAdmin } from "@/db/papers" ;
8
8
import axios from "axios" ;
9
9
// 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 ,
14
23
} ) ;
24
+ const cloudinaryConfigs = [ cloudinaryConfig1 , cloudinaryConfig2 ] ;
15
25
16
26
export async function POST ( req : Request ) {
17
27
try {
18
28
if ( ! process . env . NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET ) {
19
29
return NextResponse . json ( { message : "ServerMisconfig" } , { status : 500 } ) ;
20
30
}
31
+ const count : number = await Paper . countDocuments ( ) ;
32
+
33
+ const configIndex = cloudinaryConfigs [ count % cloudinaryConfigs . length ] ;
34
+ cloudinary . v2 . config ( configIndex ) ;
21
35
const uploadPreset = process . env . NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET ;
22
36
const formData = await req . formData ( ) ;
23
37
const files : File [ ] = formData . getAll ( "files" ) as File [ ] ;
@@ -90,6 +104,7 @@ export async function POST(req: Request) {
90
104
. replace ( / < i m g s r c = ' | ' \s * \/ > / g, "" ) ;
91
105
const paper = new PaperAdmin ( {
92
106
public_id_cloudinary,
107
+ cloudinary_index : configIndex ,
93
108
finalUrl,
94
109
thumbnailUrl,
95
110
subject,
0 commit comments