Commit eae5ae86f34b73a9b38ce18c269ba30757b4db4b
1 parent
3f9f0e6ab3
Exists in
master
and in
2 other branches
Agregado metodo delete.
Showing
1 changed file
with
20 additions
and
0 deletions
Show diff stats
rutas/imagenes.js
1 | const path = require('path') | 1 | const path = require('path') |
2 | 2 | ||
3 | router.get('/imagenes/:nombre', (req, res) => { | 3 | router.get('/imagenes/:nombre', (req, res) => { |
4 | 4 | ||
5 | var options = { | 5 | var options = { |
6 | root: path.join(__dirname, '/../img'), | 6 | root: path.join(__dirname, '/../img'), |
7 | dotfiles: 'deny', | 7 | dotfiles: 'deny', |
8 | headers: { | 8 | headers: { |
9 | 'x-timestamp': Date.now(), | 9 | 'x-timestamp': Date.now(), |
10 | 'x-sent': true | 10 | 'x-sent': true |
11 | } | 11 | } |
12 | } | 12 | } |
13 | 13 | ||
14 | res.sendFile(req.params.nombre, options, err => { | 14 | res.sendFile(req.params.nombre, options, err => { |
15 | if (err) console.log(err); | 15 | if (err) console.log(err); |
16 | }); | 16 | }); |
17 | }); | 17 | }); |
18 | 18 | ||
19 | router.post('/imagenes/guardar', (req, res) => { | 19 | router.post('/imagenes/guardar', (req, res) => { |
20 | 20 | ||
21 | let base64 = req.body.base64.split(';base64,').pop(); | 21 | let base64 = req.body.base64.split(';base64,').pop(); |
22 | let path = `${__dirname}/../img/${req.body.name}`; | 22 | let path = `${__dirname}/../img/${req.body.name}`; |
23 | 23 | ||
24 | require('fs').writeFile(path, base64, {encoding: 'base64'} , err => { | 24 | require('fs').writeFile(path, base64, {encoding: 'base64'} , err => { |
25 | 25 | ||
26 | if (err) { | 26 | if (err) { |
27 | 27 | ||
28 | console.log(err); | 28 | console.log(err); |
29 | res.status(500).send('Hubo un error'); | 29 | res.status(500).send('Hubo un error'); |
30 | } | 30 | } |
31 | }); | 31 | }); |
32 | 32 | ||
33 | knex('ARTICULOS') | 33 | knex('ARTICULOS') |
34 | .where({ | 34 | .where({ |
35 | CodArt: req.body.codigo, | 35 | CodArt: req.body.codigo, |
36 | CodSec: req.body.sector | 36 | CodSec: req.body.sector |
37 | }) | 37 | }) |
38 | .then(articulo => { | 38 | .then(articulo => { |
39 | 39 | ||
40 | let idArticulo = articulo[0].id; | 40 | let idArticulo = articulo[0].id; |
41 | 41 | ||
42 | knex('ARTICULOS_IMAGEN') | 42 | knex('ARTICULOS_IMAGEN') |
43 | .where('id_articulo', idArticulo) | 43 | .where('id_articulo', idArticulo) |
44 | .del(); | 44 | .del(); |
45 | 45 | ||
46 | knex('ARTICULOS_IMAGEN') | 46 | knex('ARTICULOS_IMAGEN') |
47 | .insert({ | 47 | .insert({ |
48 | id_articulo: idArticulo, | 48 | id_articulo: idArticulo, |
49 | imagen: req.body.name | 49 | imagen: req.body.name |
50 | }) | 50 | }) |
51 | .then(() => { | 51 | .then(() => { |
52 | res.status(200).send({}); | 52 | res.status(200).send({}); |
53 | }) | 53 | }) |
54 | 54 | ||
55 | }); | 55 | }); |
56 | 56 | ||
57 | }); | 57 | }); |
58 | 58 | ||
59 | router.post('/imagen/borrar', (req, res) => { | ||
60 | |||
61 | let deleted = false | ||
62 | let path = `${__dirname}/../img/${req.body.name}`; | ||
63 | |||
64 | knex('ARTICULOS_IMAGEN') | ||
65 | .where('id_articulo', req.body.id_articulo) | ||
66 | .andWhere('id', req.body.id) | ||
67 | .update({ | ||
68 | id_articulo: 0, | ||
69 | }) | ||
70 | .then(() => { | ||
71 | try { | ||
72 | require('fs').unlinkSync(path); | ||
73 | deleted = true; | ||
74 | res.status(200).send(deleted); | ||
75 | } catch (err) { } | ||
76 | }); | ||
77 | }); | ||
78 | |||
59 | module.exports = router; | 79 | module.exports = router; |
60 | 80 |