En esta lección se muestra el almacenamiento de un archivo por registro de la base de datos, usando servicios.
Puedes probar el ejemplo en http://srvarchivos.rf.gd/.
Este ejemplo toma como base la lección de asociaciones a uno, pero en caso de necesitar varios archivos por producto, puedes tomar como base la lección de asociaciones a muchos.
Prueba el ejemplo en http://srvarchivos.rf.gd/.
Descarga el archivo /src/srvarchivos.zip y descompáctalo.
Crea una cuenta de email pqra ti, por ejemplo, pepito@google.com. Si ya tienes un email, omite este paso.
Crea una cuenta de GitHub usando el email anterior y selecciona el nombre de usuario unsando la parte inicial del correo electrónico, por ejemplo pepito. Si ya tienes una cuenta, omite este paso.
Crea un repositorio nuevo. En el nombre del repositorio debes poner el nombre de tu sitio; por ejemplo devuelvejson
Edita los archivos que desees.
Prueba tu sitio localmente.
Necesitas un hosting. En este ejemplo se muestra como usar el hosting. https://infinityfree.com/ Si no lo has usado, lo primero que tienes que hacer es entrar a registrar tu email con el botón Registrar. Si ya tienes tu email registrado, omite este paso.
Crea una cuenta. Si ya tienes cuenta, entra a ella y crea un nuevo domino. En este ejemplo no se crean los archivos directamente en el hosting.
Sube tus archivos a GitHub. En este ejemplo no hay archivo sw.js ni necesitas esperar 11 o más minutos.
Prueba el ejemplo en http://srvarchivos.rf.gd/.
Descarga el archivo /src/srvarchivos.zip y descompáctalo.
Crea tu proyecto en GitHub:
Crea una cuenta de email para tí, por ejemplo, pepito@google.com. Si ya tienes un email, omite este paso.
Crea una cuenta de GitHub usando el email anterior y selecciona el nombre de usuario unsando la parte inicial del correo electrónico, por ejemplo pepito. Si ya tienes una cuenta, omite este paso.
Crea un repositorio nuevo. En la página principal de GitHub cliquea 📘 New.
En la página Create a new repository introduce los siguientes datos:
Proporciona el nombre de tu repositorio debajo de donde dice Repository name *.
Mantén la selección Public para que otros usuarios puedan ver tu proyecto.
Verifica la casilla Add a README file. En este archivo se muestra información sobre tu proyecto.
Cliquea License: None. y selecciona la licencia que consideres más adecuada para tu proyecto.
Cliquea Create repository.
Importa el proyecto en GitHub:
En la página principal de tu proyecto en GitHub, en la pestaña < > Code, cliquea < > Code y en la sección Branches y copia la dirección que está en HTTPS, debajo de Clone.
En Visual Studio Code, usa el botón de la izquierda para Source Control.
Cliquea el botón Clone Repository.
Pega la url que copiaste anteriormente hasta arriba, donde dice algo como Provide repository URL y presiona la teclea Intro.
Selecciona la carpeta donde se guardará la carpeta del proyecto.
Abre la carpeta del proyecto importado.
Añade el contenido de la carpeta descompactada que contiene el código del ejemplo.
Edita los archivos que desees.
Haz clic derecho en index.html
, selecciona
PHP Server: serve project y se abre el navegador para que puedas
probar localmente el ejemplo.
Para depurar paso a paso haz lo siguiente:
En el navegador, haz clic derecho en la página que deseas depurar y selecciona inspeccionar.
Recarga la página, de preferencia haciendo clic derecho en el ícono de
volver a cargar la página
y
seleccionando vaciar caché y volver a cargar de manera forzada (o
algo parecido). Si no aparece un menú emergente, simplemente cliquea
volver a cargar la página
.
Revisa que no aparezca ningún error ni en la pestañas Consola, ni
en Red.
Selecciona la pestaña Fuentes (o Sources si tu navegador está en Inglés).
Selecciona el archivo donde vas a empezar a depurar.
Haz clic en el número de la línea donde vas a empezar a depurar.
En Visual Studio Code, abre el archivo de PHP donde vas a empezar a depurar.
Haz clic en Run and Debug
.
Si no está configurada la depuración, haz clic en create a launch json file.
Haz clic en la flechita RUN AND DEBUG, al lado de la cual debe
decir Listen for Xdebug
.
Aparece un cuadro con los controles de depuración
Selecciona otra vez el archivo de PHP y haz clic en el número de la línea donde vas a empezar a depurar.
Regresa al navegador, recarga la página y empieza a usarla.
Si se ejecuta alguna de las líneas de código seleccionadas, aparece resaltada en la pestaña de fuentes. Usa los controles de depuración para avanzar, como se muestra en este video.
Sube el proyecto al hosting que elijas.
Crea una nueva carpeta para crear un nuevo proyecto que estará conectado directamente al servidor web por ftp.
Abre la nueva carpeta con Visual Studio Code.
Tecle al mismo Mayúsculas+Control+P y selecciona SFTP: Config. Aparece un archivo de configuración de FTP. Llena los datos con la configuración de FTP de tu servidor, excepto la contraseña.
Cliquea el botón de SFTP y luego haz clic en la URL de tu servidos. En la barra superior te pide la contraseña y ENTER.
Pásate a la parte de archivos y coloca tus archivos.
Cliquea con el botón derecho en la sección de archivos y selecciona Sync: Local -> Remote.
Abre un navegador y prueba el proyecto en tu hosting.
En el hosting InfinityFree, la primera vez que corres la página, puede marcar un mensaje de error, pero al recargar funciona correctamente. Puedes evitar este problema usando un dominio propio.
Para subir el código a GitHub, en la sección de SOURCE CONTROL, en Message introduce un mensaje sobre los cambios que hiciste, por ejemplo index.html corregido, selecciona v y luego Commit & Push.
Haz clic en los triángulos para expandir las carpetas
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Archivos</title> |
| 10 | |
| 11 | <script type="module" src="js/lib/manejaErrores.js"></script> |
| 12 | |
| 13 | </head> |
| 14 | |
| 15 | <body> |
| 16 | |
| 17 | <h1>Archivos</h1> |
| 18 | |
| 19 | <p><a href="agrega.html">Agregar</a></p> |
| 20 | |
| 21 | <dl id="lista"> |
| 22 | <dt>Cargando…</dt> |
| 23 | <dd><progress max="100">Cargando…</progress></dd> |
| 24 | </dl> |
| 25 | |
| 26 | <script type="module"> |
| 27 | |
| 28 | import { descargaVista } from "./js/lib/descargaVista.js" |
| 29 | |
| 30 | descargaVista("php/producto-vista-index.php") |
| 31 | |
| 32 | </script> |
| 33 | |
| 34 | </body> |
| 35 | |
| 36 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Agregar</title> |
| 10 | |
| 11 | <script type="module" src="js/lib/manejaErrores.js"></script> |
| 12 | |
| 13 | </head> |
| 14 | |
| 15 | <body> |
| 16 | |
| 17 | <form id="formulario""> |
| 18 | |
| 19 | <h1>Agregar</h1> |
| 20 | |
| 21 | <p><a href=" index.html">Cancelar</a></p> |
| 22 | |
| 23 | <p> |
| 24 | <label> |
| 25 | Nombre * |
| 26 | <input name="nombre"> |
| 27 | </label> |
| 28 | </p> |
| 29 | |
| 30 | <p> |
| 31 | <label> |
| 32 | Imagen * |
| 33 | <input name="imagen" type="file" accept="image/*"> |
| 34 | </label> |
| 35 | </p> |
| 36 | |
| 37 | <p>* Obligatorio</p> |
| 38 | |
| 39 | <p><button type="submit">Agregar</button></p> |
| 40 | |
| 41 | <figure> |
| 42 | <img id="preview" hidden alt="Imagen del producto" style="max-width: 100%;"> |
| 43 | </figure> |
| 44 | |
| 45 | </form> |
| 46 | |
| 47 | <script type="module"> |
| 48 | |
| 49 | import { submitAccion } from "./js/lib/submitAccion.js" |
| 50 | import { |
| 51 | muestraImagenSeleccionada |
| 52 | } from "./js/lib/muestraImagenSeleccionada.js" |
| 53 | |
| 54 | formulario.addEventListener( |
| 55 | "submit", |
| 56 | event => submitAccion( |
| 57 | event, "php/producto-agrega.php", formulario, "index.html" |
| 58 | ) |
| 59 | ) |
| 60 | |
| 61 | formulario.imagen.addEventListener( |
| 62 | "input", |
| 63 | () => muestraImagenSeleccionada(formulario.imagen, preview) |
| 64 | ) |
| 65 | |
| 66 | muestraImagenSeleccionada(formulario.imagen, preview) |
| 67 | |
| 68 | </script> |
| 69 | </body> |
| 70 | |
| 71 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Modificar</title> |
| 10 | |
| 11 | <script type="module" src="js/lib/manejaErrores.js"></script> |
| 12 | |
| 13 | </head> |
| 14 | |
| 15 | <body> |
| 16 | |
| 17 | <form id="formulario"> |
| 18 | |
| 19 | <h1>Modificar</h1> |
| 20 | |
| 21 | <p><a href="index.html">Cancelar</a></p> |
| 22 | |
| 23 | <input type="hidden" name="id"> |
| 24 | |
| 25 | <p> |
| 26 | <label> |
| 27 | Nombre * |
| 28 | <input name="nombre" value="Cargando…"> |
| 29 | </label> |
| 30 | </p> |
| 31 | |
| 32 | <p> |
| 33 | <label> |
| 34 | Imagen |
| 35 | <input name="imagen" type="file" accept="image/*"> |
| 36 | </label> |
| 37 | </p> |
| 38 | |
| 39 | <p>* Obligatorio</p> |
| 40 | |
| 41 | <p> |
| 42 | |
| 43 | <button type="submit">Guardar</button> |
| 44 | |
| 45 | <button id="botonEliminar" type="button"> |
| 46 | Eliminar |
| 47 | </button> |
| 48 | |
| 49 | </p> |
| 50 | |
| 51 | <figure> |
| 52 | <img id="preview" hidden alt="Imagen del producto" style="max-width: 100%;"> |
| 53 | </figure> |
| 54 | |
| 55 | </form> |
| 56 | |
| 57 | <script type="module"> |
| 58 | |
| 59 | import { descargaVista } from "./js/lib/descargaVista.js" |
| 60 | import { submitAccion } from "./js/lib/submitAccion.js" |
| 61 | import { accionElimina } from "./js/lib/accionElimina.js" |
| 62 | import { |
| 63 | muestraImagenSeleccionada |
| 64 | } from "./js/lib/muestraImagenSeleccionada.js" |
| 65 | |
| 66 | const params = new URL(location.href).searchParams |
| 67 | descargaDatos() |
| 68 | |
| 69 | async function descargaDatos() { |
| 70 | |
| 71 | if (params.size > 0) { |
| 72 | |
| 73 | await descargaVista("php/producto-vista-modifica.php?" + params) |
| 74 | muestraImagenSeleccionada(formulario.imagen, preview) |
| 75 | |
| 76 | formulario.addEventListener( |
| 77 | "submit", |
| 78 | event => submitAccion( |
| 79 | event, "php/producto-modifica.php", formulario, "index.html" |
| 80 | ) |
| 81 | ) |
| 82 | |
| 83 | formulario.imagen.addEventListener( |
| 84 | "input", |
| 85 | () => muestraImagenSeleccionada(formulario.imagen, preview) |
| 86 | ) |
| 87 | |
| 88 | botonEliminar.addEventListener( |
| 89 | "click", |
| 90 | () => accionElimina( |
| 91 | "php/producto-elimina.php", |
| 92 | formulario, |
| 93 | "Confirma la eliminación", |
| 94 | "index.html" |
| 95 | ) |
| 96 | ) |
| 97 | |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | </script> |
| 103 | |
| 104 | </body> |
| 105 | |
| 106 | </html> |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/lib/recibeEnteroObligatorio.php"; |
| 4 | require_once __DIR__ . "/lib/validaEntidadObligatoria.php"; |
| 5 | require_once __DIR__ . "/Bd.php"; |
| 6 | |
| 7 | // Evita que la imagen se cargue en el caché del navegador. |
| 8 | header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); |
| 9 | header("Cache-Control: post-check=0, pre-check=0", false); |
| 10 | header("Pragma: no-cache"); |
| 11 | |
| 12 | $archId = recibeEnteroObligatorio("id"); |
| 13 | |
| 14 | $bd = Bd::pdo(); |
| 15 | |
| 16 | $stmt = $bd->prepare("SELECT * FROM ARCHIVO WHERE ARCH_ID = :ARCH_ID"); |
| 17 | $stmt->execute([":ARCH_ID" => $archId]); |
| 18 | $archivo = $stmt->fetch(PDO::FETCH_ASSOC); |
| 19 | |
| 20 | $archivo = validaEntidadObligatoria("Archivo", $archivo); |
| 21 | |
| 22 | $bytes = $archivo["ARCH_BYTES"]; |
| 23 | $contentType = (new finfo(FILEINFO_MIME_TYPE))->buffer($bytes); |
| 24 | header("Content-Type: $contentType"); |
| 25 | echo $bytes; |
| 26 |
| 1 | <?php |
| 2 | |
| 3 | function archivoAgrega(PDO $bd, string $bytes) |
| 4 | { |
| 5 | $stmt = $bd->prepare( |
| 6 | "INSERT INTO ARCHIVO ( |
| 7 | ARCH_BYTES |
| 8 | ) values ( |
| 9 | :ARCH_BYTES |
| 10 | )" |
| 11 | ); |
| 12 | $stmt->execute([ |
| 13 | ":ARCH_BYTES" => $bytes |
| 14 | ]); |
| 15 | $archId = $bd->lastInsertId(); |
| 16 | return $archId; |
| 17 | } |
| 18 |
| 1 | <?php |
| 2 | |
| 3 | class Bd |
| 4 | { |
| 5 | private static ?PDO $pdo = null; |
| 6 | |
| 7 | static function pdo(): PDO |
| 8 | { |
| 9 | if (self::$pdo === null) { |
| 10 | |
| 11 | self::$pdo = new PDO( |
| 12 | // cadena de conexión |
| 13 | "sqlite:" . __DIR__ . "/srvarchivos.db", |
| 14 | // usuario |
| 15 | null, |
| 16 | // contraseña |
| 17 | null, |
| 18 | // Opciones: pdos no persistentes y lanza excepciones. |
| 19 | [PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
| 20 | ); |
| 21 | |
| 22 | self::$pdo->exec( |
| 23 | 'CREATE TABLE IF NOT EXISTS ARCHIVO ( |
| 24 | ARCH_ID INTEGER, |
| 25 | ARCH_BYTES BLOB NOT NULL, |
| 26 | CONSTRAINT ARCH_PK |
| 27 | PRIMARY KEY(ARCH_ID) |
| 28 | )' |
| 29 | ); |
| 30 | self::$pdo->exec( |
| 31 | 'CREATE TABLE IF NOT EXISTS PRODUCTO ( |
| 32 | PROD_ID INTEGER, |
| 33 | PROD_NOMBRE TEXT NOT NULL, |
| 34 | ARCH_ID INTEGER NOT NULL, |
| 35 | CONSTRAINT PROD_PK |
| 36 | PRIMARY KEY(PROD_ID), |
| 37 | CONSTRAINT PROD_NOM_UNQ |
| 38 | UNIQUE(PROD_NOMBRE), |
| 39 | CONSTRAINT PROD_NOM_NV |
| 40 | CHECK(LENGTH(PROD_NOMBRE) > 0), |
| 41 | CONSTRAINT PROD_ARCH_FK |
| 42 | FOREIGN KEY (ARCH_ID) REFERENCES ARCHIVO(ARCH_ID) |
| 43 | )' |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | return self::$pdo; |
| 48 | } |
| 49 | } |
| 50 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/lib/manejaErrores.php"; |
| 4 | require_once __DIR__ . "/lib/recibeTextoObligatorio.php"; |
| 5 | require_once __DIR__ . "/lib/recibeBytesObligatorios.php"; |
| 6 | require_once __DIR__ . "/lib/devuelveCreated.php"; |
| 7 | require_once __DIR__ . "/Bd.php"; |
| 8 | require_once __DIR__ . "/archivoAgrega.php"; |
| 9 | |
| 10 | $nombre = recibeTextoObligatorio("nombre"); |
| 11 | $bytes = recibeBytesObligatorios("imagen"); |
| 12 | |
| 13 | $bd = Bd::pdo(); |
| 14 | $bd->beginTransaction(); |
| 15 | |
| 16 | $archId = archivoAgrega($bd, $bytes); |
| 17 | |
| 18 | $stmt = $bd->prepare( |
| 19 | "INSERT INTO PRODUCTO ( |
| 20 | PROD_NOMBRE, ARCH_ID |
| 21 | ) values ( |
| 22 | :PROD_NOMBRE, :ARCH_ID |
| 23 | )" |
| 24 | ); |
| 25 | $stmt->execute([ |
| 26 | ":PROD_NOMBRE" => $nombre, |
| 27 | ":ARCH_ID" => $archId |
| 28 | ]); |
| 29 | $prodId = $bd->lastInsertId(); |
| 30 | |
| 31 | $bd->commit(); |
| 32 | |
| 33 | $encodeId = urlencode($prodId); |
| 34 | $encodeArchId = urlencode($archId); |
| 35 | $htmlEncodeArchId = htmlentities($encodeArchId); |
| 36 | // Los bytes de las imágenes se descargan con "archivo.php"; no desde aquí. |
| 37 | devuelveCreated("/php/producto-vista-modifica.php?id=$encodeId", [ |
| 38 | "id" => ["value" => $prodId], |
| 39 | "nombre" => ["value" => $nombre], |
| 40 | "imagen" => [ |
| 41 | "imagen" => [ |
| 42 | "data-src" => $htmlEncodeArchId === "" |
| 43 | ? "" |
| 44 | : "php/archivo.php?id=$htmlEncodeArchId" |
| 45 | ] |
| 46 | ] |
| 47 | ]); |
| 48 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/lib/manejaErrores.php"; |
| 4 | require_once __DIR__ . "/lib/recibeEnteroObligatorio.php"; |
| 5 | require_once __DIR__ . "/lib/devuelveNoContent.php"; |
| 6 | require_once __DIR__ . "/Bd.php"; |
| 7 | require_once __DIR__ . "/productoBusca.php"; |
| 8 | |
| 9 | $prodId = recibeEnteroObligatorio("id"); |
| 10 | |
| 11 | $bd = Bd::pdo(); |
| 12 | $bd->beginTransaction(); |
| 13 | |
| 14 | $producto = productoBusca($bd, $prodId); |
| 15 | if ($producto !== false) { |
| 16 | |
| 17 | $stmt = $bd->prepare("DELETE FROM PRODUCTO WHERE PROD_ID = :PROD_ID"); |
| 18 | $stmt->execute([":PROD_ID" => $prodId]); |
| 19 | |
| 20 | if ($producto["ARCH_ID"] !== null) { |
| 21 | $stmt = $bd->prepare("DELETE FROM ARCHIVO WHERE ARCH_ID = :ARCH_ID"); |
| 22 | $stmt->execute([":ARCH_ID" => $producto["ARCH_ID"]]); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | $bd->commit(); |
| 27 | |
| 28 | devuelveNoContent(); |
| 29 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/lib/manejaErrores.php"; |
| 4 | require_once __DIR__ . "/lib/recibeEnteroObligatorio.php"; |
| 5 | require_once __DIR__ . "/lib/recibeTextoObligatorio.php"; |
| 6 | require_once __DIR__ . "/lib/recibeBytesOpcionales.php"; |
| 7 | require_once __DIR__ . "/lib/validaEntidadObligatoria.php"; |
| 8 | require_once __DIR__ . "/lib/devuelveJson.php"; |
| 9 | require_once __DIR__ . "/Bd.php"; |
| 10 | require_once __DIR__ . "/productoBusca.php"; |
| 11 | |
| 12 | $prodId = recibeEnteroObligatorio("id"); |
| 13 | $nombre = recibeTextoObligatorio("nombre"); |
| 14 | $bytes = recibeBytesOpcionales("imagen"); |
| 15 | |
| 16 | $bd = Bd::pdo(); |
| 17 | $bd->beginTransaction(); |
| 18 | |
| 19 | $producto = productoBusca($bd, $prodId); |
| 20 | $producto = validaEntidadObligatoria("Producto", $producto); |
| 21 | |
| 22 | $archId = $producto["ARCH_ID"]; |
| 23 | |
| 24 | if ($bytes !== "") { |
| 25 | if ($archId === null) { |
| 26 | $archId = archivoAgrega($bd, $bytes); |
| 27 | } else { |
| 28 | $stmt = $bd->prepare( |
| 29 | "UPDATE ARCHIVO |
| 30 | SET |
| 31 | ARCH_BYTES = :ARCH_BYTES |
| 32 | WHERE |
| 33 | ARCH_ID = :ARCH_ID" |
| 34 | ); |
| 35 | $stmt->execute([ |
| 36 | ":ARCH_BYTES" => $bytes, |
| 37 | ":ARCH_ID" => $archId, |
| 38 | ]); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | $stmt = $bd->prepare( |
| 43 | "UPDATE PRODUCTO |
| 44 | SET |
| 45 | PROD_NOMBRE = :PROD_NOMBRE, |
| 46 | ARCH_ID = :ARCH_ID |
| 47 | WHERE |
| 48 | PROD_ID = :PROD_ID" |
| 49 | ); |
| 50 | $stmt->execute([ |
| 51 | ":PROD_NOMBRE" => $nombre, |
| 52 | ":ARCH_ID" => $archId, |
| 53 | ":PROD_ID" => $prodId, |
| 54 | ]); |
| 55 | |
| 56 | $bd->commit(); |
| 57 | |
| 58 | $encodeArchId = $archId === null ? "" : urlencode($archId); |
| 59 | $htmlEncodeArchId = htmlentities($encodeArchId); |
| 60 | // Los bytes de las imágenes se descargan con "archivo.php"; no desde aquí. |
| 61 | devuelveJson([ |
| 62 | "id" => ["value" => $prodId], |
| 63 | "nombre" => ["value" => $nombre], |
| 64 | "imagen" => [ |
| 65 | "data-src" => $htmlEncodeArchId === "" |
| 66 | ? "" |
| 67 | : "php/archivo.php?id=$htmlEncodeArchId" |
| 68 | ] |
| 69 | ]); |
| 70 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/lib/manejaErrores.php"; |
| 4 | require_once __DIR__ . "/lib/devuelveJson.php"; |
| 5 | require_once __DIR__ . "/Bd.php"; |
| 6 | |
| 7 | $bd = Bd::pdo(); |
| 8 | $stmt = $bd->query("SELECT * FROM PRODUCTO ORDER BY PROD_NOMBRE"); |
| 9 | $lista = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 10 | |
| 11 | $render = ""; |
| 12 | foreach ($lista as $modelo) { |
| 13 | $prodId = htmlentities($modelo["PROD_ID"]); |
| 14 | $prodNombre = htmlentities($modelo["PROD_NOMBRE"]); |
| 15 | $encodeArchId = |
| 16 | $modelo["ARCH_ID"] === null ? "" : urlencode($modelo["ARCH_ID"]); |
| 17 | $archId = $encodeArchId === "" ? "" : htmlentities($encodeArchId); |
| 18 | $src = $archId === "" ? "" : "php/archivo.php?id=$archId"; |
| 19 | // Los bytes de las imágenes se descargan con "archivo.php"; no desde aquí. |
| 20 | $render .= |
| 21 | "<div style='display: flex; flex-direction: row-reverse; |
| 22 | align-items: center; gap: 0.5rem'> |
| 23 | <dt style='flex: 1 1 0'> |
| 24 | <a href='modifica.html?id=$prodId'>$prodNombre</a> |
| 25 | </dt> |
| 26 | <dd style='flex: 1 1 0; margin: 0'> |
| 27 | <a href='modifica.html?id=$prodId'><img |
| 28 | style='width: 100%; aspect-ratio:16/9; object-fit: cover' |
| 29 | alt='Imagen del producto' src='$src'></a> |
| 30 | </dd> |
| 31 | </div>"; |
| 32 | } |
| 33 | |
| 34 | devuelveJson(["lista" => ["innerHTML" => $render]]); |
| 35 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/lib/manejaErrores.php"; |
| 4 | require_once __DIR__ . "/lib/recibeEnteroObligatorio.php"; |
| 5 | require_once __DIR__ . "/lib/validaEntidadObligatoria.php"; |
| 6 | require_once __DIR__ . "/lib/devuelveJson.php"; |
| 7 | require_once __DIR__ . "/Bd.php"; |
| 8 | require_once __DIR__ . "/productoBusca.php"; |
| 9 | |
| 10 | $prodId = recibeEnteroObligatorio("id"); |
| 11 | |
| 12 | $bd = Bd::pdo(); |
| 13 | $modelo = productoBusca($bd, $prodId); |
| 14 | |
| 15 | $modelo = validaEntidadObligatoria("Producto", $modelo); |
| 16 | |
| 17 | $encodeArchId = |
| 18 | $modelo["ARCH_ID"] === null ? "" : urlencode($modelo["ARCH_ID"]); |
| 19 | $htmlEncodeArchId = htmlentities($encodeArchId); |
| 20 | // Los bytes de las imágenes se descargan con "archivo.php"; no desde aquí. |
| 21 | devuelveJson([ |
| 22 | "id" => ["value" => $prodId], |
| 23 | "nombre" => ["value" => $modelo["PROD_NOMBRE"]], |
| 24 | "imagen" => [ |
| 25 | "data-src" => $htmlEncodeArchId === "" |
| 26 | ? "" |
| 27 | : "php/archivo.php?id=$htmlEncodeArchId" |
| 28 | ] |
| 29 | ]); |
| 30 |
| 1 | <?php |
| 2 | |
| 3 | function productoBusca(PDO $bd, int $prodId) |
| 4 | { |
| 5 | $stmt = $bd->prepare("SELECT * FROM PRODUCTO WHERE PROD_ID = :PROD_ID"); |
| 6 | $stmt->execute([":PROD_ID" => $prodId]); |
| 7 | $modelo = $stmt->fetch(PDO::FETCH_ASSOC); |
| 8 | return $modelo; |
| 9 | } |
| 10 |
| 1 | <?php |
| 2 | |
| 3 | const BAD_REQUEST = 400; |
| 4 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
| 4 | |
| 5 | function devuelveCreated($urlDelNuevo, $resultado) |
| 6 | { |
| 7 | $json = json_encode($resultado); |
| 8 | if ($json === false) { |
| 9 | devuelveResultadoNoJson(); |
| 10 | } else { |
| 11 | http_response_code(201); |
| 12 | header("Location: $urlDelNuevo"); |
| 13 | header("Content-Type: application/json; charset=utf-8"); |
| 14 | echo $json; |
| 15 | } |
| 16 | } |
| 17 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/devuelveResultadoNoJson.php"; |
| 4 | |
| 5 | function devuelveJson($resultado) |
| 6 | { |
| 7 | $json = json_encode($resultado); |
| 8 | if ($json === false) { |
| 9 | devuelveResultadoNoJson(); |
| 10 | } else { |
| 11 | header("Content-Type: application/json; charset=utf-8"); |
| 12 | echo $json; |
| 13 | } |
| 14 | exit(); |
| 15 | } |
| 16 |
| 1 | <?php |
| 2 | |
| 3 | function devuelveNoContent() |
| 4 | { |
| 5 | http_response_code(204); |
| 6 | } |
| 7 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | |
| 5 | function devuelveResultadoNoJson() |
| 6 | { |
| 7 | http_response_code(INTERNAL_SERVER_ERROR); |
| 8 | header("Content-Type: application/problem+json; charset=utf-8"); |
| 9 | |
| 10 | echo '{' . |
| 11 | "status: " . INTERNAL_SERVER_ERROR . |
| 12 | '"title": "El resultado no puede representarse como JSON."' . |
| 13 | '"type": "/errors/resultadonojson.html"' . |
| 14 | '}'; |
| 15 | } |
| 16 |
| 1 | <?php |
| 2 | |
| 3 | const INTERNAL_SERVER_ERROR = 500; |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 5 | |
| 6 | // Hace que se lance una excepción automáticamente cuando se genere un error. |
| 7 | set_error_handler(function ($severity, $message, $file, $line) { |
| 8 | throw new ErrorException($message, 0, $severity, $file, $line); |
| 9 | }); |
| 10 | |
| 11 | // Código cuando una excepción no es atrapada. |
| 12 | set_exception_handler(function (Throwable $excepcion) { |
| 13 | if ($excepcion instanceof ProblemDetailsException) { |
| 14 | devuelveProblemDetails($excepcion->problemDetails); |
| 15 | } else { |
| 16 | devuelveProblemDetails([ |
| 17 | "status" => INTERNAL_SERVER_ERROR, |
| 18 | "title" => "Error interno del servidor", |
| 19 | "detail" => $excepcion->getMessage(), |
| 20 | "type" => "/errors/errorinterno.html", |
| 21 | ]); |
| 22 | } |
| 23 | exit(); |
| 24 | }); |
| 25 | |
| 26 | function devuelveProblemDetails(array $array) |
| 27 | { |
| 28 | $json = json_encode($array); |
| 29 | if ($json === false) { |
| 30 | devuelveResultadoNoJson(); |
| 31 | } else { |
| 32 | http_response_code(isset($array["status"]) ? $array["status"] : 500); |
| 33 | header("Content-Type: application/problem+json; charset=utf-8"); |
| 34 | echo $json; |
| 35 | } |
| 36 | } |
| 37 |
| 1 | <?php |
| 2 | |
| 3 | const NOT_FOUND = 404; |
| 4 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | |
| 5 | /** |
| 6 | * Detalle de los errores devueltos por un servicio. |
| 7 | */ |
| 8 | class ProblemDetailsException extends Exception |
| 9 | { |
| 10 | |
| 11 | public array $problemDetails; |
| 12 | |
| 13 | public function __construct( |
| 14 | array $problemDetails, |
| 15 | ) { |
| 16 | |
| 17 | parent::__construct( |
| 18 | isset($problemDetails["detail"]) |
| 19 | ? $problemDetails["detail"] |
| 20 | : (isset($problemDetails["title"]) |
| 21 | ? $problemDetails["title"] |
| 22 | : "Error"), |
| 23 | $problemDetails["status"] |
| 24 | ? $problemDetails["status"] |
| 25 | : INTERNAL_SERVER_ERROR |
| 26 | ); |
| 27 | |
| 28 | $this->problemDetails = $problemDetails; |
| 29 | } |
| 30 | } |
| 31 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/INTERNAL_SERVER_ERROR.php"; |
| 4 | require_once __DIR__ . "/BAD_REQUEST.php"; |
| 5 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 6 | |
| 7 | function recibeBytes(string $parametro) |
| 8 | { |
| 9 | if (isset($_FILES[$parametro])) { |
| 10 | |
| 11 | $path = $_FILES[$parametro]["tmp_name"]; |
| 12 | |
| 13 | if ($path === "") { |
| 14 | |
| 15 | return ""; |
| 16 | } elseif (is_uploaded_file($path)) { |
| 17 | |
| 18 | $contents = file_get_contents($path); |
| 19 | |
| 20 | if ($contents === false) { |
| 21 | |
| 22 | switch ($_FILES[$parametro]['error']) { |
| 23 | |
| 24 | case UPLOAD_ERR_OK: |
| 25 | |
| 26 | return $contents; |
| 27 | |
| 28 | case UPLOAD_ERR_INI_SIZE: |
| 29 | case UPLOAD_ERR_FORM_SIZE: |
| 30 | |
| 31 | throw new ProblemDetailsException([ |
| 32 | "status" => BAD_REQUEST, |
| 33 | "title" => "Archivo demasiado largo.", |
| 34 | "type" => "/errors/archivodemasiadolargo.html", |
| 35 | "detail" => "El archivo " - $parametro . |
| 36 | " excede el tamaño máximo que el servidor puede recibir." |
| 37 | ]); |
| 38 | |
| 39 | case UPLOAD_ERR_PARTIAL: |
| 40 | throw new ProblemDetailsException([ |
| 41 | "status" => INTERNAL_SERVER_ERROR, |
| 42 | "title" => "Carga incompleta de archivo.", |
| 43 | "type" => "/errors/archivocargaincompleta.html", |
| 44 | "detail" => "Por una razón desconocida, el archivo " - $parametro . |
| 45 | " no se cargó completamente." |
| 46 | ]); |
| 47 | |
| 48 | case UPLOAD_ERR_NO_FILE: |
| 49 | |
| 50 | throw creaArchivoNoRecibido($parametro); |
| 51 | |
| 52 | case UPLOAD_ERR_NO_TMP_DIR: |
| 53 | |
| 54 | throw new ProblemDetailsException([ |
| 55 | "status" => INTERNAL_SERVER_ERROR, |
| 56 | "title" => "Falta la carpeta temporal.", |
| 57 | "type" => "/errors/faltacarpetatemporal.html", |
| 58 | "detail" => "Por una razón desconocida, falta la carpeta temporal " . |
| 59 | "para cargar el archivo $parametro." |
| 60 | ]); |
| 61 | |
| 62 | case UPLOAD_ERR_CANT_WRITE: |
| 63 | |
| 64 | throw new ProblemDetailsException([ |
| 65 | "status" => INTERNAL_SERVER_ERROR, |
| 66 | "title" => "El archivo no se guardó.", |
| 67 | "type" => "/errors/archivonoguardado.html", |
| 68 | "detail" => "Por una razón desconocida, el archivo " - $parametro . |
| 69 | " no se pudo guardar en disco." |
| 70 | ]); |
| 71 | |
| 72 | case UPLOAD_ERR_EXTENSION: |
| 73 | |
| 74 | throw new ProblemDetailsException([ |
| 75 | "status" => BAD_REQUEST, |
| 76 | "title" => "Extensión no permitida.", |
| 77 | "type" => "/errors/extensionprohibida.html", |
| 78 | "detail" => "La extensión del archivo " - $parametro . |
| 79 | " no está permitida en el servidor." |
| 80 | ]); |
| 81 | |
| 82 | default: |
| 83 | |
| 84 | throw new ProblemDetailsException([ |
| 85 | "status" => INTERNAL_SERVER_ERROR, |
| 86 | "title" => "Error no identificado recuperando el archivo.", |
| 87 | "type" => "/errors/errorrecuperandoarchivo.html", |
| 88 | "detail" => "Por una razón desconocida, el archivo " - $parametro . |
| 89 | " no se pudo recuperar." |
| 90 | ]); |
| 91 | } |
| 92 | } else { |
| 93 | |
| 94 | return $contents; |
| 95 | } |
| 96 | } else { |
| 97 | |
| 98 | throw creaArchivoNoRecibido($parametro); |
| 99 | } |
| 100 | } else { |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function creaArchivoNoRecibido(string $parametro) |
| 106 | { |
| 107 | return new ProblemDetailsException([ |
| 108 | "status" => BAD_REQUEST, |
| 109 | "title" => "Archivo no recibido.", |
| 110 | "type" => "/errors/archivonorecibido.html", |
| 111 | "detail" => "El archivo $parametro no fué recibido por el servidor." |
| 112 | ]); |
| 113 | } |
| 114 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/BAD_REQUEST.php"; |
| 4 | require_once __DIR__ . "/recibeBytes.php"; |
| 5 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 6 | |
| 7 | function recibeBytesObligatorios(string $parametro) |
| 8 | { |
| 9 | $bytes = recibeBytes($parametro); |
| 10 | |
| 11 | if ($bytes === false) |
| 12 | throw new ProblemDetailsException([ |
| 13 | "status" => BAD_REQUEST, |
| 14 | "title" => "Falta el valor $parametro.", |
| 15 | "type" => "/errors/faltavalor.html", |
| 16 | "detail" => "La solicitud no tiene el valor de $parametro." |
| 17 | ]); |
| 18 | |
| 19 | if ($bytes === "") |
| 20 | throw new ProblemDetailsException([ |
| 21 | "status" => BAD_REQUEST, |
| 22 | "title" => "Archivo no seleccionado o vacío para el campo $parametro.", |
| 23 | "type" => "/errors/archivovacio.html", |
| 24 | "detail" => "Selecciona un archivo que no esté vacío en el campo $parametro." |
| 25 | ]); |
| 26 | |
| 27 | return $bytes; |
| 28 | } |
| 29 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/recibeBytes.php"; |
| 4 | |
| 5 | function recibeBytesOpcionales(string $parametro) |
| 6 | { |
| 7 | $enteroOpcional = recibeBytes($parametro); |
| 8 | return $enteroOpcional === false ? "" : $enteroOpcional; |
| 9 | } |
| 10 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/recibeTexto.php"; |
| 4 | |
| 5 | /** |
| 6 | * Devuelve el valor entero de un parámetro recibido en el |
| 7 | * servidor por medio de GET, POST o cookie. |
| 8 | * |
| 9 | * Si el parámetro no se recibe, devuelve false |
| 10 | * |
| 11 | * Si se recibe una cadena vacía, se devuelve null. |
| 12 | * |
| 13 | * Si parámetro no se puede convertir a entero, se genera |
| 14 | * un error. |
| 15 | */ |
| 16 | function recibeEntero(string $parametro): false|null|int |
| 17 | { |
| 18 | $valor = recibeTexto($parametro); |
| 19 | if ($valor === false) { |
| 20 | return false; |
| 21 | } else { |
| 22 | $valor = trim($valor); |
| 23 | if ($valor === "") { |
| 24 | return null; |
| 25 | } else { |
| 26 | return (int) $valor; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/BAD_REQUEST.php"; |
| 4 | require_once __DIR__ . "/recibeEntero.php"; |
| 5 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 6 | |
| 7 | function recibeEnteroObligatorio(string $parametro) |
| 8 | { |
| 9 | $entero = recibeEntero($parametro); |
| 10 | |
| 11 | if ($entero === false) |
| 12 | throw new ProblemDetailsException([ |
| 13 | "status" => BAD_REQUEST, |
| 14 | "title" => "Falta el valor $parametro.", |
| 15 | "type" => "/errors/faltavalor.html", |
| 16 | "detail" => "La solicitud no tiene el valor de $parametro." |
| 17 | ]); |
| 18 | |
| 19 | if ($entero === null) |
| 20 | throw new ProblemDetailsException([ |
| 21 | "status" => BAD_REQUEST, |
| 22 | "title" => "Campo $parametro en blanco.", |
| 23 | "type" => "/errors/campoenteroenblanco.html", |
| 24 | "detail" => "Pon un número entero en el campo $parametro." |
| 25 | ]); |
| 26 | |
| 27 | return $entero; |
| 28 | } |
| 29 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/recibeEntero.php"; |
| 4 | |
| 5 | function recibeEnteroOpcional(string $parametro) |
| 6 | { |
| 7 | $enteroOpcional = recibeEntero($parametro); |
| 8 | return $enteroOpcional === false ? null : $enteroOpcional; |
| 9 | } |
| 10 |
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Devuelve el texto de un parámetro enviado al |
| 5 | * servidor por medio de GET, POST o cookie. |
| 6 | * |
| 7 | * Si el parámetro no se recibe, devuelve false. |
| 8 | */ |
| 9 | function recibeTexto(string $parametro): false|string |
| 10 | { |
| 11 | /* Si el parámetro está asignado en $_REQUEST, |
| 12 | * devuelve su valor; de lo contrario, devuelve false. |
| 13 | */ |
| 14 | $valor = isset($_REQUEST[$parametro]) |
| 15 | ? $_REQUEST[$parametro] |
| 16 | : false; |
| 17 | return $valor; |
| 18 | } |
| 19 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/BAD_REQUEST.php"; |
| 4 | require_once __DIR__ . "/recibeTexto.php"; |
| 5 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 6 | |
| 7 | function recibeTextoObligatorio(string $parametro) |
| 8 | { |
| 9 | $texto = recibeTexto($parametro); |
| 10 | |
| 11 | if ($texto === false) |
| 12 | throw new ProblemDetailsException([ |
| 13 | "status" => BAD_REQUEST, |
| 14 | "title" => "Falta el valor $parametro.", |
| 15 | "type" => "/errors/faltavalor.html", |
| 16 | "detail" => "La solicitud no tiene el valor de $parametro." |
| 17 | ]); |
| 18 | |
| 19 | $trimTexto = trim($texto); |
| 20 | |
| 21 | if ($trimTexto === "") |
| 22 | throw new ProblemDetailsException([ |
| 23 | "status" => BAD_REQUEST, |
| 24 | "title" => "Campo $parametro en blanco.", |
| 25 | "type" => "/errors/campoenblanco.html", |
| 26 | "detail" => "Pon texto en el campo $parametro." |
| 27 | ]); |
| 28 | |
| 29 | return $trimTexto; |
| 30 | } |
| 31 |
| 1 | <?php |
| 2 | |
| 3 | require_once __DIR__ . "/NOT_FOUND.php"; |
| 4 | require_once __DIR__ . "/ProblemDetailsException.php"; |
| 5 | |
| 6 | function validaEntidadObligatoria(string $nombre, $entidad) |
| 7 | { |
| 8 | |
| 9 | if ($entidad === false) |
| 10 | throw new ProblemDetailsException([ |
| 11 | "status" => NOT_FOUND, |
| 12 | "title" => "Registro de $nombre no encontrado.", |
| 13 | "type" => "/errors/entidadnoencontrada.html", |
| 14 | "detail" => "No se encontró ningún registro de $nombre con el id solicitado.", |
| 15 | ]); |
| 16 | |
| 17 | return $entidad; |
| 18 | } |
| 19 |
| 1 | import { consume } from "./consume.js" |
| 2 | import { enviaFormRecibeJson } from "./enviaFormRecibeJson.js" |
| 3 | |
| 4 | /** |
| 5 | * @param {string} url |
| 6 | * @param {HTMLFormElement} formulario |
| 7 | * @param {string} mensaje |
| 8 | * @param {string} nuevaVista |
| 9 | */ |
| 10 | export async function accionElimina(url, formulario, mensaje, nuevaVista) { |
| 11 | if (confirm(mensaje)) { |
| 12 | await consume(enviaFormRecibeJson(url, formulario)) |
| 13 | location.href = nuevaVista |
| 14 | } |
| 15 | } |
| 1 | import { ProblemDetailsError } from "./ProblemDetailsError.js" |
| 2 | |
| 3 | /** |
| 4 | * Espera a que la promesa de un fetch termine. Si |
| 5 | * hay error, lanza una excepción. |
| 6 | * |
| 7 | * @param {Promise<Response> } servicio |
| 8 | */ |
| 9 | export async function consume(servicio) { |
| 10 | const respuesta = await servicio |
| 11 | if (respuesta.ok) { |
| 12 | return respuesta |
| 13 | } else { |
| 14 | const contentType = respuesta.headers.get("Content-Type") |
| 15 | if ( |
| 16 | contentType !== null && contentType.startsWith("application/problem+json") |
| 17 | ) |
| 18 | throw new ProblemDetailsError(await respuesta.json()) |
| 19 | else |
| 20 | throw new Error(respuesta.statusText) |
| 21 | } |
| 22 | } |
| 1 | import { consume } from "./consume.js" |
| 2 | import { muestraObjeto } from "./muestraObjeto.js" |
| 3 | import { recibeJson } from "./recibeJson.js" |
| 4 | |
| 5 | /** |
| 6 | * @param {string} url |
| 7 | * @param { "GET" | "POST"| "PUT" | "PATCH" | "DELETE" | "TRACE" | "OPTIONS" |
| 8 | * | "CONNECT" | "HEAD" } metodoHttp |
| 9 | */ |
| 10 | export async function descargaVista(url, metodoHttp = "GET") { |
| 11 | const respuesta = await consume(recibeJson(url, metodoHttp)) |
| 12 | const json = await respuesta.json() |
| 13 | muestraObjeto(document, json) |
| 14 | return json |
| 15 | } |
| 1 | /** |
| 2 | * Envía los datos de un formolario a la url usando la codificación |
| 3 | * multipart/form-data. |
| 4 | * @param {string} url |
| 5 | * @param {HTMLFormElement} formulario |
| 6 | * @param { "GET" | "POST"| "PUT" | "PATCH" | "DELETE" | "TRACE" | "OPTIONS" |
| 7 | * | "CONNECT" | "HEAD" } metodoHttp |
| 8 | */ |
| 9 | export function enviaFormRecibeJson(url, formulario, metodoHttp = "POST") { |
| 10 | return fetch( |
| 11 | url, |
| 12 | { |
| 13 | method: metodoHttp, |
| 14 | headers: { "Accept": "application/json, application/problem+json" }, |
| 15 | body: new FormData(formulario) |
| 16 | } |
| 17 | ) |
| 18 | } |
| 1 | import { muestraError } from "./muestraError.js" |
| 2 | |
| 3 | /** |
| 4 | * Intercepta Response.prototype.json para capturar errores de parseo |
| 5 | * y asegurar que se reporten correctamente en navegadores Chromium. |
| 6 | */ |
| 7 | { |
| 8 | const originalJson = Response.prototype.json |
| 9 | |
| 10 | Response.prototype.json = function () { |
| 11 | // Llamamos al método original usando el contexto (this) de la respuesta |
| 12 | return originalJson.apply(this, arguments) |
| 13 | .catch((/** @type {any} */ error) => { |
| 14 | // Corrige un error de Chrome que evita el manejo correcto de errores. |
| 15 | throw new Error(error) |
| 16 | }) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | window.onerror = function ( |
| 21 | /** @type {string} */ _message, |
| 22 | /** @type {string} */ _url, |
| 23 | /** @type {number} */ _line, |
| 24 | /** @type {number} */ _column, |
| 25 | /** @type {Error} */ errorObject |
| 26 | ) { |
| 27 | muestraError(errorObject) |
| 28 | return true |
| 29 | } |
| 30 | |
| 31 | window.addEventListener('unhandledrejection', event => { |
| 32 | muestraError(event.reason) |
| 33 | event.preventDefault() |
| 34 | }) |
| 35 |
| 1 | import { ProblemDetailsError } from "./ProblemDetailsError.js" |
| 2 | |
| 3 | /** |
| 4 | * Muestra los datos de una Error en la consola y en un cuadro de alerta. |
| 5 | * @param { ProblemDetailsError | Error | null } error descripción del error. |
| 6 | */ |
| 7 | export function muestraError(error) { |
| 8 | |
| 9 | if (error === null) { |
| 10 | |
| 11 | console.error("Error") |
| 12 | alert("Error") |
| 13 | |
| 14 | } else if (error instanceof ProblemDetailsError) { |
| 15 | |
| 16 | const problemDetails = error.problemDetails |
| 17 | |
| 18 | let mensaje = |
| 19 | typeof problemDetails["title"] === "string" ? problemDetails["title"] : "" |
| 20 | if (typeof problemDetails["detail"] === "string") { |
| 21 | if (mensaje !== "") { |
| 22 | mensaje += "\n\n" |
| 23 | } |
| 24 | mensaje += problemDetails["detail"] |
| 25 | } |
| 26 | if (mensaje === "") { |
| 27 | mensaje = "Error" |
| 28 | } |
| 29 | console.error(error, problemDetails) |
| 30 | alert(mensaje) |
| 31 | |
| 32 | } else { |
| 33 | |
| 34 | console.error(error) |
| 35 | alert(error.message) |
| 36 | |
| 37 | } |
| 38 | |
| 39 | } |
| 1 | import { selectorDeNombre } from "./muestraObjeto.js" |
| 2 | |
| 3 | /** |
| 4 | * @param {HTMLInputElement} input |
| 5 | * @param {HTMLInputElement} img |
| 6 | */ |
| 7 | export function muestraImagenSeleccionada(input, img) { |
| 8 | return new Promise((resolve, reject) => { |
| 9 | setTimeout(() => { |
| 10 | |
| 11 | try { |
| 12 | |
| 13 | const dataUrl = getDataUrlDeSeleccion(input) |
| 14 | |
| 15 | if (dataUrl === "") { |
| 16 | |
| 17 | const src = input.dataset.src |
| 18 | if (src === undefined || src === "") { |
| 19 | img.hidden = true |
| 20 | img.src = "" |
| 21 | } else { |
| 22 | img.hidden = false |
| 23 | img.src = src |
| 24 | } |
| 25 | |
| 26 | } else { |
| 27 | |
| 28 | img.hidden = false |
| 29 | img.src = dataUrl |
| 30 | |
| 31 | } |
| 32 | |
| 33 | resolve(true) |
| 34 | |
| 35 | } catch (error) { |
| 36 | |
| 37 | img.hidden = true |
| 38 | |
| 39 | reject(error) |
| 40 | |
| 41 | } |
| 42 | |
| 43 | }, |
| 44 | 500) |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param {HTMLInputElement} input |
| 50 | */ |
| 51 | export function getDataUrlDeSeleccion(input) { |
| 52 | const seleccion = getArchivoSeleccionado(input) |
| 53 | if (seleccion === null) { |
| 54 | return "" |
| 55 | } else { |
| 56 | return URL.createObjectURL(seleccion) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * @param { HTMLInputElement } input |
| 63 | */ |
| 64 | export function getArchivoSeleccionado(input) { |
| 65 | if (input.type !== "file") throw new Error('El input debe ser type="file') |
| 66 | const seleccion = input.files |
| 67 | if (seleccion === null) throw new Error('El input debe ser type="file') |
| 68 | if (seleccion.length === 0) { |
| 69 | return null |
| 70 | } else { |
| 71 | return seleccion.item(0) |
| 72 | } |
| 73 | } |
| 1 | /** |
| 2 | * @param {Document | HTMLElement | ShadowRoot} raizHtml |
| 3 | * @param { any } objeto |
| 4 | */ |
| 5 | export function muestraObjeto(raizHtml, objeto) { |
| 6 | for (const [nombre, definiciones] of Object.entries(objeto)) { |
| 7 | if (Array.isArray(definiciones)) { |
| 8 | muestraArray(raizHtml, nombre, definiciones) |
| 9 | } else if (definiciones !== undefined && definiciones !== null) { |
| 10 | muestraElemento(raizHtml, nombre, definiciones) |
| 11 | } |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * @param { string } nombre |
| 17 | */ |
| 18 | export function selectorDeNombre(nombre) { |
| 19 | return `[id="${nombre}"],[name="${nombre}"],[data-name="${nombre}"]` |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @param { Document | HTMLElement | ShadowRoot } raizHtml |
| 24 | * @param { string } propiedad |
| 25 | * @param {any[]} valores |
| 26 | */ |
| 27 | function muestraArray(raizHtml, propiedad, valores) { |
| 28 | const conjunto = new Set(valores) |
| 29 | const elementos = raizHtml.querySelectorAll(selectorDeNombre(propiedad)) |
| 30 | if (elementos.length === 1 && elementos[0] instanceof HTMLSelectElement) { |
| 31 | muestraOptions(elementos[0], conjunto) |
| 32 | } else { |
| 33 | muestraInputs(elementos, conjunto) |
| 34 | } |
| 35 | |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param {HTMLSelectElement} select |
| 40 | * @param {Set<any>} conjunto |
| 41 | */ |
| 42 | function muestraOptions(select, conjunto) { |
| 43 | for (let i = 0, options = select.options, len = options.length; i < len; i++) { |
| 44 | const option = options[i] |
| 45 | option.selected = conjunto.has(option.value) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @param {NodeListOf<Element>} elementos |
| 51 | * @param {Set<any>} conjunto |
| 52 | */ |
| 53 | function muestraInputs(elementos, conjunto) { |
| 54 | for (let i = 0, len = elementos.length; i < len; i++) { |
| 55 | const elemento = elementos[i] |
| 56 | if (elemento instanceof HTMLInputElement) { |
| 57 | elemento.checked = conjunto.has(elemento.value) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | const data_ = "data-" |
| 63 | const data_Length = data_.length |
| 64 | |
| 65 | /** |
| 66 | * @param {Document | HTMLElement | ShadowRoot} raizHtml |
| 67 | * @param {string} nombre |
| 68 | * @param {{ [s: string]: any; } } definiciones |
| 69 | */ |
| 70 | function muestraElemento(raizHtml, nombre, definiciones) { |
| 71 | const elemento = raizHtml.querySelector(selectorDeNombre(nombre)) |
| 72 | if (elemento !== null) { |
| 73 | for (const [propiedad, valor] of Object.entries(definiciones)) { |
| 74 | if (propiedad in elemento) { |
| 75 | elemento[propiedad] = valor |
| 76 | } else if ( |
| 77 | propiedad.length > data_Length |
| 78 | && propiedad.startsWith(data_) |
| 79 | && elemento instanceof HTMLElement |
| 80 | ) { |
| 81 | elemento.dataset[propiedad.substring(data_Length)] = valor |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 1 | export class ProblemDetailsError extends Error { |
| 2 | |
| 3 | /** |
| 4 | * Detalle de los errores devueltos por un servicio. |
| 5 | * Crea una instancia de ProblemDetailsError. |
| 6 | * @param {object} problemDetails Objeto con la descripcipon del error. |
| 7 | */ |
| 8 | constructor(problemDetails) { |
| 9 | |
| 10 | super(typeof problemDetails["detail"] === "string" |
| 11 | ? problemDetails["detail"] |
| 12 | : (typeof problemDetails["title"] === "string" |
| 13 | ? problemDetails["title"] |
| 14 | : "Error")) |
| 15 | |
| 16 | this.problemDetails = problemDetails |
| 17 | |
| 18 | } |
| 19 | |
| 20 | } |
| 1 | |
| 2 | /** |
| 3 | * @param {string} url |
| 4 | * @param { "GET" | "POST"| "PUT" | "PATCH" | "DELETE" | "TRACE" | "OPTIONS" |
| 5 | * | "CONNECT" | "HEAD" } metodoHttp |
| 6 | */ |
| 7 | export async function recibeJson(url, metodoHttp = "GET") { |
| 8 | return fetch( |
| 9 | url, |
| 10 | { |
| 11 | method: metodoHttp, |
| 12 | headers: { "Accept": "application/json, application/problem+json" } |
| 13 | } |
| 14 | ) |
| 15 | } |
| 1 | import { consume } from "./consume.js" |
| 2 | import { enviaFormRecibeJson } from "./enviaFormRecibeJson.js" |
| 3 | |
| 4 | /** |
| 5 | * @param {Event} event |
| 6 | * @param {string} url |
| 7 | * @param {HTMLFormElement} formulario |
| 8 | * @param {string} nuevaVista |
| 9 | */ |
| 10 | export async function submitAccion(event, url, formulario, nuevaVista) { |
| 11 | event.preventDefault() |
| 12 | await consume(enviaFormRecibeJson(url, formulario)) |
| 13 | location.href = nuevaVista |
| 14 | } |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Carga incompleta de archivo</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Carga incompleta de archivo</h1> |
| 16 | |
| 17 | <p>Por una razón desconocida, un archivo no se cargó completamente.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Archivo demasiado largo</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Archivo demasiado largo</h1> |
| 16 | |
| 17 | <p>Un archivo excede el tamaño máximo que el servidor puede recibir.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>El archivo no se guardó</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>El archivo no se guardó</h1> |
| 16 | |
| 17 | <p>Por una razón desconocida, un archivo no se pudo guardar en disco.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Archivo no recibido</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Archivo no recibido</h1> |
| 16 | |
| 17 | <p>Un archivo no fué recibido por el servidor.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Archivo no seleccionado o vacío</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Archivo no seleccionado o vacío/h1> |
| 16 | |
| 17 | <p> |
| 18 | Selecciona un archivo que no esté vacío en el selector de archivos obligatorio |
| 19 | que está en blanco.</p> |
| 20 | |
| 21 | </body> |
| 22 | |
| 23 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Campo en blanco</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Campo en blanco</h1> |
| 16 | |
| 17 | <p>Pon texto en el campo obligatorio que está en blanco.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Campo entero en blanco</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Campo entero en blanco</h1> |
| 16 | |
| 17 | <p> |
| 18 | Pon un número entero en el campo que pide un valor entero obligatorio y está |
| 19 | en blanco. |
| 20 | </p> |
| 21 | |
| 22 | </body> |
| 23 | |
| 24 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Registro no encontrado</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Registro no encontrado</h1> |
| 16 | |
| 17 | <p>No se encontró ningún registro con el id solicitado.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Error interno del servidor</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Error interno del servidor</h1> |
| 16 | |
| 17 | <p>Se presentó de forma inesperada un error interno del servidor.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Error interno del servidor</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Error interno del servidor</h1> |
| 16 | |
| 17 | <p>Se presentó de forma inesperada un error interno del servidor.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Extensión no permitida</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Extensión no permitida</h1> |
| 16 | |
| 17 | <p>La extensión de un archivo no está permitida en el servidor.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Falta la carpeta temporal</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Falta la carpeta temporal</h1> |
| 16 | |
| 17 | <p> |
| 18 | Por una razón desconocida, falta la carpeta temporal para cargar archivos. |
| 19 | </p> |
| 20 | |
| 21 | </body> |
| 22 | |
| 23 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>Falta un valor</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>Falta un valor</h1> |
| 16 | |
| 17 | <p>La solicitud no tiene un valor obligatorio.</p> |
| 18 | |
| 19 | </body> |
| 20 | |
| 21 | </html> |
| 1 | <!DOCTYPE html> |
| 2 | <html lang="es"> |
| 3 | |
| 4 | <head> |
| 5 | |
| 6 | <meta charset="UTF-8"> |
| 7 | <meta name="viewport" content="width=device-width"> |
| 8 | |
| 9 | <title>El resultado no puede representarse como JSON</title> |
| 10 | |
| 11 | </head> |
| 12 | |
| 13 | <body> |
| 14 | |
| 15 | <h1>El resultado no puede representarse como JSON</h1> |
| 16 | |
| 17 | <p> |
| 18 | Debido a un error interno del servidor, el resultado generado, no se puede |
| 19 | recuperar. |
| 20 | </p> |
| 21 | |
| 22 | </body> |
| 23 | |
| 24 | </html> |
Este archivo ayuda a detectar errores en los archivos del proyecto.
Lo utiliza principalmente Visual Studio Code.
No se explica aquí su estructura, pero puede encontrarse la explicación de todo en la documentación del sitio de Visual Studio Code.
| 1 |
{
|
| 2 |
"compilerOptions": {
|
| 3 |
"checkJs": true, |
| 4 |
"strictNullChecks": true, |
| 5 |
"target": "ES6", |
| 6 |
"module": "Node16", |
| 7 |
"moduleResolution": "Node16", |
| 8 |
"lib": [
|
| 9 |
"ES2017",
|
| 10 |
"WebWorker",
|
| 11 |
"DOM"
|
| 12 |
] |
| 18 |
} |
En esta lección se mostró el almacenamiento de un archivo por registro de la base de datos, usando servicios.