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 (
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
image BLOB,
template TEXT,
name TEXT,
createdAt TEXT NOT NULL,
image BLOB NOT NULL,
content TEXT
);`);
@ -33,8 +32,7 @@ createPostersTable.run();
/**
* @typedef {Object} Poster
* @property {number} [id]
* @property {string} [createdAt] Date
* @property {Blob} [image]
* @property {string} [template]
* @property {string} [name]
* @property {string} [content] Stringified contents
*/

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

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

@ -1,5 +1,7 @@
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';
/** @type {import('./$types').PageServerLoad} */
@ -10,7 +12,6 @@ export const load = () => {
posters
}
}
console.log(posters);
throw error(404, "error");
};
@ -18,7 +19,15 @@ export const load = () => {
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
}
}
}

Loading…
Cancel
Save