59 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-05-26 02:14:59 -04:00
import { NextResponse } from "next/server";
import { createClient } from "@/utils/supabase/server";
2024-05-27 00:14:31 -04:00
import sharp from 'sharp';
async function blurImage(blob: Buffer): Promise<Buffer> {
// Convert the blob to a sharp object
const image = sharp(blob);
// Blur the image
const blurredImage = await image.blur(75).toBuffer();
return blurredImage;
}
2024-05-26 02:14:59 -04:00
export async function GET(
request: Request,
{ params }: { params: { id: string } }
) {
2024-05-26 02:14:59 -04:00
const supabase = createClient();
2024-05-27 00:14:31 -04:00
const user = await supabase.auth.getUser();
2024-05-27 20:40:23 -04:00
const url = new URL(request.url);
const search = url.searchParams.get("nsfw");
const nsfw = search === "true";
2024-05-27 00:14:31 -04:00
const { data: gallery, error: galleryError } = await supabase
.from('galleries')
.select('*')
.eq('name', params.id)
2024-05-27 00:14:31 -04:00
.single();
let { data: files, error } = await supabase.storage.from('galleries').list(params.id);
if (files == null || files?.length == 0) {
return NextResponse.error();
}
// Loop through each file, download it, convert it to base64, and add the data URL to the array
let { data: blobdata, error: fileError } = await supabase.storage.from('galleries').download(params.id + "/" + files[0].name);
if (fileError || blobdata == null) {
//console.error('Error downloading file:', error);
return NextResponse.error();
}
let blobBuffer = Buffer.from(await blobdata.arrayBuffer());
let userId = user.data.user?.id;
let { data: subscription, error: rolesError } = await supabase
2024-05-27 00:14:31 -04:00
.from('user_subscriptions')
.select('*')
.eq('user_id', userId)
.single();
2024-05-27 20:40:23 -04:00
if(nsfw && gallery.nsfw){
2024-05-27 11:40:00 -04:00
blobBuffer = await blurImage(blobBuffer);
2024-05-26 02:14:59 -04:00
}
const contentType = files[0].name.endsWith('.png') ? 'image/png' : 'image/jpeg';
const dataUrl = `data:${contentType};base64,${blobBuffer.toString('base64')}`;
// Return a JSON response with the array of URLs
return new Response(dataUrl);
2024-05-26 02:14:59 -04:00
}