Skip to content

3.1.3 SUBMOD.001.003 : Get AML File by id

Nils-Christopher Wiesenauer edited this page Apr 17, 2021 · 20 revisions

3.1 Module requirements

3.3 Module Context

The submodule to get an AML file by id provides the functionality to get a file by id. This function is needed when the user wants to edit a specific AML file. He opens the file and gives an id from the GUI.

Component REST API Submodule3

6. Implementation

6.1 Get document by _id from database

The needed /file/:id and /file/:id/download GET endpoints are defined below. Here we are using the created FILE model to get a file document by _id from MongoDB by using ´FILE.getFile(_id, callback)´.

const FILE = require('./models/file');
...
app.get('/file/:id', (req, res) => {
  FILE.getFile(req.params.id, (err, file) => {
    >> 6.2 Response data
  });
});

app.get(`/file/:id/download`, (req, res) => {
  FILE.getFile(req.params.id, (err, file) => {
    >> 6.3 Response download
  });
});
...

6.2 Response data

After finding of our document we need to check if there is no error. If an error ocurred, we are sending a JSend with status 'error' and a defined message back. If everything was correct and a file could we found, we are modifying our base64 to ASCII. That because we need ASCII to modify our content of the file.

...
if (!err && file) {
  let newObj = {name: file.name, content: Buffer(file.base64, 'base64').toString('ascii')}
  res.status(200).send({ status: 'success', data: newObj });
} else {
  res.status(200).send({ status: 'error', message: 'Unable to get file' });
}
...

6.3 Response download

After finding of our document we need to check if there is no error. If an error ocurred, we are sending a JSend with status 'error' and a defined message back. If everything was correct and a file could we found, we are sending it as a JSend with status 'success' back.

...
if (!err && file) {
  res.status(200).send({status: 'success', data: file});
} else {
  res.status(200).send({status: 'error', message: 'Unable to download file'});
}
...