@ -0,0 +1,45 @@
|
||||
import * as cookie from "cookie";
|
||||
|
||||
/** @type {import('@sveltejs/kit').Handle} */
|
||||
export const handle = async ({ event, resolve }) => {
|
||||
const cookieHeader = event.request.headers.get("cookie");
|
||||
const cookies = cookie.parse(cookieHeader ?? "");
|
||||
|
||||
if (!cookies.session) {
|
||||
return await resolve(event);
|
||||
}
|
||||
|
||||
// Esto es lo que hace pillar una sesión y que tengo que implementar con better-sqlite3
|
||||
// const session = await db.user.findUnique({
|
||||
// where: { id: cookies.session },
|
||||
// });
|
||||
|
||||
// if (session) {
|
||||
// event.locals.user = {
|
||||
// username: session.username,
|
||||
// name: session.name,
|
||||
// slug: session.slug,
|
||||
// isAdmin: session.isAdmin,
|
||||
// };
|
||||
// }
|
||||
|
||||
return await resolve(event);
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {*} param0
|
||||
* @returns
|
||||
*/
|
||||
export const getSession = ({ locals }) => {
|
||||
if (!locals.user) return {};
|
||||
|
||||
return {
|
||||
user: {
|
||||
username: locals.user.username,
|
||||
name: locals.user.name,
|
||||
slug: locals.user.slug,
|
||||
isAdmin: locals.user.isAdmin,
|
||||
},
|
||||
};
|
||||
};
|
@ -1,15 +1,15 @@
|
||||
/** @type {import('@sveltejs/kit').Handle} */
|
||||
export const handle = async ({ event, resolve }) => {
|
||||
let userid = event.cookies.get('userid');
|
||||
let userid = event.cookies.get('userid');
|
||||
|
||||
if (!userid) {
|
||||
// if this is the first time the user has visited this app,
|
||||
// set a cookie so that we recognise them when they return
|
||||
userid = crypto.randomUUID();
|
||||
event.cookies.set('userid', userid, { path: '/' });
|
||||
}
|
||||
if (!userid) {
|
||||
// if this is the first time the user has visited this app,
|
||||
// set a cookie so that we recognise them when they return
|
||||
userid = crypto.randomUUID();
|
||||
event.cookies.set('userid', userid, { path: '/' });
|
||||
}
|
||||
|
||||
event.locals.userid = userid;
|
||||
event.locals.userid = userid;
|
||||
|
||||
return resolve(event);
|
||||
return resolve(event);
|
||||
};
|
||||
|
@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @async
|
||||
* @function send
|
||||
* @param {HTMLFormElement} form
|
||||
* @returns {Promise<JSON>}
|
||||
*/
|
||||
export const send = async (form) => {
|
||||
const response = await fetch(form.action, {
|
||||
method: form.method,
|
||||
body: new FormData(form),
|
||||
headers: { accept: "application/json" },
|
||||
});
|
||||
return await response.json();
|
||||
};
|
Binary file not shown.
@ -0,0 +1,38 @@
|
||||
/** @typedef {typeof import('better-sqlite3')} better-sqlite3 */
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
export const db = new Database('./src/lib/db/carteles_dev.sqlite', { verbose: console.log });
|
||||
db.pragma("journal_mode = WAL");
|
||||
db.pragma("synchronous = normal");
|
||||
db.pragma("temp_store = memory");
|
||||
|
||||
const createUserTable = db.prepare(`CREATE TABLE IF NOT EXISTS Users (
|
||||
email TEXT PRIMARY KEY UNIQUE NOT NULL,
|
||||
password TEXT,
|
||||
isAdmin INTEGER
|
||||
);`);
|
||||
|
||||
const createPostersTable = db.prepare(`CREATE TABLE IF NOT EXISTS Posters (
|
||||
id INTEGER PRIMARY KEY UNIQUE NOT NULL,
|
||||
createdAt TEXT NOT NULL,
|
||||
image BLOB NOT NULL,
|
||||
content TEXT
|
||||
);`);
|
||||
|
||||
createUserTable.run();
|
||||
createPostersTable.run();
|
||||
|
||||
/**
|
||||
* @typedef {Object} User
|
||||
* @property {string} [email]
|
||||
* @property {string} [password]
|
||||
* @property {boolean} [isAdmin]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} Poster
|
||||
* @property {number} [id]
|
||||
* @property {string} [createdAt] Date
|
||||
* @property {Blob} [image]
|
||||
* @property {string} [content] Stringified contents
|
||||
*/
|
@ -0,0 +1,95 @@
|
||||
import { db } from '$lib/db/db.js';
|
||||
/**
|
||||
* Return of functions that change change DB, either 'success' or 'failure' with explanation
|
||||
* @typedef {Object} ChangeDBResult
|
||||
* @property {string} [error]
|
||||
* @property {string} [success]
|
||||
* @property {string} [id]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @function addUserToDB Adds user to the db
|
||||
* @param {string} email
|
||||
* @param {string} passwordHash
|
||||
* @param {boolean} isAdmin
|
||||
* @returns {ChangeDBResult} return
|
||||
*/
|
||||
export const addUserToDB = (email, passwordHash, isAdmin) => {
|
||||
if (!email || !passwordHash || !isAdmin) {
|
||||
return {
|
||||
error: "Either email, password or isAdmin are missing"
|
||||
};
|
||||
}
|
||||
const addUser = db.prepare(`INSERT INTO Users(email,password,isAdmin) VALUES(?,?,?);`)
|
||||
const result = addUser.run(email, passwordHash, isAdmin);
|
||||
if (result.changes === 1) {
|
||||
return {
|
||||
success: `User '${email}' added to DB`
|
||||
}
|
||||
}
|
||||
return { error: `Could not add user '${email}' to DB`, };
|
||||
}
|
||||
|
||||
/** Removes user from db
|
||||
* @param {string} email
|
||||
* @returns {ChangeDBResult} return
|
||||
*/
|
||||
export const delUserFromDB = (email) => {
|
||||
if (!email) {
|
||||
return {
|
||||
error: "Email not provided"
|
||||
}
|
||||
}
|
||||
const delUser = db.prepare(`DELETE FROM Users WHERE email=${email};`);
|
||||
const result = delUser.run();
|
||||
if (result.changes === 1) {
|
||||
return {
|
||||
success: `User '${email}' removed successfully`
|
||||
}
|
||||
}
|
||||
return { error: `Could not remove user '${email}' from DB` }
|
||||
}
|
||||
|
||||
/**
|
||||
* @function addPosterToDB
|
||||
* @param {any} image
|
||||
* @param {string} content
|
||||
* @returns {ChangeDBResult} return
|
||||
*/
|
||||
export const addPosterToDB = (image, content) => {
|
||||
|
||||
const id = crypto.randomUUID(); // Create random ID for Poster
|
||||
console.log("New id is: ", id);
|
||||
|
||||
const createdAt = Date.now().toString;
|
||||
|
||||
if (!image) { // Return error if mandatory info is missing
|
||||
return {
|
||||
error: "Image missing"
|
||||
}
|
||||
}
|
||||
|
||||
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}`,
|
||||
id
|
||||
}
|
||||
}
|
||||
return { // Default return just in case
|
||||
error: `Could not save poster '${id}' to DB. Something went wrong`
|
||||
}
|
||||
}
|
||||
|
||||
export const getAllPostersFromDB = () => {
|
||||
const getPosters = db.prepare(`SELECT * FROM Posters;`);
|
||||
const result = getPosters.all();
|
||||
return result;
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<script>
|
||||
/** @type {import('./$types').PageData} */
|
||||
export let data;
|
||||
|
||||
$: posters = data.posters;
|
||||
|
||||
/** @type {Blob} */ let image = new Blob();
|
||||
/** @type {string} */ let template = '';
|
||||
/** @type {string} */ let content = '';
|
||||
</script>
|
||||
|
||||
<h1>Test</h1>
|
||||
<ul>
|
||||
{#each posters as poster}
|
||||
<li>{poster.name}</li>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
<form method="POST">
|
||||
<input name="template" bind:value={template} placeholder="Plantilla" type="text" />
|
||||
<input name="content" bind:value={content} placeholder="Contenido" type="text" />
|
||||
<input name="image" bind:value={image} type="file" />
|
||||
<button>Send</button>
|
||||
</form>
|
||||
|
||||
<style>
|
||||
input {
|
||||
display: block;
|
||||
margin: 1rem 0;
|
||||
}
|
||||
</style>
|
Binary file not shown.
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 3.3 MiB |
Loading…
Reference in New Issue