-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
391 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,7 @@ SECRETORPRIVATEKEY= | |
|
||
|
||
GOOGLE_CLIENT_ID= | ||
GOOGLE_SECRET_ID= | ||
GOOGLE_SECRET_ID= | ||
|
||
|
||
CLOUDINARY_URL= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ | |
|
||
node_modules/ | ||
|
||
.env | ||
.env | ||
|
||
uploads/productos/**.* | ||
uploads/usuarios/**.* |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
const cloudinary = require('cloudinary').v2 | ||
cloudinary.config( process.env.CLOUDINARY_URL ); | ||
|
||
const { response } = require('express'); | ||
const { subirArchivo } = require('../helpers'); | ||
|
||
const { Usuario, Producto } = require('../models'); | ||
|
||
|
||
const cargarArchivo = async(req, res = response) => { | ||
|
||
|
||
try { | ||
|
||
// txt, md | ||
// const nombre = await subirArchivo( req.files, ['txt','md'], 'textos' ); | ||
const nombre = await subirArchivo( req.files, undefined, 'imgs' ); | ||
res.json({ nombre }); | ||
|
||
} catch (msg) { | ||
res.status(400).json({ msg }); | ||
} | ||
|
||
} | ||
|
||
|
||
const actualizarImagen = async(req, res = response ) => { | ||
|
||
const { id, coleccion } = req.params; | ||
|
||
let modelo; | ||
|
||
switch ( coleccion ) { | ||
case 'usuarios': | ||
modelo = await Usuario.findById(id); | ||
if ( !modelo ) { | ||
return res.status(400).json({ | ||
msg: `No existe un usuario con el id ${ id }` | ||
}); | ||
} | ||
|
||
break; | ||
|
||
case 'productos': | ||
modelo = await Producto.findById(id); | ||
if ( !modelo ) { | ||
return res.status(400).json({ | ||
msg: `No existe un producto con el id ${ id }` | ||
}); | ||
} | ||
|
||
break; | ||
|
||
default: | ||
return res.status(500).json({ msg: 'Se me olvidó validar esto'}); | ||
} | ||
|
||
|
||
// Limpiar imágenes previas | ||
if ( modelo.img ) { | ||
// Hay que borrar la imagen del servidor | ||
const pathImagen = path.join( __dirname, '../uploads', coleccion, modelo.img ); | ||
if ( fs.existsSync( pathImagen ) ) { | ||
fs.unlinkSync( pathImagen ); | ||
} | ||
} | ||
|
||
|
||
const nombre = await subirArchivo( req.files, undefined, coleccion ); | ||
modelo.img = nombre; | ||
|
||
await modelo.save(); | ||
|
||
|
||
res.json( modelo ); | ||
|
||
} | ||
|
||
|
||
const actualizarImagenCloudinary = async(req, res = response ) => { | ||
|
||
const { id, coleccion } = req.params; | ||
|
||
let modelo; | ||
|
||
switch ( coleccion ) { | ||
case 'usuarios': | ||
modelo = await Usuario.findById(id); | ||
if ( !modelo ) { | ||
return res.status(400).json({ | ||
msg: `No existe un usuario con el id ${ id }` | ||
}); | ||
} | ||
|
||
break; | ||
|
||
case 'productos': | ||
modelo = await Producto.findById(id); | ||
if ( !modelo ) { | ||
return res.status(400).json({ | ||
msg: `No existe un producto con el id ${ id }` | ||
}); | ||
} | ||
|
||
break; | ||
|
||
default: | ||
return res.status(500).json({ msg: 'Se me olvidó validar esto'}); | ||
} | ||
|
||
|
||
// Limpiar imágenes previas | ||
if ( modelo.img ) { | ||
const nombreArr = modelo.img.split('/'); | ||
const nombre = nombreArr[ nombreArr.length - 1 ]; | ||
const [ public_id ] = nombre.split('.'); | ||
cloudinary.uploader.destroy( public_id ); | ||
} | ||
|
||
|
||
const { tempFilePath } = req.files.archivo | ||
const { secure_url } = await cloudinary.uploader.upload( tempFilePath ); | ||
modelo.img = secure_url; | ||
|
||
await modelo.save(); | ||
|
||
|
||
res.json( modelo ); | ||
|
||
} | ||
|
||
const mostrarImagen = async(req, res = response ) => { | ||
|
||
const { id, coleccion } = req.params; | ||
|
||
let modelo; | ||
|
||
switch ( coleccion ) { | ||
case 'usuarios': | ||
modelo = await Usuario.findById(id); | ||
if ( !modelo ) { | ||
return res.status(400).json({ | ||
msg: `No existe un usuario con el id ${ id }` | ||
}); | ||
} | ||
|
||
break; | ||
|
||
case 'productos': | ||
modelo = await Producto.findById(id); | ||
if ( !modelo ) { | ||
return res.status(400).json({ | ||
msg: `No existe un producto con el id ${ id }` | ||
}); | ||
} | ||
|
||
break; | ||
|
||
default: | ||
return res.status(500).json({ msg: 'Se me olvidó validar esto'}); | ||
} | ||
|
||
|
||
// Limpiar imágenes previas | ||
if ( modelo.img ) { | ||
// Hay que borrar la imagen del servidor | ||
const pathImagen = path.join( __dirname, '../uploads', coleccion, modelo.img ); | ||
if ( fs.existsSync( pathImagen ) ) { | ||
return res.sendFile( pathImagen ) | ||
} | ||
} | ||
|
||
const pathImagen = path.join( __dirname, '../assets/no-image.jpg'); | ||
res.sendFile( pathImagen ); | ||
} | ||
|
||
|
||
|
||
|
||
module.exports = { | ||
cargarArchivo, | ||
actualizarImagen, | ||
mostrarImagen, | ||
actualizarImagenCloudinary | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
|
||
const dbValidators = require('./db-validators'); | ||
const generarJWT = require('./generar-jwt'); | ||
const googleVerify = require('./google-verify'); | ||
const subirArchivo = require('./subir-archivo'); | ||
|
||
|
||
module.exports = { | ||
...dbValidators, | ||
...generarJWT, | ||
...googleVerify, | ||
...subirArchivo, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const path = require('path'); | ||
const { v4: uuidv4 } = require('uuid'); | ||
|
||
const subirArchivo = ( files, extensionesValidas = ['png','jpg','jpeg','gif'], carpeta = '' ) => { | ||
|
||
return new Promise( (resolve, reject) => { | ||
|
||
const { archivo } = files; | ||
const nombreCortado = archivo.name.split('.'); | ||
const extension = nombreCortado[ nombreCortado.length - 1 ]; | ||
|
||
// Validar la extension | ||
if ( !extensionesValidas.includes( extension ) ) { | ||
return reject(`La extensión ${ extension } no es permitida - ${ extensionesValidas }`); | ||
} | ||
|
||
const nombreTemp = uuidv4() + '.' + extension; | ||
const uploadPath = path.join( __dirname, '../uploads/', carpeta, nombreTemp ); | ||
|
||
archivo.mv(uploadPath, (err) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
|
||
resolve( nombreTemp ); | ||
}); | ||
|
||
}); | ||
|
||
} | ||
|
||
|
||
|
||
module.exports = { | ||
subirArchivo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
|
||
|
||
const validaCampos = require('../middlewares/validar-campos'); | ||
const validarJWT = require('../middlewares/validar-jwt'); | ||
const validaRoles = require('../middlewares/validar-roles'); | ||
const validarJWT = require('../middlewares/validar-jwt'); | ||
const validaRoles = require('../middlewares/validar-roles'); | ||
const validarArchivo = require('../middlewares/validar-archivo'); | ||
|
||
module.exports = { | ||
...validaCampos, | ||
...validarJWT, | ||
...validaRoles, | ||
...validarArchivo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { response } = require("express") | ||
|
||
|
||
const validarArchivoSubir = (req, res = response, next ) => { | ||
|
||
if (!req.files || Object.keys(req.files).length === 0 || !req.files.archivo ) { | ||
return res.status(400).json({ | ||
msg: 'No hay archivos que subir - validarArchivoSubir' | ||
}); | ||
} | ||
|
||
next(); | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
validarArchivoSubir | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.