cambios y más cambios, aunque aún no funciona definitivamente

pull/16/head
Borja Robert 2 years ago
parent 4a1711e020
commit 4be8be4406

Binary file not shown.

@ -14,9 +14,8 @@ const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS Users (
const createPostersTable = db.prepare(`CREATE TABLE IF NOT EXISTS Posters ( const createPostersTable = db.prepare(`CREATE TABLE IF NOT EXISTS Posters (
id INTEGER PRIMARY KEY UNIQUE NOT NULL, id INTEGER PRIMARY KEY UNIQUE NOT NULL,
image BLOB, createdAt TEXT NOT NULL,
template TEXT, image BLOB NOT NULL,
name TEXT,
content TEXT content TEXT
);`); );`);
@ -33,8 +32,7 @@ createPostersTable.run();
/** /**
* @typedef {Object} Poster * @typedef {Object} Poster
* @property {number} [id] * @property {number} [id]
* @property {string} [createdAt] Date
* @property {Blob} [image] * @property {Blob} [image]
* @property {string} [template]
* @property {string} [name]
* @property {string} [content] Stringified contents * @property {string} [content] Stringified contents
*/ */

@ -4,6 +4,7 @@ import { db } from '$lib/db/db.js';
* @typedef {Object} ChangeDBResult * @typedef {Object} ChangeDBResult
* @property {string} [error] * @property {string} [error]
* @property {string} [success] * @property {string} [success]
* @property {string} [id]
*/ */
/** /**
@ -51,29 +52,35 @@ export const delUserFromDB = (email) => {
/** /**
* @function addPosterToDB * @function addPosterToDB
* @param {Blob} image * @param {any} image
* @param {string} template
* @param {string} name
* @param {string} content * @param {string} content
* @returns {ChangeDBResult} return * @returns {ChangeDBResult} return
*/ */
export const addPosterToDB = (image, template, name, content) => { export const addPosterToDB = (image, content) => {
const id = crypto.randomUUID(); // Create random ID for Poster const id = crypto.randomUUID(); // Create random ID for Poster
console.log("New id is: ", id); console.log("New id is: ", id);
if (!image || !template || !content) { // Return error if mandatory info is missing const createdAt = Date.now().toString;
if (!image) { // Return error if mandatory info is missing
return { return {
error: "Image, template or content missing" error: "Image missing"
} }
} }
const addPoster = db.prepare(`INSERT INTO Posters(id,image,template,name,content) VALUES (?,?,?,?,?);`); const addPoster = db.prepare(`INSERT INTO Posters(id,createdAt,image,content) VALUES ($id,$createdAt,$image,$content);`);
const result = addPoster.run(id, image, template, name, content); const result = addPoster.run({
id: id,
createdAt: createdAt,
image: image,
content: content
});
if (result.changes === 1) { if (result.changes === 1) {
return { return {
success: `Poster added to DB with id ${id}` success: `Poster added to DB with id ${id}`,
id
} }
} }
return { // Default return just in case return { // Default return just in case

@ -37,10 +37,8 @@
const img = await html2canvas($canvas, { scale: 2 }); const img = await html2canvas($canvas, { scale: 2 });
const image = img.toDataURL('image/png'); const image = img.toDataURL('image/png');
const data = new FormData(); const data = new FormData();
data.set('image', image); data.append('image', image);
data.set('name', 'test'); data.append(
data.set('template', 'verde');
data.set(
'content', 'content',
JSON.stringify({ JSON.stringify({
title: $title, title: $title,
@ -54,16 +52,12 @@
city: $city city: $city
}) })
); );
console.log(data.get('image'));
const req = await fetch('/admin', {
// const req = await fetch('/admin', { method: 'POST',
// method: 'POST', body: data
// headers: { });
// 'content-type': 'multipart/form-data', console.log(req);
// accept: 'application/json'
// },
// body: data
// });
} }
}; };

@ -1,5 +1,7 @@
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
import { canvas } from '$lib/stores/store'; import fs from 'fs';
import stream from 'stream';
// import { canvas } from '$lib/stores/store';
import { getAllPostersFromDB, addPosterToDB } from '$lib/db/utils'; import { getAllPostersFromDB, addPosterToDB } from '$lib/db/utils';
/** @type {import('./$types').PageServerLoad} */ /** @type {import('./$types').PageServerLoad} */
@ -10,7 +12,6 @@ export const load = () => {
posters posters
} }
} }
console.log(posters);
throw error(404, "error"); throw error(404, "error");
}; };
@ -18,7 +19,15 @@ export const load = () => {
export const actions = { export const actions = {
default: async ({ request }) => { default: async ({ request }) => {
console.log("Estoy aquí"); console.log("Estoy aquí");
const path = crypto.randomUUID();
const data = await request.formData(); 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
}
} }
} }

Loading…
Cancel
Save