aboutsummaryrefslogtreecommitdiff
path: root/api/routes/getChunk/index.js
blob: 031d2c89a0fac60b6c10cb214d45c579002aaebd (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
/*
 * Receives an SHA512SUM as a chunk's hash and if exists it
 * returns the chunk's content
 *
 */

const fs = require('fs');
const config = require("../../config.js");

module.exports = (req, res) => {
    var args = req.url.split("/");
    var hash = args[3];
    regex= /[a-f0-9]{128}/
    if (regex.test(hash))
    {
        var path = config.chunksDir+"/"+hash;
        try
        {
            if(fs.existsSync(path))
            {
                res.writeHead(200, {'Content-Type': 'application/json'});
                res.end(fs.readFileSync(path));
            }
        }
        catch (error)
        {
            res.writeHead(404, {'Content-Type': 'application/json'});
            res.end(JSON.stringify({"error":error.message}));
        }
    }
    else
    {
        res.writeHead(404, {'Content-Type': 'application/json'});
        res.end(JSON.stringify({error:"No hash"}));
    }
}