File size: 2,027 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module.exports = function (app) {
  var Response = require('../lib/httpResponse.js');
  var Image = require('mongoose').model('Image');
  var acl = require('../lib/auth').acl;

  // Get image
  app.get(
    '/api/images/:imageId',
    acl.hasPermission('images:read'),
    function (req, res) {
      Image.getOne(req.params.imageId)
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Create image
  app.post(
    '/api/images/',
    acl.hasPermission('images:create'),
    function (req, res) {
      if (!req.body.value) {
        Response.BadParameters(res, 'Missing required parameters: value');
        return;
      }

      // Type validation
      if (typeof req.body.value !== 'string') {
        Response.BadParameters(res, 'value parameter must be a String');
        return;
      }

      var image = {};
      // Required parameters
      image.value = req.body.value;

      // Optional parameters
      if (req.body.name) image.name = req.body.name;
      if (req.body.auditId) image.auditId = req.body.auditId;

      Image.create(image)
        .then(data => Response.Created(res, data))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Delete image
  app.delete(
    '/api/images/:imageId',
    acl.hasPermission('images:delete'),
    function (req, res) {
      Image.delete(req.params.imageId)
        .then(data => {
          Response.Ok(res, 'Image deleted successfully');
        })
        .catch(err => {
          Response.Internal(res, err);
        });
    },
  );

  // Download image file
  app.get(
    '/api/images/download/:imageId',
    acl.hasPermission('images:read'),
    function (req, res) {
      Image.getOne(req.params.imageId)
        .then(data => {
          var imgBase64 = data.value.split(',')[1];
          var img = Buffer.from(imgBase64, 'base64');
          Response.SendImage(res, img);
        })
        .catch(err => {
          Response.Internal(res, err);
        });
    },
  );
};