|
| 1 | +import { supabaseAdmin } from "@/lib/supabase"; |
| 2 | +import { NextResponse } from "next/server"; |
| 3 | + |
| 4 | +const generateRandomString = (sizeInBytes: number) => { |
| 5 | + const characters = |
| 6 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; |
| 7 | + let result = ""; |
| 8 | + const charactersLength = characters.length; |
| 9 | + for (let i = 0; i < sizeInBytes; i += 1) { |
| 10 | + result += characters.charAt(Math.floor(Math.random() * charactersLength)); |
| 11 | + } |
| 12 | + return result; |
| 13 | +}; |
| 14 | + |
| 15 | +export async function POST() { |
| 16 | + try { |
| 17 | + const fileSizeInBytes = 1024; |
| 18 | + const randomText = generateRandomString(fileSizeInBytes); |
| 19 | + const fileName = `keep-alive-${Date.now()}.txt`; |
| 20 | + |
| 21 | + const { data, error } = await supabaseAdmin.storage |
| 22 | + .from(process.env.SUPABASE_TEXT_BUCKET!) |
| 23 | + .upload(`keep-alive/${fileName}`, randomText, { |
| 24 | + contentType: "text/plain", |
| 25 | + upsert: true, |
| 26 | + }); |
| 27 | + |
| 28 | + if (error) { |
| 29 | + console.error("Supabase upload error:", error); |
| 30 | + return NextResponse.json( |
| 31 | + { msg: "Failed to upload file to Supabase", error: error.message }, |
| 32 | + { status: 500 } |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + return NextResponse.json({ msg: "Keep-alive file uploaded successfully", path: data.path }); |
| 37 | + } catch (err) { |
| 38 | + console.error("Server error in keep-alive endpoint:", err); |
| 39 | + return NextResponse.json({ msg: "Server Error" }, { status: 500 }); |
| 40 | + } |
| 41 | +} |
0 commit comments