Spaces:
Runtime error
Runtime error
delete prisma document + image in dataset
Browse files- app/api/collections/[id]/route.ts +46 -4
- app/api/route.ts +2 -2
- prisma/schema.prisma +1 -1
- utils/remover.ts +25 -0
- {app/api → utils}/uploader.ts +3 -3
app/api/collections/[id]/route.ts
CHANGED
@@ -1,14 +1,56 @@
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
const { id } = await request.json()
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
return Response.json(
|
11 |
{
|
|
|
12 |
status: 200,
|
13 |
ok: true
|
14 |
}
|
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
3 |
+
import { RemoverDataset } from '@/utils/remover'
|
4 |
|
5 |
+
const prisma = new PrismaClient()
|
|
|
6 |
|
7 |
+
export async function DELETE(request: Request, { params }: { params: { id: string } }) {
|
8 |
+
const { headers } = request
|
9 |
+
|
10 |
+
if (!headers?.get('Authorization') || headers.get('Authorization') !== `Bearer ${process.env.HF_TOKEN}`) {
|
11 |
+
return Response.json(
|
12 |
+
{
|
13 |
+
status: 401,
|
14 |
+
ok: false,
|
15 |
+
message: 'Wrong castle fam :/'
|
16 |
+
}
|
17 |
+
)
|
18 |
+
}
|
19 |
+
|
20 |
+
const collection = await prisma.collection.findUnique({
|
21 |
+
where: {
|
22 |
+
id: parseInt(params.id)
|
23 |
+
}
|
24 |
+
})
|
25 |
+
if (!collection) {
|
26 |
+
return Response.json(
|
27 |
+
{
|
28 |
+
status: 404,
|
29 |
+
ok: false
|
30 |
+
}
|
31 |
+
)
|
32 |
+
}
|
33 |
+
|
34 |
+
const isImageDeleted = await RemoverDataset(collection?.file_name as string)
|
35 |
+
if (!isImageDeleted.ok) {
|
36 |
+
return Response.json(
|
37 |
+
{
|
38 |
+
status: 500,
|
39 |
+
ok: false,
|
40 |
+
message: isImageDeleted.message
|
41 |
+
}
|
42 |
+
)
|
43 |
+
}
|
44 |
+
|
45 |
+
await prisma.collection.delete({
|
46 |
+
where: {
|
47 |
+
id: parseInt(params.id)
|
48 |
+
}
|
49 |
+
})
|
50 |
|
51 |
return Response.json(
|
52 |
{
|
53 |
+
message: `Image ${params.id} deleted`,
|
54 |
status: 200,
|
55 |
ok: true
|
56 |
}
|
app/api/route.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import { PrismaClient } from '@prisma/client'
|
2 |
|
3 |
import list_styles from "@/assets/list_styles.json"
|
4 |
-
import { UploaderDataset } from '
|
5 |
import { isTextNSFW } from '@/utils/checker/prompt'
|
6 |
import { isImageNSFW } from '@/utils/checker/image'
|
7 |
|
@@ -11,7 +11,7 @@ export async function POST(
|
|
11 |
request: Request,
|
12 |
) {
|
13 |
const global_headers = {
|
14 |
-
Authorization: `Bearer ${process.env.
|
15 |
'Content-Type': 'application/json',
|
16 |
['x-use-cache']: "0"
|
17 |
}
|
|
|
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 |
|
|
|
11 |
request: Request,
|
12 |
) {
|
13 |
const global_headers = {
|
14 |
+
Authorization: `Bearer ${process.env.HF_TOKEN}`,
|
15 |
'Content-Type': 'application/json',
|
16 |
['x-use-cache']: "0"
|
17 |
}
|
prisma/schema.prisma
CHANGED
@@ -4,7 +4,7 @@ generator client {
|
|
4 |
|
5 |
datasource db {
|
6 |
provider = "sqlite"
|
7 |
-
url = "file
|
8 |
}
|
9 |
|
10 |
model Collection {
|
|
|
4 |
|
5 |
datasource db {
|
6 |
provider = "sqlite"
|
7 |
+
url = "file:../data/dev.db"
|
8 |
}
|
9 |
|
10 |
model Collection {
|
utils/remover.ts
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { deleteFile } from "../node_modules/@huggingface/hub/dist";
|
2 |
+
import type { RepoDesignation, Credentials } from "../node_modules/@huggingface/hub/dist";
|
3 |
+
|
4 |
+
export const RemoverDataset = async (name: string) => {
|
5 |
+
const repo: RepoDesignation = { type: "dataset", name: "enzostvs/stable-diffusion-tpu-generations" };
|
6 |
+
const credentials: Credentials = { accessToken: process.env.HF_TOKEN as string };
|
7 |
+
|
8 |
+
const res: any = await deleteFile({
|
9 |
+
repo,
|
10 |
+
credentials,
|
11 |
+
path: `images/${name}.png`,
|
12 |
+
});
|
13 |
+
|
14 |
+
if (res?.error) return {
|
15 |
+
status: 500,
|
16 |
+
ok: false,
|
17 |
+
message: res?.error
|
18 |
+
};
|
19 |
+
|
20 |
+
return {
|
21 |
+
status: 200,
|
22 |
+
ok: true,
|
23 |
+
};
|
24 |
+
|
25 |
+
}
|
{app/api → utils}/uploader.ts
RENAMED
@@ -1,9 +1,9 @@
|
|
1 |
-
import { uploadFile } from "
|
2 |
-
import type { RepoDesignation, Credentials } from "
|
3 |
|
4 |
export const UploaderDataset = async (blob: Blob, name: string) => {
|
5 |
const repo: RepoDesignation = { type: "dataset", name: "enzostvs/stable-diffusion-tpu-generations" };
|
6 |
-
const credentials: Credentials = { accessToken: process.env.
|
7 |
|
8 |
const res: any = await uploadFile({
|
9 |
repo,
|
|
|
1 |
+
import { uploadFile } from "../node_modules/@huggingface/hub/dist";
|
2 |
+
import type { RepoDesignation, Credentials } from "../node_modules/@huggingface/hub/dist";
|
3 |
|
4 |
export const UploaderDataset = async (blob: Blob, name: string) => {
|
5 |
const repo: RepoDesignation = { type: "dataset", name: "enzostvs/stable-diffusion-tpu-generations" };
|
6 |
+
const credentials: Credentials = { accessToken: process.env.HF_TOKEN as string };
|
7 |
|
8 |
const res: any = await uploadFile({
|
9 |
repo,
|