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-27 11:17:56 -04:00
|
|
|
|
2024-05-26 02:14:59 -04:00
|
|
|
export async function GET(
|
2024-05-27 11:17:56 -04:00
|
|
|
request: Request,
|
|
|
|
{ params }: { params: { id: string } }
|
|
|
|
) {
|
|
|
|
const galleryId = params.id.toLowerCase().replace(/\s+/g, '_');
|
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 11:17:56 -04:00
|
|
|
|
|
|
|
|
2024-05-27 00:14:31 -04:00
|
|
|
const { data: gallery, error: galleryError } = await supabase
|
|
|
|
.from('galleries')
|
|
|
|
.select('*')
|
2024-05-27 11:17:56 -04:00
|
|
|
.eq('name', params.id)
|
2024-05-27 00:14:31 -04:00
|
|
|
.single();
|
2024-05-27 11:17:56 -04:00
|
|
|
|
|
|
|
let { data: files, error } = await supabase.storage.from('galleries').list(galleryId);
|
|
|
|
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(galleryId + "/" + 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 11:17:56 -04:00
|
|
|
switch (gallery.tier) {
|
|
|
|
case "Tier 3":
|
|
|
|
if (subscription?.subscription != "Tier 3") {
|
|
|
|
blobBuffer = await blurImage(blobBuffer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Tier 2":
|
|
|
|
if (subscription?.subscription != "Tier 3" && subscription?.subscription != "Tier 2") {
|
|
|
|
blobBuffer = await blurImage(blobBuffer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Tier 1":
|
|
|
|
if (subscription?.subscription != "Tier 3" && subscription?.subscription != "Tier 2" && subscription?.subscription != "Tier 1") {
|
|
|
|
blobBuffer = await blurImage(blobBuffer);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2024-05-26 02:14:59 -04:00
|
|
|
}
|
2024-05-27 11:17:56 -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
|
|
|
}
|