93 lines
3.3 KiB
TypeScript
93 lines
3.3 KiB
TypeScript
import { json } from "@sveltejs/kit";
|
|
import type { RequestHandler } from "./$types";
|
|
import { putObject } from "$lib/helpers/minio";
|
|
import { generateImage } from "$lib/helpers/stability";
|
|
import sharp from "sharp";
|
|
import * as pb from "$lib/helpers/pb"
|
|
import { S3_BUCKET_NAME, S3_ENDPOINT_URL } from "$env/static/private";
|
|
|
|
async function compressImage(imageName: string, imageBuffer: Buffer) {
|
|
|
|
const ja = performance.now()
|
|
const jpgBuffer = await sharp(imageBuffer)
|
|
.jpeg({ quality: 70 })
|
|
.withMetadata()
|
|
.toBuffer();
|
|
|
|
await putObject(imageName.replace(".png", ".jpg"), jpgBuffer, { "Content-Type": "image/jpeg" });
|
|
|
|
const jb = performance.now() - ja;
|
|
console.log(`[AI] JPG compression took ${jb}ms`)
|
|
|
|
const wa = performance.now()
|
|
const webpBuffer = await sharp(imageBuffer)
|
|
.webp({ quality: 70 })
|
|
.withMetadata()
|
|
.toBuffer()
|
|
|
|
await putObject(imageName.replace(".png", ".webp"), webpBuffer, { "Content-Type": "image/webp" });
|
|
const wb = performance.now() - wa;
|
|
console.log(`[AI] WebP compression took ${wb}ms`)
|
|
|
|
const aa = performance.now()
|
|
const aviBuffer = await sharp(imageBuffer)
|
|
.avif({ quality: 70 })
|
|
.withMetadata()
|
|
.toBuffer()
|
|
|
|
await putObject(imageName.replace(".png", ".avif"), aviBuffer, { "Content-Type": "image/avif" });
|
|
const ab = performance.now() - aa;
|
|
console.log(`[AI] AVIF compression took ${ab}ms`)
|
|
}
|
|
|
|
export const GET: RequestHandler = async ({ params }) => {
|
|
|
|
const inputId = params.id;
|
|
if (!inputId) {
|
|
throw new Error("Missing name");
|
|
}
|
|
|
|
const invite = await pb.getInvite(inputId);
|
|
if (!invite) {
|
|
throw new Error("Invite not found");
|
|
}
|
|
|
|
const { hair_color, hair_type, hair_length, noble_name, skin_color, name } = invite;
|
|
|
|
console.log(`[AI] Generating image for ${name} ${JSON.stringify({ hair_type, hair_color, hair_length, skin_color, name })}`)
|
|
if (!hair_type || !hair_color || !hair_length || !noble_name || !skin_color) {
|
|
throw new Error("Missing hairType, hairColor or hairLength");
|
|
}
|
|
|
|
const prompt = `realistic sharp portrait oil painting of a masked ${noble_name}, baroque, in the style of Charles Vess, masked ball attire, opulence, mystery, elegance, ${hair_length} ${hair_type} ${hair_color} hair, ${skin_color} skin`;
|
|
const negativePrompt = "blurry, multiple persons, picture frame, nsfw"
|
|
|
|
const a = performance.now()
|
|
const image = await generateImage(prompt, negativePrompt);
|
|
const d = performance.now() - a;
|
|
console.log(`[AI] Image generation took ${d}ms`)
|
|
|
|
const imageName = `${Math.random().toString(16).substring(3, 10)}-${noble_name.toLowerCase().split(" ").slice(0, 5).join("-").slice(0, 25).split("-").filter(l => l.length).join("-")}.png`
|
|
|
|
const imageBuffer = Buffer.from(image.base64, 'base64');
|
|
|
|
const a2 = performance.now()
|
|
const pngBuffer = await sharp(imageBuffer)
|
|
.png({ compressionLevel: 9, adaptiveFiltering: true, force: true })
|
|
.withMetadata()
|
|
.toBuffer()
|
|
|
|
await putObject(imageName, pngBuffer, { "Content-Type": "image/png" });
|
|
const d2 = performance.now() - a2;
|
|
console.log(`[AI] PNG compression took ${d2}ms`)
|
|
|
|
await compressImage(imageName, imageBuffer)
|
|
|
|
await pb.setInvitePortrait(inputId, `https://${S3_ENDPOINT_URL}/${S3_BUCKET_NAME}/${imageName}`);
|
|
|
|
return json({
|
|
url: `https://${S3_ENDPOINT_URL}/${S3_BUCKET_NAME}/${imageName}`
|
|
})
|
|
}
|
|
|