<commit_before>var files = require('express').Router()
  , logger = require('ezseed-logger')('files')
  , config = require('../lib/config')
  , db = require('ezseed-database').db
  , archiver = require('archiver')
  , p = require('path')
  , fs = require('fs')
  , debug = require('debug')('ezseed:api:files')

var previous_filesize = 0

files

.param('type', function(req, res, next) {
  var types = ['others', 'movies', 'albums', 'files']

  if(types.indexOf(req.params.type) > -1)
    next()
  else
    res.status(500).send({error: req.params.type+' is not a valid type'})
})

.param('id', function(req, res, next) {
  if(db.helpers.isObjectId(req.params.id)) {
    next()
  } else {
    res.status(500).send({error: req.params.id+' is not a valid id'})
  }

})

/** Protected methods **/
.get('/:type/:id', function(req, res) {

  db[req.params.type].get(req.params.id, function(err, docs) {
    if(err)
      throw err

    if(!docs) {
      res.status(404).send({error: 'File not found'})
    } else {
      res.send(docs)
    }
  })

})
.get('/:type/:id/:action/:os?', function(req, res) {
  debug()
  return action[req.params.action](req, res)
})

var action = {
  delete: function(params) {
    //@todo rpc
  },
  cover: function(req, res) {
    if(req.params.type !== 'albums') {
      return res.status(501).send({error: "Can't get cover for type "+req.params.type}).end()
    }

    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

    db[req.params.type].get(req.params.id, function(err, docs) {<commit_msg>feat(download): Add download through path<commit_after>var files = require('express').Router()
  , logger = require('ezseed-logger')('files')
  , config = require('../lib/config')
  , db = require('ezseed-database').db
  , archiver = require('archiver')
  , p = require('path')
  , fs = require('fs')
  , debug = require('debug')('ezseed:api:files')

var previous_filesize = 0

files

.param('type', function(req, res, next) {
  var types = ['others', 'movies', 'albums', 'files']

  if(types.indexOf(req.params.type) > -1)
    next()
  else
    res.status(500).send({error: req.params.type+' is not a valid type'})
})

.param('id', function(req, res, next) {
  if(db.helpers.isObjectId(req.params.id)) {
    next()
  } else {
    res.status(500).send({error: req.params.id+' is not a valid id'})
  }

})

.get('/download', function(req, res, next) {
  fs.exists(req.query.path, function(exists) {
    if(!exists) {
      return res.status(404).end()
    }

    fs.stat(req.query.path, function(err, stats) {
      if(stats.isDirectory()) {
        return res.status(418).end()
      }

      if(!req.query.read) {
        return res.download(req.query.path)
      } else {
        return res.sendFile(req.query.path)
      }
    })
 
  })
})

.get('/:type/:id', function(req, res) {

  db[req.params.type].get(req.params.id, function(err, docs) {
    if(err)
      throw err

    if(!docs) {
      res.status(404).send({error: 'File not found'})
    } else {
      res.send(docs)
    }
  })

})
.get('/:type/:id/:action/:os?', function(req, res) {
  return action[req.params.action](req, res)
})

var action = {
  delete: function(params) {
    //@todo rpc
  },
  cover: function(req, res) {
    if(req.params.type !== 'albums') {
      return res.status(501).send({error: "Can't get cover for type "+req.params.type}).end()
    }

    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

    db[req.params.type].get(req.params.id, function(err, docs) {
{'fraction_non_alphanumeric': 0.20371381758601856, 'fraction_numerical': 0.009284543965046422, 'mean_word_length': 3.2742123687281213, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5950695', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1321, 'n_words': 240}