aboutsummaryrefslogtreecommitdiff
path: root/api/routes/getTr/index.js
blob: 11ef38e353181460753119a4af2c85aa8f9dc3d3 (plain)
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
const fs = require("fs");
const config = require("../../config");

/*
 * Gets a file chunk
 *
 * Returns:
 *  the chunk
 *
 */
function fetchFtr(tr, res){
    res.set('Content-Type', 'application/json');
    const path = config.workDir+"/ftr/"+tr;
    try {
        if(fs.existsSync(path)){
            res.send(fs.readFileSync(path));
        }
    } catch (error) {
        res.send({"error":error.message});
    }
};

module.exports = (req, res) => {
    console.log(req.params)
    res.set('Content-Type', 'application/json');
    if ( (req.params.tr) && typeof req.params.tr === "string" && req.params.tr.length === 128 ){
        regex= /[a-f0-9]{128}/;
        if (regex.test(req.params.tr)){
            let tr = req.params.tr;
            fetchFtr(tr,res);
        } else {
            res.send({error:"Invalid data: regexp failed to pass"});
        }
    } else {
        res.send({error:"Invalid data: no valid zblock was provided"});
    }
}