all files / cloudsim-grant/ download.js

13.79% Statements 4/29
0% Branches 0/10
0% Functions 0/4
13.79% Lines 4/29
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                                                                                                      
'use stric'
 
const fs = require('fs')
const path = require('path')
 
// middleware function to download
// a single file.
// req.fileInfo is either a string or a structure which contains:
//   path: full path of the file
//   type: the MIME type
//   name: the name of the file, as seen by the browser
exports.downloadFilePath = function (req, res) {
 
  if(!req.fileInfo) {
    const msg = 'Internal Server Error: file information not in request'
    console.log(msg, filePath)
    return res.satus(500).end(msg)
  }
 
  // req.fileInfo might be a string, or an object with a path property
  const filePath = req.fileInfo.path?req.fileInfo.path:req.fileInfo
  // req may contain a MIME type (zip by default)
  // const contentType = req.fileInfo.type?req.fileInfo.type:'application/zip'
  // the file may have a name to be saved to when downloaded
  const x = req.fileInfo.name
  const dName = x?x:path.basename(filePath)
 
  // load and serve the file
  fs.stat(filePath, function(err, stat) {
    if(err) {
      if('ENOENT' === err.code) {
        res.statusCode = 404
        console.log('file "' + filePath + '" not found')
        return res.end('Not Found');
      }
    } else {
      res.setHeader('Content-type', 'application/zip')
      res.setHeader('Content-disposition', 'attachment; filename=' + dName)
      res.setHeader('Content-Length', stat.size)
      var stream = fs.createReadStream(filePath);
      stream.pipe(res);
      stream.on('error', function() {
        const msg = 'Internal Server Error while reading "' + dName  + '"'
        console.log(msg, filePath)
        return res.status(500).end(msg)
      })
      stream.on('end', function() {
        console.log('"' + filePath + '" downloaded')
        return res.end();
      })
    }
  })
}