Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,569 Bytes
f05d33c 4712663 f05d33c 35140b4 f05d33c 644d65a f05d33c 644d65a f05d33c 8a18175 f05d33c 644d65a f05d33c 644d65a f05d33c 644d65a f05d33c 644d65a f05d33c 35140b4 0fd75db 35140b4 e6de30f 35140b4 752d7f4 0fd75db 35140b4 b628664 4712663 35140b4 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
import { json, type RequestEvent } from '@sveltejs/kit';
import { promises } from 'fs';
import prisma from '$lib/prisma';
import { tokenIsAvailable } from '$lib/utils';
/** @type {import('./$types').RequestHandler} */
export async function GET({ params, url } : RequestEvent) {
const id = params.id;
const filter = url.searchParams.get('filter') || 'new'
const search = url.searchParams.get('search') || ''
const gallery = await prisma.gallery.findFirst({
where: {
id,
},
select: {
image: true,
id: true,
prompt: true,
createdAt: true,
user: {
select: {
id: true,
name: true,
sub: true,
picture: true,
preferred_username: true,
}
},
comments: {
select: {
id: true,
createdAt: true,
text: true,
user: {
select: {
id: true,
name: true,
sub: true,
picture: true,
preferred_username: true,
}
}
}
},
model: {
select: {
image: true,
id: true,
}
},
reactions: {
select: {
id: true,
emoji: true,
userId: true,
user: {
select: {
id: true,
name: true,
sub: true,
picture: true,
preferred_username: true,
}
}
}
}
}
})
if (!gallery) {
return json({
error: {
token: "Gallery not found"
}
}, { status: 404 })
}
const next = await prisma.gallery.findFirst({
where: {
isPublic: true,
createdAt: {
lt: gallery.createdAt
},
OR: [
{ prompt: { contains: search } },
],
},
orderBy: {
...(filter === 'new' ? {
createdAt: 'desc'
} : {
reactions: {
_count: 'desc'
}
}
)
},
select: {
id: true,
}
})
const previous = await prisma.gallery.findFirst({
where: {
isPublic: true,
createdAt: {
gt: gallery.createdAt
},
OR: [
{ prompt: { contains: search } },
],
},
orderBy: {
...(filter === 'new' ? {
createdAt: 'desc'
} : {
reactions: {
_count: 'desc'
}
}
)
},
select: {
id: true,
}
})
return json({
gallery,
next: next ? next.id : undefined,
previous: previous ? previous.id : undefined,
})
}
export async function DELETE({ params, cookies }: RequestEvent) {
const id = params.id;
const token = cookies.get('hf_access_token')
if (!token) {
return json({
error: "You must be logged"
}, { status: 401 })
}
const user = await tokenIsAvailable(token)
if (!user) {
return json({
error: "Invalid token"
}, { status: 401 })
}
const gallery = await prisma.gallery.findFirst({
where: {
id
}
})
if (gallery?.userId !== user.sub && !process?.env?.SECRET_HF_ADMIN?.includes(user?.sub)) {
return json({
error: "You are not authorized to delete this gallery"
}, { status: 401 })
}
if (!gallery) {
return json({
error: "Gallery not found"
}, { status: 404 })
}
await prisma.gallery.delete({
where: {
id
}
})
await promises.unlink(`${process.env.PUBLIC_FILE_UPLOAD_DIR}/${gallery?.image}`)
return json({
success: true
})
} |