From 4be8be4406113108a80547e5626ee58cac1cba51 Mon Sep 17 00:00:00 2001 From: Borja Robert Date: Wed, 12 Oct 2022 20:59:56 +0200 Subject: [PATCH] =?UTF-8?q?cambios=20y=20m=C3=A1s=20cambios,=20aunque=20a?= =?UTF-8?q?=C3=BAn=20no=20funciona=20definitivamente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/db/carteles_dev.sqlite | Bin 20480 -> 4096 bytes src/lib/db/db.js | 8 +++----- src/lib/db/utils.js | 25 ++++++++++++++++--------- src/lib/form/Form.svelte | 22 ++++++++-------------- src/routes/admin/+page.server.js | 13 +++++++++++-- 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/lib/db/carteles_dev.sqlite b/src/lib/db/carteles_dev.sqlite index cb0d28e852f43641682dadf7b2bdfd3ac8724117..f7e68bce7177e18b47d4398bd58eb9bf52218d1b 100644 GIT binary patch delta 26 gcmZozz}TQLL7J73fq{W>Vxv8fp%*W?Nnj2?08$PG-T(jq literal 20480 zcmeI(O>5gQ7zc3KNs~3ux;L-n^4zvSJ19G?P^eSP2=kIQb!dAsYOS>fTh46t<*-Wz z`Di=t`|Pm8B$4dp5Oyu$FKkJVY_Oi6brHtrXHljEeJZPhD|*XznCr4*BF31j&kcPp zi;0`dK)23c{Z(C;y-9YrydSK&J!js?`@TK*Xw}UahX4d1009U<00Izz00bZafi)MH zy==7oZrAzRR{S~_r)8}~RWF+6$v6z+km8^pg|rx?*BzoPrNJ-`--ly59S=SR<16|Q zUeRPYIGcnt9K|%8MA4qPv*5Ra`q8LwA|;Ax&h@T04liRfAbIg8xGAL)QrU~0-hQ*? z2VI9{G8Iqt=Ui`=6MnBs8z+m|BnOMm#uumU`@S=~u!&4+orbO4f0D36HU&}eEVqyP zUkX!R*N)*Y))xF9<*Y0uX=z1Rwwb2tWV=5P*OcFyH?j?;Fzx z3Irek0SG_<0uX=z1Rwwb2tWV=&spG|(_*{+s}uX1gM1KGDr8w)S7N{7GA)Zc!P89M z9x9&aqS_;ssazb=O<8mE{onC^GJT*x00Izz00bZa0SG_<0uX=z1R$`60*%Jz<}d3l Bk~{za diff --git a/src/lib/db/db.js b/src/lib/db/db.js index dcaff01..370aa16 100644 --- a/src/lib/db/db.js +++ b/src/lib/db/db.js @@ -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 */ diff --git a/src/lib/db/utils.js b/src/lib/db/utils.js index de5ddb4..6f2797f 100644 --- a/src/lib/db/utils.js +++ b/src/lib/db/utils.js @@ -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 diff --git a/src/lib/form/Form.svelte b/src/lib/form/Form.svelte index 6b1906b..7d3a374 100644 --- a/src/lib/form/Form.svelte +++ b/src/lib/form/Form.svelte @@ -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); } }; diff --git a/src/routes/admin/+page.server.js b/src/routes/admin/+page.server.js index a297b03..793c3af 100644 --- a/src/routes/admin/+page.server.js +++ b/src/routes/admin/+page.server.js @@ -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 + } } }