import { error } from '@sveltejs/kit';
import fs from 'fs';
import stream from 'stream';
// import { canvas } from '$lib/stores/store';
import { getAllPostersFromDB, addPosterToDB } from '$lib/db/utils';

/** @type {import('./$types').PageServerLoad} */
export const load = () => {
  const posters = getAllPostersFromDB();
  if (posters !== undefined) {
    return {
      posters
    }
  }
  throw error(404, "error");
};

/** @type {import('./$types').Actions} */
export const actions = {
  default: async ({ request }) => {
    console.log("Estoy aquí");
    const path = crypto.randomUUID();
    const data = await request.formData();
    const image = data.get("image")?.toString();
    const content = data.get("content");
    fs.writeFileSync(`./static/${path}.png`, image, 'base64url');
    // const savetoDB = addPosterToDB(image, content);
    return {
      success: true
    }
  }

}
