enzostvs HF staff commited on
Commit
95183d4
1 Parent(s): 641a85e

rate limiting

Browse files
app/api/collections/route.ts CHANGED
@@ -7,11 +7,12 @@ const prisma = new PrismaClient()
7
  export async function GET(request: Request) {
8
  const { headers } = request
9
  const { searchParams } = new URL(request.url)
 
10
  const userId = searchParams.get('userId') ?? undefined
11
  const page = searchParams.get('page') ? parseInt(searchParams.get('page') as string) : 0
12
-
13
  let is_admin = false
14
- if (headers.get('Authorization') ) {
15
  is_admin = await isAdmin(headers) as boolean
16
  }
17
 
 
7
  export async function GET(request: Request) {
8
  const { headers } = request
9
  const { searchParams } = new URL(request.url)
10
+
11
  const userId = searchParams.get('userId') ?? undefined
12
  const page = searchParams.get('page') ? parseInt(searchParams.get('page') as string) : 0
13
+
14
  let is_admin = false
15
+ if (headers.get('Authorization')) {
16
  is_admin = await isAdmin(headers) as boolean
17
  }
18
 
app/api/route.ts CHANGED
@@ -1,14 +1,14 @@
 
1
  import { PrismaClient } from '@prisma/client'
2
 
3
  import list_styles from "@/assets/list_styles.json"
4
  import { UploaderDataset } from '../../utils/uploader'
5
  import { isTextNSFW } from '@/utils/checker/prompt'
6
- import { isImageNSFW } from '@/utils/checker/image'
7
 
8
  const prisma = new PrismaClient()
9
 
10
  export async function POST(
11
- request: Request,
12
  ) {
13
  const global_headers = {
14
  Authorization: `Bearer ${process.env.HF_TOKEN}`,
@@ -17,8 +17,25 @@ export async function POST(
17
  }
18
 
19
  const { inputs, style, userId } = await request.json()
 
 
20
  const findStyle = list_styles.find((item) => item.name === style)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  const textIsNSFW = await isTextNSFW(inputs, global_headers)
23
  if (textIsNSFW) return Response.json({ status: 401, ok: false, message: "Prompt doesn’t work, try another prompt" });
24
 
 
1
+ import { NextRequest } from 'next/server'
2
  import { PrismaClient } from '@prisma/client'
3
 
4
  import list_styles from "@/assets/list_styles.json"
5
  import { UploaderDataset } from '../../utils/uploader'
6
  import { isTextNSFW } from '@/utils/checker/prompt'
 
7
 
8
  const prisma = new PrismaClient()
9
 
10
  export async function POST(
11
+ request: NextRequest,
12
  ) {
13
  const global_headers = {
14
  Authorization: `Bearer ${process.env.HF_TOKEN}`,
 
17
  }
18
 
19
  const { inputs, style, userId } = await request.json()
20
+ const { headers } = request
21
+
22
  const findStyle = list_styles.find((item) => item.name === style)
23
 
24
+ const ip_address = headers.get("x-forwarded-for") ?? request.ip
25
+
26
+ if (!headers.get('Authorization')) {
27
+ const count = await prisma.collection.count({
28
+ where: {
29
+ ip_address,
30
+ createdAt: {
31
+ gte: new Date(Date.now() - 24 * 60 * 60 * 1000)
32
+ }
33
+ }
34
+ })
35
+
36
+ if (count > 5) return Response.json({ status: 429, ok: false, message: "You have reached the limit of 5 images per day." });
37
+ }
38
+
39
  const textIsNSFW = await isTextNSFW(inputs, global_headers)
40
  if (textIsNSFW) return Response.json({ status: 401, ok: false, message: "Prompt doesn’t work, try another prompt" });
41
 
prisma/schema.prisma CHANGED
@@ -11,6 +11,7 @@ model Collection {
11
  id Int @id @default(autoincrement())
12
  prompt String
13
  file_name String
 
14
  is_visible Boolean @default(false)
15
  createdAt DateTime @default(now())
16
  userId String? @default("")
 
11
  id Int @id @default(autoincrement())
12
  prompt String
13
  file_name String
14
+ ip_address String @default("")
15
  is_visible Boolean @default(false)
16
  createdAt DateTime @default(now())
17
  userId String? @default("")