enzostvs's picture
enzostvs HF staff
rework models + user + add comment stuffs
d6da254
raw
history blame
1.9 kB
/** @type {import('./$types').RequestHandler} */
import { UploaderDataset } from '$lib/utils/uploader';
// import { uploadImage } from '$lib/utils/upload_image';
import { json, type RequestEvent } from '@sveltejs/kit';
import prisma from '$lib/prisma';
import { tokenIsAvailable } from '$lib/utils';
export async function POST({ request, cookies } : RequestEvent) {
const { generation, image } = await request.json()
const token = cookies.get('hf_access_token')
let hf_user_id = null;
if (token) {
const user = await tokenIsAvailable(token)
if (user) hf_user_id = user?.sub;
}
if (!generation?.model?.id) {
return json({
error: {
token: "A model id is required"
}
}, { status: 400 })
}
if (!generation?.inputs) {
return json({
error: {
token: "An inputs is required"
}
}, { status: 400 })
}
const blob = await fetch(image)
.then((res) => res.blob())
.then((blob) => blob)
.catch((error) => {
return json({
error: error.message,
}, { status: 400 })
})
// const success = await uploadImage({
// name: generation.inputs,
// data: image
// })
// console.log(success)
const success: {
ok: boolean,
path?: string | undefined
} = await UploaderDataset(blob as Blob, generation.inputs)
if (!success.ok) {
return json({
error: {
token: "Error uploading image"
}
}, { status: 400 })
}
const gallery = prisma.gallery.create({
data: {
image: success.path as string,
prompt: generation.inputs,
user: {
connect: {
sub: hf_user_id
}
},
model: {
connect: {
id: generation.model.id
}
},
}
})
.catch((error) => {
console.log(error)
})
return json({
message: "Successfully generated image",
gallery
})
}