File size: 2,496 Bytes
95183d4
4f2c36e
 
6757563
0b5db2f
6757563
7e19cbb
4f2c36e
6b92c38
 
95183d4
6b92c38
2d49c67
0b5db2f
2d49c67
 
 
 
63bd9dc
95183d4
 
6757563
 
95183d4
 
538551c
 
 
 
 
 
 
 
 
95183d4
538551c
 
95183d4
6757563
641a85e
6757563
7dc9f9f
6b92c38
 
7dc9f9f
6757563
6b92c38
2d49c67
6b92c38
 
7dc9f9f
0891679
 
7dc9f9f
 
 
7e19cbb
a329b36
 
4f2c36e
6233641
7e19cbb
 
 
 
 
4f2c36e
 
7e19cbb
63bd9dc
4f2c36e
 
7dc9f9f
6757563
0891679
4f2c36e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { NextRequest } from 'next/server'
import { PrismaClient } from '@prisma/client'

import list_styles from "@/assets/list_styles.json"
import { UploaderDataset } from '../../utils/uploader'
import { isTextNSFW } from '@/utils/checker/prompt'

const prisma = new PrismaClient()

export async function POST(
  request: NextRequest,
) {
  const global_headers = {
    Authorization: `Bearer ${process.env.HF_TOKEN}`,
    'Content-Type': 'application/json',
    ['x-use-cache']: "0"
  }

  const { inputs, style, userId } = await request.json()
  const { headers } = request

  const findStyle = list_styles.find((item) => item.name === style)

  const ip_address = headers.get("x-forwarded-for") ?? request.ip 

  // if (!headers.get('Authorization')) {
  //   const count = await prisma.collection.count({
  //     where: {
  //       ip_address,
  //       createdAt: {
  //         gte: new Date(Date.now() - 24 * 60 * 60 * 1000)
  //       }
  //     }
  //   })
  
  //   if (count > 5) return Response.json({ status: 429, ok: false, message: "You have reached the limit of 5 images per day." });
  // }

  const textIsNSFW = await isTextNSFW(inputs, global_headers)
  if (textIsNSFW) return Response.json({ status: 401, ok: false, message: "Prompt doesn’t work, try another prompt" });

  const response = await fetch(`${process.env.API_SDXL_URL}`, {
    method: 'POST',
    body: JSON.stringify({
      prompt: findStyle?.prompt.replace("{prompt}", inputs) ?? inputs,
      negative_prompt: findStyle?.negative_prompt ?? "",
    }),
    headers: global_headers,
  })

  
  const res = await response.clone().json().catch(() => ({}));
  if (res?.error) return Response.json({ status: response.status, ok: false, message: res.error });
  
  const base64Image = res.images[0];
  const blob = await fetch(`data:image/png;base64,${base64Image}`).then((r) => r.blob());
  
  // const imageIsNSFW = await isImageNSFW(blob, global_headers)
  // if (imageIsNSFW) return Response.json({ status: 401, ok: false, message: "Image is not safe for work." });

  const name = Date.now() + `-${inputs.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase()}`
  const { ok, message } = await UploaderDataset(blob, name)

  if (!ok) return Response.json({ status: 500, ok: false, message });

  const new_image = await prisma.collection.create({
    data: {
      prompt: inputs,
      file_name: name,
      userId: userId ?? "",
    },
  })

  return Response.json({ image: new_image, status: 200, ok: true });

}